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 KnownBandsChannelCollectionTest { 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.getSupplicantScanFreqs()); 225 226 assertTrue(mChannelCollection.isEmpty()); 227 assertFalse(mChannelCollection.containsChannel(2400)); 228 } 229 230 /** 231 * Add something to a collection and then clear it and make sure nothing is in it 232 */ 233 @Test 234 public void clear() { 235 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 236 mChannelCollection.clear(); 237 238 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 239 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 240 assertThat(bucketSettings, channelsAre()); 241 242 assertEquals(Collections.<Integer>emptySet(), 243 mChannelCollection.getSupplicantScanFreqs()); 244 245 assertTrue(mChannelCollection.isEmpty()); 246 assertFalse(mChannelCollection.containsChannel(2400)); 247 } 248 249 /** 250 * Add a single band to the collection 251 */ 252 @Test 253 public void addBand() { 254 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 255 256 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 257 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 258 assertThat(bucketSettings, bandIs(ALL_BANDS)); 259 260 assertNull(mChannelCollection.getSupplicantScanFreqs()); 261 262 assertFalse(mChannelCollection.isEmpty()); 263 assertTrue(mChannelCollection.containsChannel(2400)); 264 assertTrue(mChannelCollection.containsChannel(5150)); 265 } 266 267 /** 268 * Add a single channel to the collection 269 */ 270 @Test 271 public void addChannel_single() { 272 mChannelCollection.addChannel(2400); 273 274 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 275 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 276 assertThat(bucketSettings, channelsAre(2400)); 277 278 assertEquals(new HashSet<Integer>(Arrays.asList(2400)), 279 mChannelCollection.getSupplicantScanFreqs()); 280 281 assertFalse(mChannelCollection.isEmpty()); 282 assertTrue(mChannelCollection.containsChannel(2400)); 283 assertFalse(mChannelCollection.containsChannel(5150)); 284 } 285 286 /** 287 * Add a multiple channels to the collection 288 */ 289 @Test 290 public void addChannel_multiple() { 291 mChannelCollection.addChannel(2400); 292 mChannelCollection.addChannel(2450); 293 294 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 295 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 296 assertThat(bucketSettings, channelsAre(2400, 2450)); 297 298 assertEquals(new HashSet<Integer>(Arrays.asList(2400, 2450)), 299 mChannelCollection.getSupplicantScanFreqs()); 300 301 assertFalse(mChannelCollection.isEmpty()); 302 assertTrue(mChannelCollection.containsChannel(2400)); 303 assertFalse(mChannelCollection.containsChannel(5150)); 304 } 305 306 /** 307 * Add a band and channel that is on that band 308 */ 309 @Test 310 public void addChannel_and_addBand_sameBand() { 311 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 312 mChannelCollection.addChannel(2400); 313 314 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 315 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 316 assertThat(bucketSettings, bandIs(ALL_BANDS)); 317 318 assertNull(mChannelCollection.getSupplicantScanFreqs()); 319 320 assertFalse(mChannelCollection.isEmpty()); 321 assertTrue(mChannelCollection.containsChannel(2400)); 322 assertTrue(mChannelCollection.containsChannel(5150)); 323 } 324 325 /** 326 * Add a band and channel that is not that band 327 */ 328 @Test 329 public void addChannel_and_addBand_withDifferentBandChannel() { 330 mChannelCollection.addBand(WifiScanner.WIFI_BAND_24_GHZ); 331 mChannelCollection.addChannel(5150); 332 333 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 334 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 335 assertThat(bucketSettings, bandIs(ALL_BANDS)); 336 337 assertNull(mChannelCollection.getSupplicantScanFreqs()); 338 339 assertFalse(mChannelCollection.isEmpty()); 340 assertTrue(mChannelCollection.containsChannel(2400)); 341 assertTrue(mChannelCollection.containsChannel(5150)); 342 } 343 344 /** 345 * Add a band that should contain all channels 346 */ 347 @Test 348 public void addChannel_and_addBand_all() { 349 mChannelCollection.addBand(WifiScanner.WIFI_BAND_BOTH_WITH_DFS); 350 mChannelCollection.addChannel(5150); 351 352 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 353 mChannelCollection.fillBucketSettings(bucketSettings, Integer.MAX_VALUE); 354 assertThat(bucketSettings, bandIs(WifiScanner.WIFI_BAND_BOTH_WITH_DFS)); 355 356 assertNull(mChannelCollection.getSupplicantScanFreqs()); 357 358 assertFalse(mChannelCollection.isEmpty()); 359 assertTrue(mChannelCollection.containsChannel(2400)); 360 assertTrue(mChannelCollection.containsChannel(5150)); 361 assertTrue(mChannelCollection.containsChannel(5600)); 362 } 363 364 /** 365 * Add enough channels on a single band that the max channels is exceeded 366 */ 367 @Test 368 public void addChannel_exceedMaxChannels() { 369 mChannelCollection.addChannel(5600); 370 mChannelCollection.addChannel(5650); 371 mChannelCollection.addChannel(5660); 372 373 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 374 mChannelCollection.fillBucketSettings(bucketSettings, 2); 375 assertThat(bucketSettings, bandIs(ALL_BANDS)); 376 } 377 378 /** 379 * Add enough channels across multiple bands that the max channels is exceeded 380 */ 381 @Test 382 public void addChannel_exceedMaxChannelsOnMultipleBands() { 383 mChannelCollection.addChannel(2400); 384 mChannelCollection.addChannel(2450); 385 mChannelCollection.addChannel(5150); 386 387 WifiNative.BucketSettings bucketSettings = new WifiNative.BucketSettings(); 388 mChannelCollection.fillBucketSettings(bucketSettings, 2); 389 assertThat(bucketSettings, bandIs(ALL_BANDS)); 390 } 391 } 392} 393