AccessPointTest.java revision 6a62328517920d2c0aa431e350fce6ba2c105bc2
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_GivesSsidCasePrecendenceAfterAlphabetical() {
173
174        final String firstName = "aaAaaa";
175        final String secondName = "aaaaaa";
176        final String thirdName = "BBBBBB";
177
178        AccessPoint firstAp = new TestAccessPointBuilder(mContext).setSsid(firstName).build();
179        AccessPoint secondAp = new TestAccessPointBuilder(mContext).setSsid(secondName).build();
180        AccessPoint thirdAp = new TestAccessPointBuilder(mContext).setSsid(thirdName).build();
181
182        assertSortingWorks(firstAp, secondAp);
183        assertSortingWorks(secondAp, thirdAp);
184    }
185
186    @Test
187    public void testCompareTo_AllSortingRulesCombined() {
188
189        AccessPoint active = new TestAccessPointBuilder(mContext).setActive(true).build();
190        AccessPoint reachableAndMinLevel = new TestAccessPointBuilder(mContext)
191                .setReachable(true).build();
192        AccessPoint saved = new TestAccessPointBuilder(mContext).setSaved(true).build();
193        AccessPoint highLevelAndReachable = new TestAccessPointBuilder(mContext)
194                .setLevel(AccessPoint.SIGNAL_LEVELS - 1).build();
195        AccessPoint firstName = new TestAccessPointBuilder(mContext).setSsid("a").build();
196        AccessPoint lastname = new TestAccessPointBuilder(mContext).setSsid("z").build();
197
198        ArrayList<AccessPoint> points = new ArrayList<AccessPoint>();
199        points.add(lastname);
200        points.add(firstName);
201        points.add(highLevelAndReachable);
202        points.add(saved);
203        points.add(reachableAndMinLevel);
204        points.add(active);
205
206        Collections.sort(points);
207        assertThat(points.indexOf(active)).isLessThan(points.indexOf(reachableAndMinLevel));
208        assertThat(points.indexOf(reachableAndMinLevel)).isLessThan(points.indexOf(saved));
209        // note: the saved AP will not appear before highLevelAndReachable,
210        // because all APs with a signal level are reachable,
211        // and isReachable() takes higher sorting precedence than isSaved().
212        assertThat(points.indexOf(saved)).isLessThan(points.indexOf(firstName));
213        assertThat(points.indexOf(highLevelAndReachable)).isLessThan(points.indexOf(firstName));
214        assertThat(points.indexOf(firstName)).isLessThan(points.indexOf(lastname));
215    }
216
217    @Test
218    public void testRssiIsSetFromScanResults() {
219        AccessPoint ap = createAccessPointWithScanResultCache();
220        int originalRssi = ap.getRssi();
221        assertThat(originalRssi).isNotEqualTo(AccessPoint.UNREACHABLE_RSSI);
222    }
223
224    @Test
225    public void testGetRssiShouldReturnSetRssiValue() {
226        AccessPoint ap = createAccessPointWithScanResultCache();
227        int originalRssi = ap.getRssi();
228        int newRssi = originalRssi - 10;
229        ap.setRssi(newRssi);
230        assertThat(ap.getRssi()).isEqualTo(newRssi);
231    }
232
233    @Test
234    public void testUpdateWithScanResultShouldAverageRssi() {
235        String ssid = "ssid";
236        int originalRssi = -65;
237        int newRssi = -80;
238        int expectedRssi = (originalRssi + newRssi) / 2;
239        AccessPoint ap =
240                new TestAccessPointBuilder(mContext).setSsid(ssid).setRssi(originalRssi).build();
241
242        ScanResult scanResult = new ScanResult();
243        scanResult.SSID = ssid;
244        scanResult.level = newRssi;
245        scanResult.BSSID = "bssid";
246        scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
247        scanResult.capabilities = "";
248        assertThat(ap.update(scanResult)).isTrue();
249
250        assertThat(ap.getRssi()).isEqualTo(expectedRssi);
251    }
252
253    @Test
254    public void testCreateFromPasspointConfig() {
255        PasspointConfiguration config = new PasspointConfiguration();
256        HomeSp homeSp = new HomeSp();
257        homeSp.setFqdn("test.com");
258        homeSp.setFriendlyName("Test Provider");
259        config.setHomeSp(homeSp);
260        AccessPoint ap = new AccessPoint(mContext, config);
261        assertThat(ap.isPasspointConfig()).isTrue();
262    }
263
264    @Test
265    public void testIsMetered_returnTrueWhenWifiConfigurationIsMetered() {
266        WifiConfiguration configuration = createWifiConfiguration();
267        configuration.meteredHint = true;
268
269        NetworkInfo networkInfo =
270                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
271        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
272        WifiInfo wifiInfo = new WifiInfo();
273        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
274        wifiInfo.setBSSID(configuration.BSSID);
275        wifiInfo.setNetworkId(configuration.networkId);
276        accessPoint.update(configuration, wifiInfo, networkInfo);
277
278        assertThat(accessPoint.isMetered()).isTrue();
279    }
280
281    @Test
282    public void testIsMetered_returnTrueWhenWifiInfoIsMetered() {
283        WifiConfiguration configuration = createWifiConfiguration();
284
285        NetworkInfo networkInfo =
286                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
287        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
288        WifiInfo wifiInfo = new WifiInfo();
289        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
290        wifiInfo.setBSSID(configuration.BSSID);
291        wifiInfo.setNetworkId(configuration.networkId);
292        wifiInfo.setMeteredHint(true);
293        accessPoint.update(configuration, wifiInfo, networkInfo);
294
295        assertThat(accessPoint.isMetered()).isTrue();
296    }
297
298    @Test
299    public void testIsMetered_returnTrueWhenNetworkInfoIsMetered() {
300        WifiConfiguration configuration = createWifiConfiguration();
301
302        NetworkInfo networkInfo =
303                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
304        networkInfo.setMetered(true);
305        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
306        WifiInfo wifiInfo = new WifiInfo();
307        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
308        wifiInfo.setBSSID(configuration.BSSID);
309        wifiInfo.setNetworkId(configuration.networkId);
310        accessPoint.update(configuration, wifiInfo, networkInfo);
311
312        assertThat(accessPoint.isMetered()).isTrue();
313    }
314
315    @Test
316    public void testIsMetered_returnTrueWhenScoredNetworkIsMetered() {
317        AccessPoint ap = createAccessPointWithScanResultCache();
318
319        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
320                .thenReturn(
321                        new ScoredNetwork(
322                                null /* NetworkKey */,
323                                null /* rssiCurve */,
324                                true /* metered */));
325        ap.update(mockWifiNetworkScoreCache, false /* scoringUiEnabled */);
326
327        assertThat(ap.isMetered()).isTrue();
328    }
329
330    @Test
331    public void testIsMetered_returnFalseByDefault() {
332        WifiConfiguration configuration = createWifiConfiguration();
333
334        NetworkInfo networkInfo =
335                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
336        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
337        WifiInfo wifiInfo = new WifiInfo();
338        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
339        wifiInfo.setBSSID(configuration.BSSID);
340        wifiInfo.setNetworkId(configuration.networkId);
341        accessPoint.update(configuration, wifiInfo, networkInfo);
342
343        assertThat(accessPoint.isMetered()).isFalse();
344    }
345
346    @Test
347    public void testSpeedLabel_returnsVeryFast() {
348        AccessPoint ap = createAccessPointWithScanResultCache();
349
350        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
351                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
352        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_VERY_FAST);
353
354        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
355
356        assertThat(ap.getSpeed()).isEqualTo(AccessPoint.SPEED_VERY_FAST);
357        assertThat(ap.getSpeedLabel())
358                .isEqualTo(mContext.getString(R.string.speed_label_very_fast));
359    }
360
361    @Test
362    public void testSpeedLabel_returnsFast() {
363        AccessPoint ap = createAccessPointWithScanResultCache();
364
365        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
366                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
367        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_FAST);
368
369        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
370
371        assertThat(ap.getSpeed()).isEqualTo(AccessPoint.SPEED_FAST);
372        assertThat(ap.getSpeedLabel())
373                .isEqualTo(mContext.getString(R.string.speed_label_fast));
374    }
375
376    @Test
377    public void testSpeedLabel_returnsOkay() {
378        AccessPoint ap = createAccessPointWithScanResultCache();
379
380        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
381                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
382        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_MEDIUM);
383
384        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
385
386        assertThat(ap.getSpeed()).isEqualTo(AccessPoint.SPEED_MEDIUM);
387        assertThat(ap.getSpeedLabel())
388                .isEqualTo(mContext.getString(R.string.speed_label_okay));
389    }
390
391    @Test
392    public void testSpeedLabel_returnsSlow() {
393        AccessPoint ap = createAccessPointWithScanResultCache();
394
395        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
396                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
397        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_SLOW);
398
399        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
400
401        assertThat(ap.getSpeed()).isEqualTo(AccessPoint.SPEED_SLOW);
402        assertThat(ap.getSpeedLabel())
403                .isEqualTo(mContext.getString(R.string.speed_label_slow));
404    }
405
406    @Test
407    public void testSummaryString_showsSpeedLabel() {
408        AccessPoint ap = createAccessPointWithScanResultCache();
409
410        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
411                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
412        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_VERY_FAST);
413
414        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
415
416        assertThat(ap.getSummary()).isEqualTo(mContext.getString(R.string.speed_label_very_fast));
417    }
418
419    @Test
420    public void testSummaryString_concatenatesSpeedLabel() {
421        AccessPoint ap = createAccessPointWithScanResultCache();
422        ap.update(new WifiConfiguration());
423
424        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
425                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
426        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.SPEED_VERY_FAST);
427
428        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
429
430        String expectedString = mContext.getString(R.string.speed_label_very_fast) + " / "
431                + mContext.getString(R.string.wifi_remembered);
432        assertThat(ap.getSummary()).isEqualTo(expectedString);
433    }
434
435    @Test
436    public void testSummaryString_showsWrongPasswordLabel() {
437        WifiConfiguration configuration = createWifiConfiguration();
438        configuration.getNetworkSelectionStatus().setNetworkSelectionStatus(
439                WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED);
440        configuration.getNetworkSelectionStatus().setNetworkSelectionDisableReason(
441                WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD);
442        AccessPoint ap = new AccessPoint(mContext, configuration);
443
444        assertThat(ap.getSummary()).isEqualTo(mContext.getString(
445                R.string.wifi_check_password_try_again));
446    }
447
448    private ScoredNetwork buildScoredNetworkWithMockBadgeCurve() {
449        Bundle attr1 = new Bundle();
450        attr1.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, mockBadgeCurve);
451        return new ScoredNetwork(
452                new NetworkKey(new WifiKey("\"ssid\"", "00:00:00:00:00:00")),
453                mockBadgeCurve,
454                false /* meteredHint */,
455                attr1);
456
457    }
458
459    private AccessPoint createAccessPointWithScanResultCache() {
460        Bundle bundle = new Bundle();
461        ArrayList<ScanResult> scanResults = new ArrayList<>();
462        for (int i = 0; i < 5; i++) {
463            ScanResult scanResult = new ScanResult();
464            scanResult.level = i;
465            scanResult.BSSID = "bssid-" + i;
466            scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
467            scanResult.capabilities = "";
468            scanResults.add(scanResult);
469        }
470
471        bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, scanResults);
472        return new AccessPoint(mContext, bundle);
473    }
474
475    private WifiConfiguration createWifiConfiguration() {
476        WifiConfiguration configuration = new WifiConfiguration();
477        configuration.BSSID = "bssid";
478        configuration.SSID = "ssid";
479        configuration.networkId = 123;
480        return configuration;
481    }
482
483    /**
484    * Assert that the first AccessPoint appears after the second AccessPoint
485    * once sorting has been completed.
486    */
487    private void assertSortingWorks(AccessPoint first, AccessPoint second) {
488
489        ArrayList<AccessPoint> points = new ArrayList<AccessPoint>();
490
491        // add in reverse order so we can tell that sorting actually changed something
492        points.add(second);
493        points.add(first);
494        Collections.sort(points);
495        assertWithMessage(
496                String.format("After sorting: second AccessPoint should have higher array index "
497                    + "than the first, but found indicies second '%s' and first '%s'.",
498                    points.indexOf(second), points.indexOf(first)))
499            .that(points.indexOf(second)).isGreaterThan(points.indexOf(first));
500    }
501
502    @Test
503    public void testBuilder_setActive() {
504        AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build();
505        assertThat(activeAp.isActive()).isTrue();
506
507        AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build();
508        assertThat(inactiveAp.isActive()).isFalse();
509    }
510
511    @Test
512    public void testBuilder_setReachable() {
513        AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build();
514        assertThat(nearAp.isReachable()).isTrue();
515
516        AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build();
517        assertThat(farAp.isReachable()).isFalse();
518    }
519
520    @Test
521    public void testBuilder_setSaved() {
522        AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build();
523        assertThat(savedAp.isSaved()).isTrue();
524
525        AccessPoint newAp = new TestAccessPointBuilder(mContext).setSaved(false).build();
526        assertThat(newAp.isSaved()).isFalse();
527    }
528
529    @Test
530    public void testBuilder_setLevel() {
531        AccessPoint testAp;
532
533        for (int i = 0; i < AccessPoint.SIGNAL_LEVELS; i++) {
534            testAp = new TestAccessPointBuilder(mContext).setLevel(i).build();
535            assertThat(testAp.getLevel()).isEqualTo(i);
536        }
537
538        // numbers larger than the max level should be set to max
539        testAp = new TestAccessPointBuilder(mContext).setLevel(AccessPoint.SIGNAL_LEVELS).build();
540        assertThat(testAp.getLevel()).isEqualTo(AccessPoint.SIGNAL_LEVELS - 1);
541
542        // numbers less than 0 should give level 0
543        testAp = new TestAccessPointBuilder(mContext).setLevel(-100).build();
544        assertThat(testAp.getLevel()).isEqualTo(0);
545    }
546
547    @Test
548    public void testBuilder_settingReachableAfterLevelDoesNotAffectLevel() {
549        int level = 1;
550        assertThat(level).isLessThan(AccessPoint.SIGNAL_LEVELS - 1);
551
552        AccessPoint testAp =
553                new TestAccessPointBuilder(mContext).setLevel(level).setReachable(true).build();
554        assertThat(testAp.getLevel()).isEqualTo(level);
555    }
556
557    @Test
558    public void testBuilder_setSsid() {
559        String name = "AmazingSsid!";
560        AccessPoint namedAp = new TestAccessPointBuilder(mContext).setSsid(name).build();
561        assertThat(namedAp.getSsidStr()).isEqualTo(name);
562    }
563
564    @Test
565    public void testBuilder_passpointConfig() {
566        String fqdn = "Test.com";
567        String providerFriendlyName = "Test Provider";
568        AccessPoint ap = new TestAccessPointBuilder(mContext).setFqdn(fqdn)
569                .setProviderFriendlyName(providerFriendlyName).build();
570        assertThat(ap.isPasspointConfig()).isTrue();
571        assertThat(ap.getPasspointFqdn()).isEqualTo(fqdn);
572        assertThat(ap.getConfigName()).isEqualTo(providerFriendlyName);
573    }
574
575    @Test
576    public void testUpdateNetworkInfo_returnsTrue() {
577        int networkId = 123;
578        int rssi = -55;
579        WifiConfiguration config = new WifiConfiguration();
580        config.networkId = networkId;
581        WifiInfo wifiInfo = new WifiInfo();
582        wifiInfo.setNetworkId(networkId);
583        wifiInfo.setRssi(rssi);
584
585        NetworkInfo networkInfo =
586                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", "");
587        networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "", "");
588
589        AccessPoint ap = new TestAccessPointBuilder(mContext)
590                .setNetworkInfo(networkInfo)
591                .setNetworkId(networkId)
592                .setRssi(rssi)
593                .setWifiInfo(wifiInfo)
594                .build();
595
596        NetworkInfo newInfo = new NetworkInfo(networkInfo);
597        newInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "", "");
598        assertThat(ap.update(config, wifiInfo, newInfo)).isTrue();
599    }
600
601    @Test
602    public void testUpdateNetworkInfoWithSameInfo_returnsFalse() {
603        int networkId = 123;
604        int rssi = -55;
605        WifiConfiguration config = new WifiConfiguration();
606        config.networkId = networkId;
607        WifiInfo wifiInfo = new WifiInfo();
608        wifiInfo.setNetworkId(networkId);
609        wifiInfo.setRssi(rssi);
610
611        NetworkInfo networkInfo =
612                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 0 /* subtype */, "WIFI", "");
613        networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "", "");
614
615        AccessPoint ap = new TestAccessPointBuilder(mContext)
616                .setNetworkInfo(networkInfo)
617                .setNetworkId(networkId)
618                .setRssi(rssi)
619                .setWifiInfo(wifiInfo)
620                .build();
621
622        NetworkInfo newInfo = new NetworkInfo(networkInfo); // same values
623        assertThat(ap.update(config, wifiInfo, newInfo)).isFalse();
624    }
625}
626