KnownBandsChannelHelperTest.java revision 39175cc807488dd849e2c530f8be30dd674fbd9f
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi.scanner;
18
19import static com.android.server.wifi.ScanTestUtil.bandIs;
20import static com.android.server.wifi.ScanTestUtil.channelsAre;
21import static com.android.server.wifi.ScanTestUtil.channelsToSpec;
22import static com.android.server.wifi.ScanTestUtil.createRequest;
23
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertNull;
28import static org.junit.Assert.assertTrue;
29
30import android.net.wifi.WifiScanner;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import com.android.server.wifi.WifiNative;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.experimental.runners.Enclosed;
38import org.junit.runner.RunWith;
39
40import java.util.Arrays;
41import java.util.Collections;
42import java.util.HashSet;
43
44/**
45 * Unit tests for {@link com.android.server.wifi.scanner.KnownBandsChannelHelper}.
46 */
47@SmallTest
48@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class
49public class KnownBandsChannelHelperTest {
50
51    /**
52     * Unit tests for
53     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.settingsContainChannel}.
54     */
55    public static class SettingsContainChannelTest {
56        KnownBandsChannelHelper mChannelHelper;
57
58        /**
59         * Called before each test
60         * Create a channel helper
61         */
62        @Before
63        public void setUp() throws Exception {
64            mChannelHelper = new KnownBandsChannelHelper(
65                    new int[]{2400, 2450},
66                    new int[]{5150, 5175},
67                    new int[]{5600, 5650, 5660});
68        }
69
70        /**
71         * check a settings object with no channels
72         */
73        @Test
74        public void emptySettings() {
75            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(),
76                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
77
78            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 2400));
79            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
80            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
81        }
82
83        /**
84         * check a settings object with some channels
85         */
86        @Test
87        public void settingsWithChannels() {
88            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 5650),
89                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
90
91            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
92            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
93            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650));
94        }
95
96        /**
97         * check a settings object with a band specified
98         */
99        @Test
100        public void settingsWithBand() {
101            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ,
102                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
103
104            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
105            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
106            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
107            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
108        }
109
110        /**
111         * check a settings object with multiple bands specified
112         */
113        @Test
114        public void settingsWithMultiBand() {
115            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_BOTH,
116                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
117
118            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
119            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
120            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150));
121            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
122        }
123    }
124
125    /**
126     * Unit tests for
127     * {@link com.android.server.wifi.scanner.KnownBandsChannelHelper.KnownBandsChannelCollection}.
128     */
129    public static class KnownBandsChannelCollectionTest {
130        ChannelHelper.ChannelCollection mChannelCollection;
131
132        /**
133         * Called before each test
134         * Create a collection to use for each test
135         */
136        @Before
137        public void setUp() throws Exception {
138            KnownBandsChannelHelper channelHelper = new KnownBandsChannelHelper(
139                    new int[]{2400, 2450},
140                    new int[]{5150, 5175},
141                    new int[]{5600, 5650, 5660});
142            mChannelCollection = channelHelper.createChannelCollection();
143        }
144
145        /**
146         * Create an empty collection
147         */
148        @Test
149        public void empty() {
150            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
151            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
152            assertThat(bucketSettings, channelsAre());
153
154            assertEquals(Collections.<Integer>emptySet(),
155                    mChannelCollection.getSupplicantScanFreqs());
156
157            assertTrue(mChannelCollection.isEmpty());
158            assertFalse(mChannelCollection.containsChannel(2400));
159            assertFalse(mChannelCollection.containsChannel(5150));
160        }
161
162        /**
163         * Add something to a collection and then clear it and make sure nothing is in it
164         */
165        @Test
166        public void clear() {
167            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
168            mChannelCollection.clear();
169
170            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
171            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
172            assertThat(bucketSettings, channelsAre());
173
174            assertEquals(Collections.<Integer>emptySet(),
175                    mChannelCollection.getSupplicantScanFreqs());
176
177            assertTrue(mChannelCollection.isEmpty());
178            assertFalse(mChannelCollection.containsChannel(2400));
179            assertFalse(mChannelCollection.containsChannel(5150));
180        }
181
182        /**
183         * Add a single band to the collection
184         */
185        @Test
186        public void addBand() {
187            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
188
189            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
190            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
191            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
192
193            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
194                    mChannelCollection.getSupplicantScanFreqs());
195
196            assertFalse(mChannelCollection.isEmpty());
197            assertTrue(mChannelCollection.containsChannel(2400));
198            assertFalse(mChannelCollection.containsChannel(5150));
199        }
200
201        /**
202         * Add a single channel to the collection
203         */
204        @Test
205        public void addChannel_single() {
206            mChannelCollection.addChannel(2400);
207
208            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
209            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
210            assertThat(bucketSettings, channelsAre(2400));
211
212            assertEquals(new HashSet<Integer>(Arrays.asList(2400)),
213                    mChannelCollection.getSupplicantScanFreqs());
214
215            assertFalse(mChannelCollection.isEmpty());
216            assertTrue(mChannelCollection.containsChannel(2400));
217            assertFalse(mChannelCollection.containsChannel(5150));
218        }
219
220        /**
221         * Add a multiple channels to the collection
222         */
223        @Test
224        public void addChannel_multiple() {
225            mChannelCollection.addChannel(2400);
226            mChannelCollection.addChannel(2450);
227
228            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
229            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
230            assertThat(bucketSettings, channelsAre(2400, 2450));
231
232            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
233                    mChannelCollection.getSupplicantScanFreqs());
234
235            assertFalse(mChannelCollection.isEmpty());
236            assertTrue(mChannelCollection.containsChannel(2400));
237            assertFalse(mChannelCollection.containsChannel(5150));
238        }
239
240        /**
241         * Add a band and channel that is on that band
242         */
243        @Test
244        public void addChannel_and_addBand_sameBand() {
245            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
246            mChannelCollection.addChannel(2400);
247
248            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
249            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
250            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_24_GHZ));
251
252            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
253                    mChannelCollection.getSupplicantScanFreqs());
254
255            assertFalse(mChannelCollection.isEmpty());
256            assertTrue(mChannelCollection.containsChannel(2400));
257            assertFalse(mChannelCollection.containsChannel(5150));
258        }
259
260        /**
261         * Add a band and channel that is not that band
262         */
263        @Test
264        public void addChannel_and_addBand_withDifferentBandChannel() {
265            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
266            mChannelCollection.addChannel(5150);
267
268            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
269            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
270            assertThat(bucketSettings, channelsAre(2400, 2450, 5150));
271
272            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450, 5150)),
273                    mChannelCollection.getSupplicantScanFreqs());
274
275            assertFalse(mChannelCollection.isEmpty());
276            assertTrue(mChannelCollection.containsChannel(2400));
277            assertTrue(mChannelCollection.containsChannel(5150));
278        }
279
280        /**
281         * Add a band that should contain all channels
282         */
283        @Test
284        public void addChannel_and_addBand_all() {
285            mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS);
286            mChannelCollection.addChannel(5150);
287
288            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
289            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
290            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS));
291
292            assertNull(mChannelCollection.getSupplicantScanFreqs());
293
294            assertFalse(mChannelCollection.isEmpty());
295            assertTrue(mChannelCollection.containsChannel(2400));
296            assertTrue(mChannelCollection.containsChannel(5150));
297            assertTrue(mChannelCollection.containsChannel(5600));
298        }
299
300        /**
301         * Add enough channels on a single band that the max channels is exceeded
302         */
303        @Test
304        public void addChannel_exceedMaxChannels() {
305            mChannelCollection.addChannel(5600);
306            mChannelCollection.addChannel(5650);
307            mChannelCollection.addChannel(5660);
308
309            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
310            mChannelCollection.fillBucketSettings(bucketSettings, 2);
311            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_5_GHZ_DFS_ONLY));
312        }
313
314        /**
315         * Add enough channels across multiple bands that the max channels is exceeded
316         */
317        @Test
318        public void addChannel_exceedMaxChannelsOnMultipleBands() {
319            mChannelCollection.addChannel(2400);
320            mChannelCollection.addChannel(2450);
321            mChannelCollection.addChannel(5150);
322
323            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
324            mChannelCollection.fillBucketSettings(bucketSettings, 2);
325            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH));
326        }
327    }
328}
329