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
testTurnScreenOnActivityOnAod()446     public void testTurnScreenOnActivityOnAod() {
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.waitForAllStoppedActivities();
464         // An activity without set showWhenLocked or dismissKeyguard cannot wakeup device and/or
465         // unlock insecure keyguard even if it has setTurnScreenOn, so the device should stay
466         // invisible and the display stay in dozing.
467         mWmState.assertVisibility(TURN_SCREEN_ON_ACTIVITY, false);
468         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
469         assertFalse(isDisplayOn(DEFAULT_DISPLAY));
470     }
471     /**
472      * Tests whether a FLAG_DISMISS_KEYGUARD activity occludes Keyguard.
473      */
474     @Test
testDismissKeyguardActivity()475     public void testDismissKeyguardActivity() {
476         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
477         lockScreenSession.gotoKeyguard();
478         mWmState.computeState();
479         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
480         launchActivity(DISMISS_KEYGUARD_ACTIVITY);
481         mWmState.waitForKeyguardShowingAndOccluded();
482         mWmState.computeState(DISMISS_KEYGUARD_ACTIVITY);
483         mWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
484         mWmState.assertKeyguardShowingAndOccluded();
485     }
486 
487     @Test
testDismissKeyguardActivity_method()488     public void testDismissKeyguardActivity_method() {
489         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
490         separateTestJournal();
491         lockScreenSession.gotoKeyguard();
492         mWmState.computeState();
493         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
494         launchActivity(DISMISS_KEYGUARD_METHOD_ACTIVITY);
495         mWmState.waitForKeyguardGone();
496         mWmState.computeState(DISMISS_KEYGUARD_METHOD_ACTIVITY);
497         mWmState.assertVisibility(DISMISS_KEYGUARD_METHOD_ACTIVITY, true);
498         assertFalse(mWmState.getKeyguardControllerState().keyguardShowing);
499         assertOnDismissSucceeded(DISMISS_KEYGUARD_METHOD_ACTIVITY);
500     }
501 
502     @Test
testDismissKeyguardActivity_method_notTop()503     public void testDismissKeyguardActivity_method_notTop() {
504         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
505         separateTestJournal();
506         lockScreenSession.gotoKeyguard();
507         mWmState.computeState();
508         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
509         launchActivity(BROADCAST_RECEIVER_ACTIVITY);
510         launchActivity(TEST_ACTIVITY);
511         mBroadcastActionTrigger.dismissKeyguardByMethod();
512         assertOnDismissError(BROADCAST_RECEIVER_ACTIVITY);
513     }
514 
515     @Test
testDismissKeyguardActivity_method_turnScreenOn()516     public void testDismissKeyguardActivity_method_turnScreenOn() {
517         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
518         separateTestJournal();
519         lockScreenSession.sleepDevice();
520         mWmState.computeState();
521         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
522         launchActivity(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
523         mWmState.waitForKeyguardGone();
524         mWmState.computeState(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
525         mWmState.assertVisibility(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY, true);
526         assertFalse(mWmState.getKeyguardControllerState().keyguardShowing);
527         assertOnDismissSucceeded(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
528         assertTrue(isDisplayOn(DEFAULT_DISPLAY));
529     }
530 
531     @Test
testDismissKeyguard_fromShowWhenLocked_notAllowed()532     public void testDismissKeyguard_fromShowWhenLocked_notAllowed() {
533         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
534         lockScreenSession.gotoKeyguard();
535         mWmState.assertKeyguardShowingAndNotOccluded();
536         launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
537         mWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
538         mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
539         mWmState.assertKeyguardShowingAndOccluded();
540         mBroadcastActionTrigger.dismissKeyguardByFlag();
541         mWmState.assertKeyguardShowingAndOccluded();
542         mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
543     }
544 
545     @Test
testKeyguardLock()546     public void testKeyguardLock() {
547         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
548         lockScreenSession.gotoKeyguard();
549         mWmState.assertKeyguardShowingAndNotOccluded();
550         launchActivity(KEYGUARD_LOCK_ACTIVITY);
551         mWmState.computeState(KEYGUARD_LOCK_ACTIVITY);
552         mWmState.assertVisibility(KEYGUARD_LOCK_ACTIVITY, true);
553         mBroadcastActionTrigger.finishBroadcastReceiverActivity();
554         mWmState.waitForKeyguardShowingAndNotOccluded();
555         mWmState.assertKeyguardShowingAndNotOccluded();
556     }
557 
558 
559     /**
560      * Turn on keyguard, and launch an activity on top of the keyguard.
561      * Next, change the orientation of the device to rotate the activity.
562      * The activity should still remain above keyguard at this point.
563      * Send the 'finish' broadcast to dismiss the activity.
564      * Ensure that the activity is gone, and the keyguard is visible.
565      */
566     @Test
testUnoccludedRotationChange()567     public void testUnoccludedRotationChange() {
568         // Go home now to make sure Home is behind Keyguard.
569         launchHomeActivity();
570         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
571         final RotationSession rotationSession = createManagedRotationSession();
572         lockScreenSession.gotoKeyguard();
573         mWmState.assertKeyguardShowingAndNotOccluded();
574 
575         launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
576         mWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
577         mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
578 
579         rotationSession.set(ROTATION_90);
580         mBroadcastActionTrigger.finishBroadcastReceiverActivity();
581         mWmState.waitForKeyguardShowingAndNotOccluded();
582         mWmState.waitForDisplayUnfrozen();
583         mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
584         mWmState.assertValidity();
585         mWmState.assertHomeActivityVisible(false);
586         mWmState.assertKeyguardShowingAndNotOccluded();
587         // The {@link SHOW_WHEN_LOCKED_ACTIVITY} has gone because of the 'finish' broadcast.
588         mWmState.waitAndAssertActivityRemoved(SHOW_WHEN_LOCKED_ACTIVITY);
589     }
590 
assertWallpaperShowing()591     private void assertWallpaperShowing() {
592         WindowState wallpaper =
593                 mWmState.findFirstWindowWithType(TYPE_WALLPAPER);
594         assertNotNull(wallpaper);
595         assertTrue(wallpaper.isSurfaceShown());
596     }
597 
598     @Test
testDismissKeyguardAttrActivity_method_turnScreenOn()599     public void testDismissKeyguardAttrActivity_method_turnScreenOn() {
600         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
601         lockScreenSession.sleepDevice();
602 
603         separateTestJournal();
604         mWmState.computeState();
605         assertTrue(mWmState.getKeyguardControllerState().keyguardShowing);
606         launchActivity(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY);
607         mWmState.waitForKeyguardGone();
608         mWmState.assertVisibility(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY, true);
609         assertFalse(mWmState.getKeyguardControllerState().keyguardShowing);
610         assertTrue(isDisplayOn(DEFAULT_DISPLAY));
611     }
612 
613     @Test
testScreenOffWhileOccludedStopsActivityNoAod()614     public void testScreenOffWhileOccludedStopsActivityNoAod() {
615         final AodSession aodSession = createManagedAodSession();
616         aodSession.setAodEnabled(false);
617         testScreenOffWhileOccludedStopsActivity(false /* assertAod */);
618     }
619 
620     @Test
testScreenOffWhileOccludedStopsActivityAod()621     public void testScreenOffWhileOccludedStopsActivityAod() {
622         final AodSession aodSession = createManagedAodSession();
623         assumeTrue(aodSession.isAodAvailable());
624         aodSession.setAodEnabled(true);
625         testScreenOffWhileOccludedStopsActivity(true /* assertAod */);
626     }
627 
628     /**
629      * @param assertAod {@code true} to check AOD status, {@code false} otherwise. Note that when
630      *        AOD is disabled for the default display, AOD status shouldn't be checked.
631      */
testScreenOffWhileOccludedStopsActivity(boolean assertAod)632     private void testScreenOffWhileOccludedStopsActivity(boolean assertAod) {
633         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
634             separateTestJournal();
635             lockScreenSession.gotoKeyguard();
636             mWmState.assertKeyguardShowingAndNotOccluded();
637             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
638             waitAndAssertTopResumedActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, DEFAULT_DISPLAY,
639                     "Activity with showWhenLocked attribute should be resumed.");
640             mWmState.assertKeyguardShowingAndOccluded();
641             if (assertAod) {
642                 mWmState.assertAodNotShowing();
643             }
644             lockScreenSession.sleepDevice();
645             if (assertAod) {
646                 mWmState.assertAodShowing();
647             }
648             mWmState.waitForAllStoppedActivities();
649             assertSingleLaunchAndStop(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
650         }
651     }
652 
653     @Test
testScreenOffCausesSingleStopNoAod()654     public void testScreenOffCausesSingleStopNoAod() {
655         final AodSession aodSession = createManagedAodSession();
656         aodSession.setAodEnabled(false);
657         testScreenOffCausesSingleStop();
658     }
659 
660     @Test
testScreenOffCausesSingleStopAod()661     public void testScreenOffCausesSingleStopAod() {
662         final AodSession aodSession = createManagedAodSession();
663         assumeTrue(aodSession.isAodAvailable());
664         aodSession.setAodEnabled(true);
665         testScreenOffCausesSingleStop();
666     }
667 
testScreenOffCausesSingleStop()668     private void testScreenOffCausesSingleStop() {
669         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
670             separateTestJournal();
671             launchActivity(TEST_ACTIVITY);
672             mWmState.assertVisibility(TEST_ACTIVITY, true);
673             lockScreenSession.sleepDevice();
674             mWmState.waitForAllStoppedActivities();
675             assertSingleLaunchAndStop(TEST_ACTIVITY);
676         }
677 
678     }
679 
680 }
681