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.cts.packageinstaller; 18 19 import android.content.Intent; 20 import android.content.pm.PackageInstaller; 21 import android.support.test.uiautomator.By; 22 import android.support.test.uiautomator.BySelector; 23 import android.support.test.uiautomator.UiObject2; 24 import android.support.test.uiautomator.Until; 25 26 /** 27 * This class tests manual package install and uninstall by a device owner. 28 */ 29 public class ManualPackageInstallTest extends BasePackageInstallTest { 30 private static final int AUTOMATOR_WAIT_TIMEOUT = 5000; 31 private static final int INSTALL_WAIT_TIME = 5000; 32 33 private static final BySelector POPUP_BUTTON_SELECTOR = By 34 .clazz(android.widget.Button.class.getName()) 35 .res("android:id/button1") 36 .pkg("com.android.settings"); 37 private static final BySelector POPUP_IMAGE_SELECTOR = By 38 .clazz(android.widget.ImageView.class.getName()) 39 .res("com.android.settings:id/admin_support_icon") 40 .pkg("com.android.settings"); 41 private static final BySelector INSTALL_BUTTON_SELECTOR = By 42 .clazz(android.widget.Button.class.getName()) 43 .res("com.android.packageinstaller:id/ok_button"); 44 45 public void testManualInstallSucceeded() throws Exception { 46 assertInstallPackage(); 47 } 48 49 public void testManualInstallBlocked() throws Exception { 50 synchronized (mPackageInstallerTimeoutLock) { 51 mCallbackReceived = false; 52 mCallbackStatus = PACKAGE_INSTALLER_STATUS_UNDEFINED; 53 } 54 // Calls the original installPackage which does not click through the install button. 55 super.installPackage(TEST_APP_LOCATION); 56 synchronized (mPackageInstallerTimeoutLock) { 57 try { 58 mPackageInstallerTimeoutLock.wait(PACKAGE_INSTALLER_TIMEOUT_MS); 59 } catch (InterruptedException e) { 60 } 61 assertTrue(mCallbackReceived); 62 assertEquals(PackageInstaller.STATUS_PENDING_USER_ACTION, mCallbackStatus); 63 } 64 65 mCallbackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 66 mContext.startActivity(mCallbackIntent); 67 68 automateDismissInstallBlockedDialog(); 69 70 // Assuming installation is not synchronous, we should wait a while before checking. 71 Thread.sleep(INSTALL_WAIT_TIME); 72 assertFalse(isPackageInstalled(TEST_APP_PKG)); 73 } 74 75 @Override 76 protected void installPackage(String packageLocation) throws Exception { 77 super.installPackage(packageLocation); 78 79 synchronized (mPackageInstallerTimeoutLock) { 80 try { 81 mPackageInstallerTimeoutLock.wait(PACKAGE_INSTALLER_TIMEOUT_MS); 82 } catch (InterruptedException e) { 83 } 84 assertTrue(mCallbackReceived); 85 assertEquals(PackageInstaller.STATUS_PENDING_USER_ACTION, mCallbackStatus); 86 } 87 88 // Use a receiver to listen for package install. 89 synchronized (mPackageInstallerTimeoutLock) { 90 mCallbackReceived = false; 91 mCallbackStatus = PACKAGE_INSTALLER_STATUS_UNDEFINED; 92 } 93 94 mCallbackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 95 mContext.startActivity(mCallbackIntent); 96 97 automateInstallClick(); 98 } 99 100 private void automateInstallClick() { 101 mDevice.wait(Until.hasObject(INSTALL_BUTTON_SELECTOR), AUTOMATOR_WAIT_TIMEOUT); 102 UiObject2 button = mDevice.findObject(INSTALL_BUTTON_SELECTOR); 103 assertNotNull("Install button not found", button); 104 button.click(); 105 } 106 107 private void automateDismissInstallBlockedDialog() { 108 mDevice.wait(Until.hasObject(POPUP_IMAGE_SELECTOR), AUTOMATOR_WAIT_TIMEOUT); 109 UiObject2 icon = mDevice.findObject(POPUP_IMAGE_SELECTOR); 110 assertNotNull("Policy transparency dialog icon not found", icon); 111 // "OK" button only present in the dialog if it is blocked by policy. 112 UiObject2 button = mDevice.findObject(POPUP_BUTTON_SELECTOR); 113 assertNotNull("OK button not found", button); 114 button.click(); 115 } 116 } 117