Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -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 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 20 | import static org.junit.Assert.assertEquals; |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertFalse; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertTrue; |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.any; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 25 | import static org.mockito.Mockito.anyInt; |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.atMost; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 27 | import static org.mockito.Mockito.doAnswer; |
| 28 | import static org.mockito.Mockito.doNothing; |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.doThrow; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.mock; |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.never; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 32 | import static org.mockito.Mockito.reset; |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 33 | import static org.mockito.Mockito.times; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.verify; |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 35 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 40 | import android.content.IntentFilter; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 41 | import android.net.ConnectivityManager; |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 42 | import android.net.Network; |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 43 | import android.net.NetworkInfo; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 44 | import android.net.NetworkKey; |
| 45 | import android.net.NetworkScoreManager; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 46 | import android.net.RssiCurve; |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 47 | import android.net.ScoredNetwork; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 48 | import android.net.WifiKey; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 49 | import android.net.wifi.ScanResult; |
| 50 | import android.net.wifi.WifiConfiguration; |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 51 | import android.net.wifi.WifiInfo; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 52 | import android.net.wifi.WifiManager; |
| 53 | import android.net.wifi.WifiNetworkScoreCache; |
| 54 | import android.net.wifi.WifiSsid; |
| 55 | import android.os.Bundle; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 56 | import android.os.Handler; |
| 57 | import android.os.HandlerThread; |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 58 | import android.os.SystemClock; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 59 | import android.provider.Settings; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 60 | import android.support.test.InstrumentationRegistry; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 61 | import android.support.test.filters.FlakyTest; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 62 | import android.support.test.filters.SmallTest; |
| 63 | import android.support.test.runner.AndroidJUnit4; |
| 64 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 65 | import org.junit.After; |
| 66 | import org.junit.Before; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 67 | import org.junit.Test; |
| 68 | import org.junit.runner.RunWith; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 69 | import org.mockito.ArgumentCaptor; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 70 | import org.mockito.Captor; |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 71 | import org.mockito.Matchers; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 72 | import org.mockito.Mock; |
| 73 | import org.mockito.MockitoAnnotations; |
| 74 | import org.mockito.invocation.InvocationOnMock; |
| 75 | import org.mockito.stubbing.Answer; |
| 76 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 77 | import java.util.ArrayList; |
| 78 | import java.util.Arrays; |
| 79 | import java.util.BitSet; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 80 | import java.util.Collections; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 81 | import java.util.List; |
Sundeep Ghuman | 04f7f34 | 2018-01-23 19:18:31 -0800 | [diff] [blame] | 82 | import java.util.Map; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 83 | import java.util.concurrent.CountDownLatch; |
| 84 | import java.util.concurrent.TimeUnit; |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 85 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 86 | // TODO(sghuman): Change these to robolectric tests b/35766684. |
| 87 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 88 | @SmallTest |
| 89 | @RunWith(AndroidJUnit4.class) |
| 90 | public class WifiTrackerTest { |
| 91 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 92 | private static final String TAG = "WifiTrackerTest"; |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 93 | private static final int LATCH_TIMEOUT = 4000; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 94 | |
| 95 | private static final String SSID_1 = "ssid1"; |
| 96 | private static final String BSSID_1 = "00:00:00:00:00:00"; |
| 97 | private static final NetworkKey NETWORK_KEY_1 = |
| 98 | new NetworkKey(new WifiKey('"' + SSID_1 + '"', BSSID_1)); |
| 99 | private static final int RSSI_1 = -30; |
| 100 | private static final byte SCORE_1 = 10; |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 101 | private static final int BADGE_1 = AccessPoint.Speed.MODERATE; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 102 | |
| 103 | private static final String SSID_2 = "ssid2"; |
| 104 | private static final String BSSID_2 = "AA:AA:AA:AA:AA:AA"; |
| 105 | private static final NetworkKey NETWORK_KEY_2 = |
| 106 | new NetworkKey(new WifiKey('"' + SSID_2 + '"', BSSID_2)); |
| 107 | private static final int RSSI_2 = -30; |
| 108 | private static final byte SCORE_2 = 15; |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 109 | private static final int BADGE_2 = AccessPoint.Speed.FAST; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 110 | |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 111 | // TODO(b/65594609): Convert mutable Data objects to instance variables / builder pattern |
| 112 | private static final int NETWORK_ID_1 = 123; |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 113 | private static final int CONNECTED_RSSI = -50; |
| 114 | private static final WifiInfo CONNECTED_AP_1_INFO = new WifiInfo(); |
| 115 | static { |
| 116 | CONNECTED_AP_1_INFO.setSSID(WifiSsid.createFromAsciiEncoded(SSID_1)); |
| 117 | CONNECTED_AP_1_INFO.setBSSID(BSSID_1); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 118 | CONNECTED_AP_1_INFO.setNetworkId(NETWORK_ID_1); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 119 | CONNECTED_AP_1_INFO.setRssi(CONNECTED_RSSI); |
| 120 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 121 | private static final WifiConfiguration CONFIGURATION_1 = new WifiConfiguration(); |
| 122 | static { |
| 123 | CONFIGURATION_1.SSID = SSID_1; |
| 124 | CONFIGURATION_1.BSSID = BSSID_1; |
| 125 | CONFIGURATION_1.networkId = NETWORK_ID_1; |
| 126 | } |
| 127 | |
| 128 | private static final int NETWORK_ID_2 = 2; |
| 129 | private static final WifiConfiguration CONFIGURATION_2 = new WifiConfiguration(); |
| 130 | static { |
| 131 | CONFIGURATION_2.SSID = SSID_2; |
| 132 | CONFIGURATION_2.BSSID = BSSID_2; |
| 133 | CONFIGURATION_2.networkId = NETWORK_ID_2; |
| 134 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 135 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 136 | @Captor ArgumentCaptor<WifiNetworkScoreCache> mScoreCacheCaptor; |
| 137 | @Mock private ConnectivityManager mockConnectivityManager; |
| 138 | @Mock private NetworkScoreManager mockNetworkScoreManager; |
| 139 | @Mock private RssiCurve mockCurve1; |
| 140 | @Mock private RssiCurve mockCurve2; |
| 141 | @Mock private RssiCurve mockBadgeCurve1; |
| 142 | @Mock private RssiCurve mockBadgeCurve2; |
| 143 | @Mock private WifiManager mockWifiManager; |
| 144 | @Mock private WifiTracker.WifiListener mockWifiListener; |
| 145 | |
| 146 | private final List<NetworkKey> mRequestedKeys = new ArrayList<>(); |
| 147 | |
| 148 | private Context mContext; |
| 149 | private CountDownLatch mAccessPointsChangedLatch; |
| 150 | private CountDownLatch mRequestScoresLatch; |
| 151 | private Handler mScannerHandler; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 152 | private HandlerThread mWorkerThread; |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 153 | |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 154 | private int mOriginalScoringUiSettingValue; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 155 | |
| 156 | @Before |
| 157 | public void setUp() { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 158 | MockitoAnnotations.initMocks(this); |
| 159 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 160 | mContext = InstrumentationRegistry.getTargetContext(); |
| 161 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 162 | mWorkerThread = new HandlerThread("TestHandlerWorkerThread"); |
| 163 | mWorkerThread.start(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 164 | |
| 165 | // Make sure the scanner doesn't try to run on the testing thread. |
| 166 | HandlerThread scannerThread = new HandlerThread("ScannerWorkerThread"); |
| 167 | scannerThread.start(); |
| 168 | mScannerHandler = new Handler(scannerThread.getLooper()); |
| 169 | |
| 170 | when(mockWifiManager.isWifiEnabled()).thenReturn(true); |
| 171 | when(mockWifiManager.getScanResults()) |
| 172 | .thenReturn(Arrays.asList(buildScanResult1(), buildScanResult2())); |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 173 | when(mockWifiManager.getConfiguredNetworks()) |
| 174 | .thenReturn(Arrays.asList(CONFIGURATION_1, CONFIGURATION_2)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 175 | |
| 176 | |
| 177 | when(mockCurve1.lookupScore(RSSI_1)).thenReturn(SCORE_1); |
| 178 | when(mockCurve2.lookupScore(RSSI_2)).thenReturn(SCORE_2); |
| 179 | |
| 180 | when(mockBadgeCurve1.lookupScore(RSSI_1)).thenReturn((byte) BADGE_1); |
| 181 | when(mockBadgeCurve2.lookupScore(RSSI_2)).thenReturn((byte) BADGE_2); |
| 182 | |
| 183 | doNothing() |
| 184 | .when(mockNetworkScoreManager) |
| 185 | .registerNetworkScoreCache( |
| 186 | anyInt(), |
| 187 | mScoreCacheCaptor.capture(), |
| 188 | Matchers.anyInt()); |
| 189 | |
| 190 | // Capture requested keys and count down latch if present |
| 191 | doAnswer( |
| 192 | new Answer<Boolean>() { |
| 193 | @Override |
| 194 | public Boolean answer(InvocationOnMock input) { |
| 195 | if (mRequestScoresLatch != null) { |
| 196 | mRequestScoresLatch.countDown(); |
| 197 | } |
| 198 | NetworkKey[] keys = (NetworkKey[]) input.getArguments()[0]; |
| 199 | for (NetworkKey key : keys) { |
| 200 | mRequestedKeys.add(key); |
| 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | }).when(mockNetworkScoreManager).requestScores(Matchers.<NetworkKey[]>any()); |
| 205 | |
| 206 | doAnswer( |
| 207 | new Answer<Void>() { |
| 208 | @Override |
| 209 | public Void answer (InvocationOnMock invocation) throws Throwable { |
| 210 | if (mAccessPointsChangedLatch != null) { |
| 211 | mAccessPointsChangedLatch.countDown(); |
| 212 | } |
| 213 | |
| 214 | return null; |
| 215 | } |
| 216 | }).when(mockWifiListener).onAccessPointsChanged(); |
| 217 | |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 218 | // Turn on Scoring UI features |
| 219 | mOriginalScoringUiSettingValue = Settings.Global.getInt( |
| 220 | InstrumentationRegistry.getTargetContext().getContentResolver(), |
| 221 | Settings.Global.NETWORK_SCORING_UI_ENABLED, |
| 222 | 0 /* disabled */); |
| 223 | Settings.Global.putInt( |
| 224 | InstrumentationRegistry.getTargetContext().getContentResolver(), |
| 225 | Settings.Global.NETWORK_SCORING_UI_ENABLED, |
| 226 | 1 /* enabled */); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 227 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @After |
| 231 | public void cleanUp() { |
| 232 | Settings.Global.putInt( |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 233 | InstrumentationRegistry.getTargetContext().getContentResolver(), |
| 234 | Settings.Global.NETWORK_SCORING_UI_ENABLED, |
| 235 | mOriginalScoringUiSettingValue); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | private static ScanResult buildScanResult1() { |
| 239 | return new ScanResult( |
| 240 | WifiSsid.createFromAsciiEncoded(SSID_1), |
| 241 | BSSID_1, |
| 242 | 0, // hessid |
| 243 | 0, //anqpDomainId |
| 244 | null, // osuProviders |
| 245 | "", // capabilities |
| 246 | RSSI_1, |
| 247 | 0, // frequency |
| 248 | SystemClock.elapsedRealtime() * 1000 /* microsecond timestamp */); |
| 249 | } |
| 250 | |
| 251 | private static ScanResult buildScanResult2() { |
| 252 | return new ScanResult( |
| 253 | WifiSsid.createFromAsciiEncoded(SSID_2), |
| 254 | BSSID_2, |
| 255 | 0, // hessid |
| 256 | 0, //anqpDomainId |
| 257 | null, // osuProviders |
| 258 | "", // capabilities |
| 259 | RSSI_2, |
| 260 | 0, // frequency |
| 261 | SystemClock.elapsedRealtime() * 1000 /* microsecond timestamp */); |
| 262 | } |
| 263 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 264 | private WifiTracker createTrackerWithImmediateBroadcastsAndInjectInitialScanResults( |
| 265 | Intent ... intents) |
| 266 | throws InterruptedException { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 267 | WifiTracker tracker = createMockedWifiTracker(); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 268 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 269 | startTracking(tracker); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 270 | for (Intent intent : intents) { |
| 271 | tracker.mReceiver.onReceive(mContext, intent); |
| 272 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 273 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 274 | sendScanResultsAndProcess(tracker); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 275 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 276 | |
| 277 | return tracker; |
| 278 | } |
| 279 | |
| 280 | private WifiTracker createMockedWifiTracker() { |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 281 | final WifiTracker wifiTracker = new WifiTracker( |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 282 | mContext, |
| 283 | mockWifiListener, |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 284 | mockWifiManager, |
| 285 | mockConnectivityManager, |
| 286 | mockNetworkScoreManager, |
Eric Schwarzenbach | 5e0535e | 2017-08-16 15:47:33 -0700 | [diff] [blame] | 287 | new IntentFilter()); // empty filter to ignore system broadcasts |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 288 | wifiTracker.setWorkThread(mWorkerThread); |
| 289 | return wifiTracker; |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | private void startTracking(WifiTracker tracker) throws InterruptedException { |
| 293 | CountDownLatch latch = new CountDownLatch(1); |
| 294 | mScannerHandler.post(new Runnable() { |
| 295 | @Override |
| 296 | public void run() { |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 297 | tracker.onStart(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 298 | latch.countDown(); |
| 299 | } |
| 300 | }); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 301 | assertTrue("Latch timed out", latch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | private void sendScanResultsAndProcess(WifiTracker tracker) throws InterruptedException { |
| 305 | mAccessPointsChangedLatch = new CountDownLatch(1); |
| 306 | Intent i = new Intent(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 307 | tracker.mReceiver.onReceive(mContext, i); |
| 308 | |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 309 | assertTrue("Latch timed out", |
| 310 | mAccessPointsChangedLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | private void updateScores() { |
| 314 | Bundle attr1 = new Bundle(); |
| 315 | attr1.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, mockBadgeCurve1); |
| 316 | ScoredNetwork sc1 = |
| 317 | new ScoredNetwork( |
| 318 | NETWORK_KEY_1, |
| 319 | mockCurve1, |
| 320 | false /* meteredHint */, |
| 321 | attr1); |
| 322 | |
| 323 | Bundle attr2 = new Bundle(); |
| 324 | attr2.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, mockBadgeCurve2); |
| 325 | ScoredNetwork sc2 = |
| 326 | new ScoredNetwork( |
| 327 | NETWORK_KEY_2, |
| 328 | mockCurve2, |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 329 | true /* meteredHint */, |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 330 | attr2); |
| 331 | |
| 332 | WifiNetworkScoreCache scoreCache = mScoreCacheCaptor.getValue(); |
| 333 | scoreCache.updateScores(Arrays.asList(sc1, sc2)); |
| 334 | } |
| 335 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 336 | private WifiTracker createTrackerWithScanResultsAndAccessPoint1Connected() |
| 337 | throws InterruptedException { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 338 | when(mockWifiManager.getConnectionInfo()).thenReturn(CONNECTED_AP_1_INFO); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 339 | |
| 340 | WifiConfiguration configuration = new WifiConfiguration(); |
| 341 | configuration.SSID = SSID_1; |
| 342 | configuration.BSSID = BSSID_1; |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 343 | configuration.networkId = NETWORK_ID_1; |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 344 | |
| 345 | NetworkInfo networkInfo = new NetworkInfo( |
| 346 | ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype"); |
| 347 | networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "connected", "test"); |
| 348 | |
| 349 | Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 350 | intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo); |
Sundeep Ghuman | db2eabb | 2017-07-28 14:51:05 -0700 | [diff] [blame] | 351 | WifiTracker tracker = |
| 352 | createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(intent); |
| 353 | assertThat(tracker.isConnected()).isTrue(); |
| 354 | return tracker; |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 357 | private void waitForHandlersToProcessCurrentlyEnqueuedMessages(WifiTracker tracker) |
| 358 | throws InterruptedException { |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 359 | // TODO(sghuman): This should no longer be necessary in a single work handler model |
| 360 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 361 | CountDownLatch workerLatch = new CountDownLatch(1); |
| 362 | tracker.mWorkHandler.post(() -> { |
| 363 | workerLatch.countDown(); |
| 364 | }); |
| 365 | assertTrue("Latch timed out while waiting for WorkerHandler", |
| 366 | workerLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 369 | private void switchToNetwork2(WifiTracker tracker) throws InterruptedException { |
| 370 | NetworkInfo networkInfo = new NetworkInfo( |
| 371 | ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype"); |
| 372 | networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTING, "connecting", "test"); |
| 373 | |
| 374 | WifiInfo info = new WifiInfo(); |
| 375 | info.setSSID(WifiSsid.createFromAsciiEncoded(SSID_2)); |
| 376 | info.setBSSID(BSSID_2); |
| 377 | info.setRssi(CONNECTED_RSSI); |
| 378 | info.setNetworkId(NETWORK_ID_2); |
| 379 | when(mockWifiManager.getConnectionInfo()).thenReturn(info); |
| 380 | |
| 381 | Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 382 | intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo); |
| 383 | tracker.mReceiver.onReceive(mContext, intent); |
| 384 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
| 385 | } |
| 386 | |
Ajay Nadathur | 7d176bc | 2016-10-24 16:55:24 -0700 | [diff] [blame] | 387 | @Test |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 388 | public void startAndStopTrackingShouldRegisterAndUnregisterScoreCache() |
| 389 | throws InterruptedException { |
| 390 | WifiTracker tracker = createMockedWifiTracker(); |
| 391 | |
| 392 | // Test register |
| 393 | startTracking(tracker); |
| 394 | verify(mockNetworkScoreManager) |
| 395 | .registerNetworkScoreCache( |
| 396 | Matchers.anyInt(), |
| 397 | mScoreCacheCaptor.capture(), |
| 398 | Matchers.anyInt()); |
| 399 | |
| 400 | WifiNetworkScoreCache scoreCache = mScoreCacheCaptor.getValue(); |
| 401 | |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 402 | CountDownLatch latch = new CountDownLatch(1); |
| 403 | doAnswer( |
| 404 | (invocation) -> { |
| 405 | latch.countDown(); |
| 406 | return null; |
| 407 | }).when(mockNetworkScoreManager) |
| 408 | .unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, scoreCache); |
| 409 | |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 410 | // Test unregister |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 411 | tracker.onStop(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 412 | |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 413 | assertTrue("Latch timed out", latch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 414 | verify(mockNetworkScoreManager) |
| 415 | .unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, scoreCache); |
| 416 | } |
| 417 | |
| 418 | @Test |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 419 | public void testGetNumSavedNetworks() throws InterruptedException { |
| 420 | WifiConfiguration validConfig = new WifiConfiguration(); |
| 421 | validConfig.SSID = SSID_1; |
| 422 | validConfig.BSSID = BSSID_1; |
| 423 | |
| 424 | WifiConfiguration selfAddedNoAssociation = new WifiConfiguration(); |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 425 | selfAddedNoAssociation.ephemeral = true; |
Amin Shaikh | a80ae0c | 2017-03-23 16:18:30 -0700 | [diff] [blame] | 426 | selfAddedNoAssociation.selfAdded = true; |
| 427 | selfAddedNoAssociation.numAssociation = 0; |
| 428 | selfAddedNoAssociation.SSID = SSID_2; |
| 429 | selfAddedNoAssociation.BSSID = BSSID_2; |
| 430 | |
| 431 | when(mockWifiManager.getConfiguredNetworks()) |
| 432 | .thenReturn(Arrays.asList(validConfig, selfAddedNoAssociation)); |
| 433 | |
| 434 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
| 435 | |
| 436 | assertEquals(1, tracker.getNumSavedNetworks()); |
| 437 | } |
| 438 | |
| 439 | @Test |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 440 | public void startTrackingShouldSetConnectedAccessPointAsActive() throws InterruptedException { |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 441 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 442 | |
| 443 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 444 | |
| 445 | assertThat(aps).hasSize(2); |
| 446 | assertThat(aps.get(0).isActive()).isTrue(); |
| 447 | } |
| 448 | |
| 449 | @Test |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 450 | public void startTrackingAfterStopTracking_shouldRequestNewScores() |
| 451 | throws InterruptedException { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 452 | // Start the tracker and inject the initial scan results and then stop tracking |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 453 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 454 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 455 | tracker.onStop(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 456 | mRequestedKeys.clear(); |
| 457 | |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 458 | mRequestScoresLatch = new CountDownLatch(1); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 459 | startTracking(tracker); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 460 | tracker.forceUpdate(); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 461 | assertTrue("Latch timed out", |
| 462 | mRequestScoresLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 463 | |
| 464 | assertTrue(mRequestedKeys.contains(NETWORK_KEY_1)); |
| 465 | assertTrue(mRequestedKeys.contains(NETWORK_KEY_2)); |
| 466 | } |
| 467 | |
| 468 | @Test |
Sundeep Ghuman | 75f9480 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 469 | public void stopTracking_shouldNotClearExistingScores() |
| 470 | throws InterruptedException { |
| 471 | // Start the tracker and inject the initial scan results and then stop tracking |
| 472 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
| 473 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 474 | tracker.onStop(); |
Sundeep Ghuman | 75f9480 | 2017-06-26 16:05:40 -0700 | [diff] [blame] | 475 | |
| 476 | assertThat(mScoreCacheCaptor.getValue().getScoredNetwork(NETWORK_KEY_1)).isNotNull(); |
| 477 | } |
| 478 | |
| 479 | @Test |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 480 | public void scoreCacheUpdateScoresShouldTriggerOnAccessPointsChanged() |
| 481 | throws InterruptedException { |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 482 | WifiTracker tracker = createMockedWifiTracker(); |
| 483 | startTracking(tracker); |
| 484 | sendScanResultsAndProcess(tracker); |
| 485 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 486 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 489 | private void updateScoresAndWaitForAccessPointsChangedCallback(WifiTracker tracker) |
| 490 | throws InterruptedException { |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 491 | // Updating scores can happen together or one after the other, so the latch countdown is set |
| 492 | // to 2. |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 493 | mAccessPointsChangedLatch = new CountDownLatch(1); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 494 | updateScores(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 495 | assertTrue("onAccessPointChanged was not called after updating scores", |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 496 | mAccessPointsChangedLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 497 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | @Test |
| 501 | public void scoreCacheUpdateScoresShouldChangeSortOrder() throws InterruptedException { |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 502 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 503 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 504 | assertTrue(aps.size() == 2); |
| 505 | assertEquals(aps.get(0).getSsidStr(), SSID_1); |
| 506 | assertEquals(aps.get(1).getSsidStr(), SSID_2); |
| 507 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 508 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 509 | |
| 510 | aps = tracker.getAccessPoints(); |
| 511 | assertTrue(aps.size() == 2); |
| 512 | assertEquals(aps.get(0).getSsidStr(), SSID_2); |
| 513 | assertEquals(aps.get(1).getSsidStr(), SSID_1); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 514 | } |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 515 | |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 516 | @Test |
| 517 | public void scoreCacheUpdateScoresShouldNotChangeSortOrderWhenSortingDisabled() |
| 518 | throws InterruptedException { |
| 519 | Settings.Global.putInt( |
| 520 | InstrumentationRegistry.getTargetContext().getContentResolver(), |
| 521 | Settings.Global.NETWORK_SCORING_UI_ENABLED, |
| 522 | 0 /* disabled */); |
| 523 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 524 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 525 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 526 | assertTrue(aps.size() == 2); |
| 527 | assertEquals(aps.get(0).getSsidStr(), SSID_1); |
| 528 | assertEquals(aps.get(1).getSsidStr(), SSID_2); |
| 529 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 530 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 531 | |
| 532 | aps = tracker.getAccessPoints(); |
| 533 | assertTrue(aps.size() == 2); |
| 534 | assertEquals(aps.get(0).getSsidStr(), SSID_1); |
| 535 | assertEquals(aps.get(1).getSsidStr(), SSID_2); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Sundeep Ghuman | 266b409 | 2017-06-16 15:12:56 -0700 | [diff] [blame] | 538 | @FlakyTest |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 539 | @Test |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 540 | public void scoreCacheUpdateScoresShouldInsertSpeedIntoAccessPoint() |
| 541 | throws InterruptedException { |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 542 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 543 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 544 | |
| 545 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 546 | |
| 547 | for (AccessPoint ap : aps) { |
| 548 | if (ap.getSsidStr().equals(SSID_1)) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 549 | assertEquals(BADGE_1, ap.getSpeed()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 550 | } else if (ap.getSsidStr().equals(SSID_2)) { |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 551 | assertEquals(BADGE_2, ap.getSpeed()); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | @Test |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 557 | public void scoreCacheUpdateMeteredShouldUpdateAccessPointMetering() |
| 558 | throws InterruptedException { |
| 559 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 560 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Stephen Chen | 21f6868 | 2017-04-04 13:23:31 -0700 | [diff] [blame] | 561 | |
| 562 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 563 | |
| 564 | for (AccessPoint ap : aps) { |
| 565 | if (ap.getSsidStr().equals(SSID_1)) { |
| 566 | assertFalse(ap.isMetered()); |
| 567 | } else if (ap.getSsidStr().equals(SSID_2)) { |
| 568 | assertTrue(ap.isMetered()); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | @Test |
Sundeep Ghuman | 55adc6b | 2017-06-05 16:46:59 -0700 | [diff] [blame] | 574 | public void noSpeedsShouldBeInsertedIntoAccessPointWhenScoringUiDisabled() |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 575 | throws InterruptedException { |
| 576 | Settings.Global.putInt( |
| 577 | InstrumentationRegistry.getTargetContext().getContentResolver(), |
| 578 | Settings.Global.NETWORK_SCORING_UI_ENABLED, |
| 579 | 0 /* disabled */); |
| 580 | |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 581 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 582 | updateScoresAndWaitForAccessPointsChangedCallback(tracker); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 583 | |
| 584 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 585 | |
| 586 | for (AccessPoint ap : aps) { |
| 587 | if (ap.getSsidStr().equals(SSID_1)) { |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 588 | assertEquals(AccessPoint.Speed.NONE, ap.getSpeed()); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 589 | } else if (ap.getSsidStr().equals(SSID_2)) { |
Sundeep Ghuman | 53200ed | 2017-06-21 16:54:36 -0700 | [diff] [blame] | 590 | assertEquals(AccessPoint.Speed.NONE, ap.getSpeed()); |
Sundeep Ghuman | e869d83 | 2017-01-25 16:23:43 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | @Test |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 596 | public void scoresShouldBeRequestedForNewScanResultOnly() throws InterruptedException { |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 597 | // Scores can be requested together or serially depending on how the scan results are |
| 598 | // processed. |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 599 | mRequestScoresLatch = new CountDownLatch(1); |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 600 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 601 | assertTrue(mRequestScoresLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 602 | mRequestedKeys.clear(); |
| 603 | |
| 604 | String ssid = "ssid3"; |
| 605 | String bssid = "00:00:00:00:00:00"; |
| 606 | ScanResult newResult = new ScanResult( |
| 607 | WifiSsid.createFromAsciiEncoded(ssid), |
| 608 | bssid, |
| 609 | 0, // hessid |
| 610 | 0, //anqpDomainId |
| 611 | null, // osuProviders |
| 612 | "", // capabilities |
| 613 | RSSI_1, |
| 614 | 0, // frequency |
| 615 | SystemClock.elapsedRealtime() * 1000); |
| 616 | when(mockWifiManager.getScanResults()) |
| 617 | .thenReturn(Arrays.asList(buildScanResult1(), buildScanResult2(), newResult)); |
| 618 | |
| 619 | mRequestScoresLatch = new CountDownLatch(1); |
| 620 | sendScanResultsAndProcess(tracker); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 621 | assertTrue(mRequestScoresLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 5519b7b | 2016-12-14 17:53:31 -0800 | [diff] [blame] | 622 | |
| 623 | assertEquals(1, mRequestedKeys.size()); |
| 624 | assertTrue(mRequestedKeys.contains(new NetworkKey(new WifiKey('"' + ssid + '"', bssid)))); |
| 625 | } |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 626 | |
| 627 | @Test |
| 628 | public void scoreCacheAndListenerShouldBeUnregisteredWhenStopTrackingIsCalled() throws Exception |
| 629 | { |
Sundeep Ghuman | 2b48990 | 2017-02-22 18:17:29 -0800 | [diff] [blame] | 630 | WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults(); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 631 | WifiNetworkScoreCache cache = mScoreCacheCaptor.getValue(); |
| 632 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 633 | tracker.onStop(); |
Sundeep Ghuman | ac7b436 | 2017-02-08 17:19:27 -0800 | [diff] [blame] | 634 | verify(mockNetworkScoreManager).unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, cache); |
| 635 | |
| 636 | // Verify listener is unregistered so updating a score does not throw an error by posting |
| 637 | // a message to the dead work handler |
| 638 | mWorkerThread.quit(); |
| 639 | updateScores(); |
| 640 | } |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 641 | |
| 642 | /** |
| 643 | * Verify that tracking a Passpoint AP on a device with Passpoint disabled doesn't cause |
| 644 | * any crash. |
| 645 | * |
| 646 | * @throws Exception |
| 647 | */ |
| 648 | @Test |
| 649 | public void trackPasspointApWithPasspointDisabled() throws Exception { |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 650 | // TODO(sghuman): Delete this test and replace with a passpoint test |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 651 | WifiTracker tracker = createMockedWifiTracker(); |
| 652 | |
| 653 | // Add a Passpoint AP to the scan results. |
| 654 | List<ScanResult> results = new ArrayList<>(); |
| 655 | ScanResult passpointAp = new ScanResult( |
| 656 | WifiSsid.createFromAsciiEncoded(SSID_1), |
| 657 | BSSID_1, |
| 658 | 0, // hessid |
| 659 | 0, //anqpDomainId |
| 660 | null, // osuProviders |
| 661 | "", // capabilities |
| 662 | RSSI_1, |
| 663 | 0, // frequency |
| 664 | SystemClock.elapsedRealtime() * 1000 /* microsecond timestamp */); |
| 665 | passpointAp.setFlag(ScanResult.FLAG_PASSPOINT_NETWORK); |
| 666 | results.add(passpointAp); |
| 667 | |
| 668 | // Update access point and verify UnsupportedOperationException is being caught for |
| 669 | // call to WifiManager#getMatchingWifiConfig. |
| 670 | when(mockWifiManager.getConfiguredNetworks()) |
| 671 | .thenReturn(new ArrayList<WifiConfiguration>()); |
| 672 | when(mockWifiManager.getScanResults()).thenReturn(results); |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 673 | tracker.forceUpdate(); |
Peter Qiu | f562838 | 2017-03-28 13:24:34 -0700 | [diff] [blame] | 674 | } |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 675 | |
| 676 | @Test |
| 677 | public void rssiChangeBroadcastShouldUpdateConnectedAp() throws Exception { |
| 678 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
| 679 | assertThat(tracker.getAccessPoints().get(0).isActive()).isTrue(); |
| 680 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 681 | int newRssi = CONNECTED_RSSI + 10; |
| 682 | WifiInfo info = new WifiInfo(CONNECTED_AP_1_INFO); |
| 683 | info.setRssi(newRssi); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 684 | |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 685 | CountDownLatch latch = new CountDownLatch(1); |
| 686 | |
| 687 | // Once the new info has been fetched, we need to wait for the access points to be copied |
Michael Plass | 992588c | 2017-12-04 11:39:13 -0800 | [diff] [blame] | 688 | mAccessPointsChangedLatch = new CountDownLatch(1); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 689 | doAnswer(invocation -> { |
| 690 | latch.countDown(); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 691 | return info; |
| 692 | }).when(mockWifiManager).getConnectionInfo(); |
| 693 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 694 | tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.RSSI_CHANGED_ACTION)); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 695 | assertTrue("New connection info never retrieved", |
| 696 | latch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 697 | assertTrue("onAccessPointsChanged never called", |
| 698 | mAccessPointsChangedLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 699 | |
Sundeep Ghuman | 8c79288 | 2017-04-04 17:23:29 -0700 | [diff] [blame] | 700 | assertThat(tracker.getAccessPoints().get(0).getRssi()).isEqualTo(newRssi); |
| 701 | } |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 702 | |
| 703 | @Test |
| 704 | public void forceUpdateShouldSynchronouslyFetchLatestInformation() throws Exception { |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 705 | Network mockNetwork = mock(Network.class); |
| 706 | when(mockWifiManager.getCurrentNetwork()).thenReturn(mockNetwork); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 707 | |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 708 | when(mockWifiManager.getConnectionInfo()).thenReturn(CONNECTED_AP_1_INFO); |
| 709 | |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 710 | NetworkInfo networkInfo = new NetworkInfo( |
| 711 | ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype"); |
| 712 | networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "connected", "test"); |
| 713 | when(mockConnectivityManager.getNetworkInfo(any(Network.class))).thenReturn(networkInfo); |
| 714 | |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 715 | WifiTracker tracker = createMockedWifiTracker(); |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 716 | tracker.forceUpdate(); |
| 717 | |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 718 | verify(mockWifiManager).getConnectionInfo(); |
Sundeep Ghuman | 0d492e8 | 2018-01-26 12:45:02 -0800 | [diff] [blame] | 719 | verify(mockWifiManager, times(1)).getConfiguredNetworks(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 720 | verify(mockConnectivityManager).getNetworkInfo(any(Network.class)); |
| 721 | |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 722 | verify(mockWifiListener, never()).onAccessPointsChanged(); // mStaleAccessPoints is true |
Sundeep Ghuman | 9c0b97e | 2017-04-13 16:05:46 -0700 | [diff] [blame] | 723 | assertThat(tracker.getAccessPoints().size()).isEqualTo(2); |
| 724 | assertThat(tracker.getAccessPoints().get(0).isActive()).isTrue(); |
| 725 | } |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 726 | |
| 727 | @Test |
| 728 | public void stopTrackingShouldRemoveWifiListenerCallbacks() throws Exception { |
| 729 | WifiTracker tracker = createMockedWifiTracker(); |
| 730 | startTracking(tracker); |
| 731 | |
Michael Plass | fd4a90d | 2017-12-04 14:01:51 -0800 | [diff] [blame] | 732 | CountDownLatch ready = new CountDownLatch(1); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 733 | CountDownLatch latch = new CountDownLatch(1); |
| 734 | CountDownLatch lock = new CountDownLatch(1); |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 735 | tracker.mWorkHandler.post(() -> { |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 736 | try { |
Michael Plass | fd4a90d | 2017-12-04 14:01:51 -0800 | [diff] [blame] | 737 | ready.countDown(); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 738 | lock.await(); |
| 739 | latch.countDown(); |
| 740 | } catch (InterruptedException e) { |
| 741 | fail("Interrupted Exception while awaiting lock release: " + e); |
| 742 | } |
| 743 | }); |
| 744 | |
| 745 | // Enqueue messages |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 746 | tracker.mWorkHandler.sendEmptyMessage(WifiTracker.WorkHandler.MSG_UPDATE_ACCESS_POINTS); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 747 | |
Michael Plass | fd4a90d | 2017-12-04 14:01:51 -0800 | [diff] [blame] | 748 | try { |
| 749 | ready.await(); // Make sure we have entered the first message handler |
| 750 | } catch (InterruptedException e) {} |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 751 | tracker.onStop(); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 752 | |
| 753 | verify(mockWifiListener, atMost(1)).onAccessPointsChanged(); |
| 754 | verify(mockWifiListener, atMost(1)).onConnectedChanged(); |
| 755 | verify(mockWifiListener, atMost(1)).onWifiStateChanged(anyInt()); |
| 756 | |
| 757 | lock.countDown(); |
Sundeep Ghuman | ce6bab8 | 2017-04-19 16:21:46 -0700 | [diff] [blame] | 758 | assertTrue("Latch timed out", latch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 759 | |
Sundeep Ghuman | 91f4ccb | 2018-01-26 18:58:56 -0800 | [diff] [blame] | 760 | assertThat(tracker.mWorkHandler.hasMessages( |
| 761 | WifiTracker.WorkHandler.MSG_UPDATE_ACCESS_POINTS)).isFalse(); |
| 762 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | 71f4a82 | 2017-04-18 19:51:46 -0700 | [diff] [blame] | 763 | |
| 764 | verifyNoMoreInteractions(mockWifiListener); |
| 765 | } |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 766 | |
| 767 | @Test |
| 768 | public void stopTrackingShouldSetStaleBitWhichPreventsCallbacksUntilNextScanResult() |
| 769 | throws Exception { |
| 770 | WifiTracker tracker = createMockedWifiTracker(); |
| 771 | startTracking(tracker); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 772 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 773 | |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 774 | tracker.onStop(); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 775 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 776 | |
| 777 | startTracking(tracker); |
| 778 | |
| 779 | tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); |
| 780 | tracker.mReceiver.onReceive( |
| 781 | mContext, new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION)); |
| 782 | tracker.mReceiver.onReceive( |
| 783 | mContext, new Intent(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION)); |
| 784 | |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 785 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | d4a79ac | 2017-06-07 18:11:39 -0700 | [diff] [blame] | 786 | |
| 787 | verify(mockWifiListener, never()).onAccessPointsChanged(); |
| 788 | |
| 789 | sendScanResultsAndProcess(tracker); // verifies onAccessPointsChanged is invoked |
| 790 | } |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 791 | |
| 792 | @Test |
Sundeep Ghuman | d2b84a8 | 2017-07-05 14:19:30 -0700 | [diff] [blame] | 793 | public void startTrackingShouldNotSendAnyCallbacksUntilScanResultsAreProcessed() |
| 794 | throws Exception { |
| 795 | WifiTracker tracker = createMockedWifiTracker(); |
| 796 | startTracking(tracker); |
| 797 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
| 798 | |
| 799 | tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); |
| 800 | tracker.mReceiver.onReceive( |
| 801 | mContext, new Intent(WifiManager.CONFIGURED_NETWORKS_CHANGED_ACTION)); |
| 802 | tracker.mReceiver.onReceive( |
| 803 | mContext, new Intent(WifiManager.LINK_CONFIGURATION_CHANGED_ACTION)); |
| 804 | |
| 805 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
| 806 | verify(mockWifiListener, never()).onAccessPointsChanged(); |
| 807 | |
| 808 | sendScanResultsAndProcess(tracker); // verifies onAccessPointsChanged is invoked |
| 809 | } |
| 810 | |
| 811 | @Test |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 812 | public void disablingWifiShouldClearExistingAccessPoints() throws Exception { |
| 813 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
| 814 | |
| 815 | when(mockWifiManager.isWifiEnabled()).thenReturn(false); |
| 816 | mAccessPointsChangedLatch = new CountDownLatch(1); |
| 817 | tracker.mReceiver.onReceive(mContext, new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION)); |
| 818 | |
Sundeep Ghuman | c5bbecb | 2018-01-29 18:56:36 -0800 | [diff] [blame] | 819 | assertTrue(mAccessPointsChangedLatch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS)); |
Sundeep Ghuman | e801309 | 2017-06-21 22:35:30 -0700 | [diff] [blame] | 820 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
| 821 | |
| 822 | assertThat(tracker.getAccessPoints()).isEmpty(); |
| 823 | } |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 824 | |
| 825 | @Test |
| 826 | public void onConnectedChangedCallback_shouldNotBeInvokedWhenNoStateChange() throws Exception { |
| 827 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
| 828 | verify(mockWifiListener, times(1)).onConnectedChanged(); |
| 829 | |
| 830 | NetworkInfo networkInfo = new NetworkInfo( |
| 831 | ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype"); |
| 832 | networkInfo.setDetailedState(NetworkInfo.DetailedState.CONNECTED, "connected", "test"); |
| 833 | |
| 834 | Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 835 | intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo); |
| 836 | tracker.mReceiver.onReceive(mContext, intent); |
| 837 | |
Sundeep Ghuman | db2eabb | 2017-07-28 14:51:05 -0700 | [diff] [blame] | 838 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 839 | verify(mockWifiListener, times(1)).onConnectedChanged(); |
| 840 | } |
| 841 | |
| 842 | @Test |
Sundeep Ghuman | db2eabb | 2017-07-28 14:51:05 -0700 | [diff] [blame] | 843 | public void onConnectedChangedCallback_shouldBeInvokedWhenStateChanges() throws Exception { |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 844 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
| 845 | verify(mockWifiListener, times(1)).onConnectedChanged(); |
| 846 | |
| 847 | NetworkInfo networkInfo = new NetworkInfo( |
| 848 | ConnectivityManager.TYPE_WIFI, 0, "Type Wifi", "subtype"); |
| 849 | networkInfo.setDetailedState( |
Sundeep Ghuman | db2eabb | 2017-07-28 14:51:05 -0700 | [diff] [blame] | 850 | NetworkInfo.DetailedState.DISCONNECTED, "disconnected", "test"); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 851 | |
| 852 | Intent intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 853 | intent.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo); |
| 854 | tracker.mReceiver.onReceive(mContext, intent); |
| 855 | |
Sundeep Ghuman | db2eabb | 2017-07-28 14:51:05 -0700 | [diff] [blame] | 856 | waitForHandlersToProcessCurrentlyEnqueuedMessages(tracker); |
| 857 | assertThat(tracker.isConnected()).isFalse(); |
Sundeep Ghuman | 4205874 | 2017-07-21 18:42:10 -0700 | [diff] [blame] | 858 | verify(mockWifiListener, times(2)).onConnectedChanged(); |
| 859 | } |
Sundeep Ghuman | db9b94c | 2017-09-06 11:46:21 -0700 | [diff] [blame] | 860 | |
| 861 | @Test |
| 862 | public void updateNetworkInfoWithNewConnectedNetwork_switchesNetworks() throws Exception { |
| 863 | WifiTracker tracker = createTrackerWithScanResultsAndAccessPoint1Connected(); |
| 864 | |
| 865 | switchToNetwork2(tracker); |
| 866 | |
| 867 | List<AccessPoint> aps = tracker.getAccessPoints(); |
| 868 | assertThat(aps.get(0).getSsidStr()).isEqualTo(SSID_2); |
| 869 | |
| 870 | assertThat(aps.get(0).isReachable()).isTrue(); |
| 871 | assertThat(aps.get(1).isReachable()).isTrue(); |
| 872 | } |
Peter Qiu | abea726 | 2017-05-31 10:18:17 -0700 | [diff] [blame] | 873 | } |