1 /* 2 * Copyright (C) 2017 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 package com.android.cts.deviceandprofileowner; 17 18 import android.app.ActivityManager; 19 import android.app.ActivityOptions; 20 import android.app.admin.DevicePolicyManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.os.Build; 28 import android.os.Bundle; 29 import android.os.SystemClock; 30 import android.support.test.uiautomator.By; 31 import android.support.test.uiautomator.UiDevice; 32 import android.support.test.uiautomator.Until; 33 import android.telecom.TelecomManager; 34 import android.util.Log; 35 36 import androidx.test.InstrumentationRegistry; 37 38 import java.time.Duration; 39 import com.android.compatibility.common.util.PollingCheck; 40 41 /** 42 * Test class that is meant to be driven from the host and can't be run alone, which is required 43 * for tests that include rebooting or other connection-breaking steps. For this reason, this class 44 * does not override tearDown and setUp just initializes the test state, changing nothing in the 45 * device. Therefore, the host is responsible for making sure the tests leave the device in a clean 46 * state after running. 47 */ 48 public class LockTaskHostDrivenTest extends BaseDeviceAdminTest { 49 50 private static final String TAG = LockTaskHostDrivenTest.class.getName(); 51 private static final int ACTIVITY_RESUMED_TIMEOUT_MILLIS = 20000; // 20 seconds 52 private static final int LOCK_TASK_STATE_CHANGE_TIMEOUT_MILLIS = 10000; // 10 seconds 53 private static final String ACTION_EMERGENCY_DIAL = "com.android.phone.EmergencyDialer.DIAL"; 54 private static final String LOCK_TASK_ACTIVITY 55 = LockTaskUtilityActivityIfAllowed.class.getName(); 56 57 private UiDevice mUiDevice; 58 private Context mContext; 59 private PackageManager mPackageManager; 60 private ActivityManager mActivityManager; 61 private DevicePolicyManager mDevicePolicyManager; 62 setUp()63 public void setUp() { 64 mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 65 mContext = InstrumentationRegistry.getContext(); 66 mPackageManager = mContext.getPackageManager(); 67 mActivityManager = mContext.getSystemService(ActivityManager.class); 68 mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); 69 } 70 testStartLockTask_noAsserts()71 public void testStartLockTask_noAsserts() throws Exception { 72 Log.d(TAG, "testStartLockTask_noAsserts on host-driven test (no cleanup)"); 73 setLockTaskPackages(mContext.getPackageName()); 74 setDefaultHomeIntentReceiver(); 75 launchLockTaskActivity(); 76 mUiDevice.waitForIdle(); 77 } 78 testCleanupLockTask_noAsserts()79 public void testCleanupLockTask_noAsserts() { 80 Log.d(TAG, "testCleanupLockTask_noAsserts on host-driven test"); 81 mDevicePolicyManager.clearPackagePersistentPreferredActivities( 82 ADMIN_RECEIVER_COMPONENT, 83 mContext.getPackageName()); 84 setLockTaskPackages(); 85 mDevicePolicyManager.setLockTaskFeatures(ADMIN_RECEIVER_COMPONENT, 0); 86 // In case some activity is still in foreground 87 mUiDevice.pressHome(); 88 } 89 testLockTaskIsActive()90 public void testLockTaskIsActive() throws Exception { 91 Log.d(TAG, "testLockTaskIsActive on host-driven test"); 92 waitAndCheckLockedActivityIsResumed(); 93 checkLockedActivityIsRunning(); 94 } 95 96 /** 97 * On low-RAM devices, this test can take too long to finish, so the test runner can incorrectly 98 * assume it's finished. Therefore, only use it once in a given test. 99 * 100 * <p>Does not test that the locked activity is initially in the foreground, since running this 101 * test in instrumentation can immediately kill the locked activity (while maintaining lock task 102 * mode). 103 */ testLockTaskIsActiveAndCantBeInterrupted()104 public void testLockTaskIsActiveAndCantBeInterrupted() throws Exception { 105 Log.d(TAG, "testLockTaskIsActiveAndCantBeInterrupted on host-driven test"); 106 waitAndEnsureLockTaskUtilityActivityIsRunning(); 107 checkLockedActivityIsRunning(); 108 109 mUiDevice.pressHome(); 110 mUiDevice.waitForIdle(); 111 checkLockedActivityIsRunning(); 112 113 mUiDevice.pressRecentApps(); 114 mUiDevice.waitForIdle(); 115 checkLockedActivityIsRunning(); 116 117 mUiDevice.pressBack(); 118 mUiDevice.waitForIdle(); 119 checkLockedActivityIsRunning(); 120 121 mUiDevice.waitForIdle(); 122 } 123 checkLockedActivityIsRunning()124 private void checkLockedActivityIsRunning() { 125 String activityName = 126 mActivityManager.getAppTasks().get(0).getTaskInfo().topActivity.getClassName(); 127 assertEquals(LOCK_TASK_ACTIVITY, activityName); 128 129 PollingCheck.waitFor( 130 () -> (mActivityManager.getLockTaskModeState() 131 == ActivityManager.LOCK_TASK_MODE_LOCKED)); 132 } 133 134 /** 135 * Ensures the locked activity is resumed or otherwise launches it but without starting lock 136 * task if it is not already in that mode. 137 */ waitAndEnsureLockTaskUtilityActivityIsRunning()138 private void waitAndEnsureLockTaskUtilityActivityIsRunning() throws Exception { 139 mUiDevice.waitForIdle(); 140 final boolean lockedActivityIsResumed = 141 LockTaskUtilityActivity.waitUntilActivityResumed(ACTIVITY_RESUMED_TIMEOUT_MILLIS); 142 if (!lockedActivityIsResumed) { 143 launchLockTaskUtilityActivityWithoutStartingLockTask(); 144 waitAndCheckLockedActivityIsResumed(); 145 } 146 } 147 waitAndCheckLockedActivityIsResumed()148 private void waitAndCheckLockedActivityIsResumed() throws Exception { 149 mUiDevice.waitForIdle(); 150 assertTrue( 151 LockTaskUtilityActivity.waitUntilActivityResumed(ACTIVITY_RESUMED_TIMEOUT_MILLIS)); 152 } 153 launchLockTaskActivity()154 private void launchLockTaskActivity() { 155 Intent intent = new Intent(mContext, LockTaskUtilityActivityIfAllowed.class); 156 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 157 intent.putExtra(LockTaskUtilityActivity.START_LOCK_TASK, true); 158 mContext.startActivity(intent); 159 } 160 launchLockTaskUtilityActivityWithoutStartingLockTask()161 private void launchLockTaskUtilityActivityWithoutStartingLockTask() { 162 final Intent intent = new Intent(mContext, LockTaskUtilityActivityIfAllowed.class); 163 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 164 mContext.startActivity(intent); 165 } 166 setLockTaskPackages(String... packages)167 private void setLockTaskPackages(String... packages) { 168 mDevicePolicyManager.setLockTaskPackages(ADMIN_RECEIVER_COMPONENT, packages); 169 } 170 setDefaultHomeIntentReceiver()171 private void setDefaultHomeIntentReceiver() { 172 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN); 173 intentFilter.addCategory(Intent.CATEGORY_HOME); 174 intentFilter.addCategory(Intent.CATEGORY_DEFAULT); 175 mDevicePolicyManager.addPersistentPreferredActivity( 176 ADMIN_RECEIVER_COMPONENT, intentFilter, 177 new ComponentName(mContext.getPackageName(), LOCK_TASK_ACTIVITY)); 178 } 179 } 180