1 /* 2 * Copyright (C) 2015 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 org.mockito.Mockito.any; 20 import static org.mockito.Mockito.anyInt; 21 import static org.mockito.Mockito.anyString; 22 import static org.mockito.Mockito.doAnswer; 23 import static org.mockito.Mockito.eq; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.content.pm.ActivityInfo; 32 import android.content.pm.IPackageInstallObserver; 33 import android.content.pm.PackageManager; 34 import android.content.pm.PackageInfo; 35 import android.net.Uri; 36 import android.os.Bundle; 37 import android.test.AndroidTestCase; 38 import android.test.mock.MockContentProvider; 39 import android.test.mock.MockContentResolver; 40 import android.test.suitebuilder.annotation.SmallTest; 41 42 import org.mockito.invocation.InvocationOnMock; 43 import org.mockito.stubbing.Answer; 44 45 public class InstallPackageTaskTest extends AndroidTestCase { 46 private static final String TEST_PACKAGE_NAME = "com.android.test"; 47 private static final String TEST_PACKAGE_LOCATION = "/sdcard/TestPackage.apk"; 48 49 private static final String PACKAGE_NAME = InstallPackageTask.class.getPackage().getName(); 50 51 private Context mContext; 52 private PackageManager mPackageManager; 53 private InstallPackageTask.Callback mCallback; 54 private InstallPackageTask mTask; 55 private PackageInfo mPackageInfo; 56 private ActivityInfo mActivityInfo; 57 private MockContentResolver mContentResolver; 58 private MockContentProvider mContentProvider; 59 60 @Override 61 protected void setUp() throws Exception { 62 super.setUp(); 63 // this is necessary for mockito to work 64 System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); 65 66 mContext = mock(Context.class); 67 mActivityInfo = new ActivityInfo(); 68 mPackageInfo = new PackageInfo(); 69 mPackageManager = mock(PackageManager.class); 70 mContentProvider = new MyMockContentProvider(); 71 mContentResolver = new MockContentResolver(); 72 73 mActivityInfo.permission = android.Manifest.permission.BIND_DEVICE_ADMIN; 74 75 mPackageInfo.packageName = TEST_PACKAGE_NAME; 76 mPackageInfo.receivers = new ActivityInfo[] {mActivityInfo}; 77 78 mCallback = mock(InstallPackageTask.Callback.class); 79 80 when(mPackageManager.getPackageArchiveInfo(eq(TEST_PACKAGE_LOCATION), anyInt())). 81 thenReturn(mPackageInfo); 82 83 mContentResolver.addProvider("settings", mContentProvider); 84 85 when(mContext.getPackageManager()).thenReturn(mPackageManager); 86 when(mContext.getPackageName()).thenReturn(PACKAGE_NAME); 87 when(mContext.getContentResolver()).thenReturn(mContentResolver); 88 89 mTask = new InstallPackageTask(mContext, mCallback); 90 } 91 92 @SmallTest 93 public void testInstall_NoPackages() { 94 mTask.run(); 95 verify(mPackageManager, never()).installPackage( 96 any(Uri.class), 97 any(IPackageInstallObserver.class), 98 anyInt(), 99 anyString()); 100 verify(mCallback, times(1)).onSuccess(); 101 } 102 103 @SmallTest 104 public void testInstall_OnePackage() { 105 mTask.addInstallIfNecessary(TEST_PACKAGE_NAME, TEST_PACKAGE_LOCATION); 106 Answer installerAnswer = new Answer() { 107 @Override 108 public Object answer(InvocationOnMock invocation) throws Throwable { 109 Object[] arguments = invocation.getArguments(); 110 IPackageInstallObserver observer = (IPackageInstallObserver) 111 arguments[1]; 112 int flags = (Integer) arguments[2]; 113 // make sure that the flags value has been set 114 assertTrue(0 != (flags & PackageManager.INSTALL_REPLACE_EXISTING)); 115 observer.packageInstalled(TEST_PACKAGE_NAME, 116 PackageManager.INSTALL_SUCCEEDED); 117 return null; 118 } 119 }; 120 doAnswer(installerAnswer).when(mPackageManager).installPackage( 121 eq(Uri.parse("file://" + TEST_PACKAGE_LOCATION)), 122 any(IPackageInstallObserver.class), 123 anyInt(), 124 eq(PACKAGE_NAME)); 125 mTask.run(); 126 verify(mCallback, times(1)).onSuccess(); 127 } 128 129 @SmallTest 130 public void testInstall_InstallFailedVersionDowngrade() { 131 mTask.addInstallIfNecessary(TEST_PACKAGE_NAME, TEST_PACKAGE_LOCATION); 132 Answer installerAnswer = new Answer() { 133 @Override 134 public Object answer(InvocationOnMock invocation) throws Throwable { 135 Object[] arguments = invocation.getArguments(); 136 IPackageInstallObserver observer = (IPackageInstallObserver) 137 arguments[1]; 138 observer.packageInstalled(null, 139 PackageManager.INSTALL_FAILED_VERSION_DOWNGRADE); 140 return null; 141 } 142 }; 143 doAnswer(installerAnswer).when(mPackageManager).installPackage( 144 eq(Uri.parse("file://" + TEST_PACKAGE_LOCATION)), 145 any(IPackageInstallObserver.class), 146 anyInt(), 147 eq(PACKAGE_NAME)); 148 mTask.run(); 149 verify(mCallback, times(1)).onSuccess(); 150 } 151 152 @SmallTest 153 public void testPackageHasNoReceivers() { 154 mPackageInfo.receivers = null; 155 mTask.addInstallIfNecessary(TEST_PACKAGE_NAME, TEST_PACKAGE_LOCATION); 156 mTask.run(); 157 verify(mPackageManager, never()).installPackage( 158 any(Uri.class), 159 any(IPackageInstallObserver.class), 160 anyInt(), 161 anyString()); 162 verify(mCallback, times(1)).onError(InstallPackageTask.ERROR_PACKAGE_INVALID); 163 } 164 165 private static class MyMockContentProvider extends MockContentProvider { 166 public MyMockContentProvider() { 167 } 168 169 @Override 170 public Bundle call(String method, String request, Bundle args) { 171 return new Bundle(); 172 } 173 } 174 } 175