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.ACTIVITY_TYPE_STANDARD;
20 import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
21 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
22 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
23 import static android.server.wm.ComponentNameUtils.getActivityName;
24 import static android.server.wm.ComponentNameUtils.getWindowName;
25 import static android.server.wm.UiDeviceUtils.pressBackButton;
26 import static android.server.wm.UiDeviceUtils.pressHomeButton;
27 import static android.server.wm.app.Components.BROADCAST_RECEIVER_ACTIVITY;
28 import static android.server.wm.app.Components.DISMISS_KEYGUARD_ACTIVITY;
29 import static android.server.wm.app.Components.DISMISS_KEYGUARD_METHOD_ACTIVITY;
30 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY;
31 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY;
32 import static android.server.wm.app.Components.INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY;
33 import static android.server.wm.app.Components.KEYGUARD_LOCK_ACTIVITY;
34 import static android.server.wm.app.Components.LAUNCHING_ACTIVITY;
35 import static android.server.wm.app.Components.NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY;
36 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ACTIVITY;
37 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ATTR_ACTIVITY;
38 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY;
39 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_DIALOG_ACTIVITY;
40 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY;
41 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY;
42 import static android.server.wm.app.Components.TEST_ACTIVITY;
43 import static android.server.wm.app.Components.TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY;
44 import static android.server.wm.app.Components.TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY;
45 import static android.view.Display.DEFAULT_DISPLAY;
46 import static android.view.Surface.ROTATION_90;
47 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
48 
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertNotNull;
51 import static org.junit.Assert.assertTrue;
52 import static org.junit.Assume.assumeTrue;
53 
54 import android.content.ComponentName;
55 import android.content.res.Configuration;
56 import android.hardware.display.AmbientDisplayConfiguration;
57 import android.platform.test.annotations.Presubmit;
58 import android.provider.Settings;
59 import android.server.wm.CommandSession.ActivitySession;
60 import android.server.wm.CommandSession.ActivitySessionClient;
61 import android.server.wm.WindowManagerState.WindowState;
62 import android.server.wm.settings.SettingsSession;
63 
64 import androidx.test.filters.FlakyTest;
65 
66 import org.junit.Before;
67 import org.junit.Test;
68 
69 /**
70  * Build/Install/Run:
71  *     atest CtsWindowManagerDeviceTestCases:KeyguardTests
72  */
73 @Presubmit
74 public class KeyguardTests extends KeyguardTestBase {
75     class AodSession extends SettingsSession<Integer> {
76         private AmbientDisplayConfiguration mConfig;
77 
AodSession()78         AodSession() {
79             super(Settings.Secure.getUriFor(Settings.Secure.DOZE_ALWAYS_ON),
80                     Settings.Secure::getInt,
81                     Settings.Secure::putInt);
82             mConfig = new AmbientDisplayConfiguration(mContext);
83         }
84 
isAodAvailable()85         boolean isAodAvailable() {
86             return mConfig.alwaysOnAvailable();
87         }
88 
setAodEnabled(boolean enabled)89         void setAodEnabled(boolean enabled) throws Exception {
90             set(enabled ? 1 : 0);
91         }
92     }
93 
94     @Before
95     @Override
setUp()96     public void setUp() throws Exception {
97         super.setUp();
98         assumeTrue(supportsInsecureLock());
99         assertFalse(isUiModeLockedToVrHeadset());
100     }
101 
102     @Test
testKeyguardHidesActivity()103     public void testKeyguardHidesActivity() throws Exception {
104         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
105             launchActivity(TEST_ACTIVITY);
106             mAmWmState.computeState(TEST_ACTIVITY);
107             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
108             lockScreenSession.gotoKeyguard();
109             mAmWmState.computeState(true);
110             mAmWmState.assertKeyguardShowingAndNotOccluded();
111             assertTrue(mKeyguardManager.isKeyguardLocked());
112             mAmWmState.assertVisibility(TEST_ACTIVITY, false);
113         }
114         assertFalse(mKeyguardManager.isKeyguardLocked());
115     }
116 
117     @Test
118     @FlakyTest(bugId = 110276714)
testShowWhenLockedActivity()119     public void testShowWhenLockedActivity() throws Exception {
120         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
121             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
122             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
123             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
124             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ACTIVITY);
125             mAmWmState.computeState(true);
126             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
127             mAmWmState.assertKeyguardShowingAndOccluded();
128         }
129     }
130 
131     /**
132      * Tests whether dialogs from SHOW_WHEN_LOCKED activities are also visible if Keyguard is
133      * showing.
134      */
135     @Test
testShowWhenLockedActivity_withDialog()136     public void testShowWhenLockedActivity_withDialog() throws Exception {
137         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
138             launchActivity(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY);
139             mAmWmState.computeState(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY);
140             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY, true);
141             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY);
142             mAmWmState.computeState(true);
143             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY, true);
144             assertTrue(mAmWmState.getWmState().allWindowsVisible(
145                     getWindowName(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY)));
146             mAmWmState.assertKeyguardShowingAndOccluded();
147         }
148     }
149 
150     /**
151      * Tests whether multiple SHOW_WHEN_LOCKED activities are shown if the topmost is translucent.
152      */
153     @Test
testMultipleShowWhenLockedActivities()154     public void testMultipleShowWhenLockedActivities() throws Exception {
155         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
156             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
157             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
158             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY,
159                     SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
160             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
161             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
162             lockScreenSession.gotoKeyguard(
163                     SHOW_WHEN_LOCKED_ACTIVITY, SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
164             mAmWmState.computeState(true);
165             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
166             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
167             mAmWmState.assertKeyguardShowingAndOccluded();
168         }
169     }
170 
171     /**
172      * If we have a translucent SHOW_WHEN_LOCKED_ACTIVITY, the wallpaper should also be showing.
173      */
174     @Test
testTranslucentShowWhenLockedActivity()175     public void testTranslucentShowWhenLockedActivity() throws Exception {
176         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
177             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
178             mAmWmState.computeState(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
179             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
180             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
181             mAmWmState.computeState(true);
182             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
183             assertWallpaperShowing();
184             mAmWmState.assertKeyguardShowingAndOccluded();
185         }
186     }
187 
188     /**
189      * If we have a translucent SHOW_WHEN_LOCKED activity, the activity behind should not be shown.
190      */
191     @Test
192     @FlakyTest
testTranslucentDoesntRevealBehind()193     public void testTranslucentDoesntRevealBehind() throws Exception {
194         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
195             launchActivity(TEST_ACTIVITY);
196             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
197             mAmWmState.computeState(TEST_ACTIVITY, SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
198             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
199             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
200             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
201             mAmWmState.computeState(true);
202             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
203             mAmWmState.assertVisibility(TEST_ACTIVITY, false);
204             mAmWmState.assertKeyguardShowingAndOccluded();
205         }
206     }
207 
208     @Test
testDialogShowWhenLockedActivity()209     public void testDialogShowWhenLockedActivity() throws Exception {
210         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
211             launchActivity(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY);
212             mAmWmState.computeState(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY);
213             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY, true);
214             lockScreenSession.gotoKeyguard();
215             mAmWmState.computeState(true);
216             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY, true);
217             assertWallpaperShowing();
218             mAmWmState.assertKeyguardShowingAndOccluded();
219         }
220     }
221 
222     /**
223      * Test that showWhenLocked activity is fullscreen when shown over keyguard
224      */
225     @Test
226     @Presubmit
testShowWhenLockedActivityWhileSplit()227     public void testShowWhenLockedActivityWhileSplit() throws Exception {
228         assumeTrue(supportsSplitScreenMultiWindow());
229 
230         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
231             launchActivitiesInSplitScreen(
232                     getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
233                     getLaunchActivityBuilder().setTargetActivity(SHOW_WHEN_LOCKED_ACTIVITY)
234                             .setRandomData(true)
235                             .setMultipleTask(false)
236             );
237             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
238             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ACTIVITY);
239             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
240             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
241             mAmWmState.assertKeyguardShowingAndOccluded();
242             mAmWmState.assertDoesNotContainStack("Activity must be full screen.",
243                     WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_STANDARD);
244         }
245     }
246 
247     /**
248      * Tests whether an activity that has called setInheritShowWhenLocked(true) above a
249      * SHOW_WHEN_LOCKED activity is visible if Keyguard is locked.
250      */
251     @Test
252     @FlakyTest
testInheritShowWhenLockedAdd()253     public void testInheritShowWhenLockedAdd() throws Exception {
254         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
255             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
256             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
257 
258             launchActivity(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY);
259             mAmWmState.computeState(
260                     SHOW_WHEN_LOCKED_ATTR_ACTIVITY, INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY);
261             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
262             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY, true);
263 
264             lockScreenSession.gotoKeyguard();
265             mAmWmState.computeState(true);
266             mAmWmState.assertKeyguardShowingAndOccluded();
267             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
268             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_ADD_ACTIVITY, true);
269         }
270     }
271 
272     /**
273      * Tests whether an activity that has the manifest attribute inheritShowWhenLocked but then
274      * calls setInheritShowWhenLocked(false) above a SHOW_WHEN_LOCKED activity is invisible if
275      * Keyguard is locked.
276      */
277     @Test
278     @FlakyTest
testInheritShowWhenLockedRemove()279     public void testInheritShowWhenLockedRemove() throws Exception {
280         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
281             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
282             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
283 
284             launchActivity(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY);
285             mAmWmState.computeState(
286                     SHOW_WHEN_LOCKED_ATTR_ACTIVITY, INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY);
287             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
288             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY, true);
289 
290             lockScreenSession.gotoKeyguard();
291             mAmWmState.computeState(true);
292             mAmWmState.assertKeyguardShowingAndNotOccluded();
293             assertTrue(mKeyguardManager.isKeyguardLocked());
294             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
295             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_REMOVE_ACTIVITY, false);
296         }
297     }
298 
299     /**
300      * Tests whether an activity that has the manifest attribute inheritShowWhenLocked above a
301      * SHOW_WHEN_LOCKED activity is visible if Keyguard is locked.
302      * */
303     @Test
304     @FlakyTest
testInheritShowWhenLockedAttr()305     public void testInheritShowWhenLockedAttr() throws Exception {
306         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
307             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
308             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
309 
310             launchActivity(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
311             mAmWmState.computeState(
312                     SHOW_WHEN_LOCKED_ATTR_ACTIVITY, INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
313             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
314             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
315 
316             lockScreenSession.gotoKeyguard();
317             mAmWmState.computeState(true);
318             mAmWmState.assertKeyguardShowingAndOccluded();
319             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
320             mAmWmState.assertVisibility(INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
321         }
322     }
323 
324     /**
325      * Tests whether an activity that doesn't have the manifest attribute inheritShowWhenLocked
326      * above a SHOW_WHEN_LOCKED activity is invisible if Keyguard is locked.
327      * */
328     @Test
329     @FlakyTest
testNoInheritShowWhenLocked()330     public void testNoInheritShowWhenLocked() throws Exception {
331         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
332             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
333             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
334 
335             launchActivity(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
336             mAmWmState.computeState(
337                     SHOW_WHEN_LOCKED_ATTR_ACTIVITY, NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
338             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
339             mAmWmState.assertVisibility(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
340 
341             lockScreenSession.gotoKeyguard();
342             mAmWmState.computeState(true);
343             mAmWmState.assertKeyguardShowingAndNotOccluded();
344             assertTrue(mKeyguardManager.isKeyguardLocked());
345             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
346             mAmWmState.assertVisibility(NO_INHERIT_SHOW_WHEN_LOCKED_ATTR_ACTIVITY, false);
347         }
348     }
349 
350     @Test
testNoTransientConfigurationWhenShowWhenLockedRequestsOrientation()351     public void testNoTransientConfigurationWhenShowWhenLockedRequestsOrientation() {
352         try (final LockScreenSession lockScreenSession = new LockScreenSession();
353                 final ActivitySessionClient activitySession = new ActivitySessionClient(mContext)) {
354             final ActivitySession showWhenLockedActivitySession =
355                     activitySession.startActivity(getLaunchActivityBuilder()
356                             .setUseInstrumentation()
357                             .setTargetActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY));
358             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY, true);
359 
360             lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY);
361 
362             separateTestJournal();
363 
364             final int displayId = mAmWmState.getAmState()
365                     .getDisplayByActivity(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY);
366             ActivityManagerState.ActivityDisplay display = mAmWmState.getAmState()
367                     .getDisplay(displayId);
368             final int origDisplayOrientation = display.mFullConfiguration.orientation;
369             final int orientation = origDisplayOrientation == Configuration.ORIENTATION_LANDSCAPE
370                     ? SCREEN_ORIENTATION_PORTRAIT
371                     : SCREEN_ORIENTATION_LANDSCAPE;
372             showWhenLockedActivitySession.requestOrientation(orientation);
373 
374             mAmWmState.waitForActivityOrientation(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY,
375                     orientation == SCREEN_ORIENTATION_LANDSCAPE
376                             ? Configuration.ORIENTATION_LANDSCAPE
377                             : Configuration.ORIENTATION_PORTRAIT);
378 
379             display = mAmWmState.getAmState().getDisplay(displayId);
380 
381             // If the window is a non-fullscreen window (e.g. a freeform window) or the display is
382             // squared, there won't be activity lifecycle.
383             if (display.mFullConfiguration.orientation != origDisplayOrientation) {
384                 assertActivityLifecycle(SHOW_WHEN_LOCKED_ATTR_ROTATION_ACTIVITY,
385                         false /* relaunched */);
386             }
387         }
388     }
389 
390     /**
391      * Test that when a normal activity finished and an existing FLAG_DISMISS_KEYGUARD activity
392      * becomes the top activity, it should be resumed.
393      */
394     @Test
395     @FlakyTest
testResumeDismissKeyguardActivityFromBackground()396     public void testResumeDismissKeyguardActivityFromBackground() {
397         testResumeOccludingActivityFromBackground(DISMISS_KEYGUARD_ACTIVITY);
398     }
399 
400     /**
401      * Test that when a normal activity finished and an existing SHOW_WHEN_LOCKED activity becomes
402      * the top activity, it should be resumed.
403      */
404     @Test
testResumeShowWhenLockedActivityFromBackground()405     public void testResumeShowWhenLockedActivityFromBackground() {
406         testResumeOccludingActivityFromBackground(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
407     }
408 
testResumeOccludingActivityFromBackground(ComponentName occludingActivity)409     private void testResumeOccludingActivityFromBackground(ComponentName occludingActivity) {
410         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
411             lockScreenSession.gotoKeyguard();
412             mAmWmState.assertKeyguardShowingAndNotOccluded();
413 
414             // Launch an activity which is able to occlude keyguard.
415             getLaunchActivityBuilder().setUseInstrumentation()
416                     .setTargetActivity(occludingActivity).execute();
417 
418             // Launch an activity without SHOW_WHEN_LOCKED and finish it.
419             getLaunchActivityBuilder().setUseInstrumentation()
420                     .setMultipleTask(true)
421                     // Don't wait for activity visible because keyguard will show.
422                     .setWaitForLaunched(false)
423                     .setTargetActivity(BROADCAST_RECEIVER_ACTIVITY).execute();
424             mAmWmState.waitForKeyguardShowingAndNotOccluded();
425             mAmWmState.assertKeyguardShowingAndNotOccluded();
426 
427             mBroadcastActionTrigger.finishBroadcastReceiverActivity();
428             mAmWmState.waitForKeyguardShowingAndOccluded();
429 
430             // The occluding activity should be resumed because it becomes the top activity.
431             mAmWmState.computeState(occludingActivity);
432             mAmWmState.assertVisibility(occludingActivity, true);
433             assertTrue(occludingActivity + " must be resumed.",
434                     mAmWmState.getAmState().hasActivityState(occludingActivity,
435                             ActivityManagerState.STATE_RESUMED));
436         }
437     }
438 
439     /**
440      * Tests whether a FLAG_DISMISS_KEYGUARD activity occludes Keyguard.
441      */
442     @Test
testDismissKeyguardActivity()443     public void testDismissKeyguardActivity() throws Exception {
444         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
445             lockScreenSession.gotoKeyguard();
446             mAmWmState.computeState(true);
447             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
448             launchActivity(DISMISS_KEYGUARD_ACTIVITY);
449             mAmWmState.waitForKeyguardShowingAndOccluded();
450             mAmWmState.computeState(DISMISS_KEYGUARD_ACTIVITY);
451             mAmWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
452             mAmWmState.assertKeyguardShowingAndOccluded();
453         }
454     }
455 
456     @Test
testDismissKeyguardActivity_method()457     public void testDismissKeyguardActivity_method() throws Exception {
458         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
459             separateTestJournal();
460             lockScreenSession.gotoKeyguard();
461             mAmWmState.computeState(true);
462             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
463             launchActivity(DISMISS_KEYGUARD_METHOD_ACTIVITY);
464             mAmWmState.waitForKeyguardGone();
465             mAmWmState.computeState(DISMISS_KEYGUARD_METHOD_ACTIVITY);
466             mAmWmState.assertVisibility(DISMISS_KEYGUARD_METHOD_ACTIVITY, true);
467             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
468             assertOnDismissSucceeded(DISMISS_KEYGUARD_METHOD_ACTIVITY);
469         }
470     }
471 
472     @Test
testDismissKeyguardActivity_method_notTop()473     public void testDismissKeyguardActivity_method_notTop() throws Exception {
474         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
475             separateTestJournal();
476             lockScreenSession.gotoKeyguard();
477             mAmWmState.computeState(true);
478             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
479             launchActivity(BROADCAST_RECEIVER_ACTIVITY);
480             launchActivity(TEST_ACTIVITY);
481             mBroadcastActionTrigger.dismissKeyguardByMethod();
482             assertOnDismissError(BROADCAST_RECEIVER_ACTIVITY);
483         }
484     }
485 
486     @Test
testDismissKeyguardActivity_method_turnScreenOn()487     public void testDismissKeyguardActivity_method_turnScreenOn() throws Exception {
488         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
489             separateTestJournal();
490             lockScreenSession.sleepDevice();
491             mAmWmState.computeState(true);
492             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
493             launchActivity(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
494             mAmWmState.waitForKeyguardGone();
495             mAmWmState.computeState(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
496             mAmWmState.assertVisibility(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY, true);
497             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
498             assertOnDismissSucceeded(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
499             assertTrue(isDisplayOn(DEFAULT_DISPLAY));
500         }
501     }
502 
503     @Test
testDismissKeyguard_fromShowWhenLocked_notAllowed()504     public void testDismissKeyguard_fromShowWhenLocked_notAllowed() throws Exception {
505         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
506             lockScreenSession.gotoKeyguard();
507             mAmWmState.assertKeyguardShowingAndNotOccluded();
508             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
509             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
510             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
511             mAmWmState.assertKeyguardShowingAndOccluded();
512             mBroadcastActionTrigger.dismissKeyguardByFlag();
513             mAmWmState.assertKeyguardShowingAndOccluded();
514             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
515         }
516     }
517 
518     @Test
testKeyguardLock()519     public void testKeyguardLock() throws Exception {
520         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
521             lockScreenSession.gotoKeyguard();
522             mAmWmState.assertKeyguardShowingAndNotOccluded();
523             launchActivity(KEYGUARD_LOCK_ACTIVITY);
524             mAmWmState.computeState(KEYGUARD_LOCK_ACTIVITY);
525             mAmWmState.assertVisibility(KEYGUARD_LOCK_ACTIVITY, true);
526             mBroadcastActionTrigger.finishBroadcastReceiverActivity();
527             mAmWmState.waitForKeyguardShowingAndNotOccluded();
528             mAmWmState.assertKeyguardShowingAndNotOccluded();
529         }
530     }
531 
532     @Test
testUnoccludeRotationChange()533     public void testUnoccludeRotationChange() throws Exception {
534 
535         // Go home now to make sure Home is behind Keyguard.
536         pressHomeButton();
537         try (final LockScreenSession lockScreenSession = new LockScreenSession();
538              final RotationSession rotationSession = new RotationSession()) {
539             lockScreenSession.gotoKeyguard();
540             mAmWmState.assertKeyguardShowingAndNotOccluded();
541             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
542             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
543             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
544 
545             rotationSession.set(ROTATION_90);
546             pressBackButton();
547             mAmWmState.waitForKeyguardShowingAndNotOccluded();
548             mAmWmState.waitForDisplayUnfrozen();
549             mAmWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
550             mAmWmState.assertSanity();
551             mAmWmState.assertHomeActivityVisible(false);
552             mAmWmState.assertKeyguardShowingAndNotOccluded();
553             // The activity may not be destroyed immediately.
554             mAmWmState.waitForWithWmState(
555                     wmState -> !wmState.containsWindow(getWindowName(SHOW_WHEN_LOCKED_ACTIVITY)),
556                     "Waiting for " + getActivityName(SHOW_WHEN_LOCKED_ACTIVITY) + " to be removed");
557             // The {@link SHOW_WHEN_LOCKED_ACTIVITY} has gone because of {@link pressBackButton()}.
558             mAmWmState.assertNotExist(SHOW_WHEN_LOCKED_ACTIVITY);
559         }
560     }
561 
assertWallpaperShowing()562     private void assertWallpaperShowing() {
563         WindowState wallpaper =
564                 mAmWmState.getWmState().findFirstWindowWithType(TYPE_WALLPAPER);
565         assertNotNull(wallpaper);
566         assertTrue(wallpaper.isShown());
567     }
568 
569     @Test
testDismissKeyguardAttrActivity_method_turnScreenOn()570     public void testDismissKeyguardAttrActivity_method_turnScreenOn() throws Exception {
571         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
572             lockScreenSession.sleepDevice();
573 
574             separateTestJournal();
575             mAmWmState.computeState(true);
576             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
577             launchActivity(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY);
578             mAmWmState.waitForKeyguardGone();
579             mAmWmState.assertVisibility(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY, true);
580             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
581             assertOnDismissSucceeded(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY);
582             assertTrue(isDisplayOn(DEFAULT_DISPLAY));
583         }
584     }
585 
586     @Test
testScreenOffWhileOccludedStopsActivityNoAod()587     public void testScreenOffWhileOccludedStopsActivityNoAod() throws Exception {
588         try (final AodSession aodSession = new AodSession()) {
589             aodSession.setAodEnabled(false);
590             testScreenOffWhileOccludedStopsActivity(false /* assertAod */);
591         }
592     }
593 
594     @Test
testScreenOffWhileOccludedStopsActivityAod()595     public void testScreenOffWhileOccludedStopsActivityAod() throws Exception {
596         try (final AodSession aodSession = new AodSession()) {
597             assumeTrue(aodSession.isAodAvailable());
598             aodSession.setAodEnabled(true);
599             testScreenOffWhileOccludedStopsActivity(true /* assertAod */);
600         }
601     }
602 
603     /**
604      * @param assertAod {@code true} to check AOD status, {@code false} otherwise. Note that when
605      *        AOD is disabled for the default display, AOD status shouldn't be checked.
606      */
testScreenOffWhileOccludedStopsActivity(boolean assertAod)607     private void testScreenOffWhileOccludedStopsActivity(boolean assertAod) {
608         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
609             separateTestJournal();
610             lockScreenSession.gotoKeyguard();
611             mAmWmState.assertKeyguardShowingAndNotOccluded();
612             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
613             waitAndAssertTopResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, DEFAULT_DISPLAY,
614                     "Activity with showWhenLocked attribute should be resumed.");
615             mAmWmState.assertKeyguardShowingAndOccluded();
616             if (assertAod) {
617                 mAmWmState.assertAodNotShowing();
618             }
619             lockScreenSession.sleepDevice();
620             if (assertAod) {
621                 mAmWmState.assertAodShowing();
622             }
623             mAmWmState.waitForAllStoppedActivities();
624             assertSingleLaunchAndStop(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
625         }
626     }
627 
628     @Test
testScreenOffCausesSingleStopNoAod()629     public void testScreenOffCausesSingleStopNoAod() throws Exception {
630         try (final AodSession aodSession = new AodSession()) {
631             aodSession.setAodEnabled(false);
632             testScreenOffCausesSingleStop();
633         }
634     }
635 
636     @Test
testScreenOffCausesSingleStopAod()637     public void testScreenOffCausesSingleStopAod() throws Exception {
638         try (final AodSession aodSession = new AodSession()) {
639             assumeTrue(aodSession.isAodAvailable());
640             aodSession.setAodEnabled(true);
641             testScreenOffCausesSingleStop();
642         }
643     }
644 
testScreenOffCausesSingleStop()645     private void testScreenOffCausesSingleStop() {
646         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
647             separateTestJournal();
648             launchActivity(TEST_ACTIVITY);
649             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
650             lockScreenSession.sleepDevice();
651             mAmWmState.waitForAllStoppedActivities();
652             assertSingleLaunchAndStop(TEST_ACTIVITY);
653         }
654 
655     }
656 
657 }
658