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