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.provisioning; 18 19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE; 20 21 import static org.mockito.Mockito.doAnswer; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.content.ContextWrapper; 29 30 import androidx.test.filters.SmallTest; 31 32 import com.android.managedprovisioning.R; 33 import com.android.managedprovisioning.model.ProvisioningParams; 34 import com.android.managedprovisioning.task.AbstractProvisioningTask; 35 import com.android.managedprovisioning.task.CreateAndProvisionManagedProfileTask; 36 37 import org.mockito.Mock; 38 import org.mockito.invocation.InvocationOnMock; 39 import org.mockito.stubbing.Answer; 40 41 import java.util.concurrent.CountDownLatch; 42 import java.util.concurrent.TimeUnit; 43 44 /** 45 * Unit tests for {@link ProfileOwnerProvisioningController}. 46 */ 47 48 public class ProfileOwnerProvisioningControllerTest extends ProvisioningControllerBaseTest { 49 50 private static final int TEST_PARENT_USER_ID = 1; 51 private static final int TEST_PROFILE_USER_ID = 2; 52 private static final ComponentName TEST_ADMIN = new ComponentName("com.test.admin", 53 "com.test.admin.AdminReceiver"); 54 55 @Mock private ProvisioningControllerCallback mCallback; 56 private Context mContext; 57 58 @Override setUp()59 public void setUp() throws Exception { 60 super.setUp(); 61 mContext = new ContextWrapper(getContext()); 62 } 63 64 @SmallTest testRunAllTasks()65 public void testRunAllTasks() throws Exception { 66 // GIVEN device profile owner provisioning was invoked 67 createController(); 68 69 // WHEN starting the test run 70 mController.start(mHandler); 71 72 // THEN the create and provision managed profile task is run 73 verifyTaskRun(CreateAndProvisionManagedProfileTask.class); 74 75 // WHEN the task completes successfully 76 CreateAndProvisionManagedProfileTask createAndProvisionManagedProfileTask = mock( 77 CreateAndProvisionManagedProfileTask.class); 78 when(createAndProvisionManagedProfileTask.getProfileUserId()).thenReturn( 79 TEST_PROFILE_USER_ID); 80 mController.onSuccess(createAndProvisionManagedProfileTask); 81 82 // THEN the provisioning complete callback should have happened 83 verify(mCallback).provisioningTasksCompleted(); 84 } 85 86 @SmallTest testCancel()87 public void testCancel() throws Exception { 88 // GIVEN device profile owner provisioning was invoked 89 createController(); 90 91 // WHEN starting the test run 92 mController.start(mHandler); 93 94 // THEN the create and provision managed profile task is run 95 AbstractProvisioningTask task = verifyTaskRun(CreateAndProvisionManagedProfileTask.class); 96 97 // latch used to wait for onCancelled callback 98 final CountDownLatch latch = new CountDownLatch(1); 99 doAnswer(new Answer<Void>() { 100 @Override 101 public Void answer(InvocationOnMock invocationOnMock) throws Throwable { 102 latch.countDown(); 103 return null; 104 } 105 }).when(mCallback).cleanUpCompleted(); 106 107 // WHEN the user cancels the provisioning progress 108 mController.cancel(); 109 110 // THEN the activity is informed that progress has been cancelled 111 assertTrue(latch.await(1, TimeUnit.SECONDS)); 112 113 // WHEN the create and provision managed profile task eventually finishes 114 mController.onSuccess(task); 115 116 // THEN no more tasks should be run 117 assertNull(mHandler.getLastTask()); 118 } 119 120 @SmallTest testError()121 public void testError() throws Exception { 122 // GIVEN device profile owner provisioning was invoked 123 createController(); 124 125 // WHEN starting the test run 126 mController.start(mHandler); 127 128 // THEN the create and provision managed profile task is run 129 AbstractProvisioningTask task = verifyTaskRun(CreateAndProvisionManagedProfileTask.class); 130 131 // WHEN the task encountered an error 132 mController.onError(task, 0); 133 134 // THEN the activity should be informed about the error 135 verify(mCallback).error(R.string.cant_set_up_profile, 136 R.string.managed_provisioning_error_text, false); 137 } 138 createController()139 private void createController() { 140 ProvisioningParams params = new ProvisioningParams.Builder() 141 .setDeviceAdminComponentName(TEST_ADMIN) 142 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 143 .build(); 144 145 mController = ProfileOwnerProvisioningController.createInstance( 146 mContext, 147 params, 148 TEST_PARENT_USER_ID, 149 mCallback); 150 } 151 } 152