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