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