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