Merge "Add CTS test for silent package install" into mnc-dev
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SilentPackageInstallerTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SilentPackageInstallerTest.java
new file mode 100644
index 0000000..8e63543
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SilentPackageInstallerTest.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.deviceowner;
+
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.IntentSender;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageInstaller;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * This class tests silent package install and uninstall by a device owner.
+ */
+public class SilentPackageInstallerTest extends BaseDeviceOwnerTest {
+ private static final String TEST_APP_LOCATION = "/data/local/tmp/CtsSimpleApp.apk";
+ private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp";
+ private static final int PACKAGE_INSTALLER_TIMEOUT_MS = 60000; // 60 seconds
+ private static final String ACTION_INSTALL_COMMIT =
+ "com.android.cts.deviceowner.INTENT_PACKAGE_INSTALL_COMMIT";
+
+ private PackageManager mPackageManager;
+ private PackageInstaller mPackageInstaller;
+ private PackageInstaller.Session mSession;
+ private boolean mCallbackReceived;
+
+ private final Object mPackageInstallerTimeoutLock = new Object();
+
+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ assertEquals(PackageInstaller.STATUS_SUCCESS, intent.getIntExtra(
+ PackageInstaller.EXTRA_STATUS,
+ PackageInstaller.STATUS_FAILURE));
+ assertEquals(TEST_APP_PKG, intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME));
+ mContext.unregisterReceiver(this);
+ synchronized (mPackageInstallerTimeoutLock) {
+ mCallbackReceived = true;
+ mPackageInstallerTimeoutLock.notify();
+ }
+ }
+ };
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mPackageManager = mContext.getPackageManager();
+ mPackageInstaller = mPackageManager.getPackageInstaller();
+ assertNotNull(mPackageInstaller);
+ mCallbackReceived = false;
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ try {
+ mContext.unregisterReceiver(mBroadcastReceiver);
+ } catch (IllegalArgumentException e) {
+ // ignore
+ }
+ if (mSession != null) {
+ mSession.abandon();
+ }
+ super.tearDown();
+ }
+
+ public void testSilentInstallUninstall() throws Exception {
+ // check that app is not already installed
+ assertFalse(isPackageInstalled(TEST_APP_PKG));
+
+ // install the app
+ installPackage(TEST_APP_LOCATION);
+ synchronized (mPackageInstallerTimeoutLock) {
+ try {
+ mPackageInstallerTimeoutLock.wait(PACKAGE_INSTALLER_TIMEOUT_MS);
+ } catch (InterruptedException e) {
+ }
+ assertTrue(mCallbackReceived);
+ }
+ assertTrue(isPackageInstalled(TEST_APP_PKG));
+
+ // uninstall the app again
+ synchronized (mPackageInstallerTimeoutLock) {
+ mCallbackReceived = false;
+ mPackageInstaller.uninstall(TEST_APP_PKG, getCommitCallback(0));
+ try {
+ mPackageInstallerTimeoutLock.wait(PACKAGE_INSTALLER_TIMEOUT_MS);
+ } catch (InterruptedException e) {
+ }
+ assertTrue(mCallbackReceived);
+ }
+ assertFalse(isPackageInstalled(TEST_APP_PKG));
+ }
+
+ private void installPackage(String packageLocation) throws Exception {
+ PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
+ PackageInstaller.SessionParams.MODE_FULL_INSTALL);
+ int sessionId = mPackageInstaller.createSession(params);
+ mSession = mPackageInstaller.openSession(sessionId);
+
+ File file = new File(packageLocation);
+ InputStream in = new FileInputStream(file);
+ OutputStream out = mSession.openWrite("SilentPackageInstallerTest", 0, file.length());
+ byte[] buffer = new byte[65536];
+ int c;
+ while ((c = in.read(buffer)) != -1) {
+ out.write(buffer, 0, c);
+ }
+ mSession.fsync(out);
+ out.close();
+ mSession.commit(getCommitCallback(sessionId));
+ mSession.close();
+ }
+
+ private IntentSender getCommitCallback(int sessionId) {
+ // Create an intent-filter and register the receiver
+ String action = ACTION_INSTALL_COMMIT + "." + sessionId;
+ IntentFilter intentFilter = new IntentFilter();
+ intentFilter.addAction(action);
+ mContext.registerReceiver(mBroadcastReceiver, intentFilter);
+
+ // Create a PendingIntent and use it to generate the IntentSender
+ Intent broadcastIntent = new Intent(action);
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(
+ mContext,
+ sessionId,
+ broadcastIntent,
+ PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
+ return pendingIntent.getIntentSender();
+ }
+
+ private boolean isPackageInstalled(String packageName) {
+ try {
+ PackageInfo pi = mPackageManager.getPackageInfo(packageName, 0);
+ return pi != null;
+ } catch (PackageManager.NameNotFoundException e) {
+ return false;
+ }
+ }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
index c0e6479..7e0d959 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
@@ -54,7 +54,7 @@
protected static final String ADMIN_RECEIVER_TEST_CLASS =
MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
- private CtsBuildHelper mCtsBuild;
+ protected CtsBuildHelper mCtsBuild;
private HashSet<String> mAvailableFeatures;
protected boolean mHasFeature;
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
index b84e9fe..65bb877 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
@@ -20,6 +20,8 @@
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.log.LogUtil.CLog;
+import java.io.File;
+
/**
* Set of tests for Device Owner use cases.
*/
@@ -28,6 +30,10 @@
private static final String DEVICE_OWNER_PKG = "com.android.cts.deviceowner";
private static final String DEVICE_OWNER_APK = "CtsDeviceOwnerApp.apk";
+ private static final String TEST_APP_APK = "CtsSimpleApp.apk";
+ private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp";
+ private static final String TEST_APP_LOCATION = "/data/local/tmp/";
+
private static final String ADMIN_RECEIVER_TEST_CLASS =
DEVICE_OWNER_PKG + ".BaseDeviceOwnerTest$BasicAdminReceiver";
private static final String CLEAR_DEVICE_OWNER_TEST_CLASS =
@@ -81,6 +87,19 @@
executeDeviceOwnerTest("ScreenCaptureDisabledTest");
}
+ public void testSilentPackageInstaller() throws Exception {
+ final File apk = mCtsBuild.getTestApp(TEST_APP_APK);
+ try {
+ getDevice().uninstallPackage(TEST_APP_PKG);
+ assertTrue(getDevice().pushFile(apk, TEST_APP_LOCATION + apk.getName()));
+ executeDeviceOwnerTest("SilentPackageInstallerTest");
+ } finally {
+ String command = "rm " + TEST_APP_LOCATION + apk.getName();
+ String commandOutput = getDevice().executeShellCommand(command);
+ getDevice().uninstallPackage(TEST_APP_PKG);
+ }
+ }
+
private void executeDeviceOwnerTest(String testClassName) throws Exception {
if (!mHasFeature) {
return;