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