1 /* 2 * Copyright 2016, 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 org.junit.Assert.assertEquals; 22 import static org.mockito.AdditionalMatchers.aryEq; 23 import static org.mockito.Matchers.any; 24 import static org.mockito.Matchers.anyInt; 25 import static org.mockito.Matchers.anyString; 26 import static org.mockito.Matchers.eq; 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.verifyNoMoreInteractions; 30 import static org.mockito.Mockito.when; 31 32 import android.content.pm.UserInfo; 33 import android.os.UserManager; 34 35 import androidx.test.InstrumentationRegistry; 36 import androidx.test.filters.SmallTest; 37 38 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 39 import com.android.managedprovisioning.model.ProvisioningParams; 40 import com.android.managedprovisioning.task.nonrequiredapps.NonRequiredAppsLogic; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 47 import java.util.Arrays; 48 import java.util.LinkedHashSet; 49 50 @SmallTest 51 public class CreateManagedProfileTaskTest { 52 private static final int TEST_PARENT_USER_ID = 111; 53 private static final int TEST_USER_ID = 123; 54 private static final String TEST_DPC_PACKAGE_NAME = "com.test.dpc"; 55 private static final ProvisioningParams TEST_PARAMS = new ProvisioningParams.Builder() 56 .setDeviceAdminPackageName(TEST_DPC_PACKAGE_NAME) 57 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 58 .build(); 59 private static final String[] SYSTEM_APPS_TO_DELETE = {"app.a", "app.b"}; 60 61 private @Mock UserManager mUserManager; 62 private @Mock NonRequiredAppsLogic mLogic; 63 private @Mock AbstractProvisioningTask.Callback mCallback; 64 65 private CreateManagedProfileTask mTask; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 mTask = new CreateManagedProfileTask(InstrumentationRegistry.getTargetContext(), 71 TEST_PARAMS, mCallback, mUserManager, mLogic, 72 mock(ProvisioningAnalyticsTracker.class)); 73 // GIVEN that a set of system apps should not be installed on the new user 74 when(mLogic.getSystemAppsToRemove(TEST_PARENT_USER_ID)) 75 .thenReturn(new LinkedHashSet<String>(Arrays.asList(SYSTEM_APPS_TO_DELETE))); 76 } 77 78 @Test testSuccess()79 public void testSuccess() { 80 // GIVEN that a new profile can be created 81 when(mUserManager.createProfileForUserEvenWhenDisallowed( 82 anyString(), anyInt(), eq(TEST_PARENT_USER_ID), 83 aryEq(SYSTEM_APPS_TO_DELETE))) 84 .thenReturn(new UserInfo(TEST_USER_ID, null, 0)); 85 86 // WHEN the CreateManagedProfileTask is run 87 mTask.run(TEST_PARENT_USER_ID); 88 // THEN success callback should have happened 89 verify(mCallback).onSuccess(mTask); 90 // THEN any other callback should not happen 91 verifyNoMoreInteractions(mCallback); 92 // THEN list of system apps in the new user should be saved 93 verify(mLogic).maybeTakeSystemAppsSnapshot(TEST_USER_ID); 94 // THEN the id of the new profile should be returned by getProfileUserId 95 assertEquals(TEST_USER_ID, mTask.getProfileUserId()); 96 } 97 98 @Test testError()99 public void testError() { 100 // GIVEN that a new profile can't be created 101 when(mUserManager.createProfileForUserEvenWhenDisallowed( 102 anyString(), anyInt(), eq(TEST_PARENT_USER_ID), 103 aryEq(SYSTEM_APPS_TO_DELETE))) 104 .thenReturn(null); 105 106 // WHEN the CreateManagedProfileTask is run 107 mTask.run(TEST_PARENT_USER_ID); 108 // THEN error callback should have happened 109 verify(mCallback).onError(mTask, 0); 110 // THEN any other callback should not happen 111 verifyNoMoreInteractions(mCallback); 112 } 113 } 114