1 /* 2 * Copyright (C) 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.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.verifyNoMoreInteractions; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.content.pm.PackageManager; 29 import android.os.UserHandle; 30 31 import androidx.test.filters.SmallTest; 32 33 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 34 import com.android.managedprovisioning.model.ProvisioningParams; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 /** 42 * Unit tests for {@link InstallExistingPackageTask}. 43 */ 44 @SmallTest 45 public class InstallExistingPackageTaskTest { 46 private static final String ADMIN_PACKAGE_NAME = "com.admin.test"; 47 private static final String ADMIN_RECEIVER_NAME = ADMIN_PACKAGE_NAME + ".AdminReceiver"; 48 private static final ComponentName ADMIN_COMPONENT_NAME = new ComponentName(ADMIN_PACKAGE_NAME, 49 ADMIN_RECEIVER_NAME); 50 private static final String INSTALL_PACKAGE_NAME = "com.install.package"; 51 private static final int TEST_USER_ID = 123; 52 private static final int OTHER_TEST_USER_ID = 456; 53 private final ProvisioningParams TEST_PARAMS = new ProvisioningParams.Builder() 54 .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE) 55 .setDeviceAdminComponentName(ADMIN_COMPONENT_NAME) 56 .build(); 57 58 @Mock private Context mContext; 59 @Mock private PackageManager mPackageManager; 60 @Mock private AbstractProvisioningTask.Callback mCallback; 61 private InstallExistingPackageTask mTask; 62 63 @Before setUp()64 public void setUp() throws Exception { 65 MockitoAnnotations.initMocks(this); 66 67 when(mContext.getPackageManager()).thenReturn(mPackageManager); 68 69 mTask = new InstallExistingPackageTask(INSTALL_PACKAGE_NAME, mContext, TEST_PARAMS, 70 mCallback, mock(ProvisioningAnalyticsTracker.class), UserHandle.USER_NULL); 71 } 72 73 @Test testSuccess()74 public void testSuccess() throws Exception { 75 // GIVEN that installing the existing package succeeds 76 when(mPackageManager.installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID)) 77 .thenReturn(PackageManager.INSTALL_SUCCEEDED); 78 79 // WHEN running the task 80 mTask.run(TEST_USER_ID); 81 82 // THEN the existing package should have been installed 83 verify(mPackageManager).installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID); 84 verify(mCallback).onSuccess(mTask); 85 verifyNoMoreInteractions(mCallback); 86 } 87 88 @Test testSuccess_userIdFromConstructor()89 public void testSuccess_userIdFromConstructor() throws Exception { 90 // GIVEN that installing the existing package succeeds 91 when(mPackageManager.installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID)) 92 .thenReturn(PackageManager.INSTALL_SUCCEEDED); 93 94 InstallExistingPackageTask task = 95 new InstallExistingPackageTask(INSTALL_PACKAGE_NAME, mContext, TEST_PARAMS, 96 mCallback, mock(ProvisioningAnalyticsTracker.class), TEST_USER_ID); 97 // WHEN running the task 98 task.run(OTHER_TEST_USER_ID); 99 100 // THEN the existing package should have been installed 101 verify(mPackageManager).installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID); 102 verify(mCallback).onSuccess(task); 103 verifyNoMoreInteractions(mCallback); 104 } 105 106 @Test testPackageNotFound()107 public void testPackageNotFound() throws Exception { 108 // GIVEN that the package is not present on the device 109 when(mPackageManager.installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID)) 110 .thenThrow(new PackageManager.NameNotFoundException()); 111 112 // WHEN running the task 113 mTask.run(TEST_USER_ID); 114 115 // THEN an error should be returned 116 verify(mCallback).onError(mTask, 0, /* errorMessage= */ null); 117 verifyNoMoreInteractions(mCallback); 118 } 119 120 @Test testInstallFailed()121 public void testInstallFailed() throws Exception { 122 // GIVEN that the package is not present on the device 123 when(mPackageManager.installExistingPackageAsUser(INSTALL_PACKAGE_NAME, TEST_USER_ID)) 124 .thenReturn(PackageManager.INSTALL_FAILED_INVALID_APK); 125 126 // WHEN running the task 127 mTask.run(TEST_USER_ID); 128 129 // THEN an error should be returned 130 verify(mCallback).onError(mTask, 0, /* errorMessage= */ null); 131 verifyNoMoreInteractions(mCallback); 132 } 133 } 134