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.cts;
18 
19 import android.server.cts.WindowManagerState.WindowState;
20 import android.server.cts.ActivityManagerState.ActivityStack;
21 
22 /**
23  * Build: mmma -j32 cts/hostsidetests/services
24  * Run: cts/hostsidetests/services/activityandwindowmanager/util/run-test CtsServicesHostTestCases android.server.cts.KeyguardTests
25  */
26 public class KeyguardTests extends KeyguardTestBase {
27 
28     @Override
setUp()29     public void setUp() throws Exception {
30         super.setUp();
31 
32         // Set screen lock (swipe)
33         mDevice.executeShellCommand("locksettings set-disabled false");
34     }
35 
36     @Override
tearDown()37     public void tearDown() throws Exception {
38         super.tearDown();
39 
40         // Remove screen lock
41         mDevice.executeShellCommand("locksettings set-disabled true");
42     }
43 
testKeyguardHidesActivity()44     public void testKeyguardHidesActivity() throws Exception {
45         if (!isHandheld()) {
46             return;
47         }
48         launchActivity("TestActivity");
49         mAmWmState.computeState(mDevice, new String[] { "TestActivity"});
50         mAmWmState.assertVisibility("TestActivity", true);
51         gotoKeyguard();
52         mAmWmState.computeState(mDevice, null);
53         assertShowingAndNotOccluded();
54         mAmWmState.assertVisibility("TestActivity", false);
55         unlockDevice();
56     }
57 
testShowWhenLockedActivity()58     public void testShowWhenLockedActivity() throws Exception {
59         if (!isHandheld()) {
60             return;
61         }
62         launchActivity("ShowWhenLockedActivity");
63         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedActivity"});
64         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
65         gotoKeyguard();
66         mAmWmState.computeState(mDevice, null);
67         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
68         assertShowingAndOccluded();
69         pressHomeButton();
70         unlockDevice();
71     }
72 
73     /**
74      * Tests whether dialogs from SHOW_WHEN_LOCKED activities are also visible if Keyguard is
75      * showing.
76      */
testShowWhenLockedActivity_withDialog()77     public void testShowWhenLockedActivity_withDialog() throws Exception {
78         if (!isHandheld()) {
79             return;
80         }
81         launchActivity("ShowWhenLockedWithDialogActivity");
82         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedWithDialogActivity"});
83         mAmWmState.assertVisibility("ShowWhenLockedWithDialogActivity", true);
84         gotoKeyguard();
85         mAmWmState.computeState(mDevice, null);
86         mAmWmState.assertVisibility("ShowWhenLockedWithDialogActivity", true);
87         assertTrue(mAmWmState.getWmState().allWindowsVisible(
88                 getWindowName("ShowWhenLockedWithDialogActivity")));
89         assertShowingAndOccluded();
90         pressHomeButton();
91         unlockDevice();
92     }
93 
94     /**
95      * Tests whether multiple SHOW_WHEN_LOCKED activities are shown if the topmost is translucent.
96      */
testMultipleShowWhenLockedActivities()97     public void testMultipleShowWhenLockedActivities() throws Exception {
98         if (!isHandheld()) {
99             return;
100         }
101         launchActivity("ShowWhenLockedActivity");
102         launchActivity("ShowWhenLockedTranslucentActivity");
103         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedActivity",
104                 "ShowWhenLockedTranslucentActivity"});
105         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
106         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
107         gotoKeyguard();
108         mAmWmState.computeState(mDevice, null);
109         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
110         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
111         assertShowingAndOccluded();
112         pressHomeButton();
113         unlockDevice();
114     }
115 
116     /**
117      * If we have a translucent SHOW_WHEN_LOCKED_ACTIVITY, the wallpaper should also be showing.
118      */
testTranslucentShowWhenLockedActivity()119     public void testTranslucentShowWhenLockedActivity() throws Exception {
120         if (!isHandheld()) {
121             return;
122         }
123         launchActivity("ShowWhenLockedTranslucentActivity");
124         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedTranslucentActivity"});
125         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
126         gotoKeyguard();
127         mAmWmState.computeState(mDevice, null);
128         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
129         assertWallpaperShowing();
130         assertShowingAndOccluded();
131         pressHomeButton();
132         unlockDevice();
133     }
134 
135     /**
136      * If we have a translucent SHOW_WHEN_LOCKED activity, the activity behind should not be shown.
137      */
testTranslucentDoesntRevealBehind()138     public void testTranslucentDoesntRevealBehind() throws Exception {
139         if (!isHandheld()) {
140             return;
141         }
142         launchActivity("TestActivity");
143         launchActivity("ShowWhenLockedTranslucentActivity");
144         mAmWmState.computeState(mDevice, new String[] { "TestActivity",
145                 "ShowWhenLockedTranslucentActivity"});
146         mAmWmState.assertVisibility("TestActivity", true);
147         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
148         gotoKeyguard();
149         mAmWmState.computeState(mDevice, null);
150         mAmWmState.assertVisibility("ShowWhenLockedTranslucentActivity", true);
151         mAmWmState.assertVisibility("TestActivity", false);
152         assertShowingAndOccluded();
153         pressHomeButton();
154         unlockDevice();
155     }
156 
testDialogShowWhenLockedActivity()157     public void testDialogShowWhenLockedActivity() throws Exception {
158         if (!isHandheld()) {
159             return;
160         }
161         launchActivity("ShowWhenLockedDialogActivity");
162         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedDialogActivity"});
163         mAmWmState.assertVisibility("ShowWhenLockedDialogActivity", true);
164         gotoKeyguard();
165         mAmWmState.computeState(mDevice, null);
166         mAmWmState.assertVisibility("ShowWhenLockedDialogActivity", true);
167         assertWallpaperShowing();
168         assertShowingAndOccluded();
169         pressHomeButton();
170         unlockDevice();
171     }
172 
173     /**
174      * Test that showWhenLocked activity is fullscreen when shown over keyguard
175      */
testShowWhenLockedActivityWhileSplit()176     public void testShowWhenLockedActivityWhileSplit() throws Exception {
177         if (!isHandheld() || !supportsSplitScreenMultiWindow()) {
178             return;
179         }
180         launchActivityInDockStack(LAUNCHING_ACTIVITY);
181         launchActivityToSide(true, false, "ShowWhenLockedActivity");
182         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
183         gotoKeyguard();
184         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedActivity" });
185         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
186         assertShowingAndOccluded();
187         mAmWmState.assertDoesNotContainStack("Activity must be full screen.",
188                 ActivityManagerTestBase.DOCKED_STACK_ID);
189         pressHomeButton();
190         unlockDevice();
191     }
192 
193     /**
194      * Tests whether a FLAG_DISMISS_KEYGUARD activity occludes Keyguard.
195      */
testDismissKeyguardActivity()196     public void testDismissKeyguardActivity() throws Exception {
197         if (!isHandheld()) {
198             return;
199         }
200         gotoKeyguard();
201         mAmWmState.computeState(mDevice, null);
202         assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
203         launchActivity("DismissKeyguardActivity");
204         mAmWmState.waitForKeyguardShowingAndOccluded(mDevice);
205         mAmWmState.computeState(mDevice, new String[] { "DismissKeyguardActivity"});
206         mAmWmState.assertVisibility("DismissKeyguardActivity", true);
207         assertShowingAndOccluded();
208     }
209 
testDismissKeyguardActivity_method()210     public void testDismissKeyguardActivity_method() throws Exception {
211         if (!isHandheld()) {
212             return;
213         }
214         final String logSeparator = clearLogcat();
215         gotoKeyguard();
216         mAmWmState.computeState(mDevice, null);
217         assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
218         launchActivity("DismissKeyguardMethodActivity");
219         mAmWmState.waitForKeyguardGone(mDevice);
220         mAmWmState.computeState(mDevice, new String[] { "DismissKeyguardMethodActivity"});
221         mAmWmState.assertVisibility("DismissKeyguardMethodActivity", true);
222         assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
223         assertOnDismissSucceededInLogcat(logSeparator);
224     }
225 
testDismissKeyguardActivity_method_notTop()226     public void testDismissKeyguardActivity_method_notTop() throws Exception {
227         if (!isHandheld()) {
228             return;
229         }
230         final String logSeparator = clearLogcat();
231         gotoKeyguard();
232         mAmWmState.computeState(mDevice, null);
233         assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
234         launchActivity("BroadcastReceiverActivity");
235         launchActivity("TestActivity");
236         executeShellCommand("am broadcast -a trigger_broadcast --ez dismissKeyguardMethod true");
237         assertOnDismissErrorInLogcat(logSeparator);
238     }
239 
testDismissKeyguardActivity_method_turnScreenOn()240     public void testDismissKeyguardActivity_method_turnScreenOn() throws Exception {
241         if (!isHandheld()) {
242             return;
243         }
244         final String logSeparator = clearLogcat();
245         sleepDevice();
246         mAmWmState.computeState(mDevice, null);
247         assertTrue(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
248         launchActivity("TurnScreenOnDismissKeyguardActivity");
249         mAmWmState.waitForKeyguardGone(mDevice);
250         mAmWmState.computeState(mDevice, new String[] { "TurnScreenOnDismissKeyguardActivity"});
251         mAmWmState.assertVisibility("TurnScreenOnDismissKeyguardActivity", true);
252         assertFalse(mAmWmState.getAmState().getKeyguardControllerState().keyguardShowing);
253         assertOnDismissSucceededInLogcat(logSeparator);
254     }
255 
testDismissKeyguard_fromShowWhenLocked_notAllowed()256     public void testDismissKeyguard_fromShowWhenLocked_notAllowed() throws Exception {
257         if (!isHandheld()) {
258             return;
259         }
260         gotoKeyguard();
261         mAmWmState.waitForKeyguardShowingAndNotOccluded(mDevice);
262         assertShowingAndNotOccluded();
263         launchActivity("ShowWhenLockedActivity");
264         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedActivity" });
265         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
266         assertShowingAndOccluded();
267         executeShellCommand("am broadcast -a trigger_broadcast --ez dismissKeyguard true");
268         assertShowingAndOccluded();
269         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
270     }
271 
testKeyguardLock()272     public void testKeyguardLock() throws Exception {
273         if (!isHandheld()) {
274             return;
275         }
276         gotoKeyguard();
277         mAmWmState.waitForKeyguardShowingAndNotOccluded(mDevice);
278         assertShowingAndNotOccluded();
279         launchActivity("KeyguardLockActivity");
280         mAmWmState.computeState(mDevice, new String[] { "KeyguardLockActivity" });
281         mAmWmState.assertVisibility("KeyguardLockActivity", true);
282         executeShellCommand(FINISH_ACTIVITY_BROADCAST);
283         mAmWmState.waitForKeyguardShowingAndNotOccluded(mDevice);
284         assertShowingAndNotOccluded();
285     }
286 
testUnoccludeRotationChange()287     public void testUnoccludeRotationChange() throws Exception {
288         if (!isHandheld()) {
289             return;
290         }
291         gotoKeyguard();
292         mAmWmState.waitForKeyguardShowingAndNotOccluded(mDevice);
293         assertShowingAndNotOccluded();
294         executeShellCommand(getAmStartCmd("ShowWhenLockedActivity"));
295         mAmWmState.computeState(mDevice, new String[] { "ShowWhenLockedActivity" });
296         mAmWmState.assertVisibility("ShowWhenLockedActivity", true);
297         setDeviceRotation(1);
298         pressHomeButton();
299         mAmWmState.waitForKeyguardShowingAndNotOccluded(mDevice);
300         mAmWmState.waitForDisplayUnfrozen(mDevice);
301         mAmWmState.assertSanity();
302         mAmWmState.assertHomeActivityVisible(false);
303         assertShowingAndNotOccluded();
304         mAmWmState.assertVisibility("ShowWhenLockedActivity", false);
305     }
306 
assertWallpaperShowing()307     private void assertWallpaperShowing() {
308         WindowState wallpaper =
309                 mAmWmState.getWmState().findFirstWindowWithType(WindowState.TYPE_WALLPAPER);
310         assertNotNull(wallpaper);
311         assertTrue(wallpaper.isShown());
312     }
313 }
314