Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net.wifi; |
| 18 | |
Quang Luong | 85f2342 | 2020-01-21 17:09:02 -0800 | [diff] [blame] | 19 | import static android.net.wifi.WifiConfiguration.METERED_OVERRIDE_METERED; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 20 | import static android.net.wifi.WifiManager.ActionListener; |
| 21 | import static android.net.wifi.WifiManager.BUSY; |
| 22 | import static android.net.wifi.WifiManager.ERROR; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 23 | import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_GENERIC; |
| 24 | import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE; |
| 25 | import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL; |
Rebecca Silberstein | 23d79bf | 2017-05-02 11:22:45 -0700 | [diff] [blame] | 26 | import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_TETHERING_DISALLOWED; |
Rebecca Silberstein | 6d7b130 | 2017-04-30 14:05:00 -0700 | [diff] [blame] | 27 | import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.REQUEST_REGISTERED; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 28 | import static android.net.wifi.WifiManager.NOT_AUTHORIZED; |
| 29 | import static android.net.wifi.WifiManager.OnWifiActivityEnergyInfoListener; |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 30 | import static android.net.wifi.WifiManager.SAP_START_FAILURE_GENERAL; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 31 | import static android.net.wifi.WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS; |
| 32 | import static android.net.wifi.WifiManager.STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION; |
| 33 | import static android.net.wifi.WifiManager.TxPacketCountListener; |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 34 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED; |
| 35 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING; |
| 36 | import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 37 | import static android.net.wifi.WifiManager.WIFI_FEATURE_DPP; |
| 38 | import static android.net.wifi.WifiManager.WIFI_FEATURE_OWE; |
| 39 | import static android.net.wifi.WifiManager.WIFI_FEATURE_P2P; |
| 40 | import static android.net.wifi.WifiManager.WIFI_FEATURE_PASSPOINT; |
| 41 | import static android.net.wifi.WifiManager.WIFI_FEATURE_SCANNER; |
| 42 | import static android.net.wifi.WifiManager.WIFI_FEATURE_WPA3_SAE; |
| 43 | import static android.net.wifi.WifiManager.WIFI_FEATURE_WPA3_SUITE_B; |
| 44 | import static android.net.wifi.WifiManager.WpsCallback; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 45 | |
xshu | 9806aa5 | 2018-12-03 18:38:25 -0800 | [diff] [blame] | 46 | import static org.junit.Assert.assertArrayEquals; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertFalse; |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 49 | import static org.junit.Assert.assertNotNull; |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 50 | import static org.junit.Assert.assertNull; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 51 | import static org.junit.Assert.assertTrue; |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 52 | import static org.junit.Assert.fail; |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 53 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 54 | import static org.mockito.ArgumentMatchers.nullable; |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 55 | import static org.mockito.Mockito.any; |
| 56 | import static org.mockito.Mockito.anyInt; |
| 57 | import static org.mockito.Mockito.anyList; |
| 58 | import static org.mockito.Mockito.anyString; |
| 59 | import static org.mockito.Mockito.doThrow; |
| 60 | import static org.mockito.Mockito.eq; |
| 61 | import static org.mockito.Mockito.inOrder; |
| 62 | import static org.mockito.Mockito.mock; |
| 63 | import static org.mockito.Mockito.never; |
| 64 | import static org.mockito.Mockito.spy; |
| 65 | import static org.mockito.Mockito.verify; |
| 66 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 67 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 68 | import static org.mockito.Mockito.when; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 69 | |
David Su | e6efa3f | 2019-11-29 18:02:30 -0800 | [diff] [blame] | 70 | import android.app.ActivityManager; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 71 | import android.content.Context; |
| 72 | import android.content.pm.ApplicationInfo; |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 73 | import android.net.DhcpInfo; |
lesl | d26a9f7 | 2020-01-03 17:50:06 +0800 | [diff] [blame] | 74 | import android.net.MacAddress; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 75 | import android.net.wifi.WifiManager.LocalOnlyHotspotCallback; |
| 76 | import android.net.wifi.WifiManager.LocalOnlyHotspotObserver; |
| 77 | import android.net.wifi.WifiManager.LocalOnlyHotspotReservation; |
| 78 | import android.net.wifi.WifiManager.LocalOnlyHotspotSubscription; |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 79 | import android.net.wifi.WifiManager.NetworkRequestMatchCallback; |
| 80 | import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback; |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 81 | import android.net.wifi.WifiManager.OnWifiUsabilityStatsListener; |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 82 | import android.net.wifi.WifiManager.ScanResultsCallback; |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 83 | import android.net.wifi.WifiManager.SoftApCallback; |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 84 | import android.net.wifi.WifiManager.SuggestionConnectionStatusListener; |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 85 | import android.net.wifi.WifiManager.TrafficStateCallback; |
Mingguang Xu | ec6f8ce | 2019-12-20 16:11:07 -0800 | [diff] [blame] | 86 | import android.net.wifi.WifiManager.WifiConnectedNetworkScorer; |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 87 | import android.os.Binder; |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 88 | import android.os.Build; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 89 | import android.os.Handler; |
James Mattis | 80c86d5 | 2019-10-31 14:11:40 -0700 | [diff] [blame] | 90 | import android.os.HandlerExecutor; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 91 | import android.os.IBinder; |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 92 | import android.os.RemoteException; |
David Su | 6f0e416 | 2019-12-02 21:53:33 -0800 | [diff] [blame] | 93 | import android.os.connectivity.WifiActivityEnergyInfo; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 94 | import android.os.test.TestLooper; |
Hai Shalom | e722cbd | 2019-12-04 15:50:07 -0800 | [diff] [blame] | 95 | import android.util.SparseArray; |
KOUSHIK PANUGANTI | c9f79d8 | 2018-12-18 18:43:13 -0800 | [diff] [blame] | 96 | |
| 97 | import androidx.test.filters.SmallTest; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 98 | |
| 99 | import org.junit.Before; |
| 100 | import org.junit.Test; |
| 101 | import org.mockito.ArgumentCaptor; |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 102 | import org.mockito.InOrder; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 103 | import org.mockito.Mock; |
| 104 | import org.mockito.MockitoAnnotations; |
| 105 | |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 106 | import java.util.ArrayList; |
Ecco Park | 05df45d | 2018-12-18 16:13:41 -0800 | [diff] [blame] | 107 | import java.util.HashMap; |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 108 | import java.util.List; |
Ecco Park | 05df45d | 2018-12-18 16:13:41 -0800 | [diff] [blame] | 109 | import java.util.Map; |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 110 | import java.util.Objects; |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 111 | import java.util.concurrent.Executor; |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 112 | |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Unit tests for {@link android.net.wifi.WifiManager}. |
| 115 | */ |
| 116 | @SmallTest |
| 117 | public class WifiManagerTest { |
| 118 | |
| 119 | private static final int ERROR_NOT_SET = -1; |
| 120 | private static final int ERROR_TEST_REASON = 5; |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 121 | private static final int TEST_UID = 14553; |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 122 | private static final int TEST_NETWORK_ID = 143; |
Rebecca Silberstein | 967dfb4 | 2017-05-18 01:27:57 -0700 | [diff] [blame] | 123 | private static final String TEST_PACKAGE_NAME = "TestPackage"; |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 124 | private static final String TEST_FEATURE_ID = "TestFeature"; |
Ningyuan Wang | 557875d | 2017-09-20 14:00:17 -0700 | [diff] [blame] | 125 | private static final String TEST_COUNTRY_CODE = "US"; |
xshu | 9806aa5 | 2018-12-03 18:38:25 -0800 | [diff] [blame] | 126 | private static final String[] TEST_MAC_ADDRESSES = {"da:a1:19:0:0:0"}; |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 127 | private static final int TEST_AP_FREQUENCY = 2412; |
| 128 | private static final int TEST_AP_BANDWIDTH = SoftApInfo.CHANNEL_WIDTH_20MHZ; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 129 | |
| 130 | @Mock Context mContext; |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 131 | @Mock android.net.wifi.IWifiManager mWifiService; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 132 | @Mock ApplicationInfo mApplicationInfo; |
| 133 | @Mock WifiConfiguration mApConfig; |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 134 | @Mock SoftApCallback mSoftApCallback; |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 135 | @Mock TrafficStateCallback mTrafficStateCallback; |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 136 | @Mock NetworkRequestMatchCallback mNetworkRequestMatchCallback; |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 137 | @Mock OnWifiUsabilityStatsListener mOnWifiUsabilityStatsListener; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 138 | @Mock OnWifiActivityEnergyInfoListener mOnWifiActivityEnergyInfoListener; |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 139 | @Mock SuggestionConnectionStatusListener mListener; |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 140 | @Mock Runnable mRunnable; |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 141 | @Mock Executor mExecutor; |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 142 | @Mock Executor mAnotherExecutor; |
David Su | e6efa3f | 2019-11-29 18:02:30 -0800 | [diff] [blame] | 143 | @Mock ActivityManager mActivityManager; |
Mingguang Xu | ec6f8ce | 2019-12-20 16:11:07 -0800 | [diff] [blame] | 144 | @Mock WifiConnectedNetworkScorer mWifiConnectedNetworkScorer; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 145 | |
| 146 | private Handler mHandler; |
| 147 | private TestLooper mLooper; |
| 148 | private WifiManager mWifiManager; |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 149 | private WifiNetworkSuggestion mWifiNetworkSuggestion; |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 150 | private ScanResultsCallback mScanResultsCallback; |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 151 | private WifiActivityEnergyInfo mWifiActivityEnergyInfo; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 152 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 153 | /** |
| 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 Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 197 | @Before |
| 198 | public void setUp() throws Exception { |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 199 | MockitoAnnotations.initMocks(this); |
| 200 | mLooper = new TestLooper(); |
| 201 | mHandler = spy(new Handler(mLooper.getLooper())); |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 202 | mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 203 | when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); |
Rebecca Silberstein | 967dfb4 | 2017-05-18 01:27:57 -0700 | [diff] [blame] | 204 | when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 205 | mWifiManager = new WifiManager(mContext, mWifiService, mLooper.getLooper()); |
Roshan Pius | 045d380 | 2018-07-18 13:18:55 -0700 | [diff] [blame] | 206 | verify(mWifiService).getVerboseLoggingLevel(); |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 207 | mWifiNetworkSuggestion = new WifiNetworkSuggestion(); |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 208 | mScanResultsCallback = new ScanResultsCallback() { |
| 209 | @Override |
| 210 | public void onScanResultsAvailable() { |
| 211 | mRunnable.run(); |
| 212 | } |
| 213 | }; |
Roshan Pius | 26ef682 | 2019-12-02 12:48:29 -0800 | [diff] [blame] | 214 | mWifiActivityEnergyInfo = new WifiActivityEnergyInfo(0, 0, 0, 0, 0, 0); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 215 | } |
| 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 | /** |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 256 | * 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 { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 261 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
| 262 | when(mWifiService.startTetheredHotspot(eq(softApConfig))).thenReturn(true); |
| 263 | assertTrue(mWifiManager.startTetheredHotspot(softApConfig)); |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 264 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 265 | when(mWifiService.startTetheredHotspot(eq(softApConfig))).thenReturn(false); |
| 266 | assertFalse(mWifiManager.startTetheredHotspot(softApConfig)); |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 267 | } |
| 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 Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 283 | * Test creation of a LocalOnlyHotspotReservation and verify that close properly calls |
| 284 | * WifiService.stopLocalOnlyHotspot. |
| 285 | */ |
| 286 | @Test |
| 287 | public void testCreationAndCloseOfLocalOnlyHotspotReservation() throws Exception { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 288 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 289 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 290 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 291 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 292 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 293 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 294 | callback.onStarted(mWifiManager.new LocalOnlyHotspotReservation(softApConfig)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 295 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 296 | assertEquals(softApConfig, callback.mRes.getSoftApConfiguration()); |
| 297 | WifiConfiguration wifiConfig = callback.mRes.getWifiConfiguration(); |
| 298 | assertTrue(compareWifiAndSoftApConfiguration(softApConfig, wifiConfig)); |
| 299 | |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 300 | callback.mRes.close(); |
Peter Visontay | ca835c1 | 2017-11-21 11:46:59 +0000 | [diff] [blame] | 301 | verify(mWifiService).stopLocalOnlyHotspot(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Verify stopLOHS is called when try-with-resources is used properly. |
| 306 | */ |
| 307 | @Test |
| 308 | public void testLocalOnlyHotspotReservationCallsStopProperlyInTryWithResources() |
| 309 | throws Exception { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 310 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 311 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 312 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 313 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 314 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 315 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 316 | callback.onStarted(mWifiManager.new LocalOnlyHotspotReservation(softApConfig)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 317 | |
| 318 | try (WifiManager.LocalOnlyHotspotReservation res = callback.mRes) { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 319 | assertEquals(softApConfig, res.getSoftApConfiguration()); |
| 320 | WifiConfiguration wifiConfig = callback.mRes.getWifiConfiguration(); |
| 321 | assertTrue(compareWifiAndSoftApConfiguration(softApConfig, wifiConfig)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Peter Visontay | ca835c1 | 2017-11-21 11:46:59 +0000 | [diff] [blame] | 324 | verify(mWifiService).stopLocalOnlyHotspot(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 325 | } |
| 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 Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 334 | mWifiManager.new LocalOnlyHotspotSubscription()) { |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 335 | 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() { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 371 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 372 | 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 = |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 380 | mWifiManager.new LocalOnlyHotspotReservation(softApConfig); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 381 | 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; |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 406 | public SoftApConfiguration mConfig = null; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 407 | public LocalOnlyHotspotSubscription mSub = null; |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 408 | public long mCallingThreadId = -1; |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 409 | |
| 410 | @Override |
| 411 | public void onRegistered(LocalOnlyHotspotSubscription sub) { |
| 412 | mOnRegistered = true; |
| 413 | mSub = sub; |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 414 | mCallingThreadId = Thread.currentThread().getId(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 418 | public void onStarted(SoftApConfiguration config) { |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 419 | mOnStartedCalled = true; |
| 420 | mConfig = config; |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 421 | mCallingThreadId = Thread.currentThread().getId(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onStopped() { |
| 426 | mOnStoppedCalled = true; |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 427 | mCallingThreadId = Thread.currentThread().getId(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Verify observer is properly plumbed when called. |
| 433 | */ |
| 434 | @Test |
| 435 | public void testLocalOnlyHotspotObserver() { |
| 436 | TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver(); |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 437 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 438 | 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 | |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 453 | observer.onStarted(softApConfig); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 454 | assertTrue(observer.mOnRegistered); |
| 455 | assertTrue(observer.mOnStartedCalled); |
| 456 | assertFalse(observer.mOnStoppedCalled); |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 457 | assertEquals(softApConfig, observer.mConfig); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 458 | assertEquals(sub, observer.mSub); |
| 459 | |
| 460 | observer.onStopped(); |
| 461 | assertTrue(observer.mOnRegistered); |
| 462 | assertTrue(observer.mOnStartedCalled); |
| 463 | assertTrue(observer.mOnStoppedCalled); |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 464 | assertEquals(softApConfig, observer.mConfig); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 465 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 476 | verify(mWifiService).startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), |
| 477 | anyString(), nullable(String.class), eq(null)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 478 | } |
| 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 487 | doThrow(new SecurityException()).when(mWifiService).startLocalOnlyHotspot( |
| 488 | any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class), |
| 489 | eq(null)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 490 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 500 | doThrow(new IllegalStateException()).when(mWifiService).startLocalOnlyHotspot( |
| 501 | any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class), |
| 502 | eq(null)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 503 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 504 | } |
| 505 | |
| 506 | /** |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 507 | * Verify that the handler provided by the caller is used for the callbacks. |
| 508 | */ |
| 509 | @Test |
| 510 | public void testCorrectLooperIsUsedForHandler() throws Exception { |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 511 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 512 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 513 | nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 514 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 515 | mLooper.dispatchAll(); |
| 516 | assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason); |
Rebecca Silberstein | 695cea5 | 2017-05-02 13:21:00 -0700 | [diff] [blame] | 517 | verify(mContext, never()).getMainLooper(); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 518 | verify(mContext, never()).getMainExecutor(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 519 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 529 | when(mContext.getMainExecutor()).thenReturn(altLooper.getNewExecutor()); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 530 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 531 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 532 | nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 533 | mWifiManager.startLocalOnlyHotspot(callback, null); |
| 534 | altLooper.dispatchAll(); |
| 535 | assertEquals(ERROR_INCOMPATIBLE_MODE, callback.mFailureReason); |
| 536 | assertEquals(altLooper.getLooper().getThread().getId(), callback.mCallingThreadId); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 537 | verify(mContext).getMainExecutor(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 538 | } |
| 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 { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 546 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 547 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
| 548 | TestLooper callbackLooper = new TestLooper(); |
| 549 | Handler callbackHandler = new Handler(callbackLooper.getLooper()); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 550 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 551 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 552 | when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(), |
| 553 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 554 | mWifiManager.startLocalOnlyHotspot(callback, callbackHandler); |
| 555 | callbackLooper.dispatchAll(); |
| 556 | mLooper.dispatchAll(); |
| 557 | assertFalse(callback.mOnStartedCalled); |
| 558 | assertEquals(null, callback.mRes); |
| 559 | // now trigger the callback |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 560 | internalCallback.getValue().onHotspotStarted(softApConfig); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 561 | mLooper.dispatchAll(); |
| 562 | callbackLooper.dispatchAll(); |
| 563 | assertTrue(callback.mOnStartedCalled); |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 564 | 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 Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 598 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 609 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 610 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 611 | when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(), |
| 612 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 613 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 619 | internalCallback.getValue().onHotspotStarted(null); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 620 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 634 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 635 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 636 | when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(), |
| 637 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 638 | mWifiManager.startLocalOnlyHotspot(callback, callbackHandler); |
| 639 | callbackLooper.dispatchAll(); |
| 640 | mLooper.dispatchAll(); |
| 641 | assertFalse(callback.mOnStoppedCalled); |
| 642 | // now trigger the callback |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 643 | internalCallback.getValue().onHotspotStopped(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 644 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 657 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 658 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 659 | when(mWifiService.startLocalOnlyHotspot(internalCallback.capture(), anyString(), |
| 660 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 661 | mWifiManager.startLocalOnlyHotspot(callback, callbackHandler); |
| 662 | callbackLooper.dispatchAll(); |
| 663 | mLooper.dispatchAll(); |
| 664 | assertEquals(ERROR_NOT_SET, callback.mFailureReason); |
| 665 | // now trigger the callback |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 666 | internalCallback.getValue().onHotspotFailed(ERROR_NO_CHANNEL); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 667 | mLooper.dispatchAll(); |
| 668 | callbackLooper.dispatchAll(); |
| 669 | assertEquals(ERROR_NO_CHANNEL, callback.mFailureReason); |
| 670 | } |
| 671 | |
| 672 | /** |
Rebecca Silberstein | 23d79bf | 2017-05-02 11:22:45 -0700 | [diff] [blame] | 673 | * Verify callback triggered from startLocalOnlyHotspot with an incompatible mode failure. |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 674 | */ |
| 675 | @Test |
Rebecca Silberstein | 23d79bf | 2017-05-02 11:22:45 -0700 | [diff] [blame] | 676 | public void testLocalOnlyHotspotCallbackFullOnIncompatibleMode() throws Exception { |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 677 | TestLocalOnlyHotspotCallback callback = new TestLocalOnlyHotspotCallback(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 678 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 679 | nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 680 | 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 Silberstein | 23d79bf | 2017-05-02 11:22:45 -0700 | [diff] [blame] | 689 | * 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 694 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 695 | nullable(String.class), eq(null))).thenReturn(ERROR_TETHERING_DISALLOWED); |
Rebecca Silberstein | 23d79bf | 2017-05-02 11:22:45 -0700 | [diff] [blame] | 696 | 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 Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 705 | * 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 711 | doThrow(new SecurityException()).when(mWifiService).startLocalOnlyHotspot( |
| 712 | any(ILocalOnlyHotspotCallback.class), anyString(), nullable(String.class), |
| 713 | eq(null)); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 714 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 733 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 734 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 735 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 749 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 750 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 751 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 752 | mWifiManager.cancelLocalOnlyHotspotRequest(); |
Peter Visontay | ca835c1 | 2017-11-21 11:46:59 +0000 | [diff] [blame] | 753 | verify(mWifiService).stopLocalOnlyHotspot(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 754 | } |
| 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 771 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 772 | nullable(String.class), eq(null))).thenReturn(REQUEST_REGISTERED); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 773 | mWifiManager.startLocalOnlyHotspot(callback, mHandler); |
| 774 | mWifiManager.cancelLocalOnlyHotspotRequest(); |
Peter Visontay | ca835c1 | 2017-11-21 11:46:59 +0000 | [diff] [blame] | 775 | verify(mWifiService).stopLocalOnlyHotspot(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 776 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 790 | when(mWifiService.startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), anyString(), |
| 791 | nullable(String.class), eq(null))).thenReturn(ERROR_INCOMPATIBLE_MODE); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 792 | 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 Visontay | ca835c1 | 2017-11-21 11:46:59 +0000 | [diff] [blame] | 799 | verify(mWifiService, never()).stopLocalOnlyHotspot(); |
Rebecca Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 800 | } |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 801 | |
Patrik Fimml | c7c962f | 2019-09-10 15:35:09 +0200 | [diff] [blame] | 802 | @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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 809 | verify(mWifiService).startLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class), |
| 810 | anyString(), nullable(String.class), eq(customConfig)); |
Patrik Fimml | c7c962f | 2019-09-10 15:35:09 +0200 | [diff] [blame] | 811 | } |
| 812 | |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 813 | /** |
| 814 | * Verify the watchLocalOnlyHotspot call goes to WifiServiceImpl. |
| 815 | */ |
Andreas Gampe | 35dbf35 | 2018-01-26 20:41:17 -0800 | [diff] [blame] | 816 | @Test |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 817 | public void testWatchLocalOnlyHotspot() throws Exception { |
| 818 | TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver(); |
| 819 | |
| 820 | mWifiManager.watchLocalOnlyHotspot(observer, mHandler); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 821 | verify(mWifiService).startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class)); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 822 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 832 | .startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class)); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 833 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 844 | .startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class)); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 845 | mWifiManager.watchLocalOnlyHotspot(observer, mHandler); |
| 846 | } |
| 847 | |
| 848 | /** |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 849 | * Verify an IllegalArgumentException is thrown if callback is not provided. |
| 850 | */ |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 851 | @Test |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 852 | public void registerSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() { |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 853 | try { |
James Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 854 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), null); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 855 | fail("expected IllegalArgumentException"); |
| 856 | } catch (IllegalArgumentException expected) { |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /** |
David Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 861 | * 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 Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 873 | * 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 Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /** |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 885 | * Verify the call to registerSoftApCallback goes to WifiServiceImpl. |
| 886 | */ |
| 887 | @Test |
| 888 | public void registerSoftApCallbackCallGoesToWifiServiceImpl() throws Exception { |
James Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 889 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 890 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 891 | 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 Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 900 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 901 | 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 Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 909 | * Verify client-provided callback is being called through callback proxy |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 910 | */ |
| 911 | @Test |
| 912 | public void softApCallbackProxyCallsOnStateChanged() throws Exception { |
| 913 | ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor = |
| 914 | ArgumentCaptor.forClass(ISoftApCallback.Stub.class); |
James Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 915 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 916 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(), |
| 917 | anyInt()); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 918 | |
| 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 Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 925 | * Verify client-provided callback is being called through callback proxy |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 926 | */ |
| 927 | @Test |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 928 | public void softApCallbackProxyCallsOnConnectedClientsChanged() throws Exception { |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 929 | ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor = |
| 930 | ArgumentCaptor.forClass(ISoftApCallback.Stub.class); |
James Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 931 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 932 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(), |
| 933 | anyInt()); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 934 | |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 935 | final List<WifiClient> testClients = new ArrayList(); |
| 936 | callbackCaptor.getValue().onConnectedClientsChanged(testClients); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 937 | mLooper.dispatchAll(); |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 938 | verify(mSoftApCallback).onConnectedClientsChanged(testClients); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 939 | } |
| 940 | |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 941 | |
| 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 Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 952 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 953 | 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 | |
lesl | e14a090 | 2019-12-12 17:16:30 +0800 | [diff] [blame] | 961 | |
| 962 | /* |
| 963 | * Verify client-provided callback is being called through callback proxy |
| 964 | */ |
| 965 | @Test |
| 966 | public void softApCallbackProxyCallsOnCapabilityChanged() throws Exception { |
lesl | 492e4bd | 2019-12-24 00:08:38 +0800 | [diff] [blame] | 967 | SoftApCapability testSoftApCapability = new SoftApCapability(0); |
lesl | e14a090 | 2019-12-12 17:16:30 +0800 | [diff] [blame] | 968 | 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 Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 980 | /* |
lesl | d26a9f7 | 2020-01-03 17:50:06 +0800 | [diff] [blame] | 981 | * 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 Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1000 | * Verify client-provided callback is being called through callback proxy on multiple events |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1001 | */ |
| 1002 | @Test |
| 1003 | public void softApCallbackProxyCallsOnMultipleUpdates() throws Exception { |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 1004 | SoftApInfo testSoftApInfo = new SoftApInfo(); |
| 1005 | testSoftApInfo.setFrequency(TEST_AP_FREQUENCY); |
| 1006 | testSoftApInfo.setBandwidth(TEST_AP_BANDWIDTH); |
lesl | 492e4bd | 2019-12-24 00:08:38 +0800 | [diff] [blame] | 1007 | SoftApCapability testSoftApCapability = new SoftApCapability(0); |
lesl | e14a090 | 2019-12-12 17:16:30 +0800 | [diff] [blame] | 1008 | testSoftApCapability.setMaxSupportedClients(10); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1009 | ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor = |
| 1010 | ArgumentCaptor.forClass(ISoftApCallback.Stub.class); |
James Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 1011 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 1012 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(), |
| 1013 | anyInt()); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1014 | |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 1015 | final List<WifiClient> testClients = new ArrayList(); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1016 | callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLING, 0); |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 1017 | callbackCaptor.getValue().onConnectedClientsChanged(testClients); |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 1018 | callbackCaptor.getValue().onInfoChanged(testSoftApInfo); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1019 | callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL); |
lesl | e14a090 | 2019-12-12 17:16:30 +0800 | [diff] [blame] | 1020 | callbackCaptor.getValue().onCapabilityChanged(testSoftApCapability); |
| 1021 | |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1022 | |
| 1023 | mLooper.dispatchAll(); |
| 1024 | verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLING, 0); |
James Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 1025 | verify(mSoftApCallback).onConnectedClientsChanged(testClients); |
lesl | 3fd4b64 | 2019-10-23 10:47:34 +0800 | [diff] [blame] | 1026 | verify(mSoftApCallback).onInfoChanged(testSoftApInfo); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1027 | verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL); |
lesl | e14a090 | 2019-12-12 17:16:30 +0800 | [diff] [blame] | 1028 | verify(mSoftApCallback).onCapabilityChanged(testSoftApCapability); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1032 | * Verify client-provided callback is being called on the correct thread |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1033 | */ |
| 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 Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 1040 | mWifiManager.registerSoftApCallback(new HandlerExecutor(altHandler), mSoftApCallback); |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 1041 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture(), |
| 1042 | anyInt()); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1043 | |
| 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 Mattis | cb157e8 | 2019-11-12 17:43:48 -0800 | [diff] [blame] | 1054 | mWifiManager.registerSoftApCallback(new HandlerExecutor(mHandler), mSoftApCallback); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1055 | mLooper.dispatchAll(); |
| 1056 | verify(mWifiService).registerSoftApCallback(any(IBinder.class), |
Mehdi Alizadeh | 55f3aae | 2018-01-11 17:56:30 -0800 | [diff] [blame] | 1057 | any(ISoftApCallback.Stub.class), anyInt()); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1058 | verify(mContext, never()).getMainLooper(); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1059 | verify(mContext, never()).getMainExecutor(); |
Mehdi Alizadeh | e59b937 | 2017-12-22 17:00:22 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1063 | * Verify that the handler provided by the caller is used for the observer. |
| 1064 | */ |
| 1065 | @Test |
| 1066 | public void testCorrectLooperIsUsedForObserverHandler() throws Exception { |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1067 | TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver(); |
| 1068 | mWifiManager.watchLocalOnlyHotspot(observer, mHandler); |
| 1069 | mLooper.dispatchAll(); |
| 1070 | assertTrue(observer.mOnRegistered); |
Rebecca Silberstein | 695cea5 | 2017-05-02 13:21:00 -0700 | [diff] [blame] | 1071 | verify(mContext, never()).getMainLooper(); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1072 | verify(mContext, never()).getMainExecutor(); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1073 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1083 | when(mContext.getMainExecutor()).thenReturn(altLooper.getNewExecutor()); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1084 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1089 | verify(mContext).getMainExecutor(); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1090 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1104 | verify(mWifiService).startWatchLocalOnlyHotspot(any(ILocalOnlyHotspotCallback.class)); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1105 | // 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 { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1118 | SoftApConfiguration softApConfig = generatorTestSoftApConfig(); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1119 | TestLocalOnlyHotspotObserver observer = new TestLocalOnlyHotspotObserver(); |
| 1120 | TestLooper observerLooper = new TestLooper(); |
| 1121 | Handler observerHandler = new Handler(observerLooper.getLooper()); |
| 1122 | mWifiManager.watchLocalOnlyHotspot(observer, observerHandler); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1123 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 1124 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
| 1125 | verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture()); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1126 | observerLooper.dispatchAll(); |
| 1127 | mLooper.dispatchAll(); |
| 1128 | assertFalse(observer.mOnStartedCalled); |
| 1129 | // now trigger the callback |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1130 | internalCallback.getValue().onHotspotStarted(softApConfig); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1131 | mLooper.dispatchAll(); |
| 1132 | observerLooper.dispatchAll(); |
| 1133 | assertTrue(observer.mOnStartedCalled); |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1134 | assertEquals(softApConfig, observer.mConfig); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1135 | } |
| 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1147 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 1148 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
| 1149 | verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture()); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1150 | observerLooper.dispatchAll(); |
| 1151 | mLooper.dispatchAll(); |
| 1152 | assertFalse(observer.mOnStartedCalled); |
| 1153 | // now trigger the callback |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1154 | internalCallback.getValue().onHotspotStarted(null); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1155 | 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 Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1172 | ArgumentCaptor<ILocalOnlyHotspotCallback> internalCallback = |
| 1173 | ArgumentCaptor.forClass(ILocalOnlyHotspotCallback.class); |
| 1174 | verify(mWifiService).startWatchLocalOnlyHotspot(internalCallback.capture()); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1175 | observerLooper.dispatchAll(); |
| 1176 | mLooper.dispatchAll(); |
| 1177 | assertFalse(observer.mOnStoppedCalled); |
| 1178 | // now trigger the callback |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1179 | internalCallback.getValue().onHotspotStopped(); |
Rebecca Silberstein | 4871da6 | 2017-04-27 13:57:19 -0700 | [diff] [blame] | 1180 | 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 Wang | 557875d | 2017-09-20 14:00:17 -0700 | [diff] [blame] | 1206 | |
| 1207 | /** |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 1208 | * 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1218 | class WpsCallbackTester extends WpsCallback { |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 1219 | 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1250 | assertEquals(ERROR, wpsCallback.mFailureCode); |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 1251 | 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 Mattis | aefe78c | 2019-10-04 17:46:12 -0700 | [diff] [blame] | 1265 | /** |
| 1266 | * Verify that a call to cancel WPS immediately returns a failure. |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 1267 | */ |
| 1268 | @Test |
| 1269 | public void testCancelWpsImmediatelyFailsWithCallback() { |
| 1270 | WpsCallbackTester wpsCallback = new WpsCallbackTester(); |
| 1271 | mWifiManager.cancelWps(wpsCallback); |
| 1272 | assertTrue(wpsCallback.mFailed); |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1273 | assertEquals(ERROR, wpsCallback.mFailureCode); |
Rebecca Silberstein | 6a278404 | 2018-01-19 17:45:26 -0800 | [diff] [blame] | 1274 | 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 Silberstein | cf1b40f | 2018-03-26 23:51:07 -0700 | [diff] [blame] | 1288 | /** |
| 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 Pius | c705615 | 2018-04-26 23:28:35 +0000 | [diff] [blame] | 1324 | |
| 1325 | /** |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 1326 | * Verify that a successful call properly returns true. |
| 1327 | */ |
| 1328 | @Test |
| 1329 | public void testSetSoftApConfigurationSuccessReturnsTrue() throws Exception { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1330 | SoftApConfiguration apConfig = generatorTestSoftApConfig(); |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 1331 | |
| 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 { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1342 | SoftApConfiguration apConfig = generatorTestSoftApConfig(); |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 1343 | |
| 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 { |
lesl | 115873a | 2020-01-15 14:51:26 +0800 | [diff] [blame] | 1357 | mWifiManager.setSoftApConfiguration(generatorTestSoftApConfig()); |
lesl | 692d375 | 2019-11-27 16:17:47 +0800 | [diff] [blame] | 1358 | fail("setWifiApConfiguration should rethrow Exceptions from WifiService"); |
| 1359 | } catch (SecurityException e) { } |
| 1360 | } |
| 1361 | |
| 1362 | /** |
Roshan Pius | c705615 | 2018-04-26 23:28:35 +0000 | [diff] [blame] | 1363 | * Check the call to startScan calls WifiService. |
| 1364 | */ |
| 1365 | @Test |
| 1366 | public void testStartScan() throws Exception { |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1367 | when(mWifiService.startScan(eq(TEST_PACKAGE_NAME), nullable(String.class))).thenReturn( |
| 1368 | true); |
Roshan Pius | c705615 | 2018-04-26 23:28:35 +0000 | [diff] [blame] | 1369 | assertTrue(mWifiManager.startScan()); |
| 1370 | |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1371 | when(mWifiService.startScan(eq(TEST_PACKAGE_NAME), nullable(String.class))).thenReturn( |
| 1372 | false); |
Roshan Pius | c705615 | 2018-04-26 23:28:35 +0000 | [diff] [blame] | 1373 | assertFalse(mWifiManager.startScan()); |
| 1374 | } |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1375 | |
| 1376 | /** |
| 1377 | * Verify main looper is used when handler is not provided. |
| 1378 | */ |
| 1379 | @Test |
| 1380 | public void registerTrafficStateCallbackUsesMainLooperOnNullArgumentForHandler() |
| 1381 | throws Exception { |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1382 | ArgumentCaptor<ITrafficStateCallback.Stub> callbackCaptor = |
| 1383 | ArgumentCaptor.forClass(ITrafficStateCallback.Stub.class); |
David Su | 09daba1 | 2020-01-23 12:47:24 -0800 | [diff] [blame] | 1384 | mWifiManager.registerTrafficStateCallback( |
| 1385 | new HandlerExecutor(new Handler(mLooper.getLooper())), mTrafficStateCallback); |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1386 | 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 Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 1401 | mWifiManager.registerTrafficStateCallback(new HandlerExecutor(mHandler), |
| 1402 | mTrafficStateCallback); |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1403 | 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 Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 1418 | mWifiManager.registerTrafficStateCallback(new HandlerExecutor(mHandler), |
| 1419 | mTrafficStateCallback); |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1420 | 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 Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 1438 | /** |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1439 | * 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 Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 1447 | mWifiManager.registerTrafficStateCallback(new HandlerExecutor(altHandler), |
| 1448 | mTrafficStateCallback); |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1449 | verify(mContext, never()).getMainLooper(); |
Patrik Fimml | 090ea136 | 2019-09-18 14:37:27 +0200 | [diff] [blame] | 1450 | verify(mContext, never()).getMainExecutor(); |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 1451 | 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 Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1459 | |
| 1460 | /** |
| 1461 | * Verify the call to registerNetworkRequestMatchCallback goes to WifiServiceImpl. |
| 1462 | */ |
| 1463 | @Test |
| 1464 | public void registerNetworkRequestMatchCallbackCallGoesToWifiServiceImpl() |
| 1465 | throws Exception { |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1466 | ArgumentCaptor<INetworkRequestMatchCallback.Stub> callbackCaptor = |
| 1467 | ArgumentCaptor.forClass(INetworkRequestMatchCallback.Stub.class); |
David Su | 09daba1 | 2020-01-23 12:47:24 -0800 | [diff] [blame] | 1468 | mWifiManager.registerNetworkRequestMatchCallback( |
| 1469 | new HandlerExecutor(new Handler(mLooper.getLooper())), |
| 1470 | mNetworkRequestMatchCallback); |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1471 | 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 Pius | 7d18334 | 2018-11-13 15:34:05 -0800 | [diff] [blame] | 1478 | |
| 1479 | callbackCaptor.getValue().onAbort(); |
| 1480 | assertEquals(1, mLooper.dispatchAll()); |
| 1481 | verify(mNetworkRequestMatchCallback).onAbort(); |
| 1482 | |
Roshan Pius | dc69f46 | 2018-11-02 13:21:07 -0700 | [diff] [blame] | 1483 | callbackCaptor.getValue().onMatch(new ArrayList<ScanResult>()); |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1484 | 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 Su | f7ef955 | 2019-11-05 13:44:12 -0800 | [diff] [blame] | 1504 | mWifiManager.registerNetworkRequestMatchCallback(new HandlerExecutor(mHandler), |
| 1505 | mNetworkRequestMatchCallback); |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1506 | 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 Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1522 | ArgumentCaptor<INetworkRequestMatchCallback.Stub> callbackCaptor = |
| 1523 | ArgumentCaptor.forClass(INetworkRequestMatchCallback.Stub.class); |
David Su | 09daba1 | 2020-01-23 12:47:24 -0800 | [diff] [blame] | 1524 | mWifiManager.registerNetworkRequestMatchCallback( |
| 1525 | new HandlerExecutor(new Handler(mLooper.getLooper())), |
| 1526 | mNetworkRequestMatchCallback); |
Roshan Pius | 3c38ee4 | 2018-10-09 10:06:33 -0700 | [diff] [blame] | 1527 | 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 Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1547 | |
| 1548 | /** |
Ecco Park | 05df45d | 2018-12-18 16:13:41 -0800 | [diff] [blame] | 1549 | * Check the call to getAllMatchingWifiConfigs calls getAllMatchingFqdnsForScanResults and |
| 1550 | * getWifiConfigsForPasspointProfiles of WifiService in order. |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1551 | */ |
| 1552 | @Test |
| 1553 | public void testGetAllMatchingWifiConfigs() throws Exception { |
Ecco Park | 05df45d | 2018-12-18 16:13:41 -0800 | [diff] [blame] | 1554 | 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 Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1559 | mWifiManager.getAllMatchingWifiConfigs(new ArrayList<>()); |
| 1560 | |
Ecco Park | 05df45d | 2018-12-18 16:13:41 -0800 | [diff] [blame] | 1561 | inOrder.verify(mWifiService).getAllMatchingFqdnsForScanResults(any(List.class)); |
| 1562 | inOrder.verify(mWifiService).getWifiConfigsForPasspointProfiles(any(List.class)); |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * Check the call to getMatchingOsuProviders calls getMatchingOsuProviders of WifiService |
Ecco Park | 8561aa3 | 2018-11-15 21:18:47 -0800 | [diff] [blame] | 1567 | * with the provided a list of ScanResult. |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1568 | */ |
| 1569 | @Test |
| 1570 | public void testGetMatchingOsuProviders() throws Exception { |
Ecco Park | 8561aa3 | 2018-11-15 21:18:47 -0800 | [diff] [blame] | 1571 | mWifiManager.getMatchingOsuProviders(new ArrayList<>()); |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1572 | |
Ecco Park | 8561aa3 | 2018-11-15 21:18:47 -0800 | [diff] [blame] | 1573 | verify(mWifiService).getMatchingOsuProviders(any(List.class)); |
Ecco Park | f035a04 | 2018-11-13 16:37:05 -0800 | [diff] [blame] | 1574 | } |
Roshan Pius | 98b14ec | 2018-11-02 09:41:38 -0700 | [diff] [blame] | 1575 | |
| 1576 | /** |
Nate Jiang | f6b731a | 2019-07-25 11:43:26 -0700 | [diff] [blame] | 1577 | * Verify calls to {@link WifiManager#addNetworkSuggestions(List)}, |
| 1578 | * {@link WifiManager#getNetworkSuggestions()} and |
Roshan Pius | 98b14ec | 2018-11-02 09:41:38 -0700 | [diff] [blame] | 1579 | * {@link WifiManager#removeNetworkSuggestions(List)}. |
| 1580 | */ |
| 1581 | @Test |
Nate Jiang | f6b731a | 2019-07-25 11:43:26 -0700 | [diff] [blame] | 1582 | public void addGetRemoveNetworkSuggestions() throws Exception { |
| 1583 | List<WifiNetworkSuggestion> testList = new ArrayList<>(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1584 | when(mWifiService.addNetworkSuggestions(any(List.class), anyString(), |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1585 | nullable(String.class))).thenReturn(STATUS_NETWORK_SUGGESTIONS_SUCCESS); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1586 | when(mWifiService.removeNetworkSuggestions(any(List.class), anyString())).thenReturn( |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1587 | STATUS_NETWORK_SUGGESTIONS_SUCCESS); |
Nate Jiang | f6b731a | 2019-07-25 11:43:26 -0700 | [diff] [blame] | 1588 | when(mWifiService.getNetworkSuggestions(anyString())) |
| 1589 | .thenReturn(testList); |
Roshan Pius | 98b14ec | 2018-11-02 09:41:38 -0700 | [diff] [blame] | 1590 | |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1591 | assertEquals(STATUS_NETWORK_SUGGESTIONS_SUCCESS, |
Nate Jiang | f6b731a | 2019-07-25 11:43:26 -0700 | [diff] [blame] | 1592 | mWifiManager.addNetworkSuggestions(testList)); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1593 | verify(mWifiService).addNetworkSuggestions(anyList(), eq(TEST_PACKAGE_NAME), |
| 1594 | nullable(String.class)); |
Roshan Pius | 98b14ec | 2018-11-02 09:41:38 -0700 | [diff] [blame] | 1595 | |
Nate Jiang | f6b731a | 2019-07-25 11:43:26 -0700 | [diff] [blame] | 1596 | assertEquals(testList, mWifiManager.getNetworkSuggestions()); |
| 1597 | verify(mWifiService).getNetworkSuggestions(eq(TEST_PACKAGE_NAME)); |
| 1598 | |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1599 | assertEquals(STATUS_NETWORK_SUGGESTIONS_SUCCESS, |
Roshan Pius | d1a4a56 | 2018-11-20 09:01:09 -0800 | [diff] [blame] | 1600 | mWifiManager.removeNetworkSuggestions(new ArrayList<>())); |
Roshan Pius | 98b14ec | 2018-11-02 09:41:38 -0700 | [diff] [blame] | 1601 | verify(mWifiService).removeNetworkSuggestions(anyList(), eq(TEST_PACKAGE_NAME)); |
| 1602 | } |
Roshan Pius | d1a4a56 | 2018-11-20 09:01:09 -0800 | [diff] [blame] | 1603 | |
| 1604 | /** |
| 1605 | * Verify call to {@link WifiManager#getMaxNumberOfNetworkSuggestionsPerApp()}. |
| 1606 | */ |
| 1607 | @Test |
| 1608 | public void getMaxNumberOfNetworkSuggestionsPerApp() { |
David Su | e6efa3f | 2019-11-29 18:02:30 -0800 | [diff] [blame] | 1609 | 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 Pius | d1a4a56 | 2018-11-20 09:01:09 -0800 | [diff] [blame] | 1618 | } |
xshu | 9806aa5 | 2018-12-03 18:38:25 -0800 | [diff] [blame] | 1619 | |
| 1620 | /** |
| 1621 | * Verify getting the factory MAC address. |
xshu | 9806aa5 | 2018-12-03 18:38:25 -0800 | [diff] [blame] | 1622 | */ |
| 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 Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1629 | |
| 1630 | /** |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1631 | * Verify the call to addOnWifiUsabilityStatsListener goes to WifiServiceImpl. |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1632 | */ |
| 1633 | @Test |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1634 | public void addOnWifiUsabilityStatsListeneroesToWifiServiceImpl() throws Exception { |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1635 | mExecutor = new SynchronousExecutor(); |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1636 | mWifiManager.addOnWifiUsabilityStatsListener(mExecutor, mOnWifiUsabilityStatsListener); |
| 1637 | verify(mWifiService).addOnWifiUsabilityStatsListener(any(IBinder.class), |
| 1638 | any(IOnWifiUsabilityStatsListener.Stub.class), anyInt()); |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /** |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1642 | * Verify the call to removeOnWifiUsabilityStatsListener goes to WifiServiceImpl. |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1643 | */ |
| 1644 | @Test |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1645 | public void removeOnWifiUsabilityListenerGoesToWifiServiceImpl() throws Exception { |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1646 | ArgumentCaptor<Integer> listenerIdentifier = ArgumentCaptor.forClass(Integer.class); |
| 1647 | mExecutor = new SynchronousExecutor(); |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1648 | mWifiManager.addOnWifiUsabilityStatsListener(mExecutor, mOnWifiUsabilityStatsListener); |
| 1649 | verify(mWifiService).addOnWifiUsabilityStatsListener(any(IBinder.class), |
| 1650 | any(IOnWifiUsabilityStatsListener.Stub.class), listenerIdentifier.capture()); |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1651 | |
Mingguang Xu | e77cabc | 2019-03-08 09:58:23 -0800 | [diff] [blame] | 1652 | mWifiManager.removeOnWifiUsabilityStatsListener(mOnWifiUsabilityStatsListener); |
| 1653 | verify(mWifiService).removeOnWifiUsabilityStatsListener( |
Mingguang Xu | 8683314 | 2019-01-09 13:54:29 -0800 | [diff] [blame] | 1654 | eq((int) listenerIdentifier.getValue())); |
| 1655 | } |
| 1656 | |
| 1657 | /** |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1658 | * Test behavior of isEnhancedOpenSupported |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1659 | */ |
| 1660 | @Test |
| 1661 | public void testIsEnhancedOpenSupported() throws Exception { |
| 1662 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1663 | .thenReturn(new Long(WIFI_FEATURE_OWE)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1664 | assertTrue(mWifiManager.isEnhancedOpenSupported()); |
| 1665 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1666 | .thenReturn(new Long(~WIFI_FEATURE_OWE)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1667 | assertFalse(mWifiManager.isEnhancedOpenSupported()); |
| 1668 | } |
| 1669 | |
| 1670 | /** |
| 1671 | * Test behavior of isWpa3SaeSupported |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1672 | */ |
| 1673 | @Test |
| 1674 | public void testIsWpa3SaeSupported() throws Exception { |
| 1675 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1676 | .thenReturn(new Long(WIFI_FEATURE_WPA3_SAE)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1677 | assertTrue(mWifiManager.isWpa3SaeSupported()); |
| 1678 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1679 | .thenReturn(new Long(~WIFI_FEATURE_WPA3_SAE)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1680 | assertFalse(mWifiManager.isWpa3SaeSupported()); |
| 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * Test behavior of isWpa3SuiteBSupported |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1685 | */ |
| 1686 | @Test |
| 1687 | public void testIsWpa3SuiteBSupported() throws Exception { |
| 1688 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1689 | .thenReturn(new Long(WIFI_FEATURE_WPA3_SUITE_B)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1690 | assertTrue(mWifiManager.isWpa3SuiteBSupported()); |
| 1691 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1692 | .thenReturn(new Long(~WIFI_FEATURE_WPA3_SUITE_B)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1693 | assertFalse(mWifiManager.isWpa3SuiteBSupported()); |
| 1694 | } |
| 1695 | |
| 1696 | /** |
| 1697 | * Test behavior of isEasyConnectSupported |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1698 | */ |
| 1699 | @Test |
| 1700 | public void testIsEasyConnectSupported() throws Exception { |
| 1701 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1702 | .thenReturn(new Long(WIFI_FEATURE_DPP)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1703 | assertTrue(mWifiManager.isEasyConnectSupported()); |
| 1704 | when(mWifiService.getSupportedFeatures()) |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1705 | .thenReturn(new Long(~WIFI_FEATURE_DPP)); |
Hai Shalom | 69d504b | 2019-03-29 09:48:16 -0700 | [diff] [blame] | 1706 | assertFalse(mWifiManager.isEasyConnectSupported()); |
| 1707 | } |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1708 | |
| 1709 | /** |
| 1710 | * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)} |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1711 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1727 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1748 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1759 | */ |
| 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 Luong | 1074029 | 2019-10-14 17:29:46 -0700 | [diff] [blame] | 1769 | * 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 Cohen | b6f262f | 2019-12-27 12:43:21 -0800 | [diff] [blame] | 1775 | verify(mWifiService).allowAutojoin(1, true); |
Quang Luong | 1074029 | 2019-10-14 17:29:46 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /** |
Etan Cohen | b6f262f | 2019-12-27 12:43:21 -0800 | [diff] [blame] | 1779 | * 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 | |
xshu | 380322e | 2020-01-14 17:54:31 -0800 | [diff] [blame] | 1789 | /** |
| 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 Luong | 85f2342 | 2020-01-21 17:09:02 -0800 | [diff] [blame] | 1800 | /** |
| 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 Cohen | b6f262f | 2019-12-27 12:43:21 -0800 | [diff] [blame] | 1810 | |
| 1811 | /** |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1812 | * Test behavior of {@link WifiManager#disconnect()} |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1813 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1823 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1833 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1843 | */ |
| 1844 | @Test |
| 1845 | public void testGetSupportedFeatures() throws Exception { |
| 1846 | long supportedFeatures = |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1847 | WIFI_FEATURE_SCANNER |
| 1848 | | WIFI_FEATURE_PASSPOINT |
| 1849 | | WIFI_FEATURE_P2P; |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1850 | 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1857 | 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1867 | * 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1919 | * Test behavior of {@link WifiManager#getConnectionInfo()} |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1920 | */ |
| 1921 | @Test |
| 1922 | public void testGetConnectionInfo() throws Exception { |
| 1923 | WifiInfo wifiInfo = new WifiInfo(); |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 1924 | when(mWifiService.getConnectionInfo(anyString(), nullable(String.class))).thenReturn( |
| 1925 | wifiInfo); |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1926 | |
| 1927 | assertEquals(wifiInfo, mWifiManager.getConnectionInfo()); |
| 1928 | } |
| 1929 | |
| 1930 | /** |
Ahmed ElArabawy | 9f8d806 | 2019-12-09 16:16:51 -0800 | [diff] [blame] | 1931 | * Test behavior of {@link WifiManager#is5GHzBandSupported()} |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1932 | */ |
| 1933 | @Test |
Ahmed ElArabawy | 9f8d806 | 2019-12-09 16:16:51 -0800 | [diff] [blame] | 1934 | 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** |
Ahmed ElArabawy | 9b3f076 | 2020-01-10 12:28:19 -0800 | [diff] [blame] | 1951 | * 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1962 | * Test behavior of {@link WifiManager#getDhcpInfo()} |
Roshan Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1963 | */ |
| 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 Pius | aca5281 | 2019-04-05 11:35:35 -0700 | [diff] [blame] | 1975 | */ |
| 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 Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 1984 | |
| 1985 | /** |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1986 | * Test behavior of {@link WifiManager#connect(int, ActionListener)} |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 1987 | */ |
| 1988 | @Test |
| 1989 | public void testConnectWithListener() throws Exception { |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 1990 | ActionListener externalListener = mock(ActionListener.class); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 1991 | 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2005 | binderListenerCaptor.getValue().onFailure(BUSY); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2006 | mLooper.dispatchAll(); |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2007 | verify(externalListener).onFailure(BUSY); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /** |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2011 | * Test behavior of {@link WifiManager#connect(int, ActionListener)} |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2012 | */ |
| 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2018 | ActionListener externalListener = mock(ActionListener.class); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2019 | mWifiManager.connect(TEST_NETWORK_ID, externalListener); |
| 2020 | |
| 2021 | mLooper.dispatchAll(); |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2022 | verify(externalListener).onFailure(NOT_AUTHORIZED); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /** |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2026 | * Test behavior of {@link WifiManager#connect(int, ActionListener)} |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2027 | */ |
| 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2033 | ActionListener externalListener = mock(ActionListener.class); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2034 | mWifiManager.connect(TEST_NETWORK_ID, externalListener); |
| 2035 | |
| 2036 | mLooper.dispatchAll(); |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2037 | verify(externalListener).onFailure(ERROR); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /** |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2041 | * Test behavior of {@link WifiManager#connect(int, ActionListener)} |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2042 | */ |
| 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2053 | * Test behavior of {@link WifiManager#getTxPacketCount(TxPacketCountListener)} |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2054 | */ |
| 2055 | @Test |
| 2056 | public void testGetTxPacketCount() throws Exception { |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2057 | TxPacketCountListener externalListener = |
| 2058 | mock(TxPacketCountListener.class); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2059 | 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2073 | binderListenerCaptor.getValue().onFailure(BUSY); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2074 | mLooper.dispatchAll(); |
David Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2075 | verify(externalListener).onFailure(BUSY); |
Roshan Pius | 641a683 | 2019-09-09 10:53:20 -0700 | [diff] [blame] | 2076 | } |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2077 | |
| 2078 | /** |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2079 | * Verify an IllegalArgumentException is thrown if callback is not provided. |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2080 | */ |
| 2081 | @Test(expected = IllegalArgumentException.class) |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2082 | public void testRegisterScanResultsCallbackWithNullCallback() throws Exception { |
| 2083 | mWifiManager.registerScanResultsCallback(mExecutor, null); |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * Verify an IllegalArgumentException is thrown if executor is not provided. |
| 2088 | */ |
| 2089 | @Test(expected = IllegalArgumentException.class) |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2090 | public void testRegisterCallbackWithNullExecutor() throws Exception { |
| 2091 | mWifiManager.registerScanResultsCallback(null, mScanResultsCallback); |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /** |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2095 | * Verify client provided callback is being called to the right callback. |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2096 | */ |
| 2097 | @Test |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2098 | 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) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2103 | callbackCaptor.getValue().onScanResultsAvailable(); |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2104 | verify(mRunnable).run(); |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | /** |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2108 | * Verify client provided callback is being called to the right executor. |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2109 | */ |
| 2110 | @Test |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2111 | 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) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2117 | callbackCaptor.getValue().onScanResultsAvailable(); |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2118 | verify(mExecutor, never()).execute(any(Runnable.class)); |
| 2119 | verify(mAnotherExecutor).execute(any(Runnable.class)); |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /** |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2123 | * Verify client register unregister then register again, to ensure callback still works. |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2124 | */ |
| 2125 | @Test |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2126 | 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) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | /** |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2140 | * 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) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2150 | */ |
| 2151 | @Test(expected = IllegalArgumentException.class) |
Nate(Qiang) Jiang | 10bc5db | 2019-11-20 10:23:23 -0800 | [diff] [blame] | 2152 | public void testUnregisterScanResultsCallbackWithNullCallback() throws Exception { |
| 2153 | mWifiManager.unregisterScanResultsCallback(null); |
Nate(Qiang) Jiang | ee434e8 | 2019-10-09 16:47:39 -0700 | [diff] [blame] | 2154 | } |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2155 | |
| 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2177 | int errorCode = STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION; |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2178 | 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. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 2183 | callbackCaptor.capture(), anyInt(), anyString(), nullable(String.class)); |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2184 | 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 Su | 308e25e5 | 2019-11-29 00:27:46 -0800 | [diff] [blame] | 2193 | int errorCode = STATUS_SUGGESTION_CONNECTION_FAILURE_AUTHENTICATION; |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2194 | ArgumentCaptor<ISuggestionConnectionStatusListener.Stub> callbackCaptor = |
| 2195 | ArgumentCaptor.forClass(ISuggestionConnectionStatusListener.Stub.class); |
| 2196 | mWifiManager.addSuggestionConnectionStatusListener(mExecutor, mListener); |
| 2197 | verify(mWifiService).registerSuggestionConnectionStatusListener(any(IBinder.class), |
Philip P. Moltmann | dcb850c | 2019-10-04 08:14:38 -0700 | [diff] [blame] | 2198 | callbackCaptor.capture(), anyInt(), anyString(), nullable(String.class)); |
Nate(Qiang) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2199 | 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) Jiang | d076cbc | 2019-10-21 13:38:30 -0700 | [diff] [blame] | 2216 | mWifiManager.removeSuggestionConnectionStatusListener(mListener); |
| 2217 | verify(mWifiService).unregisterSuggestionConnectionStatusListener(anyInt(), anyString()); |
| 2218 | } |
David Su | 3c92410 | 2019-11-18 12:49:59 -0800 | [diff] [blame] | 2219 | |
| 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 Chen | 3c21193 | 2019-09-16 15:31:05 +0800 | [diff] [blame] | 2237 | |
| 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 Shalom | e722cbd | 2019-12-04 15:50:07 -0800 | [diff] [blame] | 2251 | |
| 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) Jiang | 7888cc3 | 2019-12-18 11:14:28 -0800 | [diff] [blame] | 2310 | |
| 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 Xu | ec6f8ce | 2019-12-20 16:11:07 -0800 | [diff] [blame] | 2324 | |
| 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 Silberstein | ce74fc2 | 2017-03-25 12:40:29 -0700 | [diff] [blame] | 2383 | } |