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@RunWith(Enclosed.class) // WARNING: tests cannot be declared in the outer class 48public class NoBandChannelHelperTest { 49 private static final int ALL_BANDS = WifiScanner.WIFI_BAND_BOTH_WITH_DFS; 50 51 /** 52 * Unit tests for 53 * {@link com.android.server.wifi.scanner.NoBandChannelHelper.estimateScanDuration}. 54 */ 55 @SmallTest 56 public static class EstimateScanDurationTest { 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 a few channels 70 */ 71 @Test 72 public void fewChannels() { 73 WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 2450, 5100), 74 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 75 76 assertEquals(ChannelHelper.SCAN_PERIOD_PER_CHANNEL_MS * 3, 77 mChannelHelper.estimateScanDuration(testSettings)); 78 } 79 80 /** 81 * check a settings object with a band 82 */ 83 @Test 84 public void band() { 85 WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ, 86 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 87 88 assertTrue("Expected scan to take some time", 89 mChannelHelper.estimateScanDuration(testSettings) 90 >= ChannelHelper.SCAN_PERIOD_PER_CHANNEL_MS); 91 } 92 } 93 94 /** 95 * Unit tests for 96 * {@link com.android.server.wifi.scanner.NoBandChannelHelper.getAvailableScanChannels}. 97 */ 98 @SmallTest 99 public static class GetAvailableScanChannelsTest { 100 NoBandChannelHelper mChannelHelper; 101 102 /** 103 * Called before each test 104 * Create a channel helper 105 */ 106 @Before 107 public void setUp() throws Exception { 108 mChannelHelper = new NoBandChannelHelper(); 109 } 110 111 /** 112 * Test that getting the channels for each band results in the expected empty list 113 */ 114 @Test 115 public void eachBandValue() { 116 for (int band = WifiScanner.WIFI_BAND_24_GHZ; 117 band <= WifiScanner.WIFI_BAND_BOTH_WITH_DFS; ++band) { 118 WifiScanner.ChannelSpec[] channels = 119 mChannelHelper.getAvailableScanChannels(band); 120 assertEquals("expected zero channels", 0, channels.length); 121 } 122 } 123 } 124 125 /** 126 * Unit tests for 127 * {@link com.android.server.wifi.scanner.NoBandChannelHelper.settingsContainChannel}. 128 */ 129 @SmallTest 130 public static class SettingsContainChannelTest { 131 NoBandChannelHelper mChannelHelper; 132 133 /** 134 * Called before each test 135 * Create a channel helper 136 */ 137 @Before 138 public void setUp() throws Exception { 139 mChannelHelper = new NoBandChannelHelper(); 140 } 141 142 /** 143 * check a settings object with no channels 144 */ 145 @Test 146 public void emptySettings() { 147 WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(), 148 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 149 150 assertFalse(mChannelHelper.settingsContainChannel(testSettings, 2400)); 151 assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150)); 152 assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5650)); 153 } 154 155 /** 156 * check a settings object with some channels 157 */ 158 @Test 159 public void settingsWithChannels() { 160 WifiScanner.ScanSettings testSettings = createRequest(channelsToSpec(2400, 5650), 161 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 162 163 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400)); 164 assertFalse(mChannelHelper.settingsContainChannel(testSettings, 5150)); 165 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650)); 166 } 167 168 /** 169 * check a settings object with a band specified 170 */ 171 @Test 172 public void settingsWithBand() { 173 WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_24_GHZ, 174 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 175 176 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400)); 177 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450)); 178 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150)); 179 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650)); 180 } 181 182 /** 183 * check a settings object with multiple bands specified 184 */ 185 @Test 186 public void settingsWithMultiBand() { 187 WifiScanner.ScanSettings testSettings = createRequest(WifiScanner.WIFI_BAND_BOTH, 188 10000, 0, 20, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN); 189 190 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2400)); 191 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 2450)); 192 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5150)); 193 assertTrue(mChannelHelper.settingsContainChannel(testSettings, 5650)); 194 } 195 } 196 197 /** 198 * Unit tests for 199 * {@link com.android.server.wifi.scanner.NoBandChannelHelper.NoBandChannelCollection}. 200 */ 201 @SmallTest 202 public static class NoBandsChannelCollectionTest { 203 ChannelHelper.ChannelCollection mChannelCollection; 204 205 /** 206 * Called before each test 207 * Create a collection to use for each test 208 */ 209 @Before 210 public void setUp() throws Exception { 211 mChannelCollection = new NoBandChannelHelper().createChannelCollection(); 212 } 213 214 /** 215 * Create an empty collection 216 */ 217 @Test 218 public void empty() { 219 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 220 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 221 assertThat(bucketSettings, channelsAre()); 222 223 assertEquals(Collections.<Integer>emptySet(), 224 mChannelCollection.getScanFreqs()); 225 226 assertTrue(mChannelCollection.isEmpty()); 227 assertFalse(mChannelCollection.containsChannel(2400)); 228 assertFalse(mChannelCollection.isAllChannels()); 229 } 230 231 /** 232 * Add something to a collection and then clear it and make sure nothing is in it 233 */ 234 @Test 235 public void clear() { 236 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 237 mChannelCollection.clear(); 238 239 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 240 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 241 assertThat(bucketSettings, channelsAre()); 242 243 assertEquals(Collections.<Integer>emptySet(), 244 mChannelCollection.getScanFreqs()); 245 246 assertTrue(mChannelCollection.isEmpty()); 247 assertFalse(mChannelCollection.containsChannel(2400)); 248 assertFalse(mChannelCollection.isAllChannels()); 249 } 250 251 /** 252 * Add a single band to the collection 253 */ 254 @Test 255 public void addBand() { 256 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 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.getScanFreqs()); 263 264 assertFalse(mChannelCollection.isEmpty()); 265 assertTrue(mChannelCollection.containsChannel(2400)); 266 assertTrue(mChannelCollection.containsChannel(5150)); 267 assertTrue(mChannelCollection.isAllChannels()); 268 } 269 270 /** 271 * Add a single channel to the collection 272 */ 273 @Test 274 public void addChannel_single() { 275 mChannelCollection.addChannel(2400); 276 277 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 278 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 279 assertThat(bucketSettings, channelsAre(2400)); 280 281 assertEquals(new HashSet<Integer>(Arrays.asList(2400)), 282 mChannelCollection.getScanFreqs()); 283 284 assertFalse(mChannelCollection.isEmpty()); 285 assertTrue(mChannelCollection.containsChannel(2400)); 286 assertFalse(mChannelCollection.containsChannel(5150)); 287 assertFalse(mChannelCollection.isAllChannels()); 288 } 289 290 /** 291 * Add a multiple channels to the collection 292 */ 293 @Test 294 public void addChannel_multiple() { 295 mChannelCollection.addChannel(2400); 296 mChannelCollection.addChannel(2450); 297 298 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 299 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 300 assertThat(bucketSettings, channelsAre(2400, 2450)); 301 302 assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)), 303 mChannelCollection.getScanFreqs()); 304 305 assertFalse(mChannelCollection.isEmpty()); 306 assertTrue(mChannelCollection.containsChannel(2400)); 307 assertFalse(mChannelCollection.containsChannel(5150)); 308 assertFalse(mChannelCollection.isAllChannels()); 309 } 310 311 /** 312 * Add a band and channel that is on that band 313 */ 314 @Test 315 public void addChannel_and_addBand_sameBand() { 316 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 317 mChannelCollection.addChannel(2400); 318 319 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 320 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 321 assertThat(bucketSettings, bandIs(ALL_BANDS)); 322 323 assertNull(mChannelCollection.getScanFreqs()); 324 325 assertFalse(mChannelCollection.isEmpty()); 326 assertTrue(mChannelCollection.containsChannel(2400)); 327 assertTrue(mChannelCollection.containsChannel(5150)); 328 assertTrue(mChannelCollection.isAllChannels()); 329 } 330 331 /** 332 * Add a band and channel that is not that band 333 */ 334 @Test 335 public void addChannel_and_addBand_withDifferentBandChannel() { 336 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 337 mChannelCollection.addChannel(5150); 338 339 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 340 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 341 assertThat(bucketSettings, bandIs(ALL_BANDS)); 342 343 assertNull(mChannelCollection.getScanFreqs()); 344 345 assertFalse(mChannelCollection.isEmpty()); 346 assertTrue(mChannelCollection.containsChannel(2400)); 347 assertTrue(mChannelCollection.containsChannel(5150)); 348 assertTrue(mChannelCollection.isAllChannels()); 349 } 350 351 /** 352 * Add a band that should contain all channels 353 */ 354 @Test 355 public void addChannel_and_addBand_all() { 356 mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS); 357 mChannelCollection.addChannel(5150); 358 359 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 360 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 361 assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS)); 362 363 assertNull(mChannelCollection.getScanFreqs()); 364 365 assertFalse(mChannelCollection.isEmpty()); 366 assertTrue(mChannelCollection.containsChannel(2400)); 367 assertTrue(mChannelCollection.containsChannel(5150)); 368 assertTrue(mChannelCollection.containsChannel(5600)); 369 assertTrue(mChannelCollection.isAllChannels()); 370 } 371 372 /** 373 * Add enough channels on a single band that the max channels is exceeded 374 */ 375 @Test 376 public void addChannel_exceedMaxChannels() { 377 mChannelCollection.addChannel(5600); 378 mChannelCollection.addChannel(5650); 379 mChannelCollection.addChannel(5660); 380 381 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 382 mChannelCollection.fillBucketSettings(bucketSettings, 2); 383 assertThat(bucketSettings, bandIs(ALL_BANDS)); 384 assertFalse(mChannelCollection.isAllChannels()); // can't determine from just channels 385 } 386 387 /** 388 * Add enough channels across multiple bands that the max channels is exceeded 389 */ 390 @Test 391 public void addChannel_exceedMaxChannelsOnMultipleBands() { 392 mChannelCollection.addChannel(2400); 393 mChannelCollection.addChannel(2450); 394 mChannelCollection.addChannel(5150); 395 396 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 397 mChannelCollection.fillBucketSettings(bucketSettings, 2); 398 assertThat(bucketSettings, bandIs(ALL_BANDS)); 399 assertFalse(mChannelCollection.isAllChannels()); // can't determine from just channels 400 } 401 } 402} 403