AccessPointTest.java revision a28050ae42edb487239d9800b173ea334981f2b5
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 */ 16package com.android.settingslib.wifi; 17 18import static com.google.common.truth.Truth.assertThat; 19import static com.google.common.truth.Truth.assertWithMessage; 20 21import static org.mockito.Mockito.any; 22import static org.mockito.Mockito.anyInt; 23import static org.mockito.Mockito.mock; 24import static org.mockito.Mockito.never; 25import static org.mockito.Mockito.times; 26import static org.mockito.Mockito.verify; 27import static org.mockito.Mockito.when; 28 29import android.content.Context; 30import android.net.ConnectivityManager; 31import android.net.NetworkInfo; 32import android.net.NetworkKey; 33import android.net.RssiCurve; 34import android.net.ScoredNetwork; 35import android.net.WifiKey; 36import android.net.wifi.ScanResult; 37import android.net.wifi.WifiConfiguration; 38import android.net.wifi.WifiInfo; 39import android.net.wifi.WifiNetworkScoreCache; 40import android.net.wifi.WifiSsid; 41import android.net.wifi.hotspot2.PasspointConfiguration; 42import android.net.wifi.hotspot2.pps.HomeSp; 43import android.os.Bundle; 44import android.os.SystemClock; 45import android.support.test.InstrumentationRegistry; 46import android.support.test.filters.SmallTest; 47import android.support.test.runner.AndroidJUnit4; 48import android.text.SpannableString; 49import android.text.style.TtsSpan; 50 51import com.android.settingslib.R; 52 53import org.junit.Before; 54import org.junit.Test; 55import org.junit.runner.RunWith; 56import org.mockito.Mock; 57import org.mockito.MockitoAnnotations; 58 59import java.util.ArrayList; 60import java.util.Collections; 61 62@SmallTest 63@RunWith(AndroidJUnit4.class) 64public class AccessPointTest { 65 66 private static final String TEST_SSID = "test_ssid"; 67 private Context mContext; 68 @Mock private RssiCurve mockBadgeCurve; 69 @Mock private WifiNetworkScoreCache mockWifiNetworkScoreCache; 70 71 @Before 72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mContext = InstrumentationRegistry.getTargetContext(); 75 } 76 77 @Test 78 public void testSsidIsTelephoneSpan() { 79 final Bundle bundle = new Bundle(); 80 bundle.putString("key_ssid", TEST_SSID); 81 final AccessPoint ap = new AccessPoint(InstrumentationRegistry.getTargetContext(), bundle); 82 final CharSequence ssid = ap.getSsid(); 83 84 assertThat(ssid instanceof SpannableString).isTrue(); 85 86 TtsSpan[] spans = ((SpannableString) ssid).getSpans(0, TEST_SSID.length(), TtsSpan.class); 87 88 assertThat(spans.length).isEqualTo(1); 89 assertThat(spans[0].getType()).isEqualTo(TtsSpan.TYPE_TELEPHONE); 90 } 91 92 @Test 93 public void testCopyAccessPoint_dataShouldMatch() { 94 WifiConfiguration configuration = createWifiConfiguration(); 95 configuration.meteredHint = true; 96 97 NetworkInfo networkInfo = 98 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE"); 99 AccessPoint originalAccessPoint = new AccessPoint(mContext, configuration); 100 WifiInfo wifiInfo = new WifiInfo(); 101 wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID)); 102 wifiInfo.setBSSID(configuration.BSSID); 103 originalAccessPoint.update(configuration, wifiInfo, networkInfo); 104 AccessPoint copy = new AccessPoint(mContext, originalAccessPoint); 105 106 assertThat(originalAccessPoint.getSsid().toString()).isEqualTo(copy.getSsid().toString()); 107 assertThat(originalAccessPoint.getBssid()).isEqualTo(copy.getBssid()); 108 assertThat(originalAccessPoint.getConfig()).isEqualTo(copy.getConfig()); 109 assertThat(originalAccessPoint.getSecurity()).isEqualTo(copy.getSecurity()); 110 assertThat(originalAccessPoint.isMetered()).isEqualTo(copy.isMetered()); 111 assertThat(originalAccessPoint.compareTo(copy) == 0).isTrue(); 112 } 113 114 @Test 115 public void testThatCopyAccessPoint_scanCacheShouldMatch() { 116 AccessPoint original = createAccessPointWithScanResultCache(); 117 assertThat(original.getRssi()).isEqualTo(4); 118 AccessPoint copy = new AccessPoint(mContext, createWifiConfiguration()); 119 assertThat(copy.getRssi()).isEqualTo(AccessPoint.UNREACHABLE_RSSI); 120 copy.copyFrom(original); 121 assertThat(original.getRssi()).isEqualTo(copy.getRssi()); 122 } 123 124 @Test 125 public void testCompareTo_GivesActiveBeforeInactive() { 126 AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build(); 127 AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build(); 128 129 assertSortingWorks(activeAp, inactiveAp); 130 } 131 132 @Test 133 public void testCompareTo_GivesReachableBeforeUnreachable() { 134 AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build(); 135 AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build(); 136 137 assertSortingWorks(nearAp, farAp); 138 } 139 140 @Test 141 public void testCompareTo_GivesSavedBeforeUnsaved() { 142 AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build(); 143 AccessPoint notSavedAp = new TestAccessPointBuilder(mContext).setSaved(false).build(); 144 145 assertSortingWorks(savedAp, notSavedAp); 146 } 147 148 //TODO: add tests for mRankingScore sort order if ranking is exposed 149 150 @Test 151 public void testCompareTo_GivesHighLevelBeforeLowLevel() { 152 final int highLevel = AccessPoint.SIGNAL_LEVELS - 1; 153 final int lowLevel = 1; 154 assertThat(highLevel).isGreaterThan(lowLevel); 155 156 AccessPoint strongAp = new TestAccessPointBuilder(mContext).setLevel(highLevel).build(); 157 AccessPoint weakAp = new TestAccessPointBuilder(mContext).setLevel(lowLevel).build(); 158 159 assertSortingWorks(strongAp, weakAp); 160 } 161 162 @Test 163 public void testCompareTo_GivesSsidAlphabetically() { 164 165 final String firstName = "AAAAAA"; 166 final String secondName = "zzzzzz"; 167 168 AccessPoint firstAp = new TestAccessPointBuilder(mContext).setSsid(firstName).build(); 169 AccessPoint secondAp = new TestAccessPointBuilder(mContext).setSsid(secondName).build(); 170 171 assertThat(firstAp.getSsidStr().compareToIgnoreCase(secondAp.getSsidStr()) < 0).isTrue(); 172 assertSortingWorks(firstAp, secondAp); 173 } 174 175 @Test 176 public void testCompareTo_GivesSsidCasePrecendenceAfterAlphabetical() { 177 178 final String firstName = "aaAaaa"; 179 final String secondName = "aaaaaa"; 180 final String thirdName = "BBBBBB"; 181 182 AccessPoint firstAp = new TestAccessPointBuilder(mContext).setSsid(firstName).build(); 183 AccessPoint secondAp = new TestAccessPointBuilder(mContext).setSsid(secondName).build(); 184 AccessPoint thirdAp = new TestAccessPointBuilder(mContext).setSsid(thirdName).build(); 185 186 assertSortingWorks(firstAp, secondAp); 187 assertSortingWorks(secondAp, thirdAp); 188 } 189 190 @Test 191 public void testCompareTo_AllSortingRulesCombined() { 192 193 AccessPoint active = new TestAccessPointBuilder(mContext).setActive(true).build(); 194 AccessPoint reachableAndMinLevel = new TestAccessPointBuilder(mContext) 195 .setReachable(true).build(); 196 AccessPoint saved = new TestAccessPointBuilder(mContext).setSaved(true).build(); 197 AccessPoint highLevelAndReachable = new TestAccessPointBuilder(mContext) 198 .setLevel(AccessPoint.SIGNAL_LEVELS - 1).build(); 199 AccessPoint firstName = new TestAccessPointBuilder(mContext).setSsid("a").build(); 200 AccessPoint lastname = new TestAccessPointBuilder(mContext).setSsid("z").build(); 201 202 ArrayList<AccessPoint> points = new ArrayList<AccessPoint>(); 203 points.add(lastname); 204 points.add(firstName); 205 points.add(highLevelAndReachable); 206 points.add(saved); 207 points.add(reachableAndMinLevel); 208 points.add(active); 209 210 Collections.sort(points); 211 assertThat(points.indexOf(active)).isLessThan(points.indexOf(reachableAndMinLevel)); 212 assertThat(points.indexOf(reachableAndMinLevel)).isLessThan(points.indexOf(saved)); 213 // note: the saved AP will not appear before highLevelAndReachable, 214 // because all APs with a signal level are reachable, 215 // and isReachable() takes higher sorting precedence than isSaved(). 216 assertThat(points.indexOf(saved)).isLessThan(points.indexOf(firstName)); 217 assertThat(points.indexOf(highLevelAndReachable)).isLessThan(points.indexOf(firstName)); 218 assertThat(points.indexOf(firstName)).isLessThan(points.indexOf(lastname)); 219 } 220 221 @Test 222 public void testRssiIsSetFromScanResults() { 223 AccessPoint ap = createAccessPointWithScanResultCache(); 224 int originalRssi = ap.getRssi(); 225 assertThat(originalRssi).isNotEqualTo(AccessPoint.UNREACHABLE_RSSI); 226 } 227 228 @Test 229 public void testGetRssiShouldReturnSetRssiValue() { 230 AccessPoint ap = createAccessPointWithScanResultCache(); 231 int originalRssi = ap.getRssi(); 232 int newRssi = originalRssi - 10; 233 ap.setRssi(newRssi); 234 assertThat(ap.getRssi()).isEqualTo(newRssi); 235 } 236 237 @Test 238 public void testUpdateWithScanResultShouldAverageRssi() { 239 String ssid = "ssid"; 240 int originalRssi = -65; 241 int newRssi = -80; 242 int expectedRssi = (originalRssi + newRssi) / 2; 243 AccessPoint ap = 244 new TestAccessPointBuilder(mContext).setSsid(ssid).setRssi(originalRssi).build(); 245 246 ScanResult scanResult = new ScanResult(); 247 scanResult.SSID = ssid; 248 scanResult.level = newRssi; 249 scanResult.BSSID = "bssid"; 250 scanResult.timestamp = SystemClock.elapsedRealtime() * 1000; 251 scanResult.capabilities = ""; 252 assertThat(ap.update(scanResult)).isTrue(); 253 254 assertThat(ap.getRssi()).isEqualTo(expectedRssi); 255 } 256 257 @Test 258 public void testCreateFromPasspointConfig() { 259 PasspointConfiguration config = new PasspointConfiguration(); 260 HomeSp homeSp = new HomeSp(); 261 homeSp.setFqdn("test.com"); 262 homeSp.setFriendlyName("Test Provider"); 263 config.setHomeSp(homeSp); 264 AccessPoint ap = new AccessPoint(mContext, config); 265 assertThat(ap.isPasspointConfig()).isTrue(); 266 } 267 268 @Test 269 public void testIsMetered_returnTrueWhenWifiConfigurationIsMetered() { 270 WifiConfiguration configuration = createWifiConfiguration(); 271 configuration.meteredHint = true; 272 273 NetworkInfo networkInfo = 274 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE"); 275 AccessPoint accessPoint = new AccessPoint(mContext, configuration); 276 WifiInfo wifiInfo = new WifiInfo(); 277 wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID)); 278 wifiInfo.setBSSID(configuration.BSSID); 279 wifiInfo.setNetworkId(configuration.networkId); 280 accessPoint.update(configuration, wifiInfo, networkInfo); 281 282 assertThat(accessPoint.isMetered()).isTrue(); 283 } 284 285 @Test 286 public void testIsMetered_returnTrueWhenWifiInfoIsMetered() { 287 WifiConfiguration configuration = createWifiConfiguration(); 288 289 NetworkInfo networkInfo = 290 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE"); 291 AccessPoint accessPoint = new AccessPoint(mContext, configuration); 292 WifiInfo wifiInfo = new WifiInfo(); 293 wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID)); 294 wifiInfo.setBSSID(configuration.BSSID); 295 wifiInfo.setNetworkId(configuration.networkId); 296 wifiInfo.setMeteredHint(true); 297 accessPoint.update(configuration, wifiInfo, networkInfo); 298 299 assertThat(accessPoint.isMetered()).isTrue(); 300 } 301 302 @Test 303 public void testIsMetered_returnTrueWhenScoredNetworkIsMetered() { 304 AccessPoint ap = createAccessPointWithScanResultCache(); 305 306 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 307 .thenReturn( 308 new ScoredNetwork( 309 null /* NetworkKey */, 310 null /* rssiCurve */, 311 true /* metered */)); 312 ap.update(mockWifiNetworkScoreCache, false /* scoringUiEnabled */); 313 314 assertThat(ap.isMetered()).isTrue(); 315 } 316 317 @Test 318 public void testIsMetered_returnFalseByDefault() { 319 WifiConfiguration configuration = createWifiConfiguration(); 320 321 NetworkInfo networkInfo = 322 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE"); 323 AccessPoint accessPoint = new AccessPoint(mContext, configuration); 324 WifiInfo wifiInfo = new WifiInfo(); 325 wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID)); 326 wifiInfo.setBSSID(configuration.BSSID); 327 wifiInfo.setNetworkId(configuration.networkId); 328 accessPoint.update(configuration, wifiInfo, networkInfo); 329 330 assertThat(accessPoint.isMetered()).isFalse(); 331 } 332 333 @Test 334 public void testSpeedLabel_returnsVeryFast() { 335 AccessPoint ap = createAccessPointWithScanResultCache(); 336 337 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 338 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 339 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST); 340 341 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 342 343 assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.VERY_FAST); 344 assertThat(ap.getSpeedLabel()) 345 .isEqualTo(mContext.getString(R.string.speed_label_very_fast)); 346 } 347 348 @Test 349 public void testSpeedLabel_returnsFast() { 350 AccessPoint ap = createAccessPointWithScanResultCache(); 351 352 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 353 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 354 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.FAST); 355 356 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 357 358 assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.FAST); 359 assertThat(ap.getSpeedLabel()) 360 .isEqualTo(mContext.getString(R.string.speed_label_fast)); 361 } 362 363 @Test 364 public void testSpeedLabel_returnsOkay() { 365 AccessPoint ap = createAccessPointWithScanResultCache(); 366 367 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 368 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 369 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.MODERATE); 370 371 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 372 373 assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.MODERATE); 374 assertThat(ap.getSpeedLabel()) 375 .isEqualTo(mContext.getString(R.string.speed_label_okay)); 376 } 377 378 @Test 379 public void testSpeedLabel_returnsSlow() { 380 AccessPoint ap = createAccessPointWithScanResultCache(); 381 382 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 383 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 384 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.SLOW); 385 386 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 387 388 assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.SLOW); 389 assertThat(ap.getSpeedLabel()) 390 .isEqualTo(mContext.getString(R.string.speed_label_slow)); 391 } 392 393 @Test 394 public void testSpeedLabel_isDerivedFromConnectedBssid() { 395 int rssi = -55; 396 String bssid = "00:00:00:00:00:00"; 397 int networkId = 123; 398 399 WifiInfo info = new WifiInfo(); 400 info.setRssi(rssi); 401 info.setSSID(WifiSsid.createFromAsciiEncoded(TEST_SSID)); 402 info.setBSSID(bssid); 403 info.setNetworkId(networkId); 404 405 AccessPoint ap = 406 new TestAccessPointBuilder(mContext) 407 .setActive(true) 408 .setNetworkId(networkId) 409 .setSsid(TEST_SSID) 410 .setScanResultCache(buildScanResultCache()) 411 .setWifiInfo(info) 412 .build(); 413 414 NetworkKey key = new NetworkKey(new WifiKey('"' + TEST_SSID + '"', bssid)); 415 when(mockWifiNetworkScoreCache.getScoredNetwork(key)) 416 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 417 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.FAST); 418 419 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 420 421 verify(mockWifiNetworkScoreCache, times(2)).getScoredNetwork(key); 422 verify(mockWifiNetworkScoreCache, never()).getScoredNetwork(any(ScanResult.class)); 423 assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.FAST); 424 } 425 426 @Test 427 public void testSummaryString_showsSpeedLabel() { 428 AccessPoint ap = createAccessPointWithScanResultCache(); 429 430 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 431 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 432 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST); 433 434 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 435 436 assertThat(ap.getSummary()).isEqualTo(mContext.getString(R.string.speed_label_very_fast)); 437 } 438 439 @Test 440 public void testSummaryString_concatenatesSpeedLabel() { 441 AccessPoint ap = createAccessPointWithScanResultCache(); 442 ap.update(new WifiConfiguration()); 443 444 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 445 .thenReturn(buildScoredNetworkWithMockBadgeCurve()); 446 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST); 447 448 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */); 449 450 String expectedString = mContext.getString(R.string.speed_label_very_fast) + " / " 451 + mContext.getString(R.string.wifi_remembered); 452 assertThat(ap.getSummary()).isEqualTo(expectedString); 453 } 454 455 @Test 456 public void testSummaryString_showsWrongPasswordLabel() { 457 WifiConfiguration configuration = createWifiConfiguration(); 458 configuration.getNetworkSelectionStatus().setNetworkSelectionStatus( 459 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED); 460 configuration.getNetworkSelectionStatus().setNetworkSelectionDisableReason( 461 WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD); 462 AccessPoint ap = new AccessPoint(mContext, configuration); 463 464 assertThat(ap.getSummary()).isEqualTo(mContext.getString( 465 R.string.wifi_check_password_try_again)); 466 } 467 468 private ScoredNetwork buildScoredNetworkWithMockBadgeCurve() { 469 Bundle attr1 = new Bundle(); 470 attr1.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, mockBadgeCurve); 471 return new ScoredNetwork( 472 new NetworkKey(new WifiKey("\"ssid\"", "00:00:00:00:00:00")), 473 mockBadgeCurve, 474 false /* meteredHint */, 475 attr1); 476 477 } 478 479 private AccessPoint createAccessPointWithScanResultCache() { 480 Bundle bundle = new Bundle(); 481 ArrayList<ScanResult> scanResults = buildScanResultCache(); 482 bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, scanResults); 483 return new AccessPoint(mContext, bundle); 484 } 485 486 private ArrayList<ScanResult> buildScanResultCache() { 487 ArrayList<ScanResult> scanResults = new ArrayList<>(); 488 for (int i = 0; i < 5; i++) { 489 ScanResult scanResult = new ScanResult(); 490 scanResult.level = i; 491 scanResult.BSSID = "bssid-" + i; 492 scanResult.timestamp = SystemClock.elapsedRealtime() * 1000; 493 scanResult.capabilities = ""; 494 scanResults.add(scanResult); 495 } 496 return scanResults; 497 } 498 499 private WifiConfiguration createWifiConfiguration() { 500 WifiConfiguration configuration = new WifiConfiguration(); 501 configuration.BSSID = "bssid"; 502 configuration.SSID = "ssid"; 503 configuration.networkId = 123; 504 return configuration; 505 } 506 507 /** 508 * Assert that the first AccessPoint appears after the second AccessPoint 509 * once sorting has been completed. 510 */ 511 private void assertSortingWorks(AccessPoint first, AccessPoint second) { 512 513 ArrayList<AccessPoint> points = new ArrayList<AccessPoint>(); 514 515 // add in reverse order so we can tell that sorting actually changed something 516 points.add(second); 517 points.add(first); 518 Collections.sort(points); 519 assertWithMessage( 520 String.format("After sorting: second AccessPoint should have higher array index " 521 + "than the first, but found indicies second '%s' and first '%s'.", 522 points.indexOf(second), points.indexOf(first))) 523 .that(points.indexOf(second)).isGreaterThan(points.indexOf(first)); 524 } 525 526 @Test 527 public void testBuilder_setActive() { 528 AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build(); 529 assertThat(activeAp.isActive()).isTrue(); 530 531 AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build(); 532 assertThat(inactiveAp.isActive()).isFalse(); 533 } 534 535 @Test 536 public void testBuilder_setReachable() { 537 AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build(); 538 assertThat(nearAp.isReachable()).isTrue(); 539 540 AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build(); 541 assertThat(farAp.isReachable()).isFalse(); 542 } 543 544 @Test 545 public void testBuilder_setSaved() { 546 AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build(); 547 assertThat(savedAp.isSaved()).isTrue(); 548 549 AccessPoint newAp = new TestAccessPointBuilder(mContext).setSaved(false).build(); 550 assertThat(newAp.isSaved()).isFalse(); 551 } 552 553 @Test 554 public void testBuilder_setLevel() { 555 AccessPoint testAp; 556 557 for (int i = 0; i < AccessPoint.SIGNAL_LEVELS; i++) { 558 testAp = new TestAccessPointBuilder(mContext).setLevel(i).build(); 559 assertThat(testAp.getLevel()).isEqualTo(i); 560 } 561 562 // numbers larger than the max level should be set to max 563 testAp = new TestAccessPointBuilder(mContext).setLevel(AccessPoint.SIGNAL_LEVELS).build(); 564 assertThat(testAp.getLevel()).isEqualTo(AccessPoint.SIGNAL_LEVELS - 1); 565 566 // numbers less than 0 should give level 0 567 testAp = new TestAccessPointBuilder(mContext).setLevel(-100).build(); 568 assertThat(testAp.getLevel()).isEqualTo(0); 569 } 570 571 @Test 572 public void testBuilder_settingReachableAfterLevelDoesNotAffectLevel() { 573 int level = 1; 574 assertThat(level).isLessThan(AccessPoint.SIGNAL_LEVELS - 1); 575 576 AccessPoint testAp = 577 new TestAccessPointBuilder(mContext).setLevel(level).setReachable(true).build(); 578 assertThat(testAp.getLevel()).isEqualTo(level); 579 } 580 581 @Test 582 public void testBuilder_setSsid() { 583 String name = "AmazingSsid!"; 584 AccessPoint namedAp = new TestAccessPointBuilder(mContext).setSsid(name).build(); 585 assertThat(namedAp.getSsidStr()).isEqualTo(name); 586 } 587 588 @Test 589 public void testBuilder_passpointConfig() { 590 String fqdn = "Test.com"; 591 String providerFriendlyName = "Test Provider"; 592 AccessPoint ap = new TestAccessPointBuilder(mContext).setFqdn(fqdn) 593 .setProviderFriendlyName(providerFriendlyName).build(); 594 assertThat(ap.isPasspointConfig()).isTrue(); 595 assertThat(ap.getPasspointFqdn()).isEqualTo(fqdn); 596 assertThat(ap.getConfigName()).isEqualTo(providerFriendlyName); 597 } 598 599 @Test 600 public void testUpdateNetworkInfo_returnsTrue() { 601 int networkId = 123; 602 int rssi = -55; 603 WifiConfiguration config = new WifiConfiguration(); 604 config.networkId = networkId; 605 WifiInfo wifiInfo = new WifiInfo(); 606 wifiInfo.setNetworkId(networkId); 607 wifiInfo.setRssi(rssi); 608 609 NetworkInfo networkInfo = 610 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", ""); 611 networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "", ""); 612 613 AccessPoint ap = new TestAccessPointBuilder(mContext) 614 .setNetworkInfo(networkInfo) 615 .setNetworkId(networkId) 616 .setRssi(rssi) 617 .setWifiInfo(wifiInfo) 618 .build(); 619 620 NetworkInfo newInfo = new NetworkInfo(networkInfo); 621 newInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", ""); 622 assertThat(ap.update(config, wifiInfo, newInfo)).isTrue(); 623 } 624 625 @Test 626 public void testUpdateNetworkInfoWithSameInfo_returnsFalse() { 627 int networkId = 123; 628 int rssi = -55; 629 WifiConfiguration config = new WifiConfiguration(); 630 config.networkId = networkId; 631 WifiInfo wifiInfo = new WifiInfo(); 632 wifiInfo.setNetworkId(networkId); 633 wifiInfo.setRssi(rssi); 634 635 NetworkInfo networkInfo = 636 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", ""); 637 networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "", ""); 638 639 AccessPoint ap = new TestAccessPointBuilder(mContext) 640 .setNetworkInfo(networkInfo) 641 .setNetworkId(networkId) 642 .setRssi(rssi) 643 .setWifiInfo(wifiInfo) 644 .build(); 645 646 NetworkInfo newInfo = new NetworkInfo(networkInfo); // same values 647 assertThat(ap.update(config, wifiInfo, newInfo)).isFalse(); 648 } 649 650 @Test 651 public void testUpdateWithConfigChangeOnly_returnsFalseButInvokesListener() { 652 int networkId = 123; 653 int rssi = -55; 654 WifiConfiguration config = new WifiConfiguration(); 655 config.networkId = networkId; 656 config.numNoInternetAccessReports = 1; 657 658 WifiInfo wifiInfo = new WifiInfo(); 659 wifiInfo.setNetworkId(networkId); 660 wifiInfo.setRssi(rssi); 661 662 NetworkInfo networkInfo = 663 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", ""); 664 networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", ""); 665 666 AccessPoint ap = new TestAccessPointBuilder(mContext) 667 .setNetworkInfo(networkInfo) 668 .setNetworkId(networkId) 669 .setRssi(rssi) 670 .setWifiInfo(wifiInfo) 671 .build(); 672 673 AccessPoint.AccessPointListener mockListener = mock(AccessPoint.AccessPointListener.class); 674 ap.setListener(mockListener); 675 WifiConfiguration newConfig = new WifiConfiguration(config); 676 config.validatedInternetAccess = true; 677 678 assertThat(ap.update(newConfig, wifiInfo, networkInfo)).isFalse(); 679 verify(mockListener).onAccessPointChanged(ap); 680 } 681 682 @Test 683 public void testUpdateWithNullWifiConfiguration_doesNotThrowNPE() { 684 int networkId = 123; 685 int rssi = -55; 686 WifiConfiguration config = new WifiConfiguration(); 687 config.networkId = networkId; 688 WifiInfo wifiInfo = new WifiInfo(); 689 wifiInfo.setNetworkId(networkId); 690 wifiInfo.setRssi(rssi); 691 692 NetworkInfo networkInfo = 693 new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", ""); 694 networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "", ""); 695 696 AccessPoint ap = new TestAccessPointBuilder(mContext) 697 .setNetworkInfo(networkInfo) 698 .setNetworkId(networkId) 699 .setRssi(rssi) 700 .setWifiInfo(wifiInfo) 701 .build(); 702 703 ap.update(null, wifiInfo, networkInfo); 704 } 705} 706