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 org.mockito.Mockito.anyInt; 20 import static org.mockito.Mockito.times; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 import static org.mockito.MockitoAnnotations.Mock; 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 org.mockito.MockitoAnnotations; 33 34 public class SetDevicePolicyTaskTest extends AndroidTestCase { 35 private static final String ADMIN_PACKAGE_NAME = "com.admin.test"; 36 private static final String ADMIN_RECEIVER_NAME = ADMIN_PACKAGE_NAME + ".AdminReceiver"; 37 private static final ComponentName ADMIN_COMPONENT_NAME = new ComponentName(ADMIN_PACKAGE_NAME, 38 ADMIN_RECEIVER_NAME); 39 private static final String OWNER_NAME = "Test Owner"; 40 private static final int TEST_USER_ID = 123; 41 42 @Mock private Context mContext; 43 @Mock private PackageManager mPackageManager; 44 @Mock private DevicePolicyManager mDevicePolicyManager; 45 @Mock private SetDevicePolicyTask.Callback mCallback; 46 47 private SetDevicePolicyTask mTask; 48 49 @Override setUp()50 protected void setUp() throws Exception { 51 super.setUp(); 52 // This is necessary for mockito to work 53 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 54 MockitoAnnotations.initMocks(this); 55 56 when(mContext.getPackageManager()).thenReturn(mPackageManager); 57 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 58 .thenReturn(mDevicePolicyManager); 59 when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME)) 60 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); 61 when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(null); 62 when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID)) 63 .thenReturn(true); 64 65 mTask = new SetDevicePolicyTask(mContext, OWNER_NAME, mCallback, TEST_USER_ID); 66 } 67 68 @SmallTest testEnableDevicePolicyApp()69 public void testEnableDevicePolicyApp() { 70 when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME)) 71 .thenReturn(PackageManager.COMPONENT_ENABLED_STATE_DISABLED); 72 mTask.run(ADMIN_COMPONENT_NAME); 73 verify(mPackageManager).setApplicationEnabledSetting(ADMIN_PACKAGE_NAME, 74 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 75 PackageManager.DONT_KILL_APP); 76 verify(mCallback, times(1)).onSuccess(); 77 } 78 79 @SmallTest testEnableDevicePolicyApp_PackageNotFound()80 public void testEnableDevicePolicyApp_PackageNotFound() { 81 when(mPackageManager.getApplicationEnabledSetting(ADMIN_PACKAGE_NAME)) 82 .thenThrow(new IllegalArgumentException()); 83 mTask.run(ADMIN_COMPONENT_NAME); 84 verify(mCallback, times(1)).onError(); 85 } 86 87 @SmallTest testSetActiveAdmin()88 public void testSetActiveAdmin() { 89 mTask.run(ADMIN_COMPONENT_NAME); 90 verify(mDevicePolicyManager).setActiveAdmin(ADMIN_COMPONENT_NAME, true, TEST_USER_ID); 91 verify(mCallback, times(1)).onSuccess(); 92 } 93 94 @SmallTest testSetDeviceOwner()95 public void testSetDeviceOwner() { 96 mTask.run(ADMIN_COMPONENT_NAME); 97 verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID); 98 verify(mCallback, times(1)).onSuccess(); 99 } 100 101 @SmallTest testSetDeviceOwner_PreconditionsNotMet()102 public void testSetDeviceOwner_PreconditionsNotMet() { 103 when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID)) 104 .thenThrow(new IllegalStateException()); 105 mTask.run(ADMIN_COMPONENT_NAME); 106 verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID); 107 verify(mCallback, times(1)).onError(); 108 } 109 110 @SmallTest testSetDeviceOwner_ReturnFalse()111 public void testSetDeviceOwner_ReturnFalse() { 112 when(mDevicePolicyManager.setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID)) 113 .thenReturn(false); 114 mTask.run(ADMIN_COMPONENT_NAME); 115 verify(mDevicePolicyManager).setDeviceOwner(ADMIN_COMPONENT_NAME, OWNER_NAME, TEST_USER_ID); 116 verify(mCallback, times(1)).onError(); 117 } 118 } 119