1 /* 2 * Copyright (C) 2021 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_PROFILE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Matchers.any; 24 import static org.mockito.Matchers.anyInt; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.verifyNoMoreInteractions; 28 import static org.mockito.Mockito.when; 29 30 import android.app.admin.DevicePolicyManager; 31 import android.content.ComponentName; 32 import android.content.Context; 33 import android.content.res.Resources; 34 import android.os.UserHandle; 35 36 import androidx.test.filters.SmallTest; 37 38 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 39 import com.android.managedprovisioning.common.Utils; 40 import com.android.managedprovisioning.model.ProvisioningParams; 41 import com.android.managedprovisioning.task.interactacrossprofiles.CrossProfileAppsSnapshot; 42 import com.android.managedprovisioning.task.nonrequiredapps.SystemAppsSnapshot; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.junit.runners.JUnit4; 48 import org.mockito.Mock; 49 import org.mockito.MockitoAnnotations; 50 51 /** 52 * Unit tests for {@link CreateAndProvisionManagedProfileTask}. 53 */ 54 @SmallTest 55 @RunWith(JUnit4.class) 56 public class CreateAndProvisionManagedProfileTaskTest { 57 private static final int TEST_PARENT_USER_ID = 111; 58 private static final int TEST_USER_ID = 123; 59 private static final String TEST_DPC_PACKAGE_NAME = "com.test.dpc"; 60 private static final String OWNER_NAME = "ownerName"; 61 private static final ComponentName ADMIN = new ComponentName( 62 TEST_DPC_PACKAGE_NAME, ".Receiver"); 63 private static final ProvisioningParams TEST_PARAMS = new ProvisioningParams.Builder() 64 .setDeviceAdminComponentName(ADMIN) 65 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 66 .build(); 67 private static final ProvisioningParams LEAVE_SYSTEM_APPS_PARAMS = 68 new ProvisioningParams.Builder() 69 .setDeviceAdminComponentName(ADMIN) 70 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 71 .setLeaveAllSystemAppsEnabled(true) 72 .build(); 73 74 @Mock private Context mContext; 75 @Mock private DevicePolicyManager mDevicePolicyManager; 76 @Mock private CrossProfileAppsSnapshot mCrossProfileAppsSnapshot; 77 @Mock private SystemAppsSnapshot mSystemAppsSnapshot; 78 @Mock private AbstractProvisioningTask.Callback mCallback; 79 @Mock private Utils mUtils; 80 @Mock private Resources mResources; 81 82 @Before setUp()83 public void setUp() throws Exception { 84 MockitoAnnotations.initMocks(this); 85 when(mContext.getSystemServiceName(DevicePolicyManager.class)) 86 .thenReturn(Context.DEVICE_POLICY_SERVICE); 87 when(mContext.getSystemService(DevicePolicyManager.class)) 88 .thenReturn(mDevicePolicyManager); 89 when(mContext.getResources()).thenReturn(mResources); 90 when(mResources.getString(anyInt())).thenReturn(OWNER_NAME); 91 when(mUtils.findDeviceAdmin(TEST_DPC_PACKAGE_NAME, ADMIN, mContext, TEST_USER_ID)) 92 .thenReturn(ADMIN); 93 } 94 95 @Test testSuccess()96 public void testSuccess() throws Exception { 97 CreateAndProvisionManagedProfileTask task = createProvisioningTask(TEST_PARAMS); 98 when(mDevicePolicyManager.createAndProvisionManagedProfile(any())) 99 .thenReturn(new UserHandle(TEST_USER_ID)); 100 101 task.run(TEST_PARENT_USER_ID); 102 103 assertThat(task.getProfileUserId()).isEqualTo(TEST_USER_ID); 104 verify(mCallback).onSuccess(task); 105 verifyNoMoreInteractions(mCallback); 106 verify(mSystemAppsSnapshot).takeNewSnapshot(TEST_USER_ID); 107 verify(mCrossProfileAppsSnapshot).takeNewSnapshot(TEST_PARENT_USER_ID); 108 } 109 110 @Test testError()111 public void testError() throws Exception { 112 CreateAndProvisionManagedProfileTask task = createProvisioningTask(TEST_PARAMS); 113 when(mDevicePolicyManager.createAndProvisionManagedProfile(any())) 114 .thenReturn(null); 115 116 task.run(TEST_PARENT_USER_ID); 117 118 verify(mCallback).onError(task, 0); 119 verifyNoMoreInteractions(mCallback); 120 verifyNoMoreInteractions(mSystemAppsSnapshot); 121 verifyNoMoreInteractions(mCrossProfileAppsSnapshot); 122 } 123 124 @Test testLeaveSystemAppsEnabled()125 public void testLeaveSystemAppsEnabled() throws Exception { 126 CreateAndProvisionManagedProfileTask task = createProvisioningTask( 127 LEAVE_SYSTEM_APPS_PARAMS); 128 when(mDevicePolicyManager.createAndProvisionManagedProfile(any())) 129 .thenReturn(new UserHandle(TEST_USER_ID)); 130 131 task.run(TEST_PARENT_USER_ID); 132 133 assertThat(task.getProfileUserId()).isEqualTo(TEST_USER_ID); 134 verify(mCallback).onSuccess(task); 135 verifyNoMoreInteractions(mCallback); 136 verifyNoMoreInteractions(mSystemAppsSnapshot); 137 verify(mCrossProfileAppsSnapshot).takeNewSnapshot(TEST_PARENT_USER_ID); 138 } 139 createProvisioningTask(ProvisioningParams params)140 private CreateAndProvisionManagedProfileTask createProvisioningTask(ProvisioningParams params) { 141 return new CreateAndProvisionManagedProfileTask( 142 mUtils, 143 mContext, 144 mSystemAppsSnapshot, 145 mCrossProfileAppsSnapshot, 146 params, 147 mCallback, 148 mock(ProvisioningAnalyticsTracker.class)); 149 } 150 } 151