AccessPointTest.java revision 54bdcfa08180a69b852c8f71b39d30f0061ce6ff
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 android.content.Context;
22import android.net.ConnectivityManager;
23import android.net.NetworkInfo;
24import android.net.wifi.ScanResult;
25import android.net.wifi.WifiConfiguration;
26import android.net.wifi.WifiInfo;
27import android.net.wifi.WifiSsid;
28import android.os.Bundle;
29import android.os.SystemClock;
30import android.support.test.InstrumentationRegistry;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33import android.text.SpannableString;
34import android.text.style.TtsSpan;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39
40import java.util.ArrayList;
41import java.util.Collections;
42
43@SmallTest
44@RunWith(AndroidJUnit4.class)
45public class AccessPointTest {
46
47    private static final String TEST_SSID = "test_ssid";
48    private Context mContext;
49
50    @Before
51    public void setUp() {
52        mContext = InstrumentationRegistry.getTargetContext();
53    }
54
55    @Test
56    public void testSsidIsTelephoneSpan() {
57        final Bundle bundle = new Bundle();
58        bundle.putString("key_ssid", TEST_SSID);
59        final AccessPoint ap = new AccessPoint(InstrumentationRegistry.getTargetContext(), bundle);
60        final CharSequence ssid = ap.getSsid();
61
62        assertThat(ssid instanceof SpannableString).isTrue();
63
64        TtsSpan[] spans = ((SpannableString) ssid).getSpans(0, TEST_SSID.length(), TtsSpan.class);
65
66        assertThat(spans.length).isEqualTo(1);
67        assertThat(spans[0].getType()).isEqualTo(TtsSpan.TYPE_TELEPHONE);
68    }
69
70    @Test
71    public void testCopyAccessPoint_dataShouldMatch() {
72        WifiConfiguration configuration = createWifiConfiguration();
73
74        NetworkInfo networkInfo =
75                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
76        AccessPoint originalAccessPoint = new AccessPoint(mContext, configuration);
77        WifiInfo wifiInfo = new WifiInfo();
78        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
79        wifiInfo.setBSSID(configuration.BSSID);
80        originalAccessPoint.update(configuration, wifiInfo, networkInfo);
81        AccessPoint copy = new AccessPoint(mContext, originalAccessPoint);
82
83        assertThat(originalAccessPoint.getSsid().toString()).isEqualTo(copy.getSsid().toString());
84        assertThat(originalAccessPoint.getBssid()).isEqualTo(copy.getBssid());
85        assertThat(originalAccessPoint.getConfig()).isEqualTo(copy.getConfig());
86        assertThat(originalAccessPoint.getSecurity()).isEqualTo(copy.getSecurity());
87        assertThat(originalAccessPoint.compareTo(copy) == 0).isTrue();
88    }
89
90    @Test
91    public void testThatCopyAccessPoint_scanCacheShouldMatch() {
92        AccessPoint original = createAccessPointWithScanResultCache();
93        assertThat(original.getRssi()).isEqualTo(4);
94        AccessPoint copy = new AccessPoint(mContext, createWifiConfiguration());
95        assertThat(copy.getRssi()).isEqualTo(AccessPoint.UNREACHABLE_RSSI);
96        copy.copyFrom(original);
97        assertThat(original.getRssi()).isEqualTo(copy.getRssi());
98    }
99
100    @Test
101    public void testCompareTo_GivesActiveBeforeInactive() {
102        AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build();
103        AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build();
104
105        assertSortingWorks(activeAp, inactiveAp);
106    }
107
108    @Test
109    public void testCompareTo_GivesReachableBeforeUnreachable() {
110        AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build();
111        AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build();
112
113        assertSortingWorks(nearAp, farAp);
114    }
115
116    @Test
117    public void testCompareTo_GivesSavedBeforeUnsaved() {
118        AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build();
119        AccessPoint notSavedAp = new TestAccessPointBuilder(mContext).setSaved(false).build();
120
121        assertSortingWorks(savedAp, notSavedAp);
122    }
123
124    //TODO: add tests for mRankingScore sort order if ranking is exposed
125
126    @Test
127    public void testCompareTo_GivesHighLevelBeforeLowLevel() {
128        final int highLevel = AccessPoint.SIGNAL_LEVELS - 1;
129        final int lowLevel = 1;
130        assertThat(highLevel).isGreaterThan(lowLevel);
131
132        AccessPoint strongAp = new TestAccessPointBuilder(mContext).setLevel(highLevel).build();
133        AccessPoint weakAp = new TestAccessPointBuilder(mContext).setLevel(lowLevel).build();
134
135        assertSortingWorks(strongAp, weakAp);
136    }
137
138    @Test
139    public void testCompareTo_GivesSsidAlphabetically() {
140
141        final String firstName = "AAAAAA";
142        final String secondName = "zzzzzz";
143
144        AccessPoint firstAp = new TestAccessPointBuilder(mContext).setSsid(firstName).build();
145        AccessPoint secondAp = new TestAccessPointBuilder(mContext).setSsid(secondName).build();
146
147        assertThat(firstAp.getSsidStr().compareToIgnoreCase(secondAp.getSsidStr()) < 0).isTrue();
148        assertSortingWorks(firstAp, secondAp);
149    }
150
151    @Test
152    public void testCompareTo_AllSortingRulesCombined() {
153
154        AccessPoint active = new TestAccessPointBuilder(mContext).setActive(true).build();
155        AccessPoint reachableAndMinLevel = new TestAccessPointBuilder(mContext)
156                .setReachable(true).build();
157        AccessPoint saved = new TestAccessPointBuilder(mContext).setSaved(true).build();
158        AccessPoint highLevelAndReachable = new TestAccessPointBuilder(mContext)
159                .setLevel(AccessPoint.SIGNAL_LEVELS - 1).build();
160        AccessPoint firstName = new TestAccessPointBuilder(mContext).setSsid("a").build();
161        AccessPoint lastname = new TestAccessPointBuilder(mContext).setSsid("z").build();
162
163        ArrayList<AccessPoint> points = new ArrayList<AccessPoint>();
164        points.add(lastname);
165        points.add(firstName);
166        points.add(highLevelAndReachable);
167        points.add(saved);
168        points.add(reachableAndMinLevel);
169        points.add(active);
170
171        Collections.sort(points);
172        assertThat(points.indexOf(active)).isLessThan(points.indexOf(reachableAndMinLevel));
173        assertThat(points.indexOf(reachableAndMinLevel)).isLessThan(points.indexOf(saved));
174        // note: the saved AP will not appear before highLevelAndReachable,
175        // because all APs with a signal level are reachable,
176        // and isReachable() takes higher sorting precedence than isSaved().
177        assertThat(points.indexOf(saved)).isLessThan(points.indexOf(firstName));
178        assertThat(points.indexOf(highLevelAndReachable)).isLessThan(points.indexOf(firstName));
179        assertThat(points.indexOf(firstName)).isLessThan(points.indexOf(lastname));
180    }
181
182    @Test
183    public void testRssiIsSetFromScanResults() {
184        AccessPoint ap = createAccessPointWithScanResultCache();
185        int originalRssi = ap.getRssi();
186        assertThat(originalRssi).isNotEqualTo(AccessPoint.UNREACHABLE_RSSI);
187    }
188
189    @Test
190    public void testGetRssiShouldReturnSetRssiValue() {
191        AccessPoint ap = createAccessPointWithScanResultCache();
192        int originalRssi = ap.getRssi();
193        int newRssi = originalRssi - 10;
194        ap.setRssi(newRssi);
195        assertThat(ap.getRssi()).isEqualTo(newRssi);
196    }
197
198    private AccessPoint createAccessPointWithScanResultCache() {
199        Bundle bundle = new Bundle();
200        ArrayList<ScanResult> scanResults = new ArrayList<>();
201        for (int i = 0; i < 5; i++) {
202            ScanResult scanResult = new ScanResult();
203            scanResult.level = i;
204            scanResult.BSSID = "bssid-" + i;
205            scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
206            scanResults.add(scanResult);
207        }
208
209        bundle.putParcelableArrayList("key_scanresultcache", scanResults);
210        return new AccessPoint(mContext, bundle);
211    }
212
213    private WifiConfiguration createWifiConfiguration() {
214        WifiConfiguration configuration = new WifiConfiguration();
215        configuration.BSSID = "bssid";
216        configuration.SSID = "ssid";
217        configuration.networkId = 123;
218        return configuration;
219    }
220
221    /**
222    * Assert that the first AccessPoint appears after the second AccessPoint
223    * once sorting has been completed.
224    */
225    private void assertSortingWorks(AccessPoint first, AccessPoint second) {
226
227        ArrayList<AccessPoint> points = new ArrayList<AccessPoint>();
228
229        // add in reverse order so we can tell that sorting actually changed something
230        points.add(second);
231        points.add(first);
232        Collections.sort(points);
233        assertWithMessage(
234                String.format("After sorting: second AccessPoint should have higher array index "
235                    + "than the first, but found indicies second '%s' and first '%s'.",
236                    points.indexOf(second), points.indexOf(first)))
237            .that(points.indexOf(second)).isGreaterThan(points.indexOf(first));
238    }
239
240    @Test
241    public void testBuilder_setActive() {
242        AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build();
243        assertThat(activeAp.isActive()).isTrue();
244
245        AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build();
246        assertThat(inactiveAp.isActive()).isFalse();
247    }
248
249    @Test
250    public void testBuilder_setReachable() {
251        AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build();
252        assertThat(nearAp.isReachable()).isTrue();
253
254        AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build();
255        assertThat(farAp.isReachable()).isFalse();
256    }
257
258    @Test
259    public void testBuilder_setSaved() {
260        AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build();
261        assertThat(savedAp.isSaved()).isTrue();
262
263        AccessPoint newAp = new TestAccessPointBuilder(mContext).setSaved(false).build();
264        assertThat(newAp.isSaved()).isFalse();
265    }
266
267    @Test
268    public void testBuilder_setLevel() {
269        AccessPoint testAp;
270
271        for (int i = 0; i < AccessPoint.SIGNAL_LEVELS; i++) {
272            testAp = new TestAccessPointBuilder(mContext).setLevel(i).build();
273            assertThat(testAp.getLevel()).isEqualTo(i);
274        }
275
276        // numbers larger than the max level should be set to max
277        testAp = new TestAccessPointBuilder(mContext).setLevel(AccessPoint.SIGNAL_LEVELS).build();
278        assertThat(testAp.getLevel()).isEqualTo(AccessPoint.SIGNAL_LEVELS - 1);
279
280        // numbers less than 0 should give level 0
281        testAp = new TestAccessPointBuilder(mContext).setLevel(-100).build();
282        assertThat(testAp.getLevel()).isEqualTo(0);
283    }
284
285    @Test
286    public void testBuilder_settingReachableAfterLevelDoesNotAffectLevel() {
287        int level = 1;
288        assertThat(level).isLessThan(AccessPoint.SIGNAL_LEVELS - 1);
289
290        AccessPoint testAp =
291                new TestAccessPointBuilder(mContext).setLevel(level).setReachable(true).build();
292        assertThat(testAp.getLevel()).isEqualTo(level);
293    }
294
295    @Test
296    public void testBuilder_setSsid() {
297        String name = "AmazingSsid!";
298        AccessPoint namedAp = new TestAccessPointBuilder(mContext).setSsid(name).build();
299        assertThat(namedAp.getSsidStr()).isEqualTo(name);
300    }
301}
302