1 package com.android.cts.intent.sender; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.content.pm.PackageManager; 6 import android.content.res.Configuration; 7 import android.support.test.uiautomator.By; 8 import android.support.test.uiautomator.BySelector; 9 import android.support.test.uiautomator.Direction; 10 import android.support.test.uiautomator.UiDevice; 11 import android.support.test.uiautomator.UiObject2; 12 import android.support.test.uiautomator.Until; 13 import android.test.InstrumentationTestCase; 14 15 public class SuspendPackageTest extends InstrumentationTestCase { 16 private static final int WAIT_DIALOG_TIMEOUT_IN_MS = 5000; 17 private static final BySelector POPUP_IMAGE_SELECTOR = By 18 .clazz(android.widget.ImageView.class.getName()) 19 .res("com.android.settings:id/admin_support_icon") 20 .pkg("com.android.settings"); 21 22 private static final BySelector POPUP_TITLE_WATCH_SELECTOR = By 23 .clazz(android.widget.TextView.class.getName()) 24 .res("android:id/alertTitle") 25 .pkg("com.google.android.apps.wearable.settings"); 26 27 private static final BySelector POPUP_BUTTON_SELECTOR = By 28 .clazz(android.widget.Button.class.getName()) 29 .res("android:id/button1") 30 .pkg("com.android.settings"); 31 32 private IntentSenderActivity mActivity; 33 private Context mContext; 34 private PackageManager mPackageManager; 35 36 private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver"; 37 private static final String TARGET_ACTIVITY_NAME 38 = "com.android.cts.intent.receiver.SimpleIntentReceiverActivity"; 39 40 @Override 41 protected void setUp() throws Exception { 42 super.setUp(); 43 mContext = getInstrumentation().getTargetContext(); 44 mActivity = launchActivity(mContext.getPackageName(), IntentSenderActivity.class, null); 45 mPackageManager = mContext.getPackageManager(); 46 } 47 48 @Override 49 public void tearDown() throws Exception { 50 mActivity.finish(); 51 super.tearDown(); 52 } 53 54 public void testPackageSuspended() throws Exception { 55 assertPackageSuspended(true); 56 } 57 58 public void testPackageNotSuspended() throws Exception { 59 assertPackageSuspended(false); 60 } 61 62 /** 63 * Verify that the package is suspended by trying to start the activity inside it. If the 64 * package is not suspended, the target activity will return the result. 65 */ 66 private void assertPackageSuspended(boolean suspended) throws Exception { 67 Intent intent = new Intent(); 68 intent.setClassName(INTENT_RECEIVER_PKG, TARGET_ACTIVITY_NAME); 69 Intent result = mActivity.getResult(intent); 70 if (suspended) { 71 dismissPolicyTransparencyDialog(); 72 assertNull(result); 73 } else { 74 assertNotNull(result); 75 } 76 // No matter if it is suspended or not, we should be able to resolve the activity. 77 assertNotNull(mPackageManager.resolveActivity(intent, 0)); 78 } 79 80 /** 81 * Wait for the policy transparency dialog and dismiss it. 82 */ 83 private void dismissPolicyTransparencyDialog() { 84 final UiDevice device = UiDevice.getInstance(getInstrumentation()); 85 if (isWatch()) { 86 device.wait(Until.hasObject(POPUP_TITLE_WATCH_SELECTOR), WAIT_DIALOG_TIMEOUT_IN_MS); 87 final UiObject2 title = device.findObject(POPUP_TITLE_WATCH_SELECTOR); 88 assertNotNull("Policy transparency dialog title not found", title); 89 title.swipe(Direction.RIGHT, 1.0f); 90 } else { 91 device.wait(Until.hasObject(POPUP_IMAGE_SELECTOR), WAIT_DIALOG_TIMEOUT_IN_MS); 92 final UiObject2 icon = device.findObject(POPUP_IMAGE_SELECTOR); 93 assertNotNull("Policy transparency dialog icon not found", icon); 94 // "OK" button only present in the dialog if it is blocked by policy. 95 final UiObject2 button = device.findObject(POPUP_BUTTON_SELECTOR); 96 assertNotNull("OK button not found", button); 97 button.click(); 98 } 99 } 100 101 private boolean isWatch() { 102 return (getInstrumentation().getContext().getResources().getConfiguration().uiMode 103 & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_WATCH; 104 } 105 } 106