1 /* 2 * Copyright 2016, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.managedprovisioning.provisioning; 18 19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE; 20 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Matchers.eq; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.ComponentName; 25 import android.support.test.filters.SmallTest; 26 27 import com.android.managedprovisioning.R; 28 import com.android.managedprovisioning.finalization.FinalizationController; 29 import com.android.managedprovisioning.model.PackageDownloadInfo; 30 import com.android.managedprovisioning.model.ProvisioningParams; 31 import com.android.managedprovisioning.model.WifiInfo; 32 import com.android.managedprovisioning.task.AbstractProvisioningTask; 33 import com.android.managedprovisioning.task.AddWifiNetworkTask; 34 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; 35 import com.android.managedprovisioning.task.DeviceOwnerInitializeProvisioningTask; 36 import com.android.managedprovisioning.task.DisallowAddUserTask; 37 import com.android.managedprovisioning.task.DownloadPackageTask; 38 import com.android.managedprovisioning.task.InstallPackageTask; 39 import com.android.managedprovisioning.task.SetDevicePolicyTask; 40 import com.android.managedprovisioning.task.VerifyPackageTask; 41 42 import org.mockito.Mock; 43 44 /** 45 * Unit tests for {@link DeviceOwnerProvisioningController}. 46 */ 47 public class DeviceOwnerProvisioningControllerTest extends ProvisioningControllerBaseTest { 48 49 private static final int TEST_USER_ID = 123; 50 private static final ComponentName TEST_ADMIN = new ComponentName("com.test.admin", 51 "com.test.admin.AdminReceiver"); 52 53 private static final String TEST_SSID = "SomeSsid"; 54 private static final WifiInfo TEST_WIFI_INFO = new WifiInfo.Builder() 55 .setSsid(TEST_SSID) 56 .build(); 57 58 private static final String TEST_DOWNLOAD_LOCATION = "http://www.some.uri.com"; 59 private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' }; 60 private static final PackageDownloadInfo TEST_DOWNLOAD_INFO = new PackageDownloadInfo.Builder() 61 .setLocation(TEST_DOWNLOAD_LOCATION) 62 .setSignatureChecksum(TEST_PACKAGE_CHECKSUM) 63 .build(); 64 65 @Mock private ProvisioningControllerCallback mCallback; 66 @Mock private FinalizationController mFinalizationController; 67 private ProvisioningParams mParams; 68 69 @SmallTest 70 public void testRunAllTasks() throws Exception { 71 // GIVEN device owner provisioning was invoked with a wifi and download info 72 createController(TEST_WIFI_INFO, TEST_DOWNLOAD_INFO); 73 74 // WHEN starting the test run 75 mController.start(mHandler); 76 77 // THEN the initialization task is run first 78 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 79 80 // THEN the add wifi task should be run 81 taskSucceeded(AddWifiNetworkTask.class); 82 83 // THEN the download package task should be run 84 taskSucceeded(DownloadPackageTask.class); 85 86 // THEN the verify package task should be run 87 taskSucceeded(VerifyPackageTask.class); 88 89 // THEN the install package task should be run 90 taskSucceeded(InstallPackageTask.class); 91 92 // THEN the delete non-required apps task should be run 93 taskSucceeded(DeleteNonRequiredAppsTask.class); 94 95 // THEN the set device policy task should be run 96 taskSucceeded(SetDevicePolicyTask.class); 97 98 // THEN the disallow add user task should be run 99 taskSucceeded(DisallowAddUserTask.class); 100 101 // THEN the provisioning complete callback should have happened 102 verify(mCallback).provisioningTasksCompleted(); 103 } 104 105 @SmallTest 106 public void testNoWifiInfo() throws Exception { 107 // GIVEN device owner provisioning was invoked with a wifi and download info 108 createController(null, TEST_DOWNLOAD_INFO); 109 110 // WHEN starting the test run 111 mController.start(mHandler); 112 113 // THEN the initialization task is run first 114 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 115 116 // THEN the download package task should be run 117 taskSucceeded(DownloadPackageTask.class); 118 119 // THEN the verify package task should be run 120 taskSucceeded(VerifyPackageTask.class); 121 122 // THEN the install package task should be run 123 taskSucceeded(InstallPackageTask.class); 124 125 // THEN the delete non-required apps task should be run 126 taskSucceeded(DeleteNonRequiredAppsTask.class); 127 128 // THEN the set device policy task should be run 129 taskSucceeded(SetDevicePolicyTask.class); 130 131 // THEN the disallow add user task should be run 132 taskSucceeded(DisallowAddUserTask.class); 133 134 // THEN the provisioning complete callback should have happened 135 verify(mCallback).provisioningTasksCompleted(); 136 } 137 138 @SmallTest 139 public void testNoDownloadInfo() throws Exception { 140 // GIVEN device owner provisioning was invoked with a wifi and download info 141 createController(TEST_WIFI_INFO, null); 142 143 // WHEN starting the test run 144 mController.start(mHandler); 145 146 // THEN the initialization task is run first 147 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 148 149 // THEN the add wifi task should be run 150 taskSucceeded(AddWifiNetworkTask.class); 151 152 // THEN the delete non-required apps task should be run 153 taskSucceeded(DeleteNonRequiredAppsTask.class); 154 155 // THEN the set device policy task should be run 156 taskSucceeded(SetDevicePolicyTask.class); 157 158 // THEN the disallow add user task should be run 159 taskSucceeded(DisallowAddUserTask.class); 160 161 // THEN the provisioning complete callback should have happened 162 verify(mCallback).provisioningTasksCompleted(); 163 } 164 165 @SmallTest 166 public void testErrorAddWifiTask() throws Exception { 167 // GIVEN device owner provisioning was invoked with a wifi and download info 168 createController(TEST_WIFI_INFO, TEST_DOWNLOAD_INFO); 169 170 // WHEN starting the test run 171 mController.start(mHandler); 172 173 // THEN the initialization task is run first 174 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 175 176 // THEN the add wifi task should be run 177 AbstractProvisioningTask task = verifyTaskRun(AddWifiNetworkTask.class); 178 179 // WHEN the task causes an error 180 mController.onError(task, 0); 181 182 // THEN the onError callback should have been called without factory reset being required 183 verify(mCallback).error(eq(R.string.cant_set_up_device), anyInt(), eq(false)); 184 } 185 186 @SmallTest 187 public void testErrorDownloadAppTask() throws Exception { 188 // GIVEN device owner provisioning was invoked with a wifi and download info 189 createController(TEST_WIFI_INFO, TEST_DOWNLOAD_INFO); 190 191 // WHEN starting the test run 192 mController.start(mHandler); 193 194 // THEN the initialization task is run first 195 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 196 197 // THEN the add wifi task should be run 198 taskSucceeded(AddWifiNetworkTask.class); 199 200 // THEN the download package task should be run 201 AbstractProvisioningTask task = verifyTaskRun(DownloadPackageTask.class); 202 203 // WHEN the task causes an error 204 mController.onError(task, 0); 205 206 // THEN the onError callback should have been called with factory reset being required 207 verify(mCallback).error(anyInt(), anyInt(), eq(true)); 208 } 209 210 private void createController(WifiInfo wifiInfo, PackageDownloadInfo downloadInfo) { 211 mParams = new ProvisioningParams.Builder() 212 .setDeviceAdminComponentName(TEST_ADMIN) 213 .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE) 214 .setWifiInfo(wifiInfo) 215 .setDeviceAdminDownloadInfo(downloadInfo) 216 .build(); 217 218 mController = new DeviceOwnerProvisioningController( 219 getContext(), 220 mParams, 221 TEST_USER_ID, 222 mCallback, 223 mFinalizationController); 224 } 225 } 226