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