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.am;
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.server.am.ComponentNameUtils.getWindowName;
22 import static android.server.am.Components.BROADCAST_RECEIVER_ACTIVITY;
23 import static android.server.am.Components.BroadcastReceiverActivity.ACTION_TRIGGER_BROADCAST;
24 import static android.server.am.Components.BroadcastReceiverActivity.EXTRA_DISMISS_KEYGUARD;
25 import static android.server.am.Components.BroadcastReceiverActivity.EXTRA_DISMISS_KEYGUARD_METHOD;
26 import static android.server.am.Components.BroadcastReceiverActivity.EXTRA_FINISH_BROADCAST;
27 import static android.server.am.Components.DISMISS_KEYGUARD_ACTIVITY;
28 import static android.server.am.Components.DISMISS_KEYGUARD_METHOD_ACTIVITY;
29 import static android.server.am.Components.KEYGUARD_LOCK_ACTIVITY;
30 import static android.server.am.Components.LAUNCHING_ACTIVITY;
31 import static android.server.am.Components.SHOW_WHEN_LOCKED_ACTIVITY;
32 import static android.server.am.Components.SHOW_WHEN_LOCKED_ATTR_ACTIVITY;
33 import static android.server.am.Components.SHOW_WHEN_LOCKED_DIALOG_ACTIVITY;
34 import static android.server.am.Components.SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY;
35 import static android.server.am.Components.SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY;
36 import static android.server.am.Components.TEST_ACTIVITY;
37 import static android.server.am.Components.TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY;
38 import static android.server.am.Components.TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY;
39 import static android.server.am.UiDeviceUtils.pressBackButton;
40 import static android.server.am.UiDeviceUtils.pressHomeButton;
41 import static android.view.Surface.ROTATION_90;
42 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
43 
44 import static org.junit.Assert.assertFalse;
45 import static org.junit.Assert.assertNotNull;
46 import static org.junit.Assert.assertTrue;
47 import static org.junit.Assume.assumeTrue;
48 
49 import android.platform.test.annotations.Presubmit;
50 import android.server.am.WindowManagerState.WindowState;
51 
52 import org.junit.Before;
53 import org.junit.Test;
54 
55 /**
56  * Build/Install/Run:
57  *     atest CtsActivityManagerDeviceTestCases:KeyguardTests
58  */
59 public class KeyguardTests extends KeyguardTestBase {
60 
61     // TODO(b/70247058): Use {@link Context#sendBroadcast(Intent).
62     // Shell command to dismiss keyguard via {@link #BROADCAST_RECEIVER_ACTIVITY}.
63     private static final String DISMISS_KEYGUARD_BROADCAST = "am broadcast -a "
64             + ACTION_TRIGGER_BROADCAST + " --ez " + EXTRA_DISMISS_KEYGUARD + " true";
65     // Shell command to dismiss keyguard via {@link #BROADCAST_RECEIVER_ACTIVITY} method.
66     private static final String DISMISS_KEYGUARD_METHOD_BROADCAST = "am broadcast -a "
67             + ACTION_TRIGGER_BROADCAST + " --ez " + EXTRA_DISMISS_KEYGUARD_METHOD + " true";
68     // Shell command to finish {@link #BROADCAST_RECEIVER_ACTIVITY}.
69     private static final String FINISH_ACTIVITY_BROADCAST = "am broadcast -a "
70             + ACTION_TRIGGER_BROADCAST + " --ez " + EXTRA_FINISH_BROADCAST + " true";
71 
72     @Before
73     @Override
setUp()74     public void setUp() throws Exception {
75         super.setUp();
76 
77         assumeTrue(supportsInsecureLock());
78         assertFalse(isUiModeLockedToVrHeadset());
79     }
80 
81     @Test
testKeyguardHidesActivity()82     public void testKeyguardHidesActivity() throws Exception {
83         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
84             launchActivity(TEST_ACTIVITY);
85             mAmWmState.computeState(TEST_ACTIVITY);
86             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
87             lockScreenSession.gotoKeyguard();
88             mAmWmState.computeState(true);
89             mAmWmState.assertKeyguardShowingAndNotOccluded();
90             assertTrue(mKeyguardManager.isKeyguardLocked());
91             mAmWmState.assertVisibility(TEST_ACTIVITY, false);
92         }
93         assertFalse(mKeyguardManager.isKeyguardLocked());
94     }
95 
96     @Test
testShowWhenLockedActivity()97     public void testShowWhenLockedActivity() throws Exception {
98         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
99             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
100             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
101             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
102             lockScreenSession.gotoKeyguard();
103             mAmWmState.computeState(true);
104             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
105             mAmWmState.assertKeyguardShowingAndOccluded();
106         }
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() throws Exception {
115         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
116             launchActivity(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY);
117             mAmWmState.computeState(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY);
118             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY, true);
119             lockScreenSession.gotoKeyguard();
120             mAmWmState.computeState(true);
121             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY, true);
122             assertTrue(mAmWmState.getWmState().allWindowsVisible(
123                     getWindowName(SHOW_WHEN_LOCKED_WITH_DIALOG_ACTIVITY)));
124             mAmWmState.assertKeyguardShowingAndOccluded();
125         }
126     }
127 
128     /**
129      * Tests whether multiple SHOW_WHEN_LOCKED activities are shown if the topmost is translucent.
130      */
131     @Test
testMultipleShowWhenLockedActivities()132     public void testMultipleShowWhenLockedActivities() throws Exception {
133         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
134             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
135             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
136             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY,
137                     SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
138             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
139             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
140             lockScreenSession.gotoKeyguard();
141             mAmWmState.computeState(true);
142             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
143             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
144             mAmWmState.assertKeyguardShowingAndOccluded();
145         }
146     }
147 
148     /**
149      * If we have a translucent SHOW_WHEN_LOCKED_ACTIVITY, the wallpaper should also be showing.
150      */
151     @Test
testTranslucentShowWhenLockedActivity()152     public void testTranslucentShowWhenLockedActivity() throws Exception {
153         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
154             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
155             mAmWmState.computeState(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
156             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
157             lockScreenSession.gotoKeyguard();
158             mAmWmState.computeState(true);
159             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
160             assertWallpaperShowing();
161             mAmWmState.assertKeyguardShowingAndOccluded();
162         }
163     }
164 
165     /**
166      * If we have a translucent SHOW_WHEN_LOCKED activity, the activity behind should not be shown.
167      */
168     @Test
testTranslucentDoesntRevealBehind()169     public void testTranslucentDoesntRevealBehind() throws Exception {
170         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
171             launchActivity(TEST_ACTIVITY);
172             launchActivity(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
173             mAmWmState.computeState(TEST_ACTIVITY, SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY);
174             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
175             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
176             lockScreenSession.gotoKeyguard();
177             mAmWmState.computeState(true);
178             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_TRANSLUCENT_ACTIVITY, true);
179             mAmWmState.assertVisibility(TEST_ACTIVITY, false);
180             mAmWmState.assertKeyguardShowingAndOccluded();
181         }
182     }
183 
184     @Test
testDialogShowWhenLockedActivity()185     public void testDialogShowWhenLockedActivity() throws Exception {
186         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
187             launchActivity(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY);
188             mAmWmState.computeState(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY);
189             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY, true);
190             lockScreenSession.gotoKeyguard();
191             mAmWmState.computeState(true);
192             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_DIALOG_ACTIVITY, true);
193             assertWallpaperShowing();
194             mAmWmState.assertKeyguardShowingAndOccluded();
195         }
196     }
197 
198     /**
199      * Test that showWhenLocked activity is fullscreen when shown over keyguard
200      */
201     @Test
202     @Presubmit
testShowWhenLockedActivityWhileSplit()203     public void testShowWhenLockedActivityWhileSplit() throws Exception {
204         assumeTrue(supportsSplitScreenMultiWindow());
205 
206         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
207             launchActivitiesInSplitScreen(
208                     getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
209                     getLaunchActivityBuilder().setTargetActivity(SHOW_WHEN_LOCKED_ACTIVITY)
210                             .setRandomData(true)
211                             .setMultipleTask(false)
212             );
213             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
214             lockScreenSession.gotoKeyguard();
215             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
216             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
217             mAmWmState.assertKeyguardShowingAndOccluded();
218             mAmWmState.assertDoesNotContainStack("Activity must be full screen.",
219                     WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_STANDARD);
220         }
221     }
222 
223     /**
224      * Tests whether a FLAG_DISMISS_KEYGUARD activity occludes Keyguard.
225      */
226     @Test
testDismissKeyguardActivity()227     public void testDismissKeyguardActivity() throws Exception {
228         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
229             lockScreenSession.gotoKeyguard();
230             mAmWmState.computeState(true);
231             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
232             launchActivity(DISMISS_KEYGUARD_ACTIVITY);
233             mAmWmState.waitForKeyguardShowingAndOccluded();
234             mAmWmState.computeState(DISMISS_KEYGUARD_ACTIVITY);
235             mAmWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
236             mAmWmState.assertKeyguardShowingAndOccluded();
237         }
238     }
239 
240     @Test
testDismissKeyguardActivity_method()241     public void testDismissKeyguardActivity_method() throws Exception {
242         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
243             final LogSeparator logSeparator = separateLogs();
244             lockScreenSession.gotoKeyguard();
245             mAmWmState.computeState(true);
246             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
247             launchActivity(DISMISS_KEYGUARD_METHOD_ACTIVITY);
248             mAmWmState.waitForKeyguardGone();
249             mAmWmState.computeState(DISMISS_KEYGUARD_METHOD_ACTIVITY);
250             mAmWmState.assertVisibility(DISMISS_KEYGUARD_METHOD_ACTIVITY, true);
251             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
252             assertOnDismissSucceededInLogcat(logSeparator);
253         }
254     }
255 
256     @Test
testDismissKeyguardActivity_method_notTop()257     public void testDismissKeyguardActivity_method_notTop() throws Exception {
258         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
259             final LogSeparator logSeparator = separateLogs();
260             lockScreenSession.gotoKeyguard();
261             mAmWmState.computeState(true);
262             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
263             launchActivity(BROADCAST_RECEIVER_ACTIVITY);
264             launchActivity(TEST_ACTIVITY);
265             executeShellCommand(DISMISS_KEYGUARD_METHOD_BROADCAST);
266             assertOnDismissErrorInLogcat(logSeparator);
267         }
268     }
269 
270     @Test
testDismissKeyguardActivity_method_turnScreenOn()271     public void testDismissKeyguardActivity_method_turnScreenOn() throws Exception {
272         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
273             final LogSeparator logSeparator = separateLogs();
274             lockScreenSession.sleepDevice();
275             mAmWmState.computeState(true);
276             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
277             launchActivity(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
278             mAmWmState.waitForKeyguardGone();
279             mAmWmState.computeState(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY);
280             mAmWmState.assertVisibility(TURN_SCREEN_ON_DISMISS_KEYGUARD_ACTIVITY, true);
281             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
282             assertOnDismissSucceededInLogcat(logSeparator);
283             assertTrue(isDisplayOn());
284         }
285     }
286 
287     @Test
testDismissKeyguard_fromShowWhenLocked_notAllowed()288     public void testDismissKeyguard_fromShowWhenLocked_notAllowed() throws Exception {
289         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
290             lockScreenSession.gotoKeyguard();
291             mAmWmState.assertKeyguardShowingAndNotOccluded();
292             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
293             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
294             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
295             mAmWmState.assertKeyguardShowingAndOccluded();
296             executeShellCommand(DISMISS_KEYGUARD_BROADCAST);
297             mAmWmState.assertKeyguardShowingAndOccluded();
298             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
299         }
300     }
301 
302     @Test
testKeyguardLock()303     public void testKeyguardLock() throws Exception {
304         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
305             lockScreenSession.gotoKeyguard();
306             mAmWmState.assertKeyguardShowingAndNotOccluded();
307             launchActivity(KEYGUARD_LOCK_ACTIVITY);
308             mAmWmState.computeState(KEYGUARD_LOCK_ACTIVITY);
309             mAmWmState.assertVisibility(KEYGUARD_LOCK_ACTIVITY, true);
310             executeShellCommand(FINISH_ACTIVITY_BROADCAST);
311             mAmWmState.waitForKeyguardShowingAndNotOccluded();
312             mAmWmState.assertKeyguardShowingAndNotOccluded();
313         }
314     }
315 
316     @Test
testUnoccludeRotationChange()317     public void testUnoccludeRotationChange() throws Exception {
318 
319         // Go home now to make sure Home is behind Keyguard.
320         pressHomeButton();
321         try (final LockScreenSession lockScreenSession = new LockScreenSession();
322              final RotationSession rotationSession = new RotationSession()) {
323             lockScreenSession.gotoKeyguard();
324             mAmWmState.assertKeyguardShowingAndNotOccluded();
325             launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
326             mAmWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
327             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
328 
329             rotationSession.set(ROTATION_90);
330             pressBackButton();
331             mAmWmState.waitForKeyguardShowingAndNotOccluded();
332             mAmWmState.waitForDisplayUnfrozen();
333             mAmWmState.assertSanity();
334             mAmWmState.assertHomeActivityVisible(false);
335             mAmWmState.assertKeyguardShowingAndNotOccluded();
336             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, false);
337         }
338     }
339 
assertWallpaperShowing()340     private void assertWallpaperShowing() {
341         WindowState wallpaper =
342                 mAmWmState.getWmState().findFirstWindowWithType(TYPE_WALLPAPER);
343         assertNotNull(wallpaper);
344         assertTrue(wallpaper.isShown());
345     }
346 
347     @Test
testDismissKeyguardAttrActivity_method_turnScreenOn()348     public void testDismissKeyguardAttrActivity_method_turnScreenOn() throws Exception {
349         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
350             lockScreenSession.sleepDevice();
351 
352             final LogSeparator logSeparator = separateLogs();
353             mAmWmState.computeState(true);
354             assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
355             launchActivity(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY);
356             mAmWmState.waitForKeyguardGone();
357             mAmWmState.assertVisibility(TURN_SCREEN_ON_ATTR_DISMISS_KEYGUARD_ACTIVITY, true);
358             assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
359             assertOnDismissSucceededInLogcat(logSeparator);
360             assertTrue(isDisplayOn());
361         }
362     }
363 
364     @Test
testScreenOffWhileOccludedStopsActivity()365     public void testScreenOffWhileOccludedStopsActivity() throws Exception {
366         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
367             final LogSeparator logSeparator = separateLogs();
368             lockScreenSession.gotoKeyguard();
369             mAmWmState.assertKeyguardShowingAndNotOccluded();
370             launchActivity(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
371             mAmWmState.computeState(SHOW_WHEN_LOCKED_ATTR_ACTIVITY);
372             mAmWmState.assertVisibility(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, true);
373             mAmWmState.assertKeyguardShowingAndOccluded();
374             lockScreenSession.sleepDevice();
375             assertSingleLaunchAndStop(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, logSeparator);
376         }
377     }
378 
379     @Test
testScreenOffCausesSingleStop()380     public void testScreenOffCausesSingleStop() throws Exception {
381         try (final LockScreenSession lockScreenSession = new LockScreenSession()) {
382             final LogSeparator logSeparator = separateLogs();
383             launchActivity(TEST_ACTIVITY);
384             mAmWmState.assertVisibility(TEST_ACTIVITY, true);
385             lockScreenSession.sleepDevice();
386             assertSingleLaunchAndStop(TEST_ACTIVITY, logSeparator);
387         }
388     }
389 }
390