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