1/* 2 * Copyright 2017, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.managedprovisioning.task; 18 19import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 20 21import static com.android.managedprovisioning.task.AddWifiNetworkTask.ADD_NETWORK_FAIL; 22 23import static junit.framework.Assert.assertTrue; 24 25import static org.mockito.ArgumentMatchers.any; 26import static org.mockito.Mockito.verify; 27import static org.mockito.Mockito.when; 28 29import android.content.ComponentName; 30import android.content.Context; 31import android.net.ConnectivityManager; 32import android.net.wifi.WifiConfiguration; 33import android.net.wifi.WifiManager; 34import android.os.Looper; 35import android.support.test.filters.SmallTest; 36 37import com.android.managedprovisioning.common.Utils; 38import com.android.managedprovisioning.model.ProvisioningParams; 39import com.android.managedprovisioning.model.WifiInfo; 40import com.android.managedprovisioning.task.wifi.NetworkMonitor; 41import com.android.managedprovisioning.task.wifi.WifiConfigurationProvider; 42 43import org.junit.Before; 44import org.junit.Test; 45import org.mockito.Mock; 46import org.mockito.MockitoAnnotations; 47 48/** 49 * Unit-tests for {@link AddWifiNetworkTask}. 50 */ 51@SmallTest 52public class AddWifiNetworkTaskTest { 53 private static final int TEST_USER_ID = 123; 54 private static final ComponentName ADMIN = new ComponentName("com.test.admin", ".Receiver"); 55 private static final String TEST_SSID = "TEST_SSID"; 56 private static final String TEST_SSID_2 = "TEST_SSID_2"; 57 private static final ProvisioningParams NO_WIFI_INFO_PARAMS = new ProvisioningParams.Builder() 58 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 59 .setDeviceAdminComponentName(ADMIN) 60 .setWifiInfo(null) 61 .build(); 62 private static final ProvisioningParams WIFI_INFO_PARAMS = new ProvisioningParams.Builder() 63 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 64 .setDeviceAdminComponentName(ADMIN) 65 .setWifiInfo(new WifiInfo.Builder().setSsid(TEST_SSID).build()) 66 .build(); 67 private static final int ADD_NETWORK_OK = 0; 68 69 @Mock private Context mContext; 70 @Mock private ConnectivityManager mConnectivityManager; 71 @Mock private WifiManager mWifiManager; 72 @Mock private NetworkMonitor mNetworkMonitor; 73 @Mock private WifiConfigurationProvider mWifiConfigurationProvider; 74 @Mock private AbstractProvisioningTask.Callback mCallback; 75 @Mock private Utils mUtils; 76 @Mock private android.net.wifi.WifiInfo mWifiInfo; 77 @Mock private AddWifiNetworkTask.Injector mTestInjector; 78 79 private AddWifiNetworkTask mTask; 80 81 @Before 82 public void setUp() { 83 System.setProperty("dexmaker.share_classloader", "true"); 84 MockitoAnnotations.initMocks(this); 85 86 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 87 when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)) 88 .thenReturn(mConnectivityManager); 89 } 90 91 @Test 92 public void testNoWifiInfo() { 93 // GIVEN that no wifi info was passed in the parameter 94 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 95 NO_WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 96 97 // WHEN running the task 98 runTask(); 99 100 // THEN success should be called 101 verify(mCallback).onSuccess(mTask); 102 } 103 104 @Test 105 public void testWifiManagerNull() { 106 // GIVEN that wifi info was passed in the parameter 107 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 108 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 109 110 // GIVEN that mWifiManager is null 111 when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(null); 112 113 // WHEN running the task 114 runTask(); 115 116 // THEN error should be called 117 verify(mCallback).onError(mTask, 0); 118 } 119 120 @Test 121 public void testFailToEnableWifi() { 122 // GIVEN that wifi info was passed in the parameter 123 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 124 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 125 126 // GIVEN that wifi is not enabled 127 when(mWifiManager.isWifiEnabled()).thenReturn(false); 128 129 // GIVEN that enabling wifi failed 130 when(mWifiManager.setWifiEnabled(true)).thenReturn(false); 131 132 // WHEN running the task 133 runTask(); 134 135 // THEN error should be called 136 verify(mCallback).onError(mTask, 0); 137 } 138 139 @Test 140 public void testIsConnectedToSpecifiedWifiTrue() { 141 // GIVEN that wifi info was passed in the parameter 142 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 143 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 144 145 // GIVEN that wifi is enabled 146 when(mWifiManager.isWifiEnabled()).thenReturn(true); 147 148 // GIVEN connected to wifi 149 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 150 151 // GIVEN the connected SSID is the same as the wifi param 152 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 153 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID); 154 155 // WHEN running the task 156 runTask(); 157 158 // THEN success should be called 159 verify(mCallback).onSuccess(mTask); 160 } 161 162 @Test 163 public void testNoWifiInfoInProvider() { 164 // GIVEN that wifi info was passed in the parameter 165 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 166 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 167 168 // GIVEN that wifi is enabled 169 when(mWifiManager.isWifiEnabled()).thenReturn(true); 170 171 // GIVEN connected to wifi 172 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 173 174 // GIVEN the connected SSID is different from the one in wifi param 175 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 176 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID_2); 177 178 // WHEN running the task 179 runTask(); 180 181 // GIVEN generateWifiConfiguration is null 182 when(mWifiConfigurationProvider.generateWifiConfiguration(any())) 183 .thenReturn(null); 184 185 // THEN error should be called 186 verify(mCallback).onError(mTask, 0); 187 } 188 189 @Test 190 public void testFailingAddingNetwork() { 191 192 // GIVEN that wifi info was passed in the parameter 193 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 194 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 195 196 // GIVEN that wifi is enabled 197 when(mWifiManager.isWifiEnabled()).thenReturn(true); 198 199 // GIVEN connected to wifi 200 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 201 202 // GIVEN the connected SSID is different from the one in wifi param 203 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 204 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID_2); 205 206 // GIVEN WifiConfiguration is not empty 207 when(mWifiConfigurationProvider.generateWifiConfiguration(any())) 208 .thenReturn(new WifiConfiguration()); 209 210 // GIVEN addNetwork always fail 211 when(mWifiManager.addNetwork(any())).thenReturn(ADD_NETWORK_FAIL); 212 213 // WHEN running the task 214 runTask(); 215 216 // THEN error should be called 217 verify(mCallback).onError(mTask, 0); 218 } 219 220 @Test 221 public void testFailingToReconnectAfterAddingNetwork() { 222 // GIVEN that wifi info was passed in the parameter 223 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 224 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 225 226 // GIVEN that wifi is enabled 227 when(mWifiManager.isWifiEnabled()).thenReturn(true); 228 229 // GIVEN connected to wifi 230 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 231 232 // GIVEN the connected SSID is different from the one in wifi param 233 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 234 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID_2); 235 236 // GIVEN WifiConfiguration is not empty 237 when(mWifiConfigurationProvider.generateWifiConfiguration(any())) 238 .thenReturn(new WifiConfiguration()); 239 240 // GIVEN addNetwork OK 241 when(mWifiManager.addNetwork(any())).thenReturn(ADD_NETWORK_OK); 242 243 // GIVEN reconnect fail after adding network 244 when(mWifiManager.reconnect()).thenReturn(false); 245 246 // WHEN running the task 247 runTask(); 248 249 // THEN wifi should be enabled 250 assertTrue(mWifiManager.isWifiEnabled()); 251 252 // THEN error should be called 253 verify(mCallback).onError(mTask, 0); 254 } 255 256 @Test 257 public void testReconnectAfterAddingNetworkSuccess() { 258 // GIVEN that wifi info was passed in the parameter 259 mTask = new AddWifiNetworkTask(mNetworkMonitor, mWifiConfigurationProvider, mContext, 260 WIFI_INFO_PARAMS, mCallback, mUtils, mTestInjector); 261 262 // GIVEN that wifi is enabled 263 when(mWifiManager.isWifiEnabled()).thenReturn(true); 264 265 // GIVEN connected to wifi 266 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 267 268 // GIVEN the connected SSID is different from the one in wifi param 269 when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo); 270 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID_2); 271 272 // GIVEN WifiConfiguration is not empty 273 when(mWifiConfigurationProvider.generateWifiConfiguration(any())) 274 .thenReturn(new WifiConfiguration()); 275 276 // GIVEN addNetwork OK 277 when(mWifiManager.addNetwork(any())).thenReturn(ADD_NETWORK_OK); 278 279 // GIVEN reconnect ok after adding network 280 when(mWifiManager.reconnect()).thenReturn(true); 281 282 // WHEN running the task 283 runTask(); 284 285 // GIVEN it connect back to the specified network 286 when(mWifiInfo.getSSID()).thenReturn(TEST_SSID); 287 288 // WHEN network is re-connected 289 mTask.onNetworkConnected(); 290 291 // THEN success should be called 292 verify(mCallback).onSuccess(mTask); 293 } 294 295 private void runTask() { 296 if (Looper.myLooper() == null) { 297 Looper.prepare(); 298 } 299 mTask.run(TEST_USER_ID); 300 } 301} 302