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