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.doThrow; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.verifyNoMoreInteractions; 29 import static org.mockito.Mockito.when; 30 31 import android.app.admin.DevicePolicyManager; 32 import android.app.admin.ProvisioningException; 33 import android.content.ComponentName; 34 import android.content.Context; 35 import android.content.res.Resources; 36 import android.os.UserHandle; 37 38 import androidx.test.filters.SmallTest; 39 40 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 41 import com.android.managedprovisioning.common.Utils; 42 import com.android.managedprovisioning.model.ProvisioningParams; 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 68 @Mock private Context mContext; 69 @Mock private DevicePolicyManager mDevicePolicyManager; 70 @Mock private AbstractProvisioningTask.Callback mCallback; 71 @Mock private Utils mUtils; 72 @Mock private Resources mResources; 73 74 private static final String TEST_ERROR_MESSAGE = "test error message"; 75 private static final ProvisioningException PROVISIONING_EXCEPTION = new ProvisioningException( 76 new Exception(), /* provisioningError= */ 0, TEST_ERROR_MESSAGE); 77 78 @Before setUp()79 public void setUp() throws Exception { 80 MockitoAnnotations.initMocks(this); 81 when(mContext.getSystemServiceName(DevicePolicyManager.class)) 82 .thenReturn(Context.DEVICE_POLICY_SERVICE); 83 when(mContext.getSystemService(DevicePolicyManager.class)) 84 .thenReturn(mDevicePolicyManager); 85 when(mContext.getResources()).thenReturn(mResources); 86 when(mResources.getString(anyInt())).thenReturn(OWNER_NAME); 87 when(mUtils.findDeviceAdmin(TEST_DPC_PACKAGE_NAME, ADMIN, mContext, TEST_USER_ID)) 88 .thenReturn(ADMIN); 89 } 90 91 @Test testSuccess()92 public void testSuccess() throws Exception { 93 CreateAndProvisionManagedProfileTask task = createProvisioningTask(TEST_PARAMS); 94 when(mDevicePolicyManager.createAndProvisionManagedProfile(any())) 95 .thenReturn(new UserHandle(TEST_USER_ID)); 96 97 task.run(TEST_PARENT_USER_ID); 98 99 assertThat(task.getProfileUserId()).isEqualTo(TEST_USER_ID); 100 verify(mCallback).onSuccess(task); 101 verifyNoMoreInteractions(mCallback); 102 } 103 104 @Test testError()105 public void testError() throws Exception { 106 CreateAndProvisionManagedProfileTask task = createProvisioningTask(TEST_PARAMS); 107 when(mDevicePolicyManager.createAndProvisionManagedProfile(any())) 108 .thenReturn(null); 109 110 task.run(TEST_PARENT_USER_ID); 111 112 verify(mCallback).onError(task, 0, /* errorMessage= */ null); 113 verifyNoMoreInteractions(mCallback); 114 } 115 116 @Test testTextError()117 public void testTextError() throws Exception { 118 CreateAndProvisionManagedProfileTask task = createProvisioningTask(TEST_PARAMS); 119 doThrow(PROVISIONING_EXCEPTION) 120 .when(mDevicePolicyManager).createAndProvisionManagedProfile(any()); 121 122 task.run(TEST_PARENT_USER_ID); 123 124 verify(mCallback).onError(task, 0, TEST_ERROR_MESSAGE); 125 verifyNoMoreInteractions(mCallback); 126 } 127 createProvisioningTask(ProvisioningParams params)128 private CreateAndProvisionManagedProfileTask createProvisioningTask(ProvisioningParams params) { 129 return new CreateAndProvisionManagedProfileTask( 130 mUtils, 131 mContext, 132 params, 133 mCallback, 134 mock(ProvisioningAnalyticsTracker.class)); 135 } 136 } 137