1 /* 2 * Copyright (C) 2020 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.deviceandprofileowner; 18 19 import static android.app.admin.flags.Flags.FLAG_SECONDARY_LOCKSCREEN_API_ENABLED; 20 import static android.app.supervision.flags.Flags.FLAG_SUPERVISION_API; 21 22 import static com.android.cts.deviceandprofileowner.BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT; 23 24 import static junit.framework.Assert.assertFalse; 25 import static junit.framework.Assert.assertTrue; 26 27 import static org.junit.Assume.assumeTrue; 28 29 import android.app.admin.DevicePolicyManager; 30 import android.content.ComponentName; 31 import android.content.Context; 32 import android.content.Intent; 33 import android.content.pm.PackageManager; 34 import android.content.pm.ResolveInfo; 35 import android.os.PersistableBundle; 36 import android.os.Process; 37 import android.platform.test.annotations.RequiresFlagsEnabled; 38 import android.platform.test.flag.junit.CheckFlagsRule; 39 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 40 41 import androidx.test.InstrumentationRegistry; 42 import androidx.test.filters.RequiresDevice; 43 import androidx.test.runner.AndroidJUnit4; 44 import androidx.test.uiautomator.By; 45 import androidx.test.uiautomator.UiDevice; 46 import androidx.test.uiautomator.Until; 47 48 import org.junit.After; 49 import org.junit.Before; 50 import org.junit.Ignore; 51 import org.junit.Rule; 52 import org.junit.Test; 53 import org.junit.runner.RunWith; 54 55 import java.util.List; 56 57 // TODO(b/184280023): remove @RequiresDevice and @Ignores. 58 @RunWith(AndroidJUnit4.class) 59 @Ignore("Deprecated and no longer maintained") 60 public class SecondaryLockscreenTest { 61 62 @Rule 63 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 64 65 private static final int UI_AUTOMATOR_WAIT_TIME_MILLIS = 10000; 66 private static final String TAG = "SecondaryLockscreenTest"; 67 68 private Context mContext; 69 private DevicePolicyManager mDevicePolicyManager; 70 private UiDevice mUiDevice; 71 72 @Before setUp()73 public void setUp() throws Exception { 74 mContext = InstrumentationRegistry.getContext(); 75 mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); 76 mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 77 78 assumeTrue( 79 "Device does not support secure lock", 80 mContext.getPackageManager().hasSystemFeature( 81 PackageManager.FEATURE_SECURE_LOCK_SCREEN)); 82 83 // TODO(b/182994391): Replace with more generic solution to override the supervision 84 // component. 85 enableSupervisionTestAdmin(); 86 mUiDevice.executeShellCommand("locksettings set-disabled false"); 87 mUiDevice.executeShellCommand("locksettings set-pin 1234"); 88 89 mDevicePolicyManager.clearPackagePersistentPreferredActivities(ADMIN_RECEIVER_COMPONENT, 90 mContext.getPackageName()); 91 92 assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 93 mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, true); 94 assertTrue(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 95 } 96 97 @After tearDown()98 public void tearDown() throws Exception { 99 // Ensure that the test admin is enabled to clear the lockscreen state. 100 enableSupervisionTestAdmin(); 101 mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, false); 102 assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 103 // Remove any constant set, including enabling of the test admin. 104 mUiDevice.executeShellCommand("settings delete global device_policy_constants"); 105 mUiDevice.executeShellCommand("locksettings clear --old 1234"); 106 mUiDevice.executeShellCommand("locksettings set-disabled true"); 107 } 108 109 @Test 110 @Ignore("b/184280023") testSetSecondaryLockscreenEnabled()111 public void testSetSecondaryLockscreenEnabled() throws Exception { 112 enterKeyguardPin(); 113 assertTrue("Lockscreen title not shown", 114 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)), 115 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 116 117 mDevicePolicyManager.setSecondaryLockscreenEnabled(ADMIN_RECEIVER_COMPONENT, false); 118 119 // Verify that the lockscreen is dismissed after disabling the feature 120 assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 121 verifyHomeLauncherIsShown(); 122 } 123 124 @Test 125 @RequiresFlagsEnabled({FLAG_SECONDARY_LOCKSCREEN_API_ENABLED, FLAG_SUPERVISION_API}) testSetSecondaryLockscreenEnabledWithPersistableBundle()126 public void testSetSecondaryLockscreenEnabledWithPersistableBundle() throws Exception { 127 mDevicePolicyManager.setSecondaryLockscreenEnabled(true, new PersistableBundle()); 128 assertTrue(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 129 130 mDevicePolicyManager.setSecondaryLockscreenEnabled(false, new PersistableBundle()); 131 assertFalse(mDevicePolicyManager.isSecondaryLockscreenEnabled(Process.myUserHandle())); 132 } 133 134 @Test 135 @Ignore("b/184280023") testHomeButton()136 public void testHomeButton() throws Exception { 137 enterKeyguardPin(); 138 assertTrue("Lockscreen title not shown", 139 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)), 140 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 141 142 // Verify that pressing home does not dismiss the lockscreen 143 mUiDevice.pressHome(); 144 verifySecondaryLockscreenIsShown(); 145 } 146 147 @Test 148 @Ignore("b/184280023") testDismiss()149 public void testDismiss() throws Exception { 150 enterKeyguardPin(); 151 assertTrue("Lockscreen title not shown", 152 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)), 153 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 154 155 mUiDevice.findObject(By.text(SimpleKeyguardService.DISMISS_BUTTON_LABEL)).click(); 156 verifyHomeLauncherIsShown(); 157 158 // Verify that the feature is not disabled after dismissal 159 enterKeyguardPin(); 160 assertTrue(mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)), 161 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 162 verifySecondaryLockscreenIsShown(); 163 } 164 165 @RequiresDevice 166 @Test(expected = SecurityException.class) testSetSecondaryLockscreen_invalidAdmin()167 public void testSetSecondaryLockscreen_invalidAdmin() throws Exception { 168 final ComponentName badAdmin = new ComponentName("com.foo.bar", ".NonProfileOwnerReceiver"); 169 try { 170 disableSupervisionTestAdmin(); 171 mDevicePolicyManager.setSecondaryLockscreenEnabled(badAdmin, true); 172 } finally { 173 enableSupervisionTestAdmin(); 174 } 175 } 176 177 @Test(expected = SecurityException.class) 178 @RequiresFlagsEnabled({FLAG_SECONDARY_LOCKSCREEN_API_ENABLED, FLAG_SUPERVISION_API}) testSetSecondaryLockscreen_invalidCaller()179 public void testSetSecondaryLockscreen_invalidCaller() throws Exception { 180 try { 181 disableSupervisionTestAdmin(); 182 mDevicePolicyManager.setSecondaryLockscreenEnabled(true, null); 183 } finally { 184 enableSupervisionTestAdmin(); 185 } 186 } 187 enterKeyguardPin()188 private void enterKeyguardPin() throws Exception { 189 mUiDevice.executeShellCommand("input keyevent KEYCODE_SLEEP"); 190 mUiDevice.executeShellCommand("input keyevent KEYCODE_WAKEUP"); 191 assertTrue("Keyguard unexpectedly not shown", 192 mUiDevice.wait(Until.hasObject( 193 By.res("com.android.systemui", "keyguard_status_view")), 194 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 195 mUiDevice.executeShellCommand("wm dismiss-keyguard"); 196 assertTrue("Keyguard pin entry unexpectedly not shown", 197 mUiDevice.wait(Until.hasObject(By.res("com.android.systemui", "pinEntry")), 198 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 199 mUiDevice.executeShellCommand("input text 1234"); 200 mUiDevice.executeShellCommand("input keyevent KEYCODE_ENTER"); 201 } 202 verifyHomeLauncherIsShown()203 private void verifyHomeLauncherIsShown() { 204 String launcherPackageName = getLauncherPackageName(); 205 assertTrue("Lockscreen title is unexpectedly shown", 206 mUiDevice.wait(Until.gone(By.text(SimpleKeyguardService.TITLE_LABEL)), 207 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 208 assertTrue(String.format("Launcher (%s) is not shown", launcherPackageName), 209 mUiDevice.wait(Until.hasObject(By.pkg(launcherPackageName)), 210 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 211 } 212 verifySecondaryLockscreenIsShown()213 private void verifySecondaryLockscreenIsShown() { 214 String launcherPackageName = getLauncherPackageName(); 215 assertTrue("Lockscreen title is unexpectedly not shown", 216 mUiDevice.wait(Until.hasObject(By.text(SimpleKeyguardService.TITLE_LABEL)), 217 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 218 assertTrue(String.format("Launcher (%s) is unexpectedly shown", launcherPackageName), 219 mUiDevice.wait(Until.gone(By.pkg(launcherPackageName)), 220 UI_AUTOMATOR_WAIT_TIME_MILLIS)); 221 } 222 getLauncherPackageName()223 private String getLauncherPackageName() { 224 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME); 225 List<ResolveInfo> resolveInfos = mContext.getPackageManager().queryIntentActivities( 226 homeIntent, 0); 227 StringBuilder sb = new StringBuilder(); 228 for (ResolveInfo resolveInfo : resolveInfos) { 229 sb.append(resolveInfo.activityInfo.packageName).append("/").append( 230 resolveInfo.activityInfo.name).append(", "); 231 } 232 return resolveInfos.isEmpty() ? null : resolveInfos.get(0).activityInfo.packageName; 233 } 234 enableSupervisionTestAdmin()235 private void enableSupervisionTestAdmin() throws Exception { 236 mUiDevice.executeShellCommand("settings put global device_policy_constants " 237 + "use_test_admin_as_supervision_component=true"); 238 } 239 disableSupervisionTestAdmin()240 private void disableSupervisionTestAdmin() throws Exception { 241 mUiDevice.executeShellCommand("settings put global device_policy_constants " 242 + "use_test_admin_as_supervision_component=false"); 243 } 244 } 245