• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.task;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_FINANCED_DEVICE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
21 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.Mockito.when;
31 
32 import android.app.admin.DevicePolicyManager;
33 import android.content.ComponentName;
34 import android.content.Context;
35 import android.content.pm.PackageManager;
36 import android.test.AndroidTestCase;
37 import android.test.suitebuilder.annotation.SmallTest;
38 
39 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
40 import com.android.managedprovisioning.common.Utils;
41 import com.android.managedprovisioning.model.ProvisioningParams;
42 
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 
46 public class SetDeviceOwnerPolicyTaskTest extends AndroidTestCase {
47     private static final String ADMIN_PACKAGE_NAME = "com.admin.test";
48     private static final String ADMIN_RECEIVER_NAME = ADMIN_PACKAGE_NAME + ".AdminReceiver";
49     private static final ComponentName ADMIN_COMPONENT_NAME = new ComponentName(ADMIN_PACKAGE_NAME,
50             ADMIN_RECEIVER_NAME);
51     private static final int TEST_USER_ID = 123;
52 
53     @Mock private Context mContext;
54     @Mock private PackageManager mPackageManager;
55     @Mock private DevicePolicyManager mDevicePolicyManager;
56     @Mock private AbstractProvisioningTask.Callback mCallback;
57     @Mock private Utils mUtils;
58 
59     private SetDeviceOwnerPolicyTask mTask;
60 
61     @Override
setUp()62     protected void setUp() throws Exception {
63         super.setUp();
64         // This is necessary for mockito to work
65         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
66         MockitoAnnotations.initMocks(this);
67 
68         when(mContext.getPackageManager()).thenReturn(mPackageManager);
69         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
70                 .thenReturn(mDevicePolicyManager);
71         when(mContext.getResources()).thenReturn(getContext().getResources());
72 
73         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
74                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
75         when(mUtils.getCurrentDeviceOwnerComponentName(mDevicePolicyManager)).thenReturn(null);
76         when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID))
77                 .thenReturn(true);
78         when(mUtils.findDeviceAdmin(null, ADMIN_COMPONENT_NAME, mContext, TEST_USER_ID))
79                 .thenReturn(ADMIN_COMPONENT_NAME);
80     }
81 
82     @SmallTest
testEnableDevicePolicyApp_DefaultToDefault()83     public void testEnableDevicePolicyApp_DefaultToDefault() {
84         // GIVEN that we are provisioning device owner
85         createTask(ACTION_PROVISION_MANAGED_DEVICE);
86         // GIVEN that the management app is currently the manifest default
87         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
88                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
89 
90         // WHEN running the task
91         mTask.run(TEST_USER_ID);
92 
93         // THEN the management app should still be default
94         verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
95                 anyInt(), anyInt());
96         verify(mCallback).onSuccess(mTask);
97         verifyNoMoreInteractions(mCallback);
98     }
99 
100     @SmallTest
testEnableDevicePolicyApp_DisabledToDefault()101     public void testEnableDevicePolicyApp_DisabledToDefault() {
102         // GIVEN that we are provisioning device owner
103         createTask(ACTION_PROVISION_MANAGED_DEVICE);
104         // GIVEN that the management app is currently disabled
105         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
106                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
107 
108         // WHEN running the task
109         mTask.run(TEST_USER_ID);
110 
111         // THEN the management app should have been enabled
112         verify(mPackageManager).setApplicationEnabledSetting(ADMIN_PACKAGE_NAME,
113                 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
114                 PackageManager.DONT_KILL_APP);
115         verify(mCallback).onSuccess(mTask);
116         verifyNoMoreInteractions(mCallback);
117     }
118 
119     @SmallTest
testEnableDevicePolicyApp_EnabledToEnabled()120     public void testEnableDevicePolicyApp_EnabledToEnabled() {
121         // GIVEN that we are provisioning device owner
122         createTask(ACTION_PROVISION_MANAGED_DEVICE);
123         // GIVEN that the management app is currently enabled
124         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
125                 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
126 
127         // WHEN running the task
128         mTask.run(TEST_USER_ID);
129 
130         // THEN the management app should have been untouched
131         verify(mPackageManager, never()).setApplicationEnabledSetting(eq(ADMIN_PACKAGE_NAME),
132                 anyInt(), anyInt());
133         verify(mCallback).onSuccess(mTask);
134         verifyNoMoreInteractions(mCallback);
135     }
136 
137     @SmallTest
testEnableDevicePolicyApp_PackageNotFound()138     public void testEnableDevicePolicyApp_PackageNotFound() {
139         // GIVEN that we are provisioning device owner
140         createTask(ACTION_PROVISION_MANAGED_DEVICE);
141         // GIVEN that the management app is not present on the device
142         when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME))
143                 .thenThrow(new IllegalArgumentException());
144 
145         // WHEN running the task
146         mTask.run(TEST_USER_ID);
147 
148         // THEN an error should be returned
149         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
150         verifyNoMoreInteractions(mCallback);
151     }
152 
153     @SmallTest
testSetActiveAdmin()154     public void testSetActiveAdmin() {
155         // GIVEN that we are provisioning device owner
156         createTask(ACTION_PROVISION_MANAGED_DEVICE);
157 
158         // WHEN running the task
159         mTask.run(TEST_USER_ID);
160 
161         // THEN the management app should have been set as active admin
162         verify(mDevicePolicyManager).setActiveAdmin(ADMIN_COMPONENT_NAME, true, TEST_USER_ID);
163         verify(mCallback).onSuccess(mTask);
164         verifyNoMoreInteractions(mCallback);
165     }
166 
167     @SmallTest
testSetDeviceOwner()168     public void testSetDeviceOwner() {
169         // GIVEN that we are provisioning device owner
170         createTask(ACTION_PROVISION_MANAGED_DEVICE);
171 
172         // WHEN running the task
173         mTask.run(TEST_USER_ID);
174 
175         // THEN the management app should have been set as device owner
176         verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
177         verify(mCallback).onSuccess(mTask);
178         verifyNoMoreInteractions(mCallback);
179     }
180 
181     @SmallTest
testSetDeviceOwner_PreconditionsNotMet()182     public void testSetDeviceOwner_PreconditionsNotMet() {
183         // GIVEN that we are provisioning device owner
184         createTask(ACTION_PROVISION_MANAGED_DEVICE);
185 
186         // GIVEN that setting device owner is not currently allowed
187         when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME,
188                 TEST_USER_ID)).thenThrow(new IllegalStateException());
189 
190         // WHEN running the task
191         mTask.run(TEST_USER_ID);
192 
193         // THEN an error should be returned
194         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
195         verifyNoMoreInteractions(mCallback);
196     }
197 
198     @SmallTest
testSetDeviceOwner_ReturnFalse()199     public void testSetDeviceOwner_ReturnFalse() {
200         // GIVEN that we are provisioning device owner
201         createTask(ACTION_PROVISION_MANAGED_DEVICE);
202 
203         // GIVEN that setting device owner fails
204         when(mDevicePolicyManager.setDeviceOwner(
205                 ADMIN_COMPONENT_NAME, TEST_USER_ID)).thenReturn(false);
206 
207         // WHEN running the task
208         mTask.run(TEST_USER_ID);
209 
210         // THEN an error should be returned
211         verify(mCallback).onError(mTask, 0, /* errorMessage= */ null);
212         verifyNoMoreInteractions(mCallback);
213     }
214 
215     @SmallTest
testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice()216     public void testSetDeviceOwnerType_asDeviceOwner_toFinancedDevice() {
217         // GIVEN that we are provisioning a financed device.
218         createTask(ACTION_PROVISION_FINANCED_DEVICE);
219 
220         // WHEN running the task.
221         mTask.run(TEST_USER_ID);
222 
223         // THEN the device owner type should have been set as financed when there is a device
224         // owner.
225         verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, TEST_USER_ID);
226         verify(mDevicePolicyManager).setDeviceOwnerType(
227                 ADMIN_COMPONENT_NAME, DEVICE_OWNER_TYPE_FINANCED);
228         verify(mCallback).onSuccess(mTask);
229         verifyNoMoreInteractions(mCallback);
230     }
231 
232     @SmallTest
testSetDeviceOwnerType_asDeviceOwner_notCalledWhenProvisioningManagedDevice()233     public void testSetDeviceOwnerType_asDeviceOwner_notCalledWhenProvisioningManagedDevice() {
234         // GIVEN that we are provisioning a managed device.
235         createTask(ACTION_PROVISION_MANAGED_DEVICE);
236 
237         // WHEN running the task
238         mTask.run(TEST_USER_ID);
239 
240         // THEN setting the device owner type should not have been called.
241         verify(mDevicePolicyManager, never()).setDeviceOwnerType(any(), anyInt());
242         verify(mCallback).onSuccess(mTask);
243         verifyNoMoreInteractions(mCallback);
244     }
245 
createTask(String action)246     private void createTask(String action) {
247         ProvisioningParams params = new ProvisioningParams.Builder()
248                 .setDeviceAdminComponentName(ADMIN_COMPONENT_NAME)
249                 .setProvisioningAction(action)
250                 .build();
251         mTask = new SetDeviceOwnerPolicyTask(mUtils, mContext, params, mCallback,
252                 mock(ProvisioningAnalyticsTracker.class));
253     }
254 }
255