• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.cts.deviceowner;
2 
3 import android.app.PendingIntent;
4 import android.content.Intent;
5 import android.content.IntentSender;
6 import android.content.pm.PackageInstaller;
7 import android.content.pm.PackageManager;
8 
9 import com.android.compatibility.common.util.BlockingBroadcastReceiver;
10 
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 import java.util.Collections;
16 
17 /**
18  * Test case for package install and uninstall.
19  */
20 public class PackageInstallTest extends BaseAffiliatedProfileOwnerTest {
21     private static final String TEST_APP_LOCATION =
22             "/data/local/tmp/cts/packageinstaller/CtsEmptyTestApp.apk";
23     private static final String TEST_APP_PKG = "android.packageinstaller.emptytestapp.cts";
24     private static final int REQUEST_CODE = 0;
25     private static final String ACTION_INSTALL_COMMIT =
26             "com.android.cts.deviceowner.INTENT_PACKAGE_INSTALL_COMMIT";
27     private static final int PACKAGE_INSTALLER_STATUS_UNDEFINED = -1000;
28 
29     private PackageManager mPackageManager;
30     private PackageInstaller mPackageInstaller;
31     private PackageInstaller.Session mSession;
32     private BlockingBroadcastReceiver mBroadcastReceiver;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         super.setUp();
37         mPackageManager = mContext.getPackageManager();
38         mPackageInstaller = mPackageManager.getPackageInstaller();
39         assertNotNull(mPackageInstaller);
40 
41         mBroadcastReceiver = new BlockingBroadcastReceiver(mContext, ACTION_INSTALL_COMMIT);
42         mBroadcastReceiver.register();
43     }
44 
45     @Override
tearDown()46     protected void tearDown() throws Exception {
47         mBroadcastReceiver.unregisterQuietly();
48         if (mSession != null) {
49             mSession.abandon();
50         }
51 
52         super.tearDown();
53     }
54 
testPackageInstall()55     public void testPackageInstall() throws Exception {
56         assertFalse(isPackageInstalled(TEST_APP_PKG));
57 
58         // Install the package.
59         installPackage(TEST_APP_LOCATION);
60 
61         Intent intent = mBroadcastReceiver.awaitForBroadcast();
62         assertNotNull(intent);
63         assertEquals(PackageInstaller.STATUS_SUCCESS,
64                 intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
65                         PACKAGE_INSTALLER_STATUS_UNDEFINED));
66         assertEquals(TEST_APP_PKG, intent.getStringExtra(
67                 PackageInstaller.EXTRA_PACKAGE_NAME));
68         assertTrue(isPackageInstalled(TEST_APP_PKG));
69     }
70 
testPackageUninstall()71     public void testPackageUninstall() throws Exception {
72         assertTrue(isPackageInstalled(TEST_APP_PKG));
73 
74         // Uninstall the package.
75         mPackageInstaller.uninstall(TEST_APP_PKG, getCommitCallback());
76 
77         Intent intent = mBroadcastReceiver.awaitForBroadcast();
78         assertNotNull(intent);
79         assertEquals(PackageInstaller.STATUS_SUCCESS,
80                 intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
81                         PACKAGE_INSTALLER_STATUS_UNDEFINED));
82         assertEquals(TEST_APP_PKG, intent.getStringExtra(
83                 PackageInstaller.EXTRA_PACKAGE_NAME));
84         assertFalse(isPackageInstalled(TEST_APP_PKG));
85     }
86 
testKeepPackageCache()87     public void testKeepPackageCache() throws Exception {
88         // Set keep package cache.
89         mDevicePolicyManager.setKeepUninstalledPackages(getWho(),
90                 Collections.singletonList(TEST_APP_PKG));
91     }
92 
testInstallExistingPackage()93     public void testInstallExistingPackage() throws Exception {
94         assertFalse(isPackageInstalled(TEST_APP_PKG));
95 
96         // Install the existing package.
97         assertTrue(mDevicePolicyManager.installExistingPackage(getWho(), TEST_APP_PKG));
98         assertTrue(isPackageInstalled(TEST_APP_PKG));
99     }
100 
installPackage(String packageLocation)101     private void installPackage(String packageLocation) throws Exception {
102         PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
103                 PackageInstaller.SessionParams.MODE_FULL_INSTALL);
104         params.setAppPackageName(TEST_APP_PKG);
105         int sessionId = mPackageInstaller.createSession(params);
106         mSession = mPackageInstaller.openSession(sessionId);
107 
108         File file = new File(packageLocation);
109         InputStream in = new FileInputStream(file);
110         OutputStream out = mSession.openWrite("PackageInstallTest", 0, file.length());
111         byte[] buffer = new byte[65536];
112         int c;
113         while ((c = in.read(buffer)) != -1) {
114             out.write(buffer, 0, c);
115         }
116         mSession.fsync(out);
117         out.close();
118         mSession.commit(getCommitCallback());
119         mSession.close();
120     }
121 
getCommitCallback()122     private IntentSender getCommitCallback() {
123         // Create a PendingIntent and use it to generate the IntentSender
124         Intent broadcastIntent = new Intent(ACTION_INSTALL_COMMIT);
125         PendingIntent pendingIntent = PendingIntent.getBroadcast(
126                 mContext,
127                 REQUEST_CODE,
128                 broadcastIntent,
129                 PendingIntent.FLAG_UPDATE_CURRENT);
130         return pendingIntent.getIntentSender();
131     }
132 
isPackageInstalled(String packageName)133     private boolean isPackageInstalled(String packageName) {
134         try {
135             return mPackageManager.getPackageInfo(packageName, 0) != null;
136         } catch (PackageManager.NameNotFoundException e) {
137             return false;
138         }
139     }
140 }
141