NoBandChannelHelperTest.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.NoBandChannelHelper}.
46 */
47@SmallTest
48@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class
49public class NoBandChannelHelperTest {
50    private static final int ALL_BANDS = WifiScanner.WIFI_BAND_BOTH_WITH_DFS;
51
52    /**
53     * Unit tests for
54     * {@link com.android.server.wifi.scanner.NoBandChannelHelper.settingsContainChannel}.
55     */
56    public static class SettingsContainChannelTest {
57        NoBandChannelHelper mChannelHelper;
58
59        /**
60         * Called before each test
61         * Create a channel helper
62         */
63        @Before
64        public void setUp() throws Exception {
65            mChannelHelper = new NoBandChannelHelper();
66        }
67
68        /**
69         * check a settings object with no channels
70         */
71        @Test
72        public void emptySettings() {
73            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(),
74                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
75
76            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 2400));
77            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
78            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650));
79        }
80
81        /**
82         * check a settings object with some channels
83         */
84        @Test
85        public void settingsWithChannels() {
86            WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 5650),
87                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
88
89            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
90            assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150));
91            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650));
92        }
93
94        /**
95         * check a settings object with a band specified
96         */
97        @Test
98        public void settingsWithBand() {
99            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ,
100                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
101
102            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
103            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
104            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150));
105            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650));
106        }
107
108        /**
109         * check a settings object with multiple bands specified
110         */
111        @Test
112        public void settingsWithMultiBand() {
113            WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_BOTH,
114                    10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN);
115
116            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400));
117            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450));
118            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150));
119            assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650));
120        }
121    }
122
123    /**
124     * Unit tests for
125     * {@link com.android.server.wifi.scanner.NoBandChannelHelper.NoBandChannelCollection}.
126     */
127    public static class KnownBandsChannelCollectionTest {
128        ChannelHelper.ChannelCollection mChannelCollection;
129
130        /**
131         * Called before each test
132         * Create a collection to use for each test
133         */
134        @Before
135        public void setUp() throws Exception {
136            mChannelCollection = new NoBandChannelHelper().createChannelCollection();
137        }
138
139        /**
140         * Create an empty collection
141         */
142        @Test
143        public void empty() {
144            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
145            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
146            assertThat(bucketSettings, channelsAre());
147
148            assertEquals(Collections.<Integer>emptySet(),
149                    mChannelCollection.getSupplicantScanFreqs());
150
151            assertTrue(mChannelCollection.isEmpty());
152            assertFalse(mChannelCollection.containsChannel(2400));
153        }
154
155        /**
156         * Add something to a collection and then clear it and make sure nothing is in it
157         */
158        @Test
159        public void clear() {
160            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
161            mChannelCollection.clear();
162
163            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
164            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
165            assertThat(bucketSettings, channelsAre());
166
167            assertEquals(Collections.<Integer>emptySet(),
168                    mChannelCollection.getSupplicantScanFreqs());
169
170            assertTrue(mChannelCollection.isEmpty());
171            assertFalse(mChannelCollection.containsChannel(2400));
172        }
173
174        /**
175         * Add a single band to the collection
176         */
177        @Test
178        public void addBand() {
179            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
180
181            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
182            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
183            assertThat(bucketSettings, bandIs(ALL_BANDS));
184
185            assertNull(mChannelCollection.getSupplicantScanFreqs());
186
187            assertFalse(mChannelCollection.isEmpty());
188            assertTrue(mChannelCollection.containsChannel(2400));
189            assertTrue(mChannelCollection.containsChannel(5150));
190        }
191
192        /**
193         * Add a single channel to the collection
194         */
195        @Test
196        public void addChannel_single() {
197            mChannelCollection.addChannel(2400);
198
199            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
200            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
201            assertThat(bucketSettings, channelsAre(2400));
202
203            assertEquals(new HashSet<Integer>(Arrays.asList(2400)),
204                    mChannelCollection.getSupplicantScanFreqs());
205
206            assertFalse(mChannelCollection.isEmpty());
207            assertTrue(mChannelCollection.containsChannel(2400));
208            assertFalse(mChannelCollection.containsChannel(5150));
209        }
210
211        /**
212         * Add a multiple channels to the collection
213         */
214        @Test
215        public void addChannel_multiple() {
216            mChannelCollection.addChannel(2400);
217            mChannelCollection.addChannel(2450);
218
219            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
220            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
221            assertThat(bucketSettings, channelsAre(2400, 2450));
222
223            assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)),
224                    mChannelCollection.getSupplicantScanFreqs());
225
226            assertFalse(mChannelCollection.isEmpty());
227            assertTrue(mChannelCollection.containsChannel(2400));
228            assertFalse(mChannelCollection.containsChannel(5150));
229        }
230
231        /**
232         * Add a band and channel that is on that band
233         */
234        @Test
235        public void addChannel_and_addBand_sameBand() {
236            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
237            mChannelCollection.addChannel(2400);
238
239            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
240            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
241            assertThat(bucketSettings, bandIs(ALL_BANDS));
242
243            assertNull(mChannelCollection.getSupplicantScanFreqs());
244
245            assertFalse(mChannelCollection.isEmpty());
246            assertTrue(mChannelCollection.containsChannel(2400));
247            assertTrue(mChannelCollection.containsChannel(5150));
248        }
249
250        /**
251         * Add a band and channel that is not that band
252         */
253        @Test
254        public void addChannel_and_addBand_withDifferentBandChannel() {
255            mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ);
256            mChannelCollection.addChannel(5150);
257
258            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
259            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
260            assertThat(bucketSettings, bandIs(ALL_BANDS));
261
262            assertNull(mChannelCollection.getSupplicantScanFreqs());
263
264            assertFalse(mChannelCollection.isEmpty());
265            assertTrue(mChannelCollection.containsChannel(2400));
266            assertTrue(mChannelCollection.containsChannel(5150));
267        }
268
269        /**
270         * Add a band that should contain all channels
271         */
272        @Test
273        public void addChannel_and_addBand_all() {
274            mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS);
275            mChannelCollection.addChannel(5150);
276
277            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
278            mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE);
279            assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS));
280
281            assertNull(mChannelCollection.getSupplicantScanFreqs());
282
283            assertFalse(mChannelCollection.isEmpty());
284            assertTrue(mChannelCollection.containsChannel(2400));
285            assertTrue(mChannelCollection.containsChannel(5150));
286            assertTrue(mChannelCollection.containsChannel(5600));
287        }
288
289        /**
290         * Add enough channels on a single band that the max channels is exceeded
291         */
292        @Test
293        public void addChannel_exceedMaxChannels() {
294            mChannelCollection.addChannel(5600);
295            mChannelCollection.addChannel(5650);
296            mChannelCollection.addChannel(5660);
297
298            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
299            mChannelCollection.fillBucketSettings(bucketSettings, 2);
300            assertThat(bucketSettings, bandIs(ALL_BANDS));
301        }
302
303        /**
304         * Add enough channels across multiple bands that the max channels is exceeded
305         */
306        @Test
307        public void addChannel_exceedMaxChannelsOnMultipleBands() {
308            mChannelCollection.addChannel(2400);
309            mChannelCollection.addChannel(2450);
310            mChannelCollection.addChannel(5150);
311
312            WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings();
313            mChannelCollection.fillBucketSettings(bucketSettings, 2);
314            assertThat(bucketSettings, bandIs(ALL_BANDS));
315        }
316    }
317}
318