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