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