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