1 /* 2 * Copyright 2019, 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_FINANCED_DEVICE; 20 21 import static org.mockito.Mockito.verify; 22 23 import android.content.ComponentName; 24 25 import androidx.test.filters.SmallTest; 26 27 import com.android.managedprovisioning.model.PackageDownloadInfo; 28 import com.android.managedprovisioning.model.ProvisioningParams; 29 import com.android.managedprovisioning.task.DeviceOwnerInitializeProvisioningTask; 30 import com.android.managedprovisioning.task.DisallowAddUserTask; 31 import com.android.managedprovisioning.task.DownloadPackageTask; 32 import com.android.managedprovisioning.task.SetDeviceOwnerPolicyTask; 33 import com.android.managedprovisioning.task.VerifyPackageTask; 34 35 import org.mockito.Mock; 36 37 /** 38 * Unit tests for {@link FinancedDeviceProvisioningController}. 39 */ 40 public class FinancedDeviceProvisioningControllerTest extends ProvisioningControllerBaseTest { 41 42 private static final int TEST_USER_ID = 123; 43 44 private static final ComponentName TEST_ADMIN = new ComponentName("com.test.admin", 45 "com.test.admin.AdminReceiver"); 46 47 private static final String TEST_DOWNLOAD_LOCATION = "http://www.some.uri.com"; 48 private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[] { '1', '2', '3', '4', '5' }; 49 private static final PackageDownloadInfo TEST_DOWNLOAD_INFO = new PackageDownloadInfo.Builder() 50 .setLocation(TEST_DOWNLOAD_LOCATION) 51 .setSignatureChecksum(TEST_PACKAGE_CHECKSUM) 52 .build(); 53 54 @Mock private ProvisioningControllerCallback mCallback; 55 56 @SmallTest testRunAllTasks()57 public void testRunAllTasks() throws Exception { 58 // GIVEN financed device provisioning was invoked 59 createController(createProvisioningParamsBuilder().build()); 60 61 // WHEN starting the test run 62 mController.start(mHandler); 63 64 // THEN the initialization task is run first 65 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 66 67 // THEN the download package task should be run 68 taskSucceeded(DownloadPackageTask.class); 69 70 // THEN the verify package task should be run 71 taskSucceeded(VerifyPackageTask.class); 72 73 // THEN the install package tasks should be run 74 tasksDownloadAndInstallDeviceOwnerPackageSucceeded(TEST_USER_ID); 75 76 // THEN the set device policy task should be run 77 taskSucceeded(SetDeviceOwnerPolicyTask.class); 78 79 // THEN the disallow add user task should be run 80 taskSucceeded(DisallowAddUserTask.class); 81 82 // THEN the provisioning complete callback should have happened 83 verify(mCallback).provisioningTasksCompleted(); 84 } 85 86 @SmallTest testNoDownloadInfo()87 public void testNoDownloadInfo() throws Exception { 88 // GIVEN financed device provisioning was invoked with no download info 89 createController(createProvisioningParamsBuilder() 90 .setDeviceAdminDownloadInfo(null) 91 .build()); 92 93 // WHEN starting the test run 94 mController.start(mHandler); 95 96 // THEN the initialization task is run first 97 taskSucceeded(DeviceOwnerInitializeProvisioningTask.class); 98 99 // THEN the set device policy task should be run 100 taskSucceeded(SetDeviceOwnerPolicyTask.class); 101 102 // THEN the disallow add user task should be run 103 taskSucceeded(DisallowAddUserTask.class); 104 105 // THEN the provisioning complete callback should have happened 106 verify(mCallback).provisioningTasksCompleted(); 107 } 108 createController(ProvisioningParams params)109 private void createController(ProvisioningParams params) { 110 mController = FinancedDeviceProvisioningController.createInstance( 111 getContext(), 112 params, 113 TEST_USER_ID, 114 mCallback); 115 } 116 createProvisioningParamsBuilder()117 private ProvisioningParams.Builder createProvisioningParamsBuilder() { 118 return new ProvisioningParams.Builder() 119 .setDeviceAdminComponentName(TEST_ADMIN) 120 .setDeviceAdminDownloadInfo(TEST_DOWNLOAD_INFO) 121 .setProvisioningAction(ACTION_PROVISION_FINANCED_DEVICE); 122 } 123 } 124