blob: a189d507a32a44956dbcdde32993286f6a03767b [file] [log] [blame]
Rebecca Silbersteince74fc22017-03-25 12:40:29 -07001/*
2 * Copyright (C) 2017 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
17package android.net.wifi;
18
Quang Luong85f23422020-01-21 17:09:02 -080019import static android.net.wifi.WifiConfiguration.METERED_OVERRIDE_METERED;
David Su308e25e52019-11-29 00:27:46 -080020import static android.net.wifi.WifiManager.ActionListener;
21import static android.net.wifi.WifiManager.BUSY;
22import static android.net.wifi.WifiManager.ERROR;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070023import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_GENERIC;
24import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE;
25import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL;
Rebecca Silberstein23d79bf2017-05-02 11:22:45 -070026import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_TETHERING_DISALLOWED;
Rebecca Silberstein6d7b1302017-04-30 14:05:00 -070027import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.REQUEST_REGISTERED;
David Su308e25e52019-11-29 00:27:46 -080028import static android.net.wifi.WifiManager.NOT_AUTHORIZED;
29import static android.net.wifi.WifiManager.OnWifiActivityEnergyInfoListener;
Mehdi Alizadehe59b9372017-12-22 17:00:22 -080030import static android.net.wifi.WifiManager.SAP_START_FAILURE_GENERAL;
David Su308e25e52019-11-29 00:27:46 -080031import static android.net.wifi.WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS;
32import static android.net.wifi.WifiManager.STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION;
33import static android.net.wifi.WifiManager.TxPacketCountListener;
Mehdi Alizadehe59b9372017-12-22 17:00:22 -080034import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
35import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
36import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
David Su308e25e52019-11-29 00:27:46 -080037import static android.net.wifi.WifiManager.WIFI_FEATURE_DPP;
38import static android.net.wifi.WifiManager.WIFI_FEATURE_OWE;
39import static android.net.wifi.WifiManager.WIFI_FEATURE_P2P;
40import static android.net.wifi.WifiManager.WIFI_FEATURE_PASSPOINT;
41import static android.net.wifi.WifiManager.WIFI_FEATURE_SCANNER;
42import static android.net.wifi.WifiManager.WIFI_FEATURE_WPA3_SAE;
43import static android.net.wifi.WifiManager.WIFI_FEATURE_WPA3_SUITE_B;
44import static android.net.wifi.WifiManager.WpsCallback;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070045
xshu9806aa52018-12-03 18:38:25 -080046import static org.junit.Assert.assertArrayEquals;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070047import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertFalse;
Rebecca Silberstein4871da62017-04-27 13:57:19 -070049import static org.junit.Assert.assertNotNull;
Rebecca Silberstein6a2784042018-01-19 17:45:26 -080050import static org.junit.Assert.assertNull;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070051import static org.junit.Assert.assertTrue;
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -080052import static org.junit.Assert.fail;
Roshan Piusaca52812019-04-05 11:35:35 -070053import static org.mockito.ArgumentMatchers.anyBoolean;
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -070054import static org.mockito.ArgumentMatchers.nullable;
Ecco Parkf035a042018-11-13 16:37:05 -080055import static org.mockito.Mockito.any;
56import static org.mockito.Mockito.anyInt;
57import static org.mockito.Mockito.anyList;
58import static org.mockito.Mockito.anyString;
59import static org.mockito.Mockito.doThrow;
60import static org.mockito.Mockito.eq;
61import static org.mockito.Mockito.inOrder;
62import static org.mockito.Mockito.mock;
63import static org.mockito.Mockito.never;
64import static org.mockito.Mockito.spy;
65import static org.mockito.Mockito.verify;
66import static org.mockito.Mockito.verifyNoMoreInteractions;
67import static org.mockito.Mockito.verifyZeroInteractions;
68import static org.mockito.Mockito.when;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070069
David Sue6efa3f2019-11-29 18:02:30 -080070import android.app.ActivityManager;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070071import android.content.Context;
72import android.content.pm.ApplicationInfo;
Roshan Piusaca52812019-04-05 11:35:35 -070073import android.net.DhcpInfo;
lesld26a9f72020-01-03 17:50:06 +080074import android.net.MacAddress;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070075import android.net.wifi.WifiManager.LocalOnlyHotspotCallback;
76import android.net.wifi.WifiManager.LocalOnlyHotspotObserver;
77import android.net.wifi.WifiManager.LocalOnlyHotspotReservation;
78import android.net.wifi.WifiManager.LocalOnlyHotspotSubscription;
Roshan Pius3c38ee42018-10-09 10:06:33 -070079import android.net.wifi.WifiManager.NetworkRequestMatchCallback;
80import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback;
Mingguang Xue77cabc2019-03-08 09:58:23 -080081import android.net.wifi.WifiManager.OnWifiUsabilityStatsListener;
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -080082import android.net.wifi.WifiManager.ScanResultsCallback;
Mehdi Alizadehe59b9372017-12-22 17:00:22 -080083import android.net.wifi.WifiManager.SoftApCallback;
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -070084import android.net.wifi.WifiManager.SuggestionConnectionStatusListener;
Roshan Pius80fb1372018-07-16 15:43:03 -070085import android.net.wifi.WifiManager.TrafficStateCallback;
Mingguang Xuec6f8ce2019-12-20 16:11:07 -080086import android.net.wifi.WifiManager.WifiConnectedNetworkScorer;
Roshan Pius641a6832019-09-09 10:53:20 -070087import android.os.Binder;
Roshan Piusaca52812019-04-05 11:35:35 -070088import android.os.Build;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070089import android.os.Handler;
James Mattis80c86d52019-10-31 14:11:40 -070090import android.os.HandlerExecutor;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070091import android.os.IBinder;
Roshan Pius641a6832019-09-09 10:53:20 -070092import android.os.RemoteException;
David Su6f0e4162019-12-02 21:53:33 -080093import android.os.connectivity.WifiActivityEnergyInfo;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070094import android.os.test.TestLooper;
Hai Shalome722cbd2019-12-04 15:50:07 -080095import android.util.SparseArray;
KOUSHIK PANUGANTIc9f79d82018-12-18 18:43:13 -080096
97import androidx.test.filters.SmallTest;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -070098
99import org.junit.Before;
100import org.junit.Test;
101import org.mockito.ArgumentCaptor;
Roshan Pius80fb1372018-07-16 15:43:03 -0700102import org.mockito.InOrder;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700103import org.mockito.Mock;
104import org.mockito.MockitoAnnotations;
105
Roshan Pius3c38ee42018-10-09 10:06:33 -0700106import java.util.ArrayList;
Ecco Park05df45d2018-12-18 16:13:41 -0800107import java.util.HashMap;
Ecco Parkf035a042018-11-13 16:37:05 -0800108import java.util.List;
Ecco Park05df45d2018-12-18 16:13:41 -0800109import java.util.Map;
lesl115873a2020-01-15 14:51:26 +0800110import java.util.Objects;
Mingguang Xu86833142019-01-09 13:54:29 -0800111import java.util.concurrent.Executor;
Roshan Pius3c38ee42018-10-09 10:06:33 -0700112
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700113/**
114 * Unit tests for {@link android.net.wifi.WifiManager}.
115 */
116@SmallTest
117public class WifiManagerTest {
118
119 private static final int ERROR_NOT_SET = -1;
120 private static final int ERROR_TEST_REASON = 5;
Roshan Pius3c38ee42018-10-09 10:06:33 -0700121 private static final int TEST_UID = 14553;
Roshan Piusaca52812019-04-05 11:35:35 -0700122 private static final int TEST_NETWORK_ID = 143;
Rebecca Silberstein967dfb42017-05-18 01:27:57 -0700123 private static final String TEST_PACKAGE_NAME = "TestPackage";
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700124 private static final String TEST_FEATURE_ID = "TestFeature";
Ningyuan Wang557875d2017-09-20 14:00:17 -0700125 private static final String TEST_COUNTRY_CODE = "US";
xshu9806aa52018-12-03 18:38:25 -0800126 private static final String[] TEST_MAC_ADDRESSES = {"da:a1:19:0:0:0"};
lesl3fd4b642019-10-23 10:47:34 +0800127 private static final int TEST_AP_FREQUENCY = 2412;
128 private static final int TEST_AP_BANDWIDTH = SoftApInfo.CHANNEL_WIDTH_20MHZ;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700129
130 @Mock Context mContext;
James Mattisaefe78c2019-10-04 17:46:12 -0700131 @Mock android.net.wifi.IWifiManager mWifiService;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700132 @Mock ApplicationInfo mApplicationInfo;
133 @Mock WifiConfiguration mApConfig;
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800134 @Mock SoftApCallback mSoftApCallback;
Roshan Pius80fb1372018-07-16 15:43:03 -0700135 @Mock TrafficStateCallback mTrafficStateCallback;
Roshan Pius3c38ee42018-10-09 10:06:33 -0700136 @Mock NetworkRequestMatchCallback mNetworkRequestMatchCallback;
Mingguang Xue77cabc2019-03-08 09:58:23 -0800137 @Mock OnWifiUsabilityStatsListener mOnWifiUsabilityStatsListener;
David Su308e25e52019-11-29 00:27:46 -0800138 @Mock OnWifiActivityEnergyInfoListener mOnWifiActivityEnergyInfoListener;
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -0700139 @Mock SuggestionConnectionStatusListener mListener;
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -0800140 @Mock Runnable mRunnable;
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -0700141 @Mock Executor mExecutor;
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -0800142 @Mock Executor mAnotherExecutor;
David Sue6efa3f2019-11-29 18:02:30 -0800143 @Mock ActivityManager mActivityManager;
Mingguang Xuec6f8ce2019-12-20 16:11:07 -0800144 @Mock WifiConnectedNetworkScorer mWifiConnectedNetworkScorer;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700145
146 private Handler mHandler;
147 private TestLooper mLooper;
148 private WifiManager mWifiManager;
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -0700149 private WifiNetworkSuggestion mWifiNetworkSuggestion;
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -0800150 private ScanResultsCallback mScanResultsCallback;
David Su308e25e52019-11-29 00:27:46 -0800151 private WifiActivityEnergyInfo mWifiActivityEnergyInfo;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700152
lesl115873a2020-01-15 14:51:26 +0800153 /**
154 * Util function to check public field which used for softap in WifiConfiguration
155 * same as the value in SoftApConfiguration.
156 *
157 */
158 private boolean compareWifiAndSoftApConfiguration(
159 SoftApConfiguration softApConfig, WifiConfiguration wifiConfig) {
160 if (!Objects.equals(wifiConfig.SSID, softApConfig.getSsid())) {
161 return false;
162 }
163 if (!Objects.equals(wifiConfig.BSSID, softApConfig.getBssid())) {
164 return false;
165 }
166 if (!Objects.equals(wifiConfig.preSharedKey, softApConfig.getPassphrase())) {
167 return false;
168 }
169
170 if (wifiConfig.hiddenSSID != softApConfig.isHiddenSsid()) {
171 return false;
172 }
173 switch (softApConfig.getSecurityType()) {
174 case SoftApConfiguration.SECURITY_TYPE_OPEN:
175 if (wifiConfig.getAuthType() != WifiConfiguration.KeyMgmt.NONE) {
176 return false;
177 }
178 break;
179 case SoftApConfiguration.SECURITY_TYPE_WPA2_PSK:
180 if (wifiConfig.getAuthType() != WifiConfiguration.KeyMgmt.WPA2_PSK) {
181 return false;
182 }
183 break;
184 default:
185 return false;
186 }
187 return true;
188 }
189
190 private SoftApConfiguration generatorTestSoftApConfig() {
191 return new SoftApConfiguration.Builder()
192 .setSsid("TestSSID")
193 .setPassphrase("TestPassphrase", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)
194 .build();
195 }
196
James Mattisaefe78c2019-10-04 17:46:12 -0700197 @Before
198 public void setUp() throws Exception {
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700199 MockitoAnnotations.initMocks(this);
200 mLooper = new TestLooper();
201 mHandler = spy(new Handler(mLooper.getLooper()));
Roshan Piusaca52812019-04-05 11:35:35 -0700202 mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700203 when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
Rebecca Silberstein967dfb42017-05-18 01:27:57 -0700204 when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700205 mWifiManager = new WifiManager(mContext, mWifiService, mLooper.getLooper());
Roshan Pius045d3802018-07-18 13:18:55 -0700206 verify(mWifiService).getVerboseLoggingLevel();
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -0700207 mWifiNetworkSuggestion = new WifiNetworkSuggestion();
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -0800208 mScanResultsCallback = new ScanResultsCallback() {
209 @Override
210 public void onScanResultsAvailable() {
211 mRunnable.run();
212 }
213 };
Roshan Pius26ef6822019-12-02 12:48:29 -0800214 mWifiActivityEnergyInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700215 }
216
217 /**
218 * Check the call to startSoftAp calls WifiService to startSoftAp with the provided
219 * WifiConfiguration. Verify that the return value is propagated to the caller.
220 */
221 @Test
222 public void testStartSoftApCallsServiceWithWifiConfig() throws Exception {
223 when(mWifiService.startSoftAp(eq(mApConfig))).thenReturn(true);
224 assertTrue(mWifiManager.startSoftAp(mApConfig));
225
226 when(mWifiService.startSoftAp(eq(mApConfig))).thenReturn(false);
227 assertFalse(mWifiManager.startSoftAp(mApConfig));
228 }
229
230 /**
231 * Check the call to startSoftAp calls WifiService to startSoftAp with a null config. Verify
232 * that the return value is propagated to the caller.
233 */
234 @Test
235 public void testStartSoftApCallsServiceWithNullConfig() throws Exception {
236 when(mWifiService.startSoftAp(eq(null))).thenReturn(true);
237 assertTrue(mWifiManager.startSoftAp(null));
238
239 when(mWifiService.startSoftAp(eq(null))).thenReturn(false);
240 assertFalse(mWifiManager.startSoftAp(null));
241 }
242
243 /**
244 * Check the call to stopSoftAp calls WifiService to stopSoftAp.
245 */
246 @Test
247 public void testStopSoftApCallsService() throws Exception {
248 when(mWifiService.stopSoftAp()).thenReturn(true);
249 assertTrue(mWifiManager.stopSoftAp());
250
251 when(mWifiService.stopSoftAp()).thenReturn(false);
252 assertFalse(mWifiManager.stopSoftAp());
253 }
254
255 /**
lesl692d3752019-11-27 16:17:47 +0800256 * Check the call to startSoftAp calls WifiService to startSoftAp with the provided
257 * WifiConfiguration. Verify that the return value is propagated to the caller.
258 */
259 @Test
260 public void testStartTetheredHotspotCallsServiceWithSoftApConfig() throws Exception {
lesl115873a2020-01-15 14:51:26 +0800261 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
262 when(mWifiService.startTetheredHotspot(eq(softApConfig))).thenReturn(true);
263 assertTrue(mWifiManager.startTetheredHotspot(softApConfig));
lesl692d3752019-11-27 16:17:47 +0800264
lesl115873a2020-01-15 14:51:26 +0800265 when(mWifiService.startTetheredHotspot(eq(softApConfig))).thenReturn(false);
266 assertFalse(mWifiManager.startTetheredHotspot(softApConfig));
lesl692d3752019-11-27 16:17:47 +0800267 }
268
269 /**
270 * Check the call to startSoftAp calls WifiService to startSoftAp with a null config. Verify
271 * that the return value is propagated to the caller.
272 */
273 @Test
274 public void testStartTetheredHotspotCallsServiceWithNullConfig() throws Exception {
275 when(mWifiService.startTetheredHotspot(eq(null))).thenReturn(true);
276 assertTrue(mWifiManager.startTetheredHotspot(null));
277
278 when(mWifiService.startTetheredHotspot(eq(null))).thenReturn(false);
279 assertFalse(mWifiManager.startTetheredHotspot(null));
280 }
281
282 /**
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700283 * Test creation of a LocalOnlyHotspotReservation and verify that close properly calls
284 * WifiService.stopLocalOnlyHotspot.
285 */
286 @Test
287 public void testCreationAndCloseOfLocalOnlyHotspotReservation() throws Exception {
lesl115873a2020-01-15 14:51:26 +0800288 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700289 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700290 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
291 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700292 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
293
lesl115873a2020-01-15 14:51:26 +0800294 callback.onStarted(mWifiManager.new LocalOnlyHotspotReservation(softApConfig));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700295
lesl115873a2020-01-15 14:51:26 +0800296 assertEquals(softApConfig, callback.mRes.getSoftApConfiguration());
297 WifiConfiguration wifiConfig = callback.mRes.getWifiConfiguration();
298 assertTrue(compareWifiAndSoftApConfiguration(softApConfig, wifiConfig));
299
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700300 callback.mRes.close();
Peter Visontayca835c12017-11-21 11:46:59 +0000301 verify(mWifiService).stopLocalOnlyHotspot();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700302 }
303
304 /**
305 * Verify stopLOHS is called when try-with-resources is used properly.
306 */
307 @Test
308 public void testLocalOnlyHotspotReservationCallsStopProperlyInTryWithResources()
309 throws Exception {
lesl115873a2020-01-15 14:51:26 +0800310 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700311 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700312 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
313 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700314 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
315
lesl115873a2020-01-15 14:51:26 +0800316 callback.onStarted(mWifiManager.new LocalOnlyHotspotReservation(softApConfig));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700317
318 try (WifiManager.LocalOnlyHotspotReservation res = callback.mRes) {
lesl115873a2020-01-15 14:51:26 +0800319 assertEquals(softApConfig, res.getSoftApConfiguration());
320 WifiConfiguration wifiConfig = callback.mRes.getWifiConfiguration();
321 assertTrue(compareWifiAndSoftApConfiguration(softApConfig, wifiConfig));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700322 }
323
Peter Visontayca835c12017-11-21 11:46:59 +0000324 verify(mWifiService).stopLocalOnlyHotspot();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700325 }
326
327 /**
328 * Test creation of a LocalOnlyHotspotSubscription.
329 * TODO: when registrations are tracked, verify removal on close.
330 */
331 @Test
332 public void testCreationOfLocalOnlyHotspotSubscription() throws Exception {
333 try (WifiManager.LocalOnlyHotspotSubscription sub =
James Mattisaefe78c2019-10-04 17:46:12 -0700334 mWifiManager.new LocalOnlyHotspotSubscription()) {
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700335 sub.close();
336 }
337 }
338
339 public class TestLocalOnlyHotspotCallback extends LocalOnlyHotspotCallback {
340 public boolean mOnStartedCalled = false;
341 public boolean mOnStoppedCalled = false;
342 public int mFailureReason = -1;
343 public LocalOnlyHotspotReservation mRes = null;
344 public long mCallingThreadId = -1;
345
346 @Override
347 public void onStarted(LocalOnlyHotspotReservation r) {
348 mRes = r;
349 mOnStartedCalled = true;
350 mCallingThreadId = Thread.currentThread().getId();
351 }
352
353 @Override
354 public void onStopped() {
355 mOnStoppedCalled = true;
356 mCallingThreadId = Thread.currentThread().getId();
357 }
358
359 @Override
360 public void onFailed(int reason) {
361 mFailureReason = reason;
362 mCallingThreadId = Thread.currentThread().getId();
363 }
364 }
365
366 /**
367 * Verify callback is properly plumbed when called.
368 */
369 @Test
370 public void testLocalOnlyHotspotCallback() {
lesl115873a2020-01-15 14:51:26 +0800371 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700372 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
373 assertFalse(callback.mOnStartedCalled);
374 assertFalse(callback.mOnStoppedCalled);
375 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
376 assertEquals(null, callback.mRes);
377
378 // test onStarted
379 WifiManager.LocalOnlyHotspotReservation res =
lesl115873a2020-01-15 14:51:26 +0800380 mWifiManager.new LocalOnlyHotspotReservation(softApConfig);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700381 callback.onStarted(res);
382 assertEquals(res, callback.mRes);
383 assertTrue(callback.mOnStartedCalled);
384 assertFalse(callback.mOnStoppedCalled);
385 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
386
387 // test onStopped
388 callback.onStopped();
389 assertEquals(res, callback.mRes);
390 assertTrue(callback.mOnStartedCalled);
391 assertTrue(callback.mOnStoppedCalled);
392 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
393
394 // test onFailed
395 callback.onFailed(ERROR_TEST_REASON);
396 assertEquals(res, callback.mRes);
397 assertTrue(callback.mOnStartedCalled);
398 assertTrue(callback.mOnStoppedCalled);
399 assertEquals(ERROR_TEST_REASON, callback.mFailureReason);
400 }
401
402 public class TestLocalOnlyHotspotObserver extends LocalOnlyHotspotObserver {
403 public boolean mOnRegistered = false;
404 public boolean mOnStartedCalled = false;
405 public boolean mOnStoppedCalled = false;
lesl115873a2020-01-15 14:51:26 +0800406 public SoftApConfiguration mConfig = null;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700407 public LocalOnlyHotspotSubscription mSub = null;
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700408 public long mCallingThreadId = -1;
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700409
410 @Override
411 public void onRegistered(LocalOnlyHotspotSubscription sub) {
412 mOnRegistered = true;
413 mSub = sub;
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700414 mCallingThreadId = Thread.currentThread().getId();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700415 }
416
417 @Override
lesl115873a2020-01-15 14:51:26 +0800418 public void onStarted(SoftApConfiguration config) {
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700419 mOnStartedCalled = true;
420 mConfig = config;
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700421 mCallingThreadId = Thread.currentThread().getId();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700422 }
423
424 @Override
425 public void onStopped() {
426 mOnStoppedCalled = true;
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700427 mCallingThreadId = Thread.currentThread().getId();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700428 }
429 }
430
431 /**
432 * Verify observer is properly plumbed when called.
433 */
434 @Test
435 public void testLocalOnlyHotspotObserver() {
436 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
lesl115873a2020-01-15 14:51:26 +0800437 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700438 assertFalse(observer.mOnRegistered);
439 assertFalse(observer.mOnStartedCalled);
440 assertFalse(observer.mOnStoppedCalled);
441 assertEquals(null, observer.mConfig);
442 assertEquals(null, observer.mSub);
443
444 WifiManager.LocalOnlyHotspotSubscription sub =
445 mWifiManager.new LocalOnlyHotspotSubscription();
446 observer.onRegistered(sub);
447 assertTrue(observer.mOnRegistered);
448 assertFalse(observer.mOnStartedCalled);
449 assertFalse(observer.mOnStoppedCalled);
450 assertEquals(null, observer.mConfig);
451 assertEquals(sub, observer.mSub);
452
lesl115873a2020-01-15 14:51:26 +0800453 observer.onStarted(softApConfig);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700454 assertTrue(observer.mOnRegistered);
455 assertTrue(observer.mOnStartedCalled);
456 assertFalse(observer.mOnStoppedCalled);
lesl115873a2020-01-15 14:51:26 +0800457 assertEquals(softApConfig, observer.mConfig);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700458 assertEquals(sub, observer.mSub);
459
460 observer.onStopped();
461 assertTrue(observer.mOnRegistered);
462 assertTrue(observer.mOnStartedCalled);
463 assertTrue(observer.mOnStoppedCalled);
lesl115873a2020-01-15 14:51:26 +0800464 assertEquals(softApConfig, observer.mConfig);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700465 assertEquals(sub, observer.mSub);
466 }
467
468 /**
469 * Verify call to startLocalOnlyHotspot goes to WifiServiceImpl.
470 */
471 @Test
472 public void testStartLocalOnlyHotspot() throws Exception {
473 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
474 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
475
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700476 verify(mWifiService).startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class),
477 anyString(), nullable(String.class), eq(null));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700478 }
479
480 /**
481 * Verify a SecurityException is thrown for callers without proper permissions for
482 * startLocalOnlyHotspot.
483 */
484 @Test(expected = SecurityException.class)
485 public void testStartLocalOnlyHotspotThrowsSecurityException() throws Exception {
486 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700487 doThrow(new SecurityException()).when(mWifiService).startLocalOnlyHotspot(
488 any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class),
489 eq(null));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700490 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
491 }
492
493 /**
494 * Verify an IllegalStateException is thrown for callers that already have a pending request for
495 * startLocalOnlyHotspot.
496 */
497 @Test(expected = IllegalStateException.class)
498 public void testStartLocalOnlyHotspotThrowsIllegalStateException() throws Exception {
499 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700500 doThrow(new IllegalStateException()).when(mWifiService).startLocalOnlyHotspot(
501 any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class),
502 eq(null));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700503 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
504 }
505
506 /**
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700507 * Verify that the handler provided by the caller is used for the callbacks.
508 */
509 @Test
510 public void testCorrectLooperIsUsedForHandler() throws Exception {
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700511 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700512 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
513 nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700514 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
515 mLooper.dispatchAll();
516 assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason);
Rebecca Silberstein695cea52017-05-02 13:21:00 -0700517 verify(mContext, never()).getMainLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +0200518 verify(mContext, never()).getMainExecutor();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700519 }
520
521 /**
522 * Verify that the main looper's thread is used if a handler is not provided by the reqiestomg
523 * application.
524 */
525 @Test
526 public void testMainLooperIsUsedWhenHandlerNotProvided() throws Exception {
527 // record thread from looper.getThread and check ids.
528 TestLooper altLooper = new TestLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +0200529 when(mContext.getMainExecutor()).thenReturn(altLooper.getNewExecutor());
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700530 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700531 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
532 nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700533 mWifiManager.startLocalOnlyHotspot(callback, null);
534 altLooper.dispatchAll();
535 assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason);
536 assertEquals(altLooper.getLooper().getThread().getId(), callback.mCallingThreadId);
Patrik Fimml090ea1362019-09-18 14:37:27 +0200537 verify(mContext).getMainExecutor();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700538 }
539
540 /**
541 * Verify the LOHS onStarted callback is triggered when WifiManager receives a HOTSPOT_STARTED
542 * message from WifiServiceImpl.
543 */
544 @Test
545 public void testOnStartedIsCalledWithReservation() throws Exception {
lesl115873a2020-01-15 14:51:26 +0800546 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700547 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
548 TestLooper callbackLooper = new TestLooper();
549 Handler callbackHandler = new Handler(callbackLooper.getLooper());
Patrik Fimml090ea1362019-09-18 14:37:27 +0200550 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
551 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700552 when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(),
553 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700554 mWifiManager.startLocalOnlyHotspot(callback, callbackHandler);
555 callbackLooper.dispatchAll();
556 mLooper.dispatchAll();
557 assertFalse(callback.mOnStartedCalled);
558 assertEquals(null, callback.mRes);
559 // now trigger the callback
lesl115873a2020-01-15 14:51:26 +0800560 internalCallback.getValue().onHotspotStarted(softApConfig);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700561 mLooper.dispatchAll();
562 callbackLooper.dispatchAll();
563 assertTrue(callback.mOnStartedCalled);
lesl115873a2020-01-15 14:51:26 +0800564 assertEquals(softApConfig, callback.mRes.getSoftApConfiguration());
565 WifiConfiguration wifiConfig = callback.mRes.getWifiConfiguration();
566 assertTrue(compareWifiAndSoftApConfiguration(softApConfig, wifiConfig));
567 }
568
569 /**
570 * Verify the LOHS onStarted callback is triggered when WifiManager receives a HOTSPOT_STARTED
571 * message from WifiServiceImpl when softap enabled with SAE security type.
572 */
573 @Test
574 public void testOnStartedIsCalledWithReservationAndSaeSoftApConfig() throws Exception {
575 SoftApConfiguration softApConfig = new SoftApConfiguration.Builder()
576 .setSsid("TestSSID")
577 .setPassphrase("TestPassphrase", SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)
578 .build();
579 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
580 TestLooper callbackLooper = new TestLooper();
581 Handler callbackHandler = new Handler(callbackLooper.getLooper());
582 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
583 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
584 when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(),
585 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
586 mWifiManager.startLocalOnlyHotspot(callback, callbackHandler);
587 callbackLooper.dispatchAll();
588 mLooper.dispatchAll();
589 assertFalse(callback.mOnStartedCalled);
590 assertEquals(null, callback.mRes);
591 // now trigger the callback
592 internalCallback.getValue().onHotspotStarted(softApConfig);
593 mLooper.dispatchAll();
594 callbackLooper.dispatchAll();
595 assertTrue(callback.mOnStartedCalled);
596 assertEquals(softApConfig, callback.mRes.getSoftApConfiguration());
597 assertEquals(null, callback.mRes.getWifiConfiguration());
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700598 }
599
600 /**
601 * Verify onFailed is called if WifiServiceImpl sends a HOTSPOT_STARTED message with a null
602 * config.
603 */
604 @Test
605 public void testOnStartedIsCalledWithNullConfig() throws Exception {
606 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
607 TestLooper callbackLooper = new TestLooper();
608 Handler callbackHandler = new Handler(callbackLooper.getLooper());
Patrik Fimml090ea1362019-09-18 14:37:27 +0200609 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
610 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700611 when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(),
612 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700613 mWifiManager.startLocalOnlyHotspot(callback, callbackHandler);
614 callbackLooper.dispatchAll();
615 mLooper.dispatchAll();
616 assertFalse(callback.mOnStartedCalled);
617 assertEquals(null, callback.mRes);
618 // now trigger the callback
Patrik Fimml090ea1362019-09-18 14:37:27 +0200619 internalCallback.getValue().onHotspotStarted(null);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700620 mLooper.dispatchAll();
621 callbackLooper.dispatchAll();
622 assertFalse(callback.mOnStartedCalled);
623 assertEquals(ERROR_GENERIC, callback.mFailureReason);
624 }
625
626 /**
627 * Verify onStopped is called if WifiServiceImpl sends a HOTSPOT_STOPPED message.
628 */
629 @Test
630 public void testOnStoppedIsCalled() throws Exception {
631 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
632 TestLooper callbackLooper = new TestLooper();
633 Handler callbackHandler = new Handler(callbackLooper.getLooper());
Patrik Fimml090ea1362019-09-18 14:37:27 +0200634 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
635 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700636 when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(),
637 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700638 mWifiManager.startLocalOnlyHotspot(callback, callbackHandler);
639 callbackLooper.dispatchAll();
640 mLooper.dispatchAll();
641 assertFalse(callback.mOnStoppedCalled);
642 // now trigger the callback
Patrik Fimml090ea1362019-09-18 14:37:27 +0200643 internalCallback.getValue().onHotspotStopped();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700644 mLooper.dispatchAll();
645 callbackLooper.dispatchAll();
646 assertTrue(callback.mOnStoppedCalled);
647 }
648
649 /**
650 * Verify onFailed is called if WifiServiceImpl sends a HOTSPOT_FAILED message.
651 */
652 @Test
653 public void testOnFailedIsCalled() throws Exception {
654 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
655 TestLooper callbackLooper = new TestLooper();
656 Handler callbackHandler = new Handler(callbackLooper.getLooper());
Patrik Fimml090ea1362019-09-18 14:37:27 +0200657 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
658 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700659 when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(),
660 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700661 mWifiManager.startLocalOnlyHotspot(callback, callbackHandler);
662 callbackLooper.dispatchAll();
663 mLooper.dispatchAll();
664 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
665 // now trigger the callback
Patrik Fimml090ea1362019-09-18 14:37:27 +0200666 internalCallback.getValue().onHotspotFailed(ERROR_NO_CHANNEL);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700667 mLooper.dispatchAll();
668 callbackLooper.dispatchAll();
669 assertEquals(ERROR_NO_CHANNEL, callback.mFailureReason);
670 }
671
672 /**
Rebecca Silberstein23d79bf2017-05-02 11:22:45 -0700673 * Verify callback triggered from startLocalOnlyHotspot with an incompatible mode failure.
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700674 */
675 @Test
Rebecca Silberstein23d79bf2017-05-02 11:22:45 -0700676 public void testLocalOnlyHotspotCallbackFullOnIncompatibleMode() throws Exception {
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700677 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700678 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
679 nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700680 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
681 mLooper.dispatchAll();
682 assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason);
683 assertFalse(callback.mOnStartedCalled);
684 assertFalse(callback.mOnStoppedCalled);
685 assertEquals(null, callback.mRes);
686 }
687
688 /**
Rebecca Silberstein23d79bf2017-05-02 11:22:45 -0700689 * Verify callback triggered from startLocalOnlyHotspot with a tethering disallowed failure.
690 */
691 @Test
692 public void testLocalOnlyHotspotCallbackFullOnTetheringDisallowed() throws Exception {
693 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700694 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
695 nullable(String.class), eq(null))).thenReturn(ERROR_TETHERING_DISALLOWED);
Rebecca Silberstein23d79bf2017-05-02 11:22:45 -0700696 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
697 mLooper.dispatchAll();
698 assertEquals(ERROR_TETHERING_DISALLOWED, callback.mFailureReason);
699 assertFalse(callback.mOnStartedCalled);
700 assertFalse(callback.mOnStoppedCalled);
701 assertEquals(null, callback.mRes);
702 }
703
704 /**
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700705 * Verify a SecurityException resulting from an application without necessary permissions will
706 * bubble up through the call to start LocalOnlyHotspot and will not trigger other callbacks.
707 */
708 @Test(expected = SecurityException.class)
709 public void testLocalOnlyHotspotCallbackFullOnSecurityException() throws Exception {
710 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700711 doThrow(new SecurityException()).when(mWifiService).startLocalOnlyHotspot(
712 any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class),
713 eq(null));
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700714 try {
715 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
716 } catch (SecurityException e) {
717 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
718 assertFalse(callback.mOnStartedCalled);
719 assertFalse(callback.mOnStoppedCalled);
720 assertEquals(null, callback.mRes);
721 throw e;
722 }
723
724 }
725
726 /**
727 * Verify the handler passed to startLocalOnlyHotspot is correctly used for callbacks when
728 * SoftApMode fails due to a underlying error.
729 */
730 @Test
731 public void testLocalOnlyHotspotCallbackFullOnNoChannelError() throws Exception {
732 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700733 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
734 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700735 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
736 mLooper.dispatchAll();
737 //assertEquals(ERROR_NO_CHANNEL, callback.mFailureReason);
738 assertFalse(callback.mOnStartedCalled);
739 assertFalse(callback.mOnStoppedCalled);
740 assertEquals(null, callback.mRes);
741 }
742
743 /**
744 * Verify that the call to cancel a LOHS request does call stopLOHS.
745 */
746 @Test
747 public void testCancelLocalOnlyHotspotRequestCallsStopOnWifiService() throws Exception {
748 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700749 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
750 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700751 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
752 mWifiManager.cancelLocalOnlyHotspotRequest();
Peter Visontayca835c12017-11-21 11:46:59 +0000753 verify(mWifiService).stopLocalOnlyHotspot();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700754 }
755
756 /**
757 * Verify that we do not crash if cancelLocalOnlyHotspotRequest is called without an existing
758 * callback stored.
759 */
760 @Test
761 public void testCancelLocalOnlyHotspotReturnsWithoutExistingRequest() {
762 mWifiManager.cancelLocalOnlyHotspotRequest();
763 }
764
765 /**
766 * Verify that the callback is not triggered if the LOHS request was already cancelled.
767 */
768 @Test
769 public void testCallbackAfterLocalOnlyHotspotWasCancelled() throws Exception {
770 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700771 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
772 nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700773 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
774 mWifiManager.cancelLocalOnlyHotspotRequest();
Peter Visontayca835c12017-11-21 11:46:59 +0000775 verify(mWifiService).stopLocalOnlyHotspot();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700776 mLooper.dispatchAll();
777 assertEquals(ERROR_NOT_SET, callback.mFailureReason);
778 assertFalse(callback.mOnStartedCalled);
779 assertFalse(callback.mOnStoppedCalled);
780 assertEquals(null, callback.mRes);
781 }
782
783 /**
784 * Verify that calling cancel LOHS request does not crash if an error callback was already
785 * handled.
786 */
787 @Test
788 public void testCancelAfterLocalOnlyHotspotCallbackTriggered() throws Exception {
789 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700790 when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(),
791 nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE);
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700792 mWifiManager.startLocalOnlyHotspot(callback, mHandler);
793 mLooper.dispatchAll();
794 assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason);
795 assertFalse(callback.mOnStartedCalled);
796 assertFalse(callback.mOnStoppedCalled);
797 assertEquals(null, callback.mRes);
798 mWifiManager.cancelLocalOnlyHotspotRequest();
Peter Visontayca835c12017-11-21 11:46:59 +0000799 verify(mWifiService, never()).stopLocalOnlyHotspot();
Rebecca Silbersteince74fc22017-03-25 12:40:29 -0700800 }
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700801
Patrik Fimmlc7c962f2019-09-10 15:35:09 +0200802 @Test
803 public void testStartLocalOnlyHotspotForwardsCustomConfig() throws Exception {
804 SoftApConfiguration customConfig = new SoftApConfiguration.Builder()
805 .setSsid("customSsid")
806 .build();
807 TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback();
808 mWifiManager.startLocalOnlyHotspot(customConfig, mExecutor, callback);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -0700809 verify(mWifiService).startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class),
810 anyString(), nullable(String.class), eq(customConfig));
Patrik Fimmlc7c962f2019-09-10 15:35:09 +0200811 }
812
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700813 /**
814 * Verify the watchLocalOnlyHotspot call goes to WifiServiceImpl.
815 */
Andreas Gampe35dbf352018-01-26 20:41:17 -0800816 @Test
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700817 public void testWatchLocalOnlyHotspot() throws Exception {
818 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
819
820 mWifiManager.watchLocalOnlyHotspot(observer, mHandler);
Patrik Fimml090ea1362019-09-18 14:37:27 +0200821 verify(mWifiService).startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class));
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700822 }
823
824 /**
825 * Verify a SecurityException is thrown for callers without proper permissions for
826 * startWatchLocalOnlyHotspot.
827 */
828 @Test(expected = SecurityException.class)
829 public void testStartWatchLocalOnlyHotspotThrowsSecurityException() throws Exception {
830 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
831 doThrow(new SecurityException()).when(mWifiService)
Patrik Fimml090ea1362019-09-18 14:37:27 +0200832 .startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class));
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700833 mWifiManager.watchLocalOnlyHotspot(observer, mHandler);
834 }
835
836 /**
837 * Verify an IllegalStateException is thrown for callers that already have a pending request for
838 * watchLocalOnlyHotspot.
839 */
840 @Test(expected = IllegalStateException.class)
841 public void testStartWatchLocalOnlyHotspotThrowsIllegalStateException() throws Exception {
842 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
843 doThrow(new IllegalStateException()).when(mWifiService)
Patrik Fimml090ea1362019-09-18 14:37:27 +0200844 .startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class));
Rebecca Silberstein4871da62017-04-27 13:57:19 -0700845 mWifiManager.watchLocalOnlyHotspot(observer, mHandler);
846 }
847
848 /**
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800849 * Verify an IllegalArgumentException is thrown if callback is not provided.
850 */
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800851 @Test
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800852 public void registerSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() {
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800853 try {
James Mattiscb157e82019-11-12 17:43:48 -0800854 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), null);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800855 fail("expected IllegalArgumentException");
856 } catch (IllegalArgumentException expected) {
857 }
858 }
859
860 /**
David Suf7ef9552019-11-05 13:44:12 -0800861 * Verify an IllegalArgumentException is thrown if executor is null.
862 */
863 @Test
864 public void registerSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForExecutor() {
865 try {
866 mWifiManager.registerSoftApCallback(null, mSoftApCallback);
867 fail("expected IllegalArgumentException");
868 } catch (IllegalArgumentException expected) {
869 }
870 }
871
872 /**
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800873 * Verify an IllegalArgumentException is thrown if callback is not provided.
874 */
875 @Test
876 public void unregisterSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() {
877 try {
878 mWifiManager.unregisterSoftApCallback(null);
879 fail("expected IllegalArgumentException");
880 } catch (IllegalArgumentException expected) {
881 }
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800882 }
883
884 /**
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800885 * Verify the call to registerSoftApCallback goes to WifiServiceImpl.
886 */
887 @Test
888 public void registerSoftApCallbackCallGoesToWifiServiceImpl() throws Exception {
James Mattiscb157e82019-11-12 17:43:48 -0800889 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800890 verify(mWifiService).registerSoftApCallback(any(IBinder.class),
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800891 any(ISoftApCallback.Stub.class), anyInt());
892 }
893
894 /**
895 * Verify the call to unregisterSoftApCallback goes to WifiServiceImpl.
896 */
897 @Test
898 public void unregisterSoftApCallbackCallGoesToWifiServiceImpl() throws Exception {
899 ArgumentCaptor<Integer> callbackIdentifier = ArgumentCaptor.forClass(Integer.class);
James Mattiscb157e82019-11-12 17:43:48 -0800900 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800901 verify(mWifiService).registerSoftApCallback(any(IBinder.class),
902 any(ISoftApCallback.Stub.class), callbackIdentifier.capture());
903
904 mWifiManager.unregisterSoftApCallback(mSoftApCallback);
905 verify(mWifiService).unregisterSoftApCallback(eq((int) callbackIdentifier.getValue()));
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800906 }
907
908 /*
Roshan Pius80fb1372018-07-16 15:43:03 -0700909 * Verify client-provided callback is being called through callback proxy
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800910 */
911 @Test
912 public void softApCallbackProxyCallsOnStateChanged() throws Exception {
913 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
914 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
James Mattiscb157e82019-11-12 17:43:48 -0800915 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800916 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
917 anyInt());
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800918
919 callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
920 mLooper.dispatchAll();
921 verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0);
922 }
923
924 /*
Roshan Pius80fb1372018-07-16 15:43:03 -0700925 * Verify client-provided callback is being called through callback proxy
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800926 */
927 @Test
James Mattisaefe78c2019-10-04 17:46:12 -0700928 public void softApCallbackProxyCallsOnConnectedClientsChanged() throws Exception {
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800929 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
930 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
James Mattiscb157e82019-11-12 17:43:48 -0800931 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -0800932 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
933 anyInt());
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800934
James Mattisaefe78c2019-10-04 17:46:12 -0700935 final List<WifiClient> testClients = new ArrayList();
936 callbackCaptor.getValue().onConnectedClientsChanged(testClients);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800937 mLooper.dispatchAll();
James Mattisaefe78c2019-10-04 17:46:12 -0700938 verify(mSoftApCallback).onConnectedClientsChanged(testClients);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800939 }
940
lesl3fd4b642019-10-23 10:47:34 +0800941
942 /*
943 * Verify client-provided callback is being called through callback proxy
944 */
945 @Test
946 public void softApCallbackProxyCallsOnSoftApInfoChanged() throws Exception {
947 SoftApInfo testSoftApInfo = new SoftApInfo();
948 testSoftApInfo.setFrequency(TEST_AP_FREQUENCY);
949 testSoftApInfo.setBandwidth(TEST_AP_BANDWIDTH);
950 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
951 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
James Mattiscb157e82019-11-12 17:43:48 -0800952 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
lesl3fd4b642019-10-23 10:47:34 +0800953 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
954 anyInt());
955
956 callbackCaptor.getValue().onInfoChanged(testSoftApInfo);
957 mLooper.dispatchAll();
958 verify(mSoftApCallback).onInfoChanged(testSoftApInfo);
959 }
960
lesle14a0902019-12-12 17:16:30 +0800961
962 /*
963 * Verify client-provided callback is being called through callback proxy
964 */
965 @Test
966 public void softApCallbackProxyCallsOnCapabilityChanged() throws Exception {
lesl492e4bd2019-12-24 00:08:38 +0800967 SoftApCapability testSoftApCapability = new SoftApCapability(0);
lesle14a0902019-12-12 17:16:30 +0800968 testSoftApCapability.setMaxSupportedClients(10);
969 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
970 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
971 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
972 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
973 anyInt());
974
975 callbackCaptor.getValue().onCapabilityChanged(testSoftApCapability);
976 mLooper.dispatchAll();
977 verify(mSoftApCallback).onCapabilityChanged(testSoftApCapability);
978 }
979
Mehdi Alizadehe59b9372017-12-22 17:00:22 -0800980 /*
lesld26a9f72020-01-03 17:50:06 +0800981 * Verify client-provided callback is being called through callback proxy
982 */
983 @Test
984 public void softApCallbackProxyCallsOnBlockedClientConnecting() throws Exception {
985 WifiClient testWifiClient = new WifiClient(MacAddress.fromString("22:33:44:55:66:77"));
986 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
987 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
988 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
989 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
990 anyInt());
991
992 callbackCaptor.getValue().onBlockedClientConnecting(testWifiClient,
993 WifiManager.SAP_CLIENT_BLOCK_REASON_CODE_NO_MORE_STAS);
994 mLooper.dispatchAll();
995 verify(mSoftApCallback).onBlockedClientConnecting(testWifiClient,
996 WifiManager.SAP_CLIENT_BLOCK_REASON_CODE_NO_MORE_STAS);
997 }
998
999 /*
Roshan Pius80fb1372018-07-16 15:43:03 -07001000 * Verify client-provided callback is being called through callback proxy on multiple events
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001001 */
1002 @Test
1003 public void softApCallbackProxyCallsOnMultipleUpdates() throws Exception {
lesl3fd4b642019-10-23 10:47:34 +08001004 SoftApInfo testSoftApInfo = new SoftApInfo();
1005 testSoftApInfo.setFrequency(TEST_AP_FREQUENCY);
1006 testSoftApInfo.setBandwidth(TEST_AP_BANDWIDTH);
lesl492e4bd2019-12-24 00:08:38 +08001007 SoftApCapability testSoftApCapability = new SoftApCapability(0);
lesle14a0902019-12-12 17:16:30 +08001008 testSoftApCapability.setMaxSupportedClients(10);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001009 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
1010 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
James Mattiscb157e82019-11-12 17:43:48 -08001011 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -08001012 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
1013 anyInt());
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001014
James Mattisaefe78c2019-10-04 17:46:12 -07001015 final List<WifiClient> testClients = new ArrayList();
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001016 callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLING, 0);
James Mattisaefe78c2019-10-04 17:46:12 -07001017 callbackCaptor.getValue().onConnectedClientsChanged(testClients);
lesl3fd4b642019-10-23 10:47:34 +08001018 callbackCaptor.getValue().onInfoChanged(testSoftApInfo);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001019 callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL);
lesle14a0902019-12-12 17:16:30 +08001020 callbackCaptor.getValue().onCapabilityChanged(testSoftApCapability);
1021
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001022
1023 mLooper.dispatchAll();
1024 verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLING, 0);
James Mattisaefe78c2019-10-04 17:46:12 -07001025 verify(mSoftApCallback).onConnectedClientsChanged(testClients);
lesl3fd4b642019-10-23 10:47:34 +08001026 verify(mSoftApCallback).onInfoChanged(testSoftApInfo);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001027 verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL);
lesle14a0902019-12-12 17:16:30 +08001028 verify(mSoftApCallback).onCapabilityChanged(testSoftApCapability);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001029 }
1030
1031 /*
Roshan Pius80fb1372018-07-16 15:43:03 -07001032 * Verify client-provided callback is being called on the correct thread
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001033 */
1034 @Test
1035 public void softApCallbackIsCalledOnCorrectThread() throws Exception {
1036 ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
1037 ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
1038 TestLooper altLooper = new TestLooper();
1039 Handler altHandler = new Handler(altLooper.getLooper());
James Mattiscb157e82019-11-12 17:43:48 -08001040 mWifiManager.registerSoftApCallback(new HandlerExecutor(altHandler), mSoftApCallback);
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -08001041 verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(),
1042 anyInt());
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001043
1044 callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
1045 altLooper.dispatchAll();
1046 verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0);
1047 }
1048
1049 /**
1050 * Verify that the handler provided by the caller is used for registering soft AP callback.
1051 */
1052 @Test
1053 public void testCorrectLooperIsUsedForSoftApCallbackHandler() throws Exception {
James Mattiscb157e82019-11-12 17:43:48 -08001054 mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback);
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001055 mLooper.dispatchAll();
1056 verify(mWifiService).registerSoftApCallback(any(IBinder.class),
Mehdi Alizadeh55f3aae2018-01-11 17:56:30 -08001057 any(ISoftApCallback.Stub.class), anyInt());
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001058 verify(mContext, never()).getMainLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +02001059 verify(mContext, never()).getMainExecutor();
Mehdi Alizadehe59b9372017-12-22 17:00:22 -08001060 }
1061
1062 /**
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001063 * Verify that the handler provided by the caller is used for the observer.
1064 */
1065 @Test
1066 public void testCorrectLooperIsUsedForObserverHandler() throws Exception {
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001067 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1068 mWifiManager.watchLocalOnlyHotspot(observer, mHandler);
1069 mLooper.dispatchAll();
1070 assertTrue(observer.mOnRegistered);
Rebecca Silberstein695cea52017-05-02 13:21:00 -07001071 verify(mContext, never()).getMainLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +02001072 verify(mContext, never()).getMainExecutor();
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001073 }
1074
1075 /**
1076 * Verify that the main looper's thread is used if a handler is not provided by the requesting
1077 * application.
1078 */
1079 @Test
1080 public void testMainLooperIsUsedWhenHandlerNotProvidedForObserver() throws Exception {
1081 // record thread from looper.getThread and check ids.
1082 TestLooper altLooper = new TestLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +02001083 when(mContext.getMainExecutor()).thenReturn(altLooper.getNewExecutor());
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001084 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1085 mWifiManager.watchLocalOnlyHotspot(observer, null);
1086 altLooper.dispatchAll();
1087 assertTrue(observer.mOnRegistered);
1088 assertEquals(altLooper.getLooper().getThread().getId(), observer.mCallingThreadId);
Patrik Fimml090ea1362019-09-18 14:37:27 +02001089 verify(mContext).getMainExecutor();
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001090 }
1091
1092 /**
1093 * Verify the LOHS onRegistered observer callback is triggered when WifiManager receives a
1094 * HOTSPOT_OBSERVER_REGISTERED message from WifiServiceImpl.
1095 */
1096 @Test
1097 public void testOnRegisteredIsCalledWithSubscription() throws Exception {
1098 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1099 TestLooper observerLooper = new TestLooper();
1100 Handler observerHandler = new Handler(observerLooper.getLooper());
1101 assertFalse(observer.mOnRegistered);
1102 assertEquals(null, observer.mSub);
1103 mWifiManager.watchLocalOnlyHotspot(observer, observerHandler);
Patrik Fimml090ea1362019-09-18 14:37:27 +02001104 verify(mWifiService).startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class));
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001105 // now trigger the callback
1106 observerLooper.dispatchAll();
1107 mLooper.dispatchAll();
1108 assertTrue(observer.mOnRegistered);
1109 assertNotNull(observer.mSub);
1110 }
1111
1112 /**
1113 * Verify the LOHS onStarted observer callback is triggered when WifiManager receives a
1114 * HOTSPOT_STARTED message from WifiServiceImpl.
1115 */
1116 @Test
1117 public void testObserverOnStartedIsCalledWithWifiConfig() throws Exception {
lesl115873a2020-01-15 14:51:26 +08001118 SoftApConfiguration softApConfig = generatorTestSoftApConfig();
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001119 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1120 TestLooper observerLooper = new TestLooper();
1121 Handler observerHandler = new Handler(observerLooper.getLooper());
1122 mWifiManager.watchLocalOnlyHotspot(observer, observerHandler);
Patrik Fimml090ea1362019-09-18 14:37:27 +02001123 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
1124 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
1125 verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture());
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001126 observerLooper.dispatchAll();
1127 mLooper.dispatchAll();
1128 assertFalse(observer.mOnStartedCalled);
1129 // now trigger the callback
lesl115873a2020-01-15 14:51:26 +08001130 internalCallback.getValue().onHotspotStarted(softApConfig);
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001131 mLooper.dispatchAll();
1132 observerLooper.dispatchAll();
1133 assertTrue(observer.mOnStartedCalled);
lesl115873a2020-01-15 14:51:26 +08001134 assertEquals(softApConfig, observer.mConfig);
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001135 }
1136
1137 /**
1138 * Verify the LOHS onStarted observer callback is triggered not when WifiManager receives a
1139 * HOTSPOT_STARTED message from WifiServiceImpl with a null config.
1140 */
1141 @Test
1142 public void testObserverOnStartedNotCalledWithNullConfig() throws Exception {
1143 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1144 TestLooper observerLooper = new TestLooper();
1145 Handler observerHandler = new Handler(observerLooper.getLooper());
1146 mWifiManager.watchLocalOnlyHotspot(observer, observerHandler);
Patrik Fimml090ea1362019-09-18 14:37:27 +02001147 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
1148 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
1149 verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture());
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001150 observerLooper.dispatchAll();
1151 mLooper.dispatchAll();
1152 assertFalse(observer.mOnStartedCalled);
1153 // now trigger the callback
Patrik Fimml090ea1362019-09-18 14:37:27 +02001154 internalCallback.getValue().onHotspotStarted(null);
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001155 mLooper.dispatchAll();
1156 observerLooper.dispatchAll();
1157 assertFalse(observer.mOnStartedCalled);
1158 assertEquals(null, observer.mConfig);
1159 }
1160
1161
1162 /**
1163 * Verify the LOHS onStopped observer callback is triggered when WifiManager receives a
1164 * HOTSPOT_STOPPED message from WifiServiceImpl.
1165 */
1166 @Test
1167 public void testObserverOnStoppedIsCalled() throws Exception {
1168 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1169 TestLooper observerLooper = new TestLooper();
1170 Handler observerHandler = new Handler(observerLooper.getLooper());
1171 mWifiManager.watchLocalOnlyHotspot(observer, observerHandler);
Patrik Fimml090ea1362019-09-18 14:37:27 +02001172 ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback =
1173 ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class);
1174 verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture());
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001175 observerLooper.dispatchAll();
1176 mLooper.dispatchAll();
1177 assertFalse(observer.mOnStoppedCalled);
1178 // now trigger the callback
Patrik Fimml090ea1362019-09-18 14:37:27 +02001179 internalCallback.getValue().onHotspotStopped();
Rebecca Silberstein4871da62017-04-27 13:57:19 -07001180 mLooper.dispatchAll();
1181 observerLooper.dispatchAll();
1182 assertTrue(observer.mOnStoppedCalled);
1183 }
1184
1185 /**
1186 * Verify WifiServiceImpl is not called if there is not a registered LOHS observer callback.
1187 */
1188 @Test
1189 public void testUnregisterWifiServiceImplNotCalledWithoutRegisteredObserver() throws Exception {
1190 mWifiManager.unregisterLocalOnlyHotspotObserver();
1191 verifyZeroInteractions(mWifiService);
1192 }
1193
1194 /**
1195 * Verify WifiServiceImpl is called when there is a registered LOHS observer callback.
1196 */
1197 @Test
1198 public void testUnregisterWifiServiceImplCalledWithRegisteredObserver() throws Exception {
1199 TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver();
1200 TestLooper observerLooper = new TestLooper();
1201 Handler observerHandler = new Handler(observerLooper.getLooper());
1202 mWifiManager.watchLocalOnlyHotspot(observer, observerHandler);
1203 mWifiManager.unregisterLocalOnlyHotspotObserver();
1204 verify(mWifiService).stopWatchLocalOnlyHotspot();
1205 }
Ningyuan Wang557875d2017-09-20 14:00:17 -07001206
1207 /**
Rebecca Silberstein6a2784042018-01-19 17:45:26 -08001208 * Test that calls to get the current WPS config token return null and do not have any
1209 * interactions with WifiServiceImpl.
1210 */
1211 @Test
1212 public void testGetCurrentNetworkWpsNfcConfigurationTokenReturnsNull() {
1213 assertNull(mWifiManager.getCurrentNetworkWpsNfcConfigurationToken());
1214 verifyNoMoreInteractions(mWifiService);
1215 }
1216
1217
David Su308e25e52019-11-29 00:27:46 -08001218 class WpsCallbackTester extends WpsCallback {
Rebecca Silberstein6a2784042018-01-19 17:45:26 -08001219 public boolean mStarted = false;
1220 public boolean mSucceeded = false;
1221 public boolean mFailed = false;
1222 public int mFailureCode = -1;
1223
1224 @Override
1225 public void onStarted(String pin) {
1226 mStarted = true;
1227 }
1228
1229 @Override
1230 public void onSucceeded() {
1231 mSucceeded = true;
1232 }
1233
1234 @Override
1235 public void onFailed(int reason) {
1236 mFailed = true;
1237 mFailureCode = reason;
1238 }
1239
1240 }
1241
1242 /**
1243 * Verify that a call to start WPS immediately returns a failure.
1244 */
1245 @Test
1246 public void testStartWpsImmediatelyFailsWithCallback() {
1247 WpsCallbackTester wpsCallback = new WpsCallbackTester();
1248 mWifiManager.startWps(null, wpsCallback);
1249 assertTrue(wpsCallback.mFailed);
David Su308e25e52019-11-29 00:27:46 -08001250 assertEquals(ERROR, wpsCallback.mFailureCode);
Rebecca Silberstein6a2784042018-01-19 17:45:26 -08001251 assertFalse(wpsCallback.mStarted);
1252 assertFalse(wpsCallback.mSucceeded);
1253 verifyNoMoreInteractions(mWifiService);
1254 }
1255
1256 /**
1257 * Verify that a call to start WPS does not go to WifiServiceImpl if we do not have a callback.
1258 */
1259 @Test
1260 public void testStartWpsDoesNotCallWifiServiceImpl() {
1261 mWifiManager.startWps(null, null);
1262 verifyNoMoreInteractions(mWifiService);
1263 }
1264
James Mattisaefe78c2019-10-04 17:46:12 -07001265 /**
1266 * Verify that a call to cancel WPS immediately returns a failure.
Rebecca Silberstein6a2784042018-01-19 17:45:26 -08001267 */
1268 @Test
1269 public void testCancelWpsImmediatelyFailsWithCallback() {
1270 WpsCallbackTester wpsCallback = new WpsCallbackTester();
1271 mWifiManager.cancelWps(wpsCallback);
1272 assertTrue(wpsCallback.mFailed);
David Su308e25e52019-11-29 00:27:46 -08001273 assertEquals(ERROR, wpsCallback.mFailureCode);
Rebecca Silberstein6a2784042018-01-19 17:45:26 -08001274 assertFalse(wpsCallback.mStarted);
1275 assertFalse(wpsCallback.mSucceeded);
1276 verifyNoMoreInteractions(mWifiService);
1277 }
1278
1279 /**
1280 * Verify that a call to cancel WPS does not go to WifiServiceImpl if we do not have a callback.
1281 */
1282 @Test
1283 public void testCancelWpsDoesNotCallWifiServiceImpl() {
1284 mWifiManager.cancelWps(null);
1285 verifyNoMoreInteractions(mWifiService);
1286 }
1287
Rebecca Silbersteincf1b40f2018-03-26 23:51:07 -07001288 /**
1289 * Verify that a successful call properly returns true.
1290 */
1291 @Test
1292 public void testSetWifiApConfigurationSuccessReturnsTrue() throws Exception {
1293 WifiConfiguration apConfig = new WifiConfiguration();
1294
1295 when(mWifiService.setWifiApConfiguration(eq(apConfig), eq(TEST_PACKAGE_NAME)))
1296 .thenReturn(true);
1297 assertTrue(mWifiManager.setWifiApConfiguration(apConfig));
1298 }
1299
1300 /**
1301 * Verify that a failed call properly returns false.
1302 */
1303 @Test
1304 public void testSetWifiApConfigurationFailureReturnsFalse() throws Exception {
1305 WifiConfiguration apConfig = new WifiConfiguration();
1306
1307 when(mWifiService.setWifiApConfiguration(eq(apConfig), eq(TEST_PACKAGE_NAME)))
1308 .thenReturn(false);
1309 assertFalse(mWifiManager.setWifiApConfiguration(apConfig));
1310 }
1311
1312 /**
1313 * Verify Exceptions are rethrown when underlying calls to WifiService throw exceptions.
1314 */
1315 @Test
1316 public void testSetWifiApConfigurationRethrowsException() throws Exception {
1317 doThrow(new SecurityException()).when(mWifiService).setWifiApConfiguration(any(), any());
1318
1319 try {
1320 mWifiManager.setWifiApConfiguration(new WifiConfiguration());
1321 fail("setWifiApConfiguration should rethrow Exceptions from WifiService");
1322 } catch (SecurityException e) { }
1323 }
Roshan Piusc7056152018-04-26 23:28:35 +00001324
1325 /**
lesl692d3752019-11-27 16:17:47 +08001326 * Verify that a successful call properly returns true.
1327 */
1328 @Test
1329 public void testSetSoftApConfigurationSuccessReturnsTrue() throws Exception {
lesl115873a2020-01-15 14:51:26 +08001330 SoftApConfiguration apConfig = generatorTestSoftApConfig();
lesl692d3752019-11-27 16:17:47 +08001331
1332 when(mWifiService.setSoftApConfiguration(eq(apConfig), eq(TEST_PACKAGE_NAME)))
1333 .thenReturn(true);
1334 assertTrue(mWifiManager.setSoftApConfiguration(apConfig));
1335 }
1336
1337 /**
1338 * Verify that a failed call properly returns false.
1339 */
1340 @Test
1341 public void testSetSoftApConfigurationFailureReturnsFalse() throws Exception {
lesl115873a2020-01-15 14:51:26 +08001342 SoftApConfiguration apConfig = generatorTestSoftApConfig();
lesl692d3752019-11-27 16:17:47 +08001343
1344 when(mWifiService.setSoftApConfiguration(eq(apConfig), eq(TEST_PACKAGE_NAME)))
1345 .thenReturn(false);
1346 assertFalse(mWifiManager.setSoftApConfiguration(apConfig));
1347 }
1348
1349 /**
1350 * Verify Exceptions are rethrown when underlying calls to WifiService throw exceptions.
1351 */
1352 @Test
1353 public void testSetSoftApConfigurationRethrowsException() throws Exception {
1354 doThrow(new SecurityException()).when(mWifiService).setSoftApConfiguration(any(), any());
1355
1356 try {
lesl115873a2020-01-15 14:51:26 +08001357 mWifiManager.setSoftApConfiguration(generatorTestSoftApConfig());
lesl692d3752019-11-27 16:17:47 +08001358 fail("setWifiApConfiguration should rethrow Exceptions from WifiService");
1359 } catch (SecurityException e) { }
1360 }
1361
1362 /**
Roshan Piusc7056152018-04-26 23:28:35 +00001363 * Check the call to startScan calls WifiService.
1364 */
1365 @Test
1366 public void testStartScan() throws Exception {
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001367 when(mWifiService.startScan(eq(TEST_PACKAGE_NAME), nullable(String.class))).thenReturn(
1368 true);
Roshan Piusc7056152018-04-26 23:28:35 +00001369 assertTrue(mWifiManager.startScan());
1370
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001371 when(mWifiService.startScan(eq(TEST_PACKAGE_NAME), nullable(String.class))).thenReturn(
1372 false);
Roshan Piusc7056152018-04-26 23:28:35 +00001373 assertFalse(mWifiManager.startScan());
1374 }
Roshan Pius80fb1372018-07-16 15:43:03 -07001375
1376 /**
1377 * Verify main looper is used when handler is not provided.
1378 */
1379 @Test
1380 public void registerTrafficStateCallbackUsesMainLooperOnNullArgumentForHandler()
1381 throws Exception {
Roshan Pius80fb1372018-07-16 15:43:03 -07001382 ArgumentCaptor<ITrafficStateCallback.Stub> callbackCaptor =
1383 ArgumentCaptor.forClass(ITrafficStateCallback.Stub.class);
David Su09daba12020-01-23 12:47:24 -08001384 mWifiManager.registerTrafficStateCallback(
1385 new HandlerExecutor(new Handler(mLooper.getLooper())), mTrafficStateCallback);
Roshan Pius80fb1372018-07-16 15:43:03 -07001386 verify(mWifiService).registerTrafficStateCallback(
1387 any(IBinder.class), callbackCaptor.capture(), anyInt());
1388
1389 assertEquals(0, mLooper.dispatchAll());
1390 callbackCaptor.getValue().onStateChanged(TrafficStateCallback.DATA_ACTIVITY_INOUT);
1391 assertEquals(1, mLooper.dispatchAll());
1392 verify(mTrafficStateCallback).onStateChanged(TrafficStateCallback.DATA_ACTIVITY_INOUT);
1393 }
1394
1395 /**
1396 * Verify the call to unregisterTrafficStateCallback goes to WifiServiceImpl.
1397 */
1398 @Test
1399 public void unregisterTrafficStateCallbackCallGoesToWifiServiceImpl() throws Exception {
1400 ArgumentCaptor<Integer> callbackIdentifier = ArgumentCaptor.forClass(Integer.class);
David Suf7ef9552019-11-05 13:44:12 -08001401 mWifiManager.registerTrafficStateCallback(new HandlerExecutor(mHandler),
1402 mTrafficStateCallback);
Roshan Pius80fb1372018-07-16 15:43:03 -07001403 verify(mWifiService).registerTrafficStateCallback(any(IBinder.class),
1404 any(ITrafficStateCallback.Stub.class), callbackIdentifier.capture());
1405
1406 mWifiManager.unregisterTrafficStateCallback(mTrafficStateCallback);
1407 verify(mWifiService).unregisterTrafficStateCallback(
1408 eq((int) callbackIdentifier.getValue()));
1409 }
1410
1411 /*
1412 * Verify client-provided callback is being called through callback proxy on multiple events
1413 */
1414 @Test
1415 public void trafficStateCallbackProxyCallsOnMultipleUpdates() throws Exception {
1416 ArgumentCaptor<ITrafficStateCallback.Stub> callbackCaptor =
1417 ArgumentCaptor.forClass(ITrafficStateCallback.Stub.class);
David Suf7ef9552019-11-05 13:44:12 -08001418 mWifiManager.registerTrafficStateCallback(new HandlerExecutor(mHandler),
1419 mTrafficStateCallback);
Roshan Pius80fb1372018-07-16 15:43:03 -07001420 verify(mWifiService).registerTrafficStateCallback(
1421 any(IBinder.class), callbackCaptor.capture(), anyInt());
1422
1423 InOrder inOrder = inOrder(mTrafficStateCallback);
1424
1425 callbackCaptor.getValue().onStateChanged(TrafficStateCallback.DATA_ACTIVITY_IN);
1426 callbackCaptor.getValue().onStateChanged(TrafficStateCallback.DATA_ACTIVITY_INOUT);
1427 callbackCaptor.getValue().onStateChanged(TrafficStateCallback.DATA_ACTIVITY_OUT);
1428
1429 mLooper.dispatchAll();
1430 inOrder.verify(mTrafficStateCallback).onStateChanged(
1431 TrafficStateCallback.DATA_ACTIVITY_IN);
1432 inOrder.verify(mTrafficStateCallback).onStateChanged(
1433 TrafficStateCallback.DATA_ACTIVITY_INOUT);
1434 inOrder.verify(mTrafficStateCallback).onStateChanged(
1435 TrafficStateCallback.DATA_ACTIVITY_OUT);
1436 }
1437
David Suf7ef9552019-11-05 13:44:12 -08001438 /**
Roshan Pius80fb1372018-07-16 15:43:03 -07001439 * Verify client-provided callback is being called on the correct thread
1440 */
1441 @Test
1442 public void trafficStateCallbackIsCalledOnCorrectThread() throws Exception {
1443 ArgumentCaptor<ITrafficStateCallback.Stub> callbackCaptor =
1444 ArgumentCaptor.forClass(ITrafficStateCallback.Stub.class);
1445 TestLooper altLooper = new TestLooper();
1446 Handler altHandler = new Handler(altLooper.getLooper());
David Suf7ef9552019-11-05 13:44:12 -08001447 mWifiManager.registerTrafficStateCallback(new HandlerExecutor(altHandler),
1448 mTrafficStateCallback);
Roshan Pius80fb1372018-07-16 15:43:03 -07001449 verify(mContext, never()).getMainLooper();
Patrik Fimml090ea1362019-09-18 14:37:27 +02001450 verify(mContext, never()).getMainExecutor();
Roshan Pius80fb1372018-07-16 15:43:03 -07001451 verify(mWifiService).registerTrafficStateCallback(
1452 any(IBinder.class), callbackCaptor.capture(), anyInt());
1453
1454 assertEquals(0, altLooper.dispatchAll());
1455 callbackCaptor.getValue().onStateChanged(TrafficStateCallback.DATA_ACTIVITY_INOUT);
1456 assertEquals(1, altLooper.dispatchAll());
1457 verify(mTrafficStateCallback).onStateChanged(TrafficStateCallback.DATA_ACTIVITY_INOUT);
1458 }
Roshan Pius3c38ee42018-10-09 10:06:33 -07001459
1460 /**
1461 * Verify the call to registerNetworkRequestMatchCallback goes to WifiServiceImpl.
1462 */
1463 @Test
1464 public void registerNetworkRequestMatchCallbackCallGoesToWifiServiceImpl()
1465 throws Exception {
Roshan Pius3c38ee42018-10-09 10:06:33 -07001466 ArgumentCaptor<INetworkRequestMatchCallback.Stub> callbackCaptor =
1467 ArgumentCaptor.forClass(INetworkRequestMatchCallback.Stub.class);
David Su09daba12020-01-23 12:47:24 -08001468 mWifiManager.registerNetworkRequestMatchCallback(
1469 new HandlerExecutor(new Handler(mLooper.getLooper())),
1470 mNetworkRequestMatchCallback);
Roshan Pius3c38ee42018-10-09 10:06:33 -07001471 verify(mWifiService).registerNetworkRequestMatchCallback(
1472 any(IBinder.class), callbackCaptor.capture(), anyInt());
1473
1474 INetworkRequestUserSelectionCallback iUserSelectionCallback =
1475 mock(INetworkRequestUserSelectionCallback.class);
1476
1477 assertEquals(0, mLooper.dispatchAll());
Roshan Pius7d183342018-11-13 15:34:05 -08001478
1479 callbackCaptor.getValue().onAbort();
1480 assertEquals(1, mLooper.dispatchAll());
1481 verify(mNetworkRequestMatchCallback).onAbort();
1482
Roshan Piusdc69f462018-11-02 13:21:07 -07001483 callbackCaptor.getValue().onMatch(new ArrayList<ScanResult>());
Roshan Pius3c38ee42018-10-09 10:06:33 -07001484 assertEquals(1, mLooper.dispatchAll());
1485 verify(mNetworkRequestMatchCallback).onMatch(anyList());
1486
1487 callbackCaptor.getValue().onUserSelectionConnectSuccess(new WifiConfiguration());
1488 assertEquals(1, mLooper.dispatchAll());
1489 verify(mNetworkRequestMatchCallback).onUserSelectionConnectSuccess(
1490 any(WifiConfiguration.class));
1491
1492 callbackCaptor.getValue().onUserSelectionConnectFailure(new WifiConfiguration());
1493 assertEquals(1, mLooper.dispatchAll());
1494 verify(mNetworkRequestMatchCallback).onUserSelectionConnectFailure(
1495 any(WifiConfiguration.class));
1496 }
1497
1498 /**
1499 * Verify the call to unregisterNetworkRequestMatchCallback goes to WifiServiceImpl.
1500 */
1501 @Test
1502 public void unregisterNetworkRequestMatchCallbackCallGoesToWifiServiceImpl() throws Exception {
1503 ArgumentCaptor<Integer> callbackIdentifier = ArgumentCaptor.forClass(Integer.class);
David Suf7ef9552019-11-05 13:44:12 -08001504 mWifiManager.registerNetworkRequestMatchCallback(new HandlerExecutor(mHandler),
1505 mNetworkRequestMatchCallback);
Roshan Pius3c38ee42018-10-09 10:06:33 -07001506 verify(mWifiService).registerNetworkRequestMatchCallback(
1507 any(IBinder.class), any(INetworkRequestMatchCallback.class),
1508 callbackIdentifier.capture());
1509
1510 mWifiManager.unregisterNetworkRequestMatchCallback(mNetworkRequestMatchCallback);
1511 verify(mWifiService).unregisterNetworkRequestMatchCallback(
1512 eq((int) callbackIdentifier.getValue()));
1513 }
1514
1515 /**
1516 * Verify the call to NetworkRequestUserSelectionCallback goes to
1517 * WifiServiceImpl.
1518 */
1519 @Test
1520 public void networkRequestUserSelectionCallbackCallGoesToWifiServiceImpl()
1521 throws Exception {
Roshan Pius3c38ee42018-10-09 10:06:33 -07001522 ArgumentCaptor<INetworkRequestMatchCallback.Stub> callbackCaptor =
1523 ArgumentCaptor.forClass(INetworkRequestMatchCallback.Stub.class);
David Su09daba12020-01-23 12:47:24 -08001524 mWifiManager.registerNetworkRequestMatchCallback(
1525 new HandlerExecutor(new Handler(mLooper.getLooper())),
1526 mNetworkRequestMatchCallback);
Roshan Pius3c38ee42018-10-09 10:06:33 -07001527 verify(mWifiService).registerNetworkRequestMatchCallback(
1528 any(IBinder.class), callbackCaptor.capture(), anyInt());
1529
1530 INetworkRequestUserSelectionCallback iUserSelectionCallback =
1531 mock(INetworkRequestUserSelectionCallback.class);
1532 ArgumentCaptor<NetworkRequestUserSelectionCallback> userSelectionCallbackCaptor =
1533 ArgumentCaptor.forClass(NetworkRequestUserSelectionCallback.class);
1534 callbackCaptor.getValue().onUserSelectionCallbackRegistration(
1535 iUserSelectionCallback);
1536 assertEquals(1, mLooper.dispatchAll());
1537 verify(mNetworkRequestMatchCallback).onUserSelectionCallbackRegistration(
1538 userSelectionCallbackCaptor.capture());
1539
1540 WifiConfiguration selected = new WifiConfiguration();
1541 userSelectionCallbackCaptor.getValue().select(selected);
1542 verify(iUserSelectionCallback).select(selected);
1543
1544 userSelectionCallbackCaptor.getValue().reject();
1545 verify(iUserSelectionCallback).reject();
1546 }
Ecco Parkf035a042018-11-13 16:37:05 -08001547
1548 /**
Ecco Park05df45d2018-12-18 16:13:41 -08001549 * Check the call to getAllMatchingWifiConfigs calls getAllMatchingFqdnsForScanResults and
1550 * getWifiConfigsForPasspointProfiles of WifiService in order.
Ecco Parkf035a042018-11-13 16:37:05 -08001551 */
1552 @Test
1553 public void testGetAllMatchingWifiConfigs() throws Exception {
Ecco Park05df45d2018-12-18 16:13:41 -08001554 Map<String, List<ScanResult>> fqdns = new HashMap<>();
1555 fqdns.put("www.test.com", new ArrayList<>());
1556 when(mWifiService.getAllMatchingFqdnsForScanResults(any(List.class))).thenReturn(fqdns);
1557 InOrder inOrder = inOrder(mWifiService);
1558
Ecco Parkf035a042018-11-13 16:37:05 -08001559 mWifiManager.getAllMatchingWifiConfigs(new ArrayList<>());
1560
Ecco Park05df45d2018-12-18 16:13:41 -08001561 inOrder.verify(mWifiService).getAllMatchingFqdnsForScanResults(any(List.class));
1562 inOrder.verify(mWifiService).getWifiConfigsForPasspointProfiles(any(List.class));
Ecco Parkf035a042018-11-13 16:37:05 -08001563 }
1564
1565 /**
1566 * Check the call to getMatchingOsuProviders calls getMatchingOsuProviders of WifiService
Ecco Park8561aa32018-11-15 21:18:47 -08001567 * with the provided a list of ScanResult.
Ecco Parkf035a042018-11-13 16:37:05 -08001568 */
1569 @Test
1570 public void testGetMatchingOsuProviders() throws Exception {
Ecco Park8561aa32018-11-15 21:18:47 -08001571 mWifiManager.getMatchingOsuProviders(new ArrayList<>());
Ecco Parkf035a042018-11-13 16:37:05 -08001572
Ecco Park8561aa32018-11-15 21:18:47 -08001573 verify(mWifiService).getMatchingOsuProviders(any(List.class));
Ecco Parkf035a042018-11-13 16:37:05 -08001574 }
Roshan Pius98b14ec2018-11-02 09:41:38 -07001575
1576 /**
Nate Jiangf6b731a2019-07-25 11:43:26 -07001577 * Verify calls to {@link WifiManager#addNetworkSuggestions(List)},
1578 * {@link WifiManager#getNetworkSuggestions()} and
Roshan Pius98b14ec2018-11-02 09:41:38 -07001579 * {@link WifiManager#removeNetworkSuggestions(List)}.
1580 */
1581 @Test
Nate Jiangf6b731a2019-07-25 11:43:26 -07001582 public void addGetRemoveNetworkSuggestions() throws Exception {
1583 List<WifiNetworkSuggestion> testList = new ArrayList<>();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001584 when(mWifiService.addNetworkSuggestions(any(List.class), anyString(),
David Su308e25e52019-11-29 00:27:46 -08001585 nullable(String.class))).thenReturn(STATUS_NETWORK_SUGGESTIONS_SUCCESS);
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001586 when(mWifiService.removeNetworkSuggestions(any(List.class), anyString())).thenReturn(
David Su308e25e52019-11-29 00:27:46 -08001587 STATUS_NETWORK_SUGGESTIONS_SUCCESS);
Nate Jiangf6b731a2019-07-25 11:43:26 -07001588 when(mWifiService.getNetworkSuggestions(anyString()))
1589 .thenReturn(testList);
Roshan Pius98b14ec2018-11-02 09:41:38 -07001590
David Su308e25e52019-11-29 00:27:46 -08001591 assertEquals(STATUS_NETWORK_SUGGESTIONS_SUCCESS,
Nate Jiangf6b731a2019-07-25 11:43:26 -07001592 mWifiManager.addNetworkSuggestions(testList));
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001593 verify(mWifiService).addNetworkSuggestions(anyList(), eq(TEST_PACKAGE_NAME),
1594 nullable(String.class));
Roshan Pius98b14ec2018-11-02 09:41:38 -07001595
Nate Jiangf6b731a2019-07-25 11:43:26 -07001596 assertEquals(testList, mWifiManager.getNetworkSuggestions());
1597 verify(mWifiService).getNetworkSuggestions(eq(TEST_PACKAGE_NAME));
1598
David Su308e25e52019-11-29 00:27:46 -08001599 assertEquals(STATUS_NETWORK_SUGGESTIONS_SUCCESS,
Roshan Piusd1a4a562018-11-20 09:01:09 -08001600 mWifiManager.removeNetworkSuggestions(new ArrayList<>()));
Roshan Pius98b14ec2018-11-02 09:41:38 -07001601 verify(mWifiService).removeNetworkSuggestions(anyList(), eq(TEST_PACKAGE_NAME));
1602 }
Roshan Piusd1a4a562018-11-20 09:01:09 -08001603
1604 /**
1605 * Verify call to {@link WifiManager#getMaxNumberOfNetworkSuggestionsPerApp()}.
1606 */
1607 @Test
1608 public void getMaxNumberOfNetworkSuggestionsPerApp() {
David Sue6efa3f2019-11-29 18:02:30 -08001609 when(mContext.getSystemServiceName(ActivityManager.class))
1610 .thenReturn(Context.ACTIVITY_SERVICE);
1611 when(mContext.getSystemService(Context.ACTIVITY_SERVICE))
1612 .thenReturn(mActivityManager);
1613 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1614 assertEquals(256, mWifiManager.getMaxNumberOfNetworkSuggestionsPerApp());
1615
1616 when(mActivityManager.isLowRamDevice()).thenReturn(false);
1617 assertEquals(1024, mWifiManager.getMaxNumberOfNetworkSuggestionsPerApp());
Roshan Piusd1a4a562018-11-20 09:01:09 -08001618 }
xshu9806aa52018-12-03 18:38:25 -08001619
1620 /**
1621 * Verify getting the factory MAC address.
xshu9806aa52018-12-03 18:38:25 -08001622 */
1623 @Test
1624 public void testGetFactoryMacAddress() throws Exception {
1625 when(mWifiService.getFactoryMacAddresses()).thenReturn(TEST_MAC_ADDRESSES);
1626 assertArrayEquals(TEST_MAC_ADDRESSES, mWifiManager.getFactoryMacAddresses());
1627 verify(mWifiService).getFactoryMacAddresses();
1628 }
Mingguang Xu86833142019-01-09 13:54:29 -08001629
1630 /**
Mingguang Xue77cabc2019-03-08 09:58:23 -08001631 * Verify the call to addOnWifiUsabilityStatsListener goes to WifiServiceImpl.
Mingguang Xu86833142019-01-09 13:54:29 -08001632 */
1633 @Test
Mingguang Xue77cabc2019-03-08 09:58:23 -08001634 public void addOnWifiUsabilityStatsListeneroesToWifiServiceImpl() throws Exception {
Mingguang Xu86833142019-01-09 13:54:29 -08001635 mExecutor = new SynchronousExecutor();
Mingguang Xue77cabc2019-03-08 09:58:23 -08001636 mWifiManager.addOnWifiUsabilityStatsListener(mExecutor, mOnWifiUsabilityStatsListener);
1637 verify(mWifiService).addOnWifiUsabilityStatsListener(any(IBinder.class),
1638 any(IOnWifiUsabilityStatsListener.Stub.class), anyInt());
Mingguang Xu86833142019-01-09 13:54:29 -08001639 }
1640
1641 /**
Mingguang Xue77cabc2019-03-08 09:58:23 -08001642 * Verify the call to removeOnWifiUsabilityStatsListener goes to WifiServiceImpl.
Mingguang Xu86833142019-01-09 13:54:29 -08001643 */
1644 @Test
Mingguang Xue77cabc2019-03-08 09:58:23 -08001645 public void removeOnWifiUsabilityListenerGoesToWifiServiceImpl() throws Exception {
Mingguang Xu86833142019-01-09 13:54:29 -08001646 ArgumentCaptor<Integer> listenerIdentifier = ArgumentCaptor.forClass(Integer.class);
1647 mExecutor = new SynchronousExecutor();
Mingguang Xue77cabc2019-03-08 09:58:23 -08001648 mWifiManager.addOnWifiUsabilityStatsListener(mExecutor, mOnWifiUsabilityStatsListener);
1649 verify(mWifiService).addOnWifiUsabilityStatsListener(any(IBinder.class),
1650 any(IOnWifiUsabilityStatsListener.Stub.class), listenerIdentifier.capture());
Mingguang Xu86833142019-01-09 13:54:29 -08001651
Mingguang Xue77cabc2019-03-08 09:58:23 -08001652 mWifiManager.removeOnWifiUsabilityStatsListener(mOnWifiUsabilityStatsListener);
1653 verify(mWifiService).removeOnWifiUsabilityStatsListener(
Mingguang Xu86833142019-01-09 13:54:29 -08001654 eq((int) listenerIdentifier.getValue()));
1655 }
1656
1657 /**
Hai Shalom69d504b2019-03-29 09:48:16 -07001658 * Test behavior of isEnhancedOpenSupported
Hai Shalom69d504b2019-03-29 09:48:16 -07001659 */
1660 @Test
1661 public void testIsEnhancedOpenSupported() throws Exception {
1662 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001663 .thenReturn(new Long(WIFI_FEATURE_OWE));
Hai Shalom69d504b2019-03-29 09:48:16 -07001664 assertTrue(mWifiManager.isEnhancedOpenSupported());
1665 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001666 .thenReturn(new Long(~WIFI_FEATURE_OWE));
Hai Shalom69d504b2019-03-29 09:48:16 -07001667 assertFalse(mWifiManager.isEnhancedOpenSupported());
1668 }
1669
1670 /**
1671 * Test behavior of isWpa3SaeSupported
Hai Shalom69d504b2019-03-29 09:48:16 -07001672 */
1673 @Test
1674 public void testIsWpa3SaeSupported() throws Exception {
1675 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001676 .thenReturn(new Long(WIFI_FEATURE_WPA3_SAE));
Hai Shalom69d504b2019-03-29 09:48:16 -07001677 assertTrue(mWifiManager.isWpa3SaeSupported());
1678 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001679 .thenReturn(new Long(~WIFI_FEATURE_WPA3_SAE));
Hai Shalom69d504b2019-03-29 09:48:16 -07001680 assertFalse(mWifiManager.isWpa3SaeSupported());
1681 }
1682
1683 /**
1684 * Test behavior of isWpa3SuiteBSupported
Hai Shalom69d504b2019-03-29 09:48:16 -07001685 */
1686 @Test
1687 public void testIsWpa3SuiteBSupported() throws Exception {
1688 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001689 .thenReturn(new Long(WIFI_FEATURE_WPA3_SUITE_B));
Hai Shalom69d504b2019-03-29 09:48:16 -07001690 assertTrue(mWifiManager.isWpa3SuiteBSupported());
1691 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001692 .thenReturn(new Long(~WIFI_FEATURE_WPA3_SUITE_B));
Hai Shalom69d504b2019-03-29 09:48:16 -07001693 assertFalse(mWifiManager.isWpa3SuiteBSupported());
1694 }
1695
1696 /**
1697 * Test behavior of isEasyConnectSupported
Hai Shalom69d504b2019-03-29 09:48:16 -07001698 */
1699 @Test
1700 public void testIsEasyConnectSupported() throws Exception {
1701 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001702 .thenReturn(new Long(WIFI_FEATURE_DPP));
Hai Shalom69d504b2019-03-29 09:48:16 -07001703 assertTrue(mWifiManager.isEasyConnectSupported());
1704 when(mWifiService.getSupportedFeatures())
David Su308e25e52019-11-29 00:27:46 -08001705 .thenReturn(new Long(~WIFI_FEATURE_DPP));
Hai Shalom69d504b2019-03-29 09:48:16 -07001706 assertFalse(mWifiManager.isEasyConnectSupported());
1707 }
Roshan Piusaca52812019-04-05 11:35:35 -07001708
1709 /**
1710 * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)}
Roshan Piusaca52812019-04-05 11:35:35 -07001711 */
1712 @Test
1713 public void testAddNetwork() throws Exception {
1714 WifiConfiguration configuration = new WifiConfiguration();
1715 when(mWifiService.addOrUpdateNetwork(any(), anyString()))
1716 .thenReturn(TEST_NETWORK_ID);
1717
1718 assertEquals(mWifiManager.addNetwork(configuration), TEST_NETWORK_ID);
1719 verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName());
1720
1721 // send a null config
1722 assertEquals(mWifiManager.addNetwork(null), -1);
1723 }
1724
1725 /**
1726 * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)}
Roshan Piusaca52812019-04-05 11:35:35 -07001727 */
1728 @Test
1729 public void testUpdateNetwork() throws Exception {
1730 WifiConfiguration configuration = new WifiConfiguration();
1731 when(mWifiService.addOrUpdateNetwork(any(), anyString()))
1732 .thenReturn(TEST_NETWORK_ID);
1733
1734 configuration.networkId = TEST_NETWORK_ID;
1735 assertEquals(mWifiManager.updateNetwork(configuration), TEST_NETWORK_ID);
1736 verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName());
1737
1738 // config with invalid network ID
1739 configuration.networkId = -1;
1740 assertEquals(mWifiManager.updateNetwork(configuration), -1);
1741
1742 // send a null config
1743 assertEquals(mWifiManager.updateNetwork(null), -1);
1744 }
1745
1746 /**
1747 * Test behavior of {@link WifiManager#enableNetwork(int, boolean)}
Roshan Piusaca52812019-04-05 11:35:35 -07001748 */
1749 @Test
1750 public void testEnableNetwork() throws Exception {
1751 when(mWifiService.enableNetwork(anyInt(), anyBoolean(), anyString()))
1752 .thenReturn(true);
1753 assertTrue(mWifiManager.enableNetwork(TEST_NETWORK_ID, true));
1754 verify(mWifiService).enableNetwork(TEST_NETWORK_ID, true, mContext.getOpPackageName());
1755 }
1756
1757 /**
1758 * Test behavior of {@link WifiManager#disableNetwork(int)}
Roshan Piusaca52812019-04-05 11:35:35 -07001759 */
1760 @Test
1761 public void testDisableNetwork() throws Exception {
1762 when(mWifiService.disableNetwork(anyInt(), anyString()))
1763 .thenReturn(true);
1764 assertTrue(mWifiManager.disableNetwork(TEST_NETWORK_ID));
1765 verify(mWifiService).disableNetwork(TEST_NETWORK_ID, mContext.getOpPackageName());
1766 }
1767
1768 /**
Quang Luong10740292019-10-14 17:29:46 -07001769 * Test behavior of {@link WifiManager#allowAutojoin(int, boolean)}
1770 * @throws Exception
1771 */
1772 @Test
1773 public void testAllowAutojoin() throws Exception {
1774 mWifiManager.allowAutojoin(1, true);
Etan Cohenb6f262f2019-12-27 12:43:21 -08001775 verify(mWifiService).allowAutojoin(1, true);
Quang Luong10740292019-10-14 17:29:46 -07001776 }
1777
1778 /**
Etan Cohenb6f262f2019-12-27 12:43:21 -08001779 * Test behavior of {@link WifiManager#allowAutojoinPasspoint(String, boolean)}
1780 * @throws Exception
1781 */
1782 @Test
1783 public void testAllowAutojoinPasspoint() throws Exception {
1784 final String fqdn = "FullyQualifiedDomainName";
1785 mWifiManager.allowAutojoinPasspoint(fqdn, true);
1786 verify(mWifiService).allowAutojoinPasspoint(fqdn, true);
1787 }
1788
xshu380322e2020-01-14 17:54:31 -08001789 /**
1790 * Test behavior of
1791 * {@link WifiManager#setMacRandomizationSettingPasspointEnabled(String, boolean)}
1792 */
1793 @Test
1794 public void testSetMacRandomizationSettingPasspointEnabled() throws Exception {
1795 final String fqdn = "FullyQualifiedDomainName";
1796 mWifiManager.setMacRandomizationSettingPasspointEnabled(fqdn, true);
1797 verify(mWifiService).setMacRandomizationSettingPasspointEnabled(fqdn, true);
1798 }
1799
Quang Luong85f23422020-01-21 17:09:02 -08001800 /**
1801 * Test behavior of
1802 * {@link WifiManager#setMacRandomizationSettingPasspointEnabled(String, boolean)}
1803 */
1804 @Test
1805 public void testSetMeteredOverridePasspoint() throws Exception {
1806 final String fqdn = "FullyQualifiedDomainName";
1807 mWifiManager.setMeteredOverridePasspoint(fqdn, METERED_OVERRIDE_METERED);
1808 verify(mWifiService).setMeteredOverridePasspoint(fqdn, METERED_OVERRIDE_METERED);
1809 }
Etan Cohenb6f262f2019-12-27 12:43:21 -08001810
1811 /**
Roshan Piusaca52812019-04-05 11:35:35 -07001812 * Test behavior of {@link WifiManager#disconnect()}
Roshan Piusaca52812019-04-05 11:35:35 -07001813 */
1814 @Test
1815 public void testDisconnect() throws Exception {
1816 when(mWifiService.disconnect(anyString())).thenReturn(true);
1817 assertTrue(mWifiManager.disconnect());
1818 verify(mWifiService).disconnect(mContext.getOpPackageName());
1819 }
1820
1821 /**
1822 * Test behavior of {@link WifiManager#reconnect()}
Roshan Piusaca52812019-04-05 11:35:35 -07001823 */
1824 @Test
1825 public void testReconnect() throws Exception {
1826 when(mWifiService.reconnect(anyString())).thenReturn(true);
1827 assertTrue(mWifiManager.reconnect());
1828 verify(mWifiService).reconnect(mContext.getOpPackageName());
1829 }
1830
1831 /**
1832 * Test behavior of {@link WifiManager#reassociate()}
Roshan Piusaca52812019-04-05 11:35:35 -07001833 */
1834 @Test
1835 public void testReassociate() throws Exception {
1836 when(mWifiService.reassociate(anyString())).thenReturn(true);
1837 assertTrue(mWifiManager.reassociate());
1838 verify(mWifiService).reassociate(mContext.getOpPackageName());
1839 }
1840
1841 /**
1842 * Test behavior of {@link WifiManager#getSupportedFeatures()}
Roshan Piusaca52812019-04-05 11:35:35 -07001843 */
1844 @Test
1845 public void testGetSupportedFeatures() throws Exception {
1846 long supportedFeatures =
David Su308e25e52019-11-29 00:27:46 -08001847 WIFI_FEATURE_SCANNER
1848 | WIFI_FEATURE_PASSPOINT
1849 | WIFI_FEATURE_P2P;
Roshan Piusaca52812019-04-05 11:35:35 -07001850 when(mWifiService.getSupportedFeatures())
1851 .thenReturn(Long.valueOf(supportedFeatures));
1852
1853 assertTrue(mWifiManager.isWifiScannerSupported());
1854 assertTrue(mWifiManager.isPasspointSupported());
1855 assertTrue(mWifiManager.isP2pSupported());
1856 assertFalse(mWifiManager.isPortableHotspotSupported());
Roshan Piusaca52812019-04-05 11:35:35 -07001857 assertFalse(mWifiManager.isDeviceToDeviceRttSupported());
1858 assertFalse(mWifiManager.isDeviceToApRttSupported());
1859 assertFalse(mWifiManager.isPreferredNetworkOffloadSupported());
1860 assertFalse(mWifiManager.isAdditionalStaSupported());
1861 assertFalse(mWifiManager.isTdlsSupported());
1862 assertFalse(mWifiManager.isOffChannelTdlsSupported());
1863 assertFalse(mWifiManager.isEnhancedPowerReportingSupported());
1864 }
1865
1866 /**
David Su308e25e52019-11-29 00:27:46 -08001867 * Tests that passing a null Executor to {@link WifiManager#getWifiActivityEnergyInfoAsync}
1868 * throws an exception.
1869 */
1870 @Test(expected = IllegalArgumentException.class)
1871 public void testGetWifiActivityInfoNullExecutor() throws Exception {
1872 mWifiManager.getWifiActivityEnergyInfoAsync(null, mOnWifiActivityEnergyInfoListener);
1873 }
1874
1875 /**
1876 * Tests that passing a null listener to {@link WifiManager#getWifiActivityEnergyInfoAsync}
1877 * throws an exception.
1878 */
1879 @Test(expected = IllegalArgumentException.class)
1880 public void testGetWifiActivityInfoNullListener() throws Exception {
1881 mWifiManager.getWifiActivityEnergyInfoAsync(mExecutor, null);
1882 }
1883
1884 /** Tests that the listener runs on the correct Executor. */
1885 @Test
1886 public void testGetWifiActivityInfoRunsOnCorrectExecutor() throws Exception {
1887 mWifiManager.getWifiActivityEnergyInfoAsync(mExecutor, mOnWifiActivityEnergyInfoListener);
1888 ArgumentCaptor<IOnWifiActivityEnergyInfoListener> listenerCaptor =
1889 ArgumentCaptor.forClass(IOnWifiActivityEnergyInfoListener.class);
1890 verify(mWifiService).getWifiActivityEnergyInfoAsync(listenerCaptor.capture());
1891 IOnWifiActivityEnergyInfoListener listener = listenerCaptor.getValue();
1892 listener.onWifiActivityEnergyInfo(mWifiActivityEnergyInfo);
1893 verify(mExecutor).execute(any());
1894
1895 // ensure that the executor is only triggered once
1896 listener.onWifiActivityEnergyInfo(mWifiActivityEnergyInfo);
1897 verify(mExecutor).execute(any());
1898 }
1899
1900 /** Tests that the correct listener runs. */
1901 @Test
1902 public void testGetWifiActivityInfoRunsCorrectListener() throws Exception {
1903 int[] flag = {0};
1904 mWifiManager.getWifiActivityEnergyInfoAsync(
1905 new SynchronousExecutor(), info -> flag[0]++);
1906 ArgumentCaptor<IOnWifiActivityEnergyInfoListener> listenerCaptor =
1907 ArgumentCaptor.forClass(IOnWifiActivityEnergyInfoListener.class);
1908 verify(mWifiService).getWifiActivityEnergyInfoAsync(listenerCaptor.capture());
1909 IOnWifiActivityEnergyInfoListener listener = listenerCaptor.getValue();
1910 listener.onWifiActivityEnergyInfo(mWifiActivityEnergyInfo);
1911 assertEquals(1, flag[0]);
1912
1913 // ensure that the listener is only triggered once
1914 listener.onWifiActivityEnergyInfo(mWifiActivityEnergyInfo);
1915 assertEquals(1, flag[0]);
1916 }
1917
1918 /**
Roshan Piusaca52812019-04-05 11:35:35 -07001919 * Test behavior of {@link WifiManager#getConnectionInfo()}
Roshan Piusaca52812019-04-05 11:35:35 -07001920 */
1921 @Test
1922 public void testGetConnectionInfo() throws Exception {
1923 WifiInfo wifiInfo = new WifiInfo();
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07001924 when(mWifiService.getConnectionInfo(anyString(), nullable(String.class))).thenReturn(
1925 wifiInfo);
Roshan Piusaca52812019-04-05 11:35:35 -07001926
1927 assertEquals(wifiInfo, mWifiManager.getConnectionInfo());
1928 }
1929
1930 /**
Ahmed ElArabawy9f8d8062019-12-09 16:16:51 -08001931 * Test behavior of {@link WifiManager#is5GHzBandSupported()}
Roshan Piusaca52812019-04-05 11:35:35 -07001932 */
1933 @Test
Ahmed ElArabawy9f8d8062019-12-09 16:16:51 -08001934 public void testIs5GHzBandSupported() throws Exception {
1935 when(mWifiService.is5GHzBandSupported()).thenReturn(true);
1936 assertTrue(mWifiManager.is5GHzBandSupported());
1937 verify(mWifiService).is5GHzBandSupported();
1938 }
1939
1940 /**
1941 * Test behavior of {@link WifiManager#is6GHzBandSupported()}
1942 */
1943 @Test
1944 public void testIs6GHzBandSupported() throws Exception {
1945 when(mWifiService.is6GHzBandSupported()).thenReturn(true);
1946 assertTrue(mWifiManager.is6GHzBandSupported());
1947 verify(mWifiService).is6GHzBandSupported();
Roshan Piusaca52812019-04-05 11:35:35 -07001948 }
1949
1950 /**
Ahmed ElArabawy9b3f0762020-01-10 12:28:19 -08001951 * Test behavior of {@link WifiManager#isWifiStandardSupported()}
1952 */
1953 @Test
1954 public void testIsWifiStandardSupported() throws Exception {
1955 int standard = ScanResult.WIFI_STANDARD_11AX;
1956 when(mWifiService.isWifiStandardSupported(standard)).thenReturn(true);
1957 assertTrue(mWifiManager.isWifiStandardSupported(standard));
1958 verify(mWifiService).isWifiStandardSupported(standard);
1959 }
1960
1961 /**
Roshan Piusaca52812019-04-05 11:35:35 -07001962 * Test behavior of {@link WifiManager#getDhcpInfo()}
Roshan Piusaca52812019-04-05 11:35:35 -07001963 */
1964 @Test
1965 public void testGetDhcpInfo() throws Exception {
1966 DhcpInfo dhcpInfo = new DhcpInfo();
1967
1968 when(mWifiService.getDhcpInfo()).thenReturn(dhcpInfo);
1969 assertEquals(dhcpInfo, mWifiManager.getDhcpInfo());
1970 verify(mWifiService).getDhcpInfo();
1971 }
1972
1973 /**
1974 * Test behavior of {@link WifiManager#setWifiEnabled(boolean)}
Roshan Piusaca52812019-04-05 11:35:35 -07001975 */
1976 @Test
1977 public void testSetWifiEnabled() throws Exception {
1978 when(mWifiService.setWifiEnabled(anyString(), anyBoolean())).thenReturn(true);
1979 assertTrue(mWifiManager.setWifiEnabled(true));
1980 verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), true);
1981 assertTrue(mWifiManager.setWifiEnabled(false));
1982 verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), false);
1983 }
Roshan Pius641a6832019-09-09 10:53:20 -07001984
1985 /**
David Su308e25e52019-11-29 00:27:46 -08001986 * Test behavior of {@link WifiManager#connect(int, ActionListener)}
Roshan Pius641a6832019-09-09 10:53:20 -07001987 */
1988 @Test
1989 public void testConnectWithListener() throws Exception {
David Su308e25e52019-11-29 00:27:46 -08001990 ActionListener externalListener = mock(ActionListener.class);
Roshan Pius641a6832019-09-09 10:53:20 -07001991 mWifiManager.connect(TEST_NETWORK_ID, externalListener);
1992
1993 ArgumentCaptor<IActionListener> binderListenerCaptor =
1994 ArgumentCaptor.forClass(IActionListener.class);
1995 verify(mWifiService).connect(eq(null), eq(TEST_NETWORK_ID), any(Binder.class),
1996 binderListenerCaptor.capture(), anyInt());
1997 assertNotNull(binderListenerCaptor.getValue());
1998
1999 // Trigger on success.
2000 binderListenerCaptor.getValue().onSuccess();
2001 mLooper.dispatchAll();
2002 verify(externalListener).onSuccess();
2003
2004 // Trigger on failure.
David Su308e25e52019-11-29 00:27:46 -08002005 binderListenerCaptor.getValue().onFailure(BUSY);
Roshan Pius641a6832019-09-09 10:53:20 -07002006 mLooper.dispatchAll();
David Su308e25e52019-11-29 00:27:46 -08002007 verify(externalListener).onFailure(BUSY);
Roshan Pius641a6832019-09-09 10:53:20 -07002008 }
2009
2010 /**
David Su308e25e52019-11-29 00:27:46 -08002011 * Test behavior of {@link WifiManager#connect(int, ActionListener)}
Roshan Pius641a6832019-09-09 10:53:20 -07002012 */
2013 @Test
2014 public void testConnectWithListenerHandleSecurityException() throws Exception {
2015 doThrow(new SecurityException()).when(mWifiService)
2016 .connect(eq(null), anyInt(), any(IBinder.class),
2017 any(IActionListener.class), anyInt());
David Su308e25e52019-11-29 00:27:46 -08002018 ActionListener externalListener = mock(ActionListener.class);
Roshan Pius641a6832019-09-09 10:53:20 -07002019 mWifiManager.connect(TEST_NETWORK_ID, externalListener);
2020
2021 mLooper.dispatchAll();
David Su308e25e52019-11-29 00:27:46 -08002022 verify(externalListener).onFailure(NOT_AUTHORIZED);
Roshan Pius641a6832019-09-09 10:53:20 -07002023 }
2024
2025 /**
David Su308e25e52019-11-29 00:27:46 -08002026 * Test behavior of {@link WifiManager#connect(int, ActionListener)}
Roshan Pius641a6832019-09-09 10:53:20 -07002027 */
2028 @Test
2029 public void testConnectWithListenerHandleRemoteException() throws Exception {
2030 doThrow(new RemoteException()).when(mWifiService)
2031 .connect(eq(null), anyInt(), any(IBinder.class),
2032 any(IActionListener.class), anyInt());
David Su308e25e52019-11-29 00:27:46 -08002033 ActionListener externalListener = mock(ActionListener.class);
Roshan Pius641a6832019-09-09 10:53:20 -07002034 mWifiManager.connect(TEST_NETWORK_ID, externalListener);
2035
2036 mLooper.dispatchAll();
David Su308e25e52019-11-29 00:27:46 -08002037 verify(externalListener).onFailure(ERROR);
Roshan Pius641a6832019-09-09 10:53:20 -07002038 }
2039
2040 /**
David Su308e25e52019-11-29 00:27:46 -08002041 * Test behavior of {@link WifiManager#connect(int, ActionListener)}
Roshan Pius641a6832019-09-09 10:53:20 -07002042 */
2043 @Test
2044 public void testConnectWithoutListener() throws Exception {
2045 WifiConfiguration configuration = new WifiConfiguration();
2046 mWifiManager.connect(configuration, null);
2047
2048 verify(mWifiService).connect(configuration, WifiConfiguration.INVALID_NETWORK_ID, null,
2049 null, 0);
2050 }
2051
2052 /**
David Su308e25e52019-11-29 00:27:46 -08002053 * Test behavior of {@link WifiManager#getTxPacketCount(TxPacketCountListener)}
Roshan Pius641a6832019-09-09 10:53:20 -07002054 */
2055 @Test
2056 public void testGetTxPacketCount() throws Exception {
David Su308e25e52019-11-29 00:27:46 -08002057 TxPacketCountListener externalListener =
2058 mock(TxPacketCountListener.class);
Roshan Pius641a6832019-09-09 10:53:20 -07002059 mWifiManager.getTxPacketCount(externalListener);
2060
2061 ArgumentCaptor<ITxPacketCountListener> binderListenerCaptor =
2062 ArgumentCaptor.forClass(ITxPacketCountListener.class);
2063 verify(mWifiService).getTxPacketCount(anyString(), any(Binder.class),
2064 binderListenerCaptor.capture(), anyInt());
2065 assertNotNull(binderListenerCaptor.getValue());
2066
2067 // Trigger on success.
2068 binderListenerCaptor.getValue().onSuccess(6);
2069 mLooper.dispatchAll();
2070 verify(externalListener).onSuccess(6);
2071
2072 // Trigger on failure.
David Su308e25e52019-11-29 00:27:46 -08002073 binderListenerCaptor.getValue().onFailure(BUSY);
Roshan Pius641a6832019-09-09 10:53:20 -07002074 mLooper.dispatchAll();
David Su308e25e52019-11-29 00:27:46 -08002075 verify(externalListener).onFailure(BUSY);
Roshan Pius641a6832019-09-09 10:53:20 -07002076 }
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002077
2078 /**
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002079 * Verify an IllegalArgumentException is thrown if callback is not provided.
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002080 */
2081 @Test(expected = IllegalArgumentException.class)
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002082 public void testRegisterScanResultsCallbackWithNullCallback() throws Exception {
2083 mWifiManager.registerScanResultsCallback(mExecutor, null);
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002084 }
2085
2086 /**
2087 * Verify an IllegalArgumentException is thrown if executor is not provided.
2088 */
2089 @Test(expected = IllegalArgumentException.class)
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002090 public void testRegisterCallbackWithNullExecutor() throws Exception {
2091 mWifiManager.registerScanResultsCallback(null, mScanResultsCallback);
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002092 }
2093
2094 /**
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002095 * Verify client provided callback is being called to the right callback.
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002096 */
2097 @Test
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002098 public void testAddScanResultsCallbackAndReceiveEvent() throws Exception {
2099 ArgumentCaptor<IScanResultsCallback.Stub> callbackCaptor =
2100 ArgumentCaptor.forClass(IScanResultsCallback.Stub.class);
2101 mWifiManager.registerScanResultsCallback(new SynchronousExecutor(), mScanResultsCallback);
2102 verify(mWifiService).registerScanResultsCallback(callbackCaptor.capture());
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002103 callbackCaptor.getValue().onScanResultsAvailable();
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002104 verify(mRunnable).run();
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002105 }
2106
2107 /**
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002108 * Verify client provided callback is being called to the right executor.
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002109 */
2110 @Test
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002111 public void testRegisterScanResultsCallbackWithTheTargetExecutor() throws Exception {
2112 ArgumentCaptor<IScanResultsCallback.Stub> callbackCaptor =
2113 ArgumentCaptor.forClass(IScanResultsCallback.Stub.class);
2114 mWifiManager.registerScanResultsCallback(mExecutor, mScanResultsCallback);
2115 verify(mWifiService).registerScanResultsCallback(callbackCaptor.capture());
2116 mWifiManager.registerScanResultsCallback(mAnotherExecutor, mScanResultsCallback);
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002117 callbackCaptor.getValue().onScanResultsAvailable();
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002118 verify(mExecutor, never()).execute(any(Runnable.class));
2119 verify(mAnotherExecutor).execute(any(Runnable.class));
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002120 }
2121
2122 /**
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002123 * Verify client register unregister then register again, to ensure callback still works.
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002124 */
2125 @Test
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002126 public void testRegisterUnregisterThenRegisterAgainWithScanResultCallback() throws Exception {
2127 ArgumentCaptor<IScanResultsCallback.Stub> callbackCaptor =
2128 ArgumentCaptor.forClass(IScanResultsCallback.Stub.class);
2129 mWifiManager.registerScanResultsCallback(new SynchronousExecutor(), mScanResultsCallback);
2130 verify(mWifiService).registerScanResultsCallback(callbackCaptor.capture());
2131 mWifiManager.unregisterScanResultsCallback(mScanResultsCallback);
2132 callbackCaptor.getValue().onScanResultsAvailable();
2133 verify(mRunnable, never()).run();
2134 mWifiManager.registerScanResultsCallback(new SynchronousExecutor(), mScanResultsCallback);
2135 callbackCaptor.getValue().onScanResultsAvailable();
2136 verify(mRunnable).run();
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002137 }
2138
2139 /**
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002140 * Verify client unregisterScanResultsCallback.
2141 */
2142 @Test
2143 public void testUnregisterScanResultsCallback() throws Exception {
2144 mWifiManager.unregisterScanResultsCallback(mScanResultsCallback);
2145 verify(mWifiService).unregisterScanResultsCallback(any());
2146 }
2147
2148 /**
2149 * Verify client unregisterScanResultsCallback with null callback will cause an exception.
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002150 */
2151 @Test(expected = IllegalArgumentException.class)
Nate(Qiang) Jiang10bc5db2019-11-20 10:23:23 -08002152 public void testUnregisterScanResultsCallbackWithNullCallback() throws Exception {
2153 mWifiManager.unregisterScanResultsCallback(null);
Nate(Qiang) Jiangee434e82019-10-09 16:47:39 -07002154 }
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002155
2156 /**
2157 * Verify an IllegalArgumentException is thrown if executor not provided.
2158 */
2159 @Test(expected = IllegalArgumentException.class)
2160 public void testAddSuggestionConnectionStatusListenerWithNullExecutor() {
2161 mWifiManager.addSuggestionConnectionStatusListener(null, mListener);
2162 }
2163
2164 /**
2165 * Verify an IllegalArgumentException is thrown if listener is not provided.
2166 */
2167 @Test(expected = IllegalArgumentException.class)
2168 public void testAddSuggestionConnectionStatusListenerWithNullListener() {
2169 mWifiManager.addSuggestionConnectionStatusListener(mExecutor, null);
2170 }
2171
2172 /**
2173 * Verify client provided listener is being called to the right listener.
2174 */
2175 @Test
2176 public void testAddSuggestionConnectionStatusListenerAndReceiveEvent() throws Exception {
David Su308e25e52019-11-29 00:27:46 -08002177 int errorCode = STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION;
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002178 ArgumentCaptor<ISuggestionConnectionStatusListener.Stub> callbackCaptor =
2179 ArgumentCaptor.forClass(ISuggestionConnectionStatusListener.Stub.class);
2180 Executor executor = new SynchronousExecutor();
2181 mWifiManager.addSuggestionConnectionStatusListener(executor, mListener);
2182 verify(mWifiService).registerSuggestionConnectionStatusListener(any(IBinder.class),
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07002183 callbackCaptor.capture(), anyInt(), anyString(), nullable(String.class));
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002184 callbackCaptor.getValue().onConnectionStatus(mWifiNetworkSuggestion, errorCode);
2185 verify(mListener).onConnectionStatus(any(WifiNetworkSuggestion.class), eq(errorCode));
2186 }
2187
2188 /**
2189 * Verify client provided listener is being called to the right executor.
2190 */
2191 @Test
2192 public void testAddSuggestionConnectionStatusListenerWithTheTargetExecutor() throws Exception {
David Su308e25e52019-11-29 00:27:46 -08002193 int errorCode = STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION;
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002194 ArgumentCaptor<ISuggestionConnectionStatusListener.Stub> callbackCaptor =
2195 ArgumentCaptor.forClass(ISuggestionConnectionStatusListener.Stub.class);
2196 mWifiManager.addSuggestionConnectionStatusListener(mExecutor, mListener);
2197 verify(mWifiService).registerSuggestionConnectionStatusListener(any(IBinder.class),
Philip P. Moltmanndcb850c2019-10-04 08:14:38 -07002198 callbackCaptor.capture(), anyInt(), anyString(), nullable(String.class));
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002199 callbackCaptor.getValue().onConnectionStatus(any(WifiNetworkSuggestion.class), errorCode);
2200 verify(mExecutor).execute(any(Runnable.class));
2201 }
2202
2203 /**
2204 * Verify an IllegalArgumentException is thrown if listener is not provided.
2205 */
2206 @Test(expected = IllegalArgumentException.class)
2207 public void testRemoveSuggestionConnectionListenerWithNullListener() {
2208 mWifiManager.removeSuggestionConnectionStatusListener(null);
2209 }
2210
2211 /**
2212 * Verify removeSuggestionConnectionListener.
2213 */
2214 @Test
2215 public void testRemoveSuggestionConnectionListener() throws Exception {
Nate(Qiang) Jiangd076cbc2019-10-21 13:38:30 -07002216 mWifiManager.removeSuggestionConnectionStatusListener(mListener);
2217 verify(mWifiService).unregisterSuggestionConnectionStatusListener(anyInt(), anyString());
2218 }
David Su3c924102019-11-18 12:49:59 -08002219
2220 /** Test {@link WifiManager#calculateSignalLevel(int)} */
2221 @Test
2222 public void testCalculateSignalLevel() throws Exception {
2223 when(mWifiService.calculateSignalLevel(anyInt())).thenReturn(3);
2224 int actual = mWifiManager.calculateSignalLevel(-60);
2225 verify(mWifiService).calculateSignalLevel(-60);
2226 assertEquals(3, actual);
2227 }
2228
2229 /** Test {@link WifiManager#getMaxSignalLevel()} */
2230 @Test
2231 public void testGetMaxSignalLevel() throws Exception {
2232 when(mWifiService.calculateSignalLevel(anyInt())).thenReturn(4);
2233 int actual = mWifiManager.getMaxSignalLevel();
2234 verify(mWifiService).calculateSignalLevel(Integer.MAX_VALUE);
2235 assertEquals(4, actual);
2236 }
Jimmy Chen3c211932019-09-16 15:31:05 +08002237
2238 /*
2239 * Test behavior of isWapiSupported
2240 * @throws Exception
2241 */
2242 @Test
2243 public void testIsWapiSupported() throws Exception {
2244 when(mWifiService.getSupportedFeatures())
2245 .thenReturn(new Long(WifiManager.WIFI_FEATURE_WAPI));
2246 assertTrue(mWifiManager.isWapiSupported());
2247 when(mWifiService.getSupportedFeatures())
2248 .thenReturn(new Long(~WifiManager.WIFI_FEATURE_WAPI));
2249 assertFalse(mWifiManager.isWapiSupported());
2250 }
Hai Shalome722cbd2019-12-04 15:50:07 -08002251
2252 /*
2253 * Test that DPP channel list is parsed correctly
2254 */
2255 @Test
2256 public void testparseDppChannelList() throws Exception {
2257 String channelList = "81/1,2,3,4,5,6,7,8,9,10,11,115/36,40,44,48";
2258 SparseArray<int[]> expectedResult = new SparseArray<>();
2259 expectedResult.append(81, new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
2260 expectedResult.append(115, new int[]{36, 40, 44, 48});
2261
2262 SparseArray<int[]> result = WifiManager.parseDppChannelList(channelList);
2263 assertEquals(expectedResult.size(), result.size());
2264
2265 int index = 0;
2266 int key;
2267
2268 // Compare the two primitive int arrays
2269 do {
2270 try {
2271 key = result.keyAt(index);
2272 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
2273 break;
2274 }
2275 int[] expected = expectedResult.get(key);
2276 int[] output = result.get(key);
2277 assertEquals(expected.length, output.length);
2278 for (int i = 0; i < output.length; i++) {
2279 assertEquals(expected[i], output[i]);
2280 }
2281 index++;
2282 } while (true);
2283 }
2284
2285 /*
2286 * Test that DPP channel list parser gracefully fails for invalid input
2287 */
2288 @Test
2289 public void testparseDppChannelListWithInvalidFormats() throws Exception {
2290 String channelList = "1,2,3,4,5,6,7,8,9,10,11,36,40,44,48";
2291 SparseArray<int[]> result = WifiManager.parseDppChannelList(channelList);
2292 assertEquals(result.size(), 0);
2293
2294 channelList = "ajgalskgjalskjg3-09683dh";
2295 result = WifiManager.parseDppChannelList(channelList);
2296 assertEquals(result.size(), 0);
2297
2298 channelList = "13/abc,46////";
2299 result = WifiManager.parseDppChannelList(channelList);
2300 assertEquals(result.size(), 0);
2301
2302 channelList = "11/4,5,13/";
2303 result = WifiManager.parseDppChannelList(channelList);
2304 assertEquals(result.size(), 0);
2305
2306 channelList = "/24,6";
2307 result = WifiManager.parseDppChannelList(channelList);
2308 assertEquals(result.size(), 0);
2309 }
Nate(Qiang) Jiang7888cc32019-12-18 11:14:28 -08002310
2311 /**
2312 * Test getWifiConfigsForMatchedNetworkSuggestions for given scanResults.
2313 */
2314 @Test
2315 public void testGetWifiConfigsForMatchedNetworkSuggestions() throws Exception {
2316 List<WifiConfiguration> testResults = new ArrayList<>();
2317 testResults.add(new WifiConfiguration());
2318
2319 when(mWifiService.getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(any(List.class)))
2320 .thenReturn(testResults);
2321 assertEquals(testResults, mWifiManager
2322 .getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(new ArrayList<>()));
2323 }
Mingguang Xuec6f8ce2019-12-20 16:11:07 -08002324
2325 /**
2326 * Verify the call to setWifiConnectedNetworkScorer goes to WifiServiceImpl.
2327 */
2328 @Test
2329 public void setWifiConnectedNetworkScorerGoesToWifiServiceImpl() throws Exception {
2330 mExecutor = new SynchronousExecutor();
2331 mWifiManager.setWifiConnectedNetworkScorer(mExecutor, mWifiConnectedNetworkScorer);
2332 verify(mWifiService).setWifiConnectedNetworkScorer(any(IBinder.class),
2333 any(IWifiConnectedNetworkScorer.Stub.class));
2334 }
2335
2336 /**
2337 * Verify the call to clearWifiConnectedNetworkScorer goes to WifiServiceImpl.
2338 */
2339 @Test
2340 public void clearWifiConnectedNetworkScorerGoesToWifiServiceImpl() throws Exception {
2341 mExecutor = new SynchronousExecutor();
2342 mWifiManager.setWifiConnectedNetworkScorer(mExecutor, mWifiConnectedNetworkScorer);
2343 verify(mWifiService).setWifiConnectedNetworkScorer(any(IBinder.class),
2344 any(IWifiConnectedNetworkScorer.Stub.class));
2345
2346 mWifiManager.clearWifiConnectedNetworkScorer();
2347 verify(mWifiService).clearWifiConnectedNetworkScorer();
2348 }
2349
2350 /**
2351 * Verify that Wi-Fi connected scorer receives score change callback after registeration.
2352 */
2353 @Test
2354 public void verifyScorerReceiveScoreChangeCallbackAfterRegistration() throws Exception {
2355 mExecutor = new SynchronousExecutor();
2356 mWifiManager.setWifiConnectedNetworkScorer(mExecutor, mWifiConnectedNetworkScorer);
2357 ArgumentCaptor<IWifiConnectedNetworkScorer.Stub> scorerCaptor =
2358 ArgumentCaptor.forClass(IWifiConnectedNetworkScorer.Stub.class);
2359 verify(mWifiService).setWifiConnectedNetworkScorer(any(IBinder.class),
2360 scorerCaptor.capture());
2361 scorerCaptor.getValue().setScoreChangeCallback(any());
2362 mLooper.dispatchAll();
2363 verify(mWifiConnectedNetworkScorer).setScoreChangeCallback(any());
2364 }
2365
2366 /**
2367 * Verify that Wi-Fi connected scorer receives session ID when start/stop methods are called.
2368 */
2369 @Test
2370 public void verifyScorerReceiveSessionIdWhenStartStopIsCalled() throws Exception {
2371 mExecutor = new SynchronousExecutor();
2372 mWifiManager.setWifiConnectedNetworkScorer(mExecutor, mWifiConnectedNetworkScorer);
2373 ArgumentCaptor<IWifiConnectedNetworkScorer.Stub> callbackCaptor =
2374 ArgumentCaptor.forClass(IWifiConnectedNetworkScorer.Stub.class);
2375 verify(mWifiService).setWifiConnectedNetworkScorer(any(IBinder.class),
2376 callbackCaptor.capture());
2377 callbackCaptor.getValue().start(0);
2378 callbackCaptor.getValue().stop(10);
2379 mLooper.dispatchAll();
2380 verify(mWifiConnectedNetworkScorer).start(0);
2381 verify(mWifiConnectedNetworkScorer).stop(10);
2382 }
Rebecca Silbersteince74fc22017-03-25 12:40:29 -07002383}