AccessPointTest.java revision ce78a5f2d33716fde95f12b1e02df953d013e986
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    @Test
199    public void testUpdateWithScanResultShouldAverageRssi() {
200        String ssid = "ssid";
201        int originalRssi = -65;
202        int newRssi = -80;
203        int expectedRssi = (originalRssi + newRssi) / 2;
204        AccessPoint ap =
205                new TestAccessPointBuilder(mContext).setSsid(ssid).setRssi(originalRssi).build();
206
207        ScanResult scanResult = new ScanResult();
208        scanResult.SSID = ssid;
209        scanResult.level = newRssi;
210        scanResult.BSSID = "bssid";
211        scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
212        scanResult.capabilities = "";
213        assertThat(ap.update(scanResult)).isTrue();
214
215        assertThat(ap.getRssi()).isEqualTo(expectedRssi);
216    }
217
218    private AccessPoint createAccessPointWithScanResultCache() {
219        Bundle bundle = new Bundle();
220        ArrayList<ScanResult> scanResults = new ArrayList<>();
221        for (int i = 0; i < 5; i++) {
222            ScanResult scanResult = new ScanResult();
223            scanResult.level = i;
224            scanResult.BSSID = "bssid-" + i;
225            scanResult.timestamp = SystemClock.elapsedRealtime() * 1000;
226            scanResult.capabilities = "";
227            scanResults.add(scanResult);
228        }
229
230        bundle.putParcelableArrayList("key_scanresultcache", scanResults);
231        return new AccessPoint(mContext, bundle);
232    }
233
234    private WifiConfiguration createWifiConfiguration() {
235        WifiConfiguration configuration = new WifiConfiguration();
236        configuration.BSSID = "bssid";
237        configuration.SSID = "ssid";
238        configuration.networkId = 123;
239        return configuration;
240    }
241
242    /**
243    * Assert that the first AccessPoint appears after the second AccessPoint
244    * once sorting has been completed.
245    */
246    private void assertSortingWorks(AccessPoint first, AccessPoint second) {
247
248        ArrayList<AccessPoint> points = new ArrayList<AccessPoint>();
249
250        // add in reverse order so we can tell that sorting actually changed something
251        points.add(second);
252        points.add(first);
253        Collections.sort(points);
254        assertWithMessage(
255                String.format("After sorting: second AccessPoint should have higher array index "
256                    + "than the first, but found indicies second '%s' and first '%s'.",
257                    points.indexOf(second), points.indexOf(first)))
258            .that(points.indexOf(second)).isGreaterThan(points.indexOf(first));
259    }
260
261    @Test
262    public void testBuilder_setActive() {
263        AccessPoint activeAp = new TestAccessPointBuilder(mContext).setActive(true).build();
264        assertThat(activeAp.isActive()).isTrue();
265
266        AccessPoint inactiveAp = new TestAccessPointBuilder(mContext).setActive(false).build();
267        assertThat(inactiveAp.isActive()).isFalse();
268    }
269
270    @Test
271    public void testBuilder_setReachable() {
272        AccessPoint nearAp = new TestAccessPointBuilder(mContext).setReachable(true).build();
273        assertThat(nearAp.isReachable()).isTrue();
274
275        AccessPoint farAp = new TestAccessPointBuilder(mContext).setReachable(false).build();
276        assertThat(farAp.isReachable()).isFalse();
277    }
278
279    @Test
280    public void testBuilder_setSaved() {
281        AccessPoint savedAp = new TestAccessPointBuilder(mContext).setSaved(true).build();
282        assertThat(savedAp.isSaved()).isTrue();
283
284        AccessPoint newAp = new TestAccessPointBuilder(mContext).setSaved(false).build();
285        assertThat(newAp.isSaved()).isFalse();
286    }
287
288    @Test
289    public void testBuilder_setLevel() {
290        AccessPoint testAp;
291
292        for (int i = 0; i < AccessPoint.SIGNAL_LEVELS; i++) {
293            testAp = new TestAccessPointBuilder(mContext).setLevel(i).build();
294            assertThat(testAp.getLevel()).isEqualTo(i);
295        }
296
297        // numbers larger than the max level should be set to max
298        testAp = new TestAccessPointBuilder(mContext).setLevel(AccessPoint.SIGNAL_LEVELS).build();
299        assertThat(testAp.getLevel()).isEqualTo(AccessPoint.SIGNAL_LEVELS - 1);
300
301        // numbers less than 0 should give level 0
302        testAp = new TestAccessPointBuilder(mContext).setLevel(-100).build();
303        assertThat(testAp.getLevel()).isEqualTo(0);
304    }
305
306    @Test
307    public void testBuilder_settingReachableAfterLevelDoesNotAffectLevel() {
308        int level = 1;
309        assertThat(level).isLessThan(AccessPoint.SIGNAL_LEVELS - 1);
310
311        AccessPoint testAp =
312                new TestAccessPointBuilder(mContext).setLevel(level).setReachable(true).build();
313        assertThat(testAp.getLevel()).isEqualTo(level);
314    }
315
316    @Test
317    public void testBuilder_setSsid() {
318        String name = "AmazingSsid!";
319        AccessPoint namedAp = new TestAccessPointBuilder(mContext).setSsid(name).build();
320        assertThat(namedAp.getSsidStr()).isEqualTo(name);
321    }
322}
323