1 /* 2 * Copyright (C) 2016 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 android.server.wm; 18 19 import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; 20 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 21 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 22 import static android.server.wm.ComponentNameUtils.getWindowName; 23 import static android.server.wm.app.Components.BROADCAST_RECEIVER_ACTIVITY; 24 import static android.server.wm.app.Components.DISMISS_KEYGUARD_ACTIVITY; 25 import static android.server.wm.app.Components.DISMISS_KEYGUARD_METHOD_ACTIVITY; 26 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY; 27 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY; 28 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY; 29 import static android.server.wm.app.Components.KEYGUARD_LOCK_ACTIVITY; 30 import static android.server.wm.app.Components.LAUNCHING_ACTIVITY; 31 import static android.server.wm.app.Components.NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY; 32 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ACTIVITY; 33 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ATTR_ACTIVITY; 34 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY; 35 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_DIALOG_ACTIVITY; 36 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY; 37 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY; 38 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY; 39 import static android.server.wm.app.Components.TEST_ACTIVITY; 40 import static android.server.wm.app.Components.TURN_SCREEN_ON_ACTIVITY; 41 import static android.server.wm.app.Components.TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY; 42 import static android.server.wm.app.Components.TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY; 43 import static android.view.Display.DEFAULT_DISPLAY; 44 import static android.view.Surface.ROTATION_90; 45 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; 46 47 import static org.junit.Assert.assertFalse; 48 import static org.junit.Assert.assertTrue; 49 import static org.junit.Assume.assumeTrue; 50 import static org.mockito.ArgumentMatchers.anyBoolean; 51 import static org.mockito.Mockito.never; 52 import static org.mockito.Mockito.spy; 53 import static org.mockito.Mockito.times; 54 import static org.mockito.Mockito.verify; 55 56 import android.Manifest; 57 import android.app.KeyguardManager.KeyguardLockedStateListener; 58 import android.app.WallpaperManager; 59 import android.content.ComponentName; 60 import android.content.res.Configuration; 61 import android.platform.test.annotations.Presubmit; 62 import android.server.wm.CommandSession.ActivitySession; 63 import android.server.wm.CommandSession.ActivitySessionClient; 64 import android.server.wm.app.Components; 65 66 import org.junit.Before; 67 import org.junit.Ignore; 68 import org.junit.Test; 69 70 /** 71 * Build/Install/Run: 72 * atest CtsWindowManagerDeviceTestCases:KeyguardTests 73 */ 74 @Presubmit 75 @android.server.wm.annotation.Group2 76 public class KeyguardTests extends KeyguardTestBase { 77 isLockscreenLiveWpEnabled()78 boolean isLockscreenLiveWpEnabled() { 79 WallpaperManager mWallpaperManager = mContext.getSystemService(WallpaperManager.class); 80 return mWallpaperManager != null && mWallpaperManager.isLockscreenLiveWallpaperEnabled(); 81 } 82 83 @Before 84 @Override setUp()85 public void setUp() throws Exception { 86 super.setUp(); 87 assumeTrue(supportsInsecureLock()); 88 assertFalse(isUiModeLockedToVrHeadset()); 89 } 90 91 @Test testKeyguardHidesActivity()92 public void testKeyguardHidesActivity() { 93 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 94 launchActivity(TEST_ACTIVITY); 95 waitAndAssertResumedActivity(TEST_ACTIVITY); 96 lockScreenSession.gotoKeyguard(); 97 mWmState.computeState(); 98 mWmState.assertKeyguardShowingAndNotOccluded(); 99 assertTrue(mKeyguardManager.isKeyguardLocked()); 100 waitAndAssertStoppedActivity(TEST_ACTIVITY); 101 102 mObjectTracker.close(lockScreenSession); 103 assertFalse(mKeyguardManager.isKeyguardLocked()); 104 } 105 106 @Test testShowWhenLockedActivity()107 public void testShowWhenLockedActivity() { 108 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 109 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 110 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 111 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ACTIVITY); 112 mWmState.computeState(); 113 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 114 mWmState.assertKeyguardShowingAndOccluded(); 115 } 116 117 /** 118 * Tests whether dialogs from SHOW_WHEN_LOCKED activities are also visible if Keyguard is 119 * showing. 120 */ 121 @Test testShowWhenLockedActivity_withDialog()122 public void testShowWhenLockedActivity_withDialog() { 123 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 124 launchActivity(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY); 125 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY); 126 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY); 127 mWmState.waitFor((wmState) -> wmState.allWindowSurfacesShown( 128 getWindowName(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY)), 129 "Wait for all windows visible for " + SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY); 130 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY); 131 assertTrue(mWmState.allWindowSurfacesShown( 132 getWindowName(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY))); 133 mWmState.assertKeyguardShowingAndOccluded(); 134 } 135 136 /** 137 * Tests whether multiple SHOW_WHEN_LOCKED activities are shown if the topmost is translucent. 138 */ 139 @Test testMultipleShowWhenLockedActivities()140 public void testMultipleShowWhenLockedActivities() { 141 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 142 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 143 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 144 launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 145 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 146 mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true); 147 mWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true); 148 lockScreenSession.gotoKeyguard( 149 SHOW_WHEN_LOCKED_ACTIVITY, SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 150 mWmState.computeState(); 151 mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true); 152 mWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true); 153 mWmState.assertKeyguardShowingAndOccluded(); 154 } 155 156 /** 157 * Tests that when top SHOW_WHEN_LOCKED activity is finishing and the next one is also 158 * SHOW_WHEN_LOCKED, it should be able to resume next SHOW_WHEN_LOCKED activity. 159 */ 160 @Test testFinishMultipleShowWhenLockedActivities()161 public void testFinishMultipleShowWhenLockedActivities() { 162 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 163 final ActivitySessionClient activitySession = createManagedActivityClientSession(); 164 165 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 166 final ActivitySession showWhenLockedActivitySession = 167 activitySession.startActivity(getLaunchActivityBuilder() 168 .setUseInstrumentation() 169 .setNewTask(true) 170 .setMultipleTask(true) 171 .setTargetActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY)); 172 173 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 174 mWmState.assertFocusedActivity("Launching Activity must be focused", 175 SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 176 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 177 178 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 179 mWmState.assertFocusedActivity("Top activity stay un-change", 180 SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 181 mWmState.assertKeyguardShowingAndOccluded(); 182 183 showWhenLockedActivitySession.finish(); 184 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 185 mWmState.assertFocusedActivity("ShowWhenLocked activity must occludes keyguard", 186 SHOW_WHEN_LOCKED_ACTIVITY); 187 mWmState.assertKeyguardShowingAndOccluded(); 188 } 189 190 /** 191 * If we have a translucent SHOW_WHEN_LOCKED_ACTIVITY, the wallpaper should also be showing. 192 */ 193 @Test testTranslucentShowWhenLockedActivity()194 public void testTranslucentShowWhenLockedActivity() { 195 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 196 launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 197 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 198 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 199 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 200 if (isLockscreenLiveWpEnabled()) { 201 // When the flag is not enabled, the behavior of this test is not well-defined. 202 // This check would pass with a shared wallpaper, but not with a separate lock screen 203 // wallpaper. 204 mWmState.waitAndAssertWindowShown(TYPE_WALLPAPER, true); 205 } 206 mWmState.assertKeyguardShowingAndOccluded(); 207 } 208 209 /** 210 * If we have a translucent SHOW_WHEN_LOCKED activity, the activity behind should not be shown. 211 */ 212 @Test testTranslucentDoesntRevealBehind()213 public void testTranslucentDoesntRevealBehind() { 214 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 215 launchActivity(TEST_ACTIVITY); 216 launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 217 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 218 mWmState.assertVisibility(TEST_ACTIVITY, true); 219 mWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true); 220 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 221 mWmState.computeState(); 222 waitAndAssertStoppedActivity(TEST_ACTIVITY); 223 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY); 224 mWmState.assertKeyguardShowingAndOccluded(); 225 } 226 227 @Test testDialogShowWhenLockedActivity()228 public void testDialogShowWhenLockedActivity() { 229 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 230 launchActivity(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY); 231 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY); 232 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY); 233 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY); 234 if (isLockscreenLiveWpEnabled()) { 235 // When the flag is not enabled, the behavior of this test is not well-defined. 236 // This check would pass with a shared wallpaper, but not with a separate lock screen 237 // wallpaper. 238 mWmState.waitAndAssertWindowShown(TYPE_WALLPAPER, true); 239 } 240 mWmState.assertKeyguardShowingAndOccluded(); 241 } 242 243 /** 244 * Test that showWhenLocked activity is fullscreen when shown over keyguard 245 */ 246 @Test 247 @Presubmit 248 // TODO (b/169271554): Temporarily switch activity to fullscreen when needing to showWhenLocked 249 @Ignore testShowWhenLockedActivityWhileSplit()250 public void testShowWhenLockedActivityWhileSplit() { 251 assumeTrue(supportsSplitScreenMultiWindow()); 252 253 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 254 launchActivitiesInSplitScreen( 255 getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY), 256 getLaunchActivityBuilder().setTargetActivity(SHOW_WHEN_LOCKED_ACTIVITY) 257 .setRandomData(true) 258 .setMultipleTask(false)); 259 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 260 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ACTIVITY); 261 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 262 mWmState.assertKeyguardShowingAndOccluded(); 263 WindowManagerState.Activity activity = mWmState.getActivity(SHOW_WHEN_LOCKED_ACTIVITY); 264 assertFalse(activity.getWindowingMode() == WINDOWING_MODE_MULTI_WINDOW); 265 } 266 267 /** 268 * Tests whether an activity that has called setInheritShowWhenLocked(true) above a 269 * SHOW_WHEN_LOCKED activity is visible if Keyguard is locked. 270 */ 271 @Test testInheritShowWhenLockedAdd()272 public void testInheritShowWhenLockedAdd() { 273 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 274 launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 275 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 276 277 launchActivity(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY); 278 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 279 waitAndAssertResumedActivity(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY); 280 281 lockScreenSession.gotoKeyguard(); 282 mWmState.computeState(); 283 mWmState.assertKeyguardShowingAndOccluded(); 284 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 285 waitAndAssertResumedActivity(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY); 286 } 287 288 /** 289 * Tests whether an activity that has the manifest attribute inheritShowWhenLocked but then 290 * calls setInheritShowWhenLocked(false) above a SHOW_WHEN_LOCKED activity is invisible if 291 * Keyguard is locked. 292 */ 293 @Test testInheritShowWhenLockedRemove()294 public void testInheritShowWhenLockedRemove() { 295 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 296 launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 297 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 298 299 launchActivity(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY); 300 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 301 waitAndAssertResumedActivity(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY); 302 303 lockScreenSession.gotoKeyguard(); 304 mWmState.computeState(); 305 mWmState.assertKeyguardShowingAndNotOccluded(); 306 assertTrue(mKeyguardManager.isKeyguardLocked()); 307 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 308 waitAndAssertStoppedActivity(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY); 309 } 310 311 /** 312 * Tests whether an activity that has the manifest attribute inheritShowWhenLocked above a 313 * SHOW_WHEN_LOCKED activity is visible if Keyguard is locked. 314 * */ 315 @Test testInheritShowWhenLockedAttr()316 public void testInheritShowWhenLockedAttr() { 317 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 318 launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 319 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 320 321 launchActivity(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 322 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 323 waitAndAssertResumedActivity(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 324 325 lockScreenSession.gotoKeyguard(); 326 mWmState.computeState(); 327 mWmState.assertKeyguardShowingAndOccluded(); 328 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 329 waitAndAssertResumedActivity(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 330 } 331 332 /** 333 * Tests whether an activity that doesn't have the manifest attribute inheritShowWhenLocked 334 * above a SHOW_WHEN_LOCKED activity is invisible if Keyguard is locked. 335 * */ 336 @Test testNoInheritShowWhenLocked()337 public void testNoInheritShowWhenLocked() { 338 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 339 launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 340 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 341 342 launchActivity(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 343 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 344 waitAndAssertResumedActivity(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 345 346 lockScreenSession.gotoKeyguard(); 347 mWmState.computeState(); 348 mWmState.assertKeyguardShowingAndNotOccluded(); 349 assertTrue(mKeyguardManager.isKeyguardLocked()); 350 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 351 waitAndAssertStoppedActivity(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 352 } 353 354 @Test testNoTransientConfigurationWhenShowWhenLockedRequestsOrientation()355 public void testNoTransientConfigurationWhenShowWhenLockedRequestsOrientation() { 356 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 357 final ActivitySessionClient activitySession = createManagedActivityClientSession(); 358 359 final ActivitySession showWhenLockedActivitySession = 360 activitySession.startActivity(getLaunchActivityBuilder() 361 .setUseInstrumentation() 362 .setTargetActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY)); 363 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 364 365 lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 366 367 separateTestJournal(); 368 369 final int displayId = mWmState 370 .getDisplayByActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY); 371 WindowManagerState.DisplayContent display = mWmState 372 .getDisplay(displayId); 373 final int origDisplayOrientation = display.mFullConfiguration.orientation; 374 final int orientation = origDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE 375 ? SCREEN_ORIENTATION_PORTRAIT 376 : SCREEN_ORIENTATION_LANDSCAPE; 377 showWhenLockedActivitySession.requestOrientation(orientation); 378 379 mWmState.waitForActivityOrientation(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY, 380 orientation == SCREEN_ORIENTATION_LANDSCAPE 381 ? Configuration.ORIENTATION_LANDSCAPE 382 : Configuration.ORIENTATION_PORTRAIT); 383 384 display = mWmState.getDisplay(displayId); 385 386 // If the window is a non-fullscreen window (e.g. a freeform window) or the display is 387 // squared, there won't be activity lifecycle. 388 if (display.mFullConfiguration.orientation != origDisplayOrientation) { 389 assertActivityLifecycle(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY, 390 false /* relaunched */); 391 } 392 } 393 394 /** 395 * Test that when a normal activity finished and an existing FLAG_DISMISS_KEYGUARD activity 396 * becomes the top activity, it should be resumed. 397 */ 398 @Test testResumeDismissKeyguardActivityFromBackground()399 public void testResumeDismissKeyguardActivityFromBackground() { 400 testResumeOccludingActivityFromBackground(DISMISS_KEYGUARD_ACTIVITY); 401 } 402 403 /** 404 * Test that when a normal activity finished and an existing SHOW_WHEN_LOCKED activity becomes 405 * the top activity, it should be resumed. 406 */ 407 @Test testResumeShowWhenLockedActivityFromBackground()408 public void testResumeShowWhenLockedActivityFromBackground() { 409 testResumeOccludingActivityFromBackground(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 410 } 411 testResumeOccludingActivityFromBackground(ComponentName occludingActivity)412 private void testResumeOccludingActivityFromBackground(ComponentName occludingActivity) { 413 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 414 lockScreenSession.gotoKeyguard(); 415 mWmState.assertKeyguardShowingAndNotOccluded(); 416 417 // Launch an activity which is able to occlude keyguard. 418 getLaunchActivityBuilder().setUseInstrumentation() 419 .setTargetActivity(occludingActivity).execute(); 420 421 // Launch an activity without SHOW_WHEN_LOCKED and finish it. 422 final int tdaFeatureId = mWmState.getTaskDisplayAreaFeatureId(occludingActivity); 423 getLaunchActivityBuilder().setUseInstrumentation() 424 .setMultipleTask(true) 425 // Don't wait for activity visible because keyguard will show. 426 .setWaitForLaunched(false) 427 .setTargetActivity(BROADCAST_RECEIVER_ACTIVITY) 428 .setLaunchTaskDisplayAreaFeatureId(tdaFeatureId) 429 .execute(); 430 mWmState.waitForKeyguardShowingAndNotOccluded(); 431 // The activity should be launched in same TDA to ensure that 432 // keyguard is showing and not occluded. 433 assumeTrue("Should launch in same TDA", 434 mWmState.getTaskDisplayArea(occludingActivity) 435 == mWmState.getTaskDisplayArea(BROADCAST_RECEIVER_ACTIVITY) 436 ); 437 mWmState.assertKeyguardShowingAndNotOccluded(); 438 439 mBroadcastActionTrigger.finishBroadcastReceiverActivity(); 440 mWmState.waitForKeyguardShowingAndOccluded(); 441 442 // The occluding activity should be resumed because it becomes the top activity. 443 waitAndAssertResumedActivity(occludingActivity); 444 assertTrue(occludingActivity + " must be resumed.", 445 mWmState.hasActivityState(occludingActivity, 446 WindowManagerState.STATE_RESUMED)); 447 } 448 449 @Test testTurnScreenOnActivityOnAod()450 public void testTurnScreenOnActivityOnAod() { 451 final AodSession aodSession = createManagedAodSession(); 452 assumeTrue(aodSession.isAodAvailable()); 453 aodSession.setAodEnabled(true); 454 455 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 456 lockScreenSession.sleepDevice(); 457 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 458 459 final CommandSession.ActivitySessionClient activityClient = 460 createManagedActivityClientSession(); 461 activityClient.startActivity( 462 getLaunchActivityBuilder().setUseInstrumentation().setIntentExtra(extra -> { 463 extra.putBoolean(Components.TurnScreenOnActivity.EXTRA_SHOW_WHEN_LOCKED, 464 false); 465 }).setTargetActivity(TURN_SCREEN_ON_ACTIVITY)); 466 467 mWmState.waitForAllStoppedActivities(); 468 // An activity without set showWhenLocked or dismissKeyguard cannot wakeup device and/or 469 // unlock insecure keyguard even if it has setTurnScreenOn, so the device should stay 470 // invisible and the display stay in dozing. 471 waitAndAssertStoppedActivity(TURN_SCREEN_ON_ACTIVITY); 472 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 473 assertFalse(isDisplayOn(DEFAULT_DISPLAY)); 474 } 475 476 @Test testShowWhenLockedActivityBeforeAod()477 public void testShowWhenLockedActivityBeforeAod() { 478 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 479 final AodSession aodSession = createManagedAodSession(); 480 assumeTrue(aodSession.isAodAvailable()); 481 aodSession.setAodEnabled(true); 482 483 // Unlocked; ShowWhenLockedActivity should be visible 484 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 485 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 486 487 // In AOD; ShowWhenLockedActivity should NOT be visible 488 lockScreenSession.sleepDevice(); 489 mWmState.waitForKeyguardShowingAndOccluded(); 490 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 491 492 // Awake; ShowWhenLockedActivity should be visible again 493 lockScreenSession.wakeUpDevice(); 494 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 495 mWmState.assertKeyguardShowingAndOccluded(); 496 } 497 498 @Test testShowWhenLockedActivityDuringAod()499 public void testShowWhenLockedActivityDuringAod() { 500 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 501 final AodSession aodSession = createManagedAodSession(); 502 assumeTrue(aodSession.isAodAvailable()); 503 aodSession.setAodEnabled(true); 504 505 // In AOD and locked 506 lockScreenSession.sleepDevice(); 507 mWmState.waitForKeyguardShowingAndOccluded(); 508 509 // Launch ShowWhenLockedActivity invisibly under AOD 510 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 511 waitAndAssertStoppedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 512 513 // Wake up; we should see ShowWhenLockedActivity instead of KeyGuard 514 lockScreenSession.wakeUpDevice(); 515 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 516 mWmState.assertKeyguardShowingAndOccluded(); 517 } 518 519 /** 520 * Tests whether a FLAG_DISMISS_KEYGUARD activity occludes Keyguard. 521 */ 522 @Test testDismissKeyguardActivity()523 public void testDismissKeyguardActivity() { 524 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 525 lockScreenSession.gotoKeyguard(); 526 mWmState.computeState(); 527 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 528 launchActivity(DISMISS_KEYGUARD_ACTIVITY); 529 mWmState.waitForKeyguardShowingAndOccluded(); 530 waitAndAssertResumedActivity(DISMISS_KEYGUARD_ACTIVITY); 531 mWmState.assertKeyguardShowingAndOccluded(); 532 } 533 534 @Test testDismissKeyguardActivity_method()535 public void testDismissKeyguardActivity_method() { 536 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 537 separateTestJournal(); 538 lockScreenSession.gotoKeyguard(); 539 mWmState.computeState(); 540 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 541 launchActivity(DISMISS_KEYGUARD_METHOD_ACTIVITY); 542 mWmState.waitForKeyguardGone(); 543 waitAndAssertResumedActivity(DISMISS_KEYGUARD_METHOD_ACTIVITY); 544 assertFalse(mWmState.getKeyguardControllerState().keyguardShowing); 545 assertOnDismissSucceeded(DISMISS_KEYGUARD_METHOD_ACTIVITY); 546 } 547 548 @Test testDismissKeyguardActivity_method_notTop()549 public void testDismissKeyguardActivity_method_notTop() { 550 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 551 separateTestJournal(); 552 lockScreenSession.gotoKeyguard(); 553 mWmState.computeState(); 554 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 555 launchActivity(BROADCAST_RECEIVER_ACTIVITY); 556 launchActivity(TEST_ACTIVITY); 557 mBroadcastActionTrigger.dismissKeyguardByMethod(); 558 assertOnDismissError(BROADCAST_RECEIVER_ACTIVITY); 559 } 560 561 @Test testDismissKeyguardActivity_method_turnScreenOn()562 public void testDismissKeyguardActivity_method_turnScreenOn() { 563 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 564 separateTestJournal(); 565 lockScreenSession.sleepDevice(); 566 mWmState.computeState(); 567 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 568 launchActivity(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY); 569 mWmState.waitForKeyguardGone(); 570 waitAndAssertResumedActivity(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY); 571 assertFalse(mWmState.getKeyguardControllerState().keyguardShowing); 572 assertOnDismissSucceeded(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY); 573 assertTrue(isDisplayOn(DEFAULT_DISPLAY)); 574 } 575 576 @Test testDismissKeyguard_fromShowWhenLocked_notAllowed()577 public void testDismissKeyguard_fromShowWhenLocked_notAllowed() { 578 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 579 lockScreenSession.gotoKeyguard(); 580 mWmState.assertKeyguardShowingAndNotOccluded(); 581 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 582 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 583 mWmState.assertKeyguardShowingAndOccluded(); 584 mBroadcastActionTrigger.dismissKeyguardByFlag(); 585 mWmState.assertKeyguardShowingAndOccluded(); 586 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 587 } 588 589 @Test testDismissKeyguard_fromActivityOption_onlyOnce()590 public void testDismissKeyguard_fromActivityOption_onlyOnce() { 591 // TODO(b/228431314): Move this test from CTS to flicker test. 592 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 593 594 lockScreenSession.gotoKeyguard(); 595 launchActivityWithDismissKeyguard(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); 596 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); 597 598 lockScreenSession.gotoKeyguard(); 599 assertFalse(mWmState.getKeyguardControllerState().mKeyguardGoingAway); 600 } 601 602 @Test testKeyguardLock()603 public void testKeyguardLock() { 604 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 605 lockScreenSession.gotoKeyguard(); 606 mWmState.assertKeyguardShowingAndNotOccluded(); 607 launchActivity(KEYGUARD_LOCK_ACTIVITY); 608 waitAndAssertResumedActivity(KEYGUARD_LOCK_ACTIVITY); 609 mBroadcastActionTrigger.finishBroadcastReceiverActivity(); 610 mWmState.waitForKeyguardShowingAndNotOccluded(); 611 mWmState.assertKeyguardShowingAndNotOccluded(); 612 } 613 614 615 /** 616 * Turn on keyguard, and launch an activity on top of the keyguard. 617 * Next, change the orientation of the device to rotate the activity. 618 * The activity should still remain above keyguard at this point. 619 * Send the 'finish' broadcast to dismiss the activity. 620 * Ensure that the activity is gone, and the keyguard is visible. 621 */ 622 @Test testUnoccludedRotationChange()623 public void testUnoccludedRotationChange() { 624 // Go home now to make sure Home is behind Keyguard. 625 launchHomeActivity(); 626 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 627 final RotationSession rotationSession = createManagedRotationSession(); 628 lockScreenSession.gotoKeyguard(); 629 mWmState.assertKeyguardShowingAndNotOccluded(); 630 631 launchActivity(SHOW_WHEN_LOCKED_ACTIVITY); 632 waitAndAssertResumedActivity(SHOW_WHEN_LOCKED_ACTIVITY); 633 634 rotationSession.set(ROTATION_90); 635 mBroadcastActionTrigger.finishBroadcastReceiverActivity(); 636 mWmState.waitForKeyguardShowingAndNotOccluded(); 637 mWmState.waitForDisplayUnfrozen(); 638 mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY); 639 mWmState.assertValidity(); 640 mWmState.assertHomeActivityVisible(false); 641 mWmState.assertKeyguardShowingAndNotOccluded(); 642 // The {@link SHOW_WHEN_LOCKED_ACTIVITY} has gone because of the 'finish' broadcast. 643 mWmState.waitAndAssertActivityRemoved(SHOW_WHEN_LOCKED_ACTIVITY); 644 } 645 646 @Test testDismissKeyguardAttrActivity_method_turnScreenOn()647 public void testDismissKeyguardAttrActivity_method_turnScreenOn() { 648 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 649 lockScreenSession.sleepDevice(); 650 651 separateTestJournal(); 652 mWmState.computeState(); 653 assertTrue(mWmState.getKeyguardControllerState().keyguardShowing); 654 launchActivity(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY); 655 mWmState.waitForKeyguardGone(); 656 waitAndAssertResumedActivity(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY); 657 assertFalse(mWmState.getKeyguardControllerState().keyguardShowing); 658 assertTrue(isDisplayOn(DEFAULT_DISPLAY)); 659 } 660 661 @Test testScreenOffWhileOccludedStopsActivityNoAod()662 public void testScreenOffWhileOccludedStopsActivityNoAod() { 663 final AodSession aodSession = createManagedAodSession(); 664 aodSession.setAodEnabled(false); 665 testScreenOffWhileOccludedStopsActivity(false /* assertAod */); 666 } 667 668 @Test testScreenOffWhileOccludedStopsActivityAod()669 public void testScreenOffWhileOccludedStopsActivityAod() { 670 final AodSession aodSession = createManagedAodSession(); 671 assumeTrue(aodSession.isAodAvailable()); 672 aodSession.setAodEnabled(true); 673 testScreenOffWhileOccludedStopsActivity(true /* assertAod */); 674 } 675 676 /** 677 * @param assertAod {@code true} to check AOD status, {@code false} otherwise. Note that when 678 * AOD is disabled for the default display, AOD status shouldn't be checked. 679 */ testScreenOffWhileOccludedStopsActivity(boolean assertAod)680 private void testScreenOffWhileOccludedStopsActivity(boolean assertAod) { 681 try (final LockScreenSession lockScreenSession = new LockScreenSession()) { 682 separateTestJournal(); 683 lockScreenSession.gotoKeyguard(); 684 mWmState.assertKeyguardShowingAndNotOccluded(); 685 launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 686 waitAndAssertTopResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, DEFAULT_DISPLAY, 687 "Activity with showWhenLocked attribute should be resumed."); 688 mWmState.assertKeyguardShowingAndOccluded(); 689 if (assertAod) { 690 mWmState.assertAodNotShowing(); 691 } 692 lockScreenSession.sleepDevice(); 693 if (assertAod) { 694 mWmState.assertAodShowing(); 695 } 696 mWmState.waitForAllStoppedActivities(); 697 assertSingleLaunchAndStop(SHOW_WHEN_LOCKED_ATTR_ACTIVITY); 698 } 699 } 700 701 @Test testScreenOffCausesSingleStopNoAod()702 public void testScreenOffCausesSingleStopNoAod() { 703 final AodSession aodSession = createManagedAodSession(); 704 aodSession.setAodEnabled(false); 705 testScreenOffCausesSingleStop(); 706 } 707 708 @Test testScreenOffCausesSingleStopAod()709 public void testScreenOffCausesSingleStopAod() { 710 final AodSession aodSession = createManagedAodSession(); 711 assumeTrue(aodSession.isAodAvailable()); 712 aodSession.setAodEnabled(true); 713 testScreenOffCausesSingleStop(); 714 } 715 testScreenOffCausesSingleStop()716 private void testScreenOffCausesSingleStop() { 717 try (final LockScreenSession lockScreenSession = new LockScreenSession()) { 718 separateTestJournal(); 719 launchActivity(TEST_ACTIVITY); 720 waitAndAssertResumedActivity(TEST_ACTIVITY); 721 lockScreenSession.sleepDevice(); 722 mWmState.waitForAllStoppedActivities(); 723 assertSingleLaunchAndStop(TEST_ACTIVITY); 724 } 725 726 } 727 728 @Test testAddKeyguardLockedStateListener_hideToShow_callbackInvoked()729 public void testAddKeyguardLockedStateListener_hideToShow_callbackInvoked() { 730 mInstrumentation.getUiAutomation().adoptShellPermissionIdentity( 731 Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE); 732 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 733 final KeyguardLockedStateListener listener = getTestKeyguardLockedStateListener(); 734 mKeyguardManager.addKeyguardLockedStateListener(mContext.getMainExecutor(), listener); 735 736 lockScreenSession.gotoKeyguard(); 737 lockScreenSession.gotoKeyguard(); // state not changed 738 739 verify(listener, times(1)).onKeyguardLockedStateChanged(true); 740 verify(listener, never()).onKeyguardLockedStateChanged(false); 741 742 mKeyguardManager.removeKeyguardLockedStateListener(listener); 743 mInstrumentation.getUiAutomation().dropShellPermissionIdentity(); 744 } 745 746 @Test testAddKeyguardLockedStateListener_showToHide_callbackInvoked()747 public void testAddKeyguardLockedStateListener_showToHide_callbackInvoked() { 748 mInstrumentation.getUiAutomation().adoptShellPermissionIdentity( 749 Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE); 750 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 751 final KeyguardLockedStateListener listener = getTestKeyguardLockedStateListener(); 752 mKeyguardManager.addKeyguardLockedStateListener(mContext.getMainExecutor(), listener); 753 754 lockScreenSession.gotoKeyguard(); 755 lockScreenSession.unlockDevice(); 756 lockScreenSession.unlockDevice(); // state not changed 757 758 verify(listener, times(1)).onKeyguardLockedStateChanged(true); 759 verify(listener, times(1)).onKeyguardLockedStateChanged(false); 760 761 mKeyguardManager.removeKeyguardLockedStateListener(listener); 762 mInstrumentation.getUiAutomation().dropShellPermissionIdentity(); 763 } 764 765 @Test testAddRemoveKeyguardLockedStateListener_callbackNotInvoked()766 public void testAddRemoveKeyguardLockedStateListener_callbackNotInvoked() { 767 mInstrumentation.getUiAutomation().adoptShellPermissionIdentity( 768 Manifest.permission.SUBSCRIBE_TO_KEYGUARD_LOCKED_STATE); 769 final LockScreenSession lockScreenSession = createManagedLockScreenSession(); 770 final KeyguardLockedStateListener listener = getTestKeyguardLockedStateListener(); 771 mKeyguardManager.addKeyguardLockedStateListener(mContext.getMainExecutor(), listener); 772 mKeyguardManager.removeKeyguardLockedStateListener(listener); 773 774 lockScreenSession.gotoKeyguard(); 775 lockScreenSession.unlockDevice(); 776 lockScreenSession.gotoKeyguard(); 777 778 verify(listener, never()).onKeyguardLockedStateChanged(anyBoolean()); 779 780 mInstrumentation.getUiAutomation().dropShellPermissionIdentity(); 781 } 782 getTestKeyguardLockedStateListener()783 public KeyguardLockedStateListener getTestKeyguardLockedStateListener() { 784 return spy(new TestKeyguardLockedStateListener()); 785 } 786 787 public static class TestKeyguardLockedStateListener implements KeyguardLockedStateListener { 788 @Override onKeyguardLockedStateChanged(boolean isKeyguardLocked)789 public void onKeyguardLockedStateChanged(boolean isKeyguardLocked) { 790 } 791 } 792 } 793