1 /*
2  * Copyright (C) 2017 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.server.wm.WindowManagerState.STATE_RESUMED;
20 import static android.server.wm.WindowManagerState.STATE_STOPPED;
21 import static android.server.wm.ActivityManagerTestBase.LockScreenSession.FLAG_REMOVE_ACTIVITIES_ON_CLOSE;
22 import static android.server.wm.app.Components.DISMISS_KEYGUARD_ACTIVITY;
23 import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ACTIVITY;
24 import static android.server.wm.app.Components.TEST_ACTIVITY;
25 import static android.server.wm.app.Components.VIRTUAL_DISPLAY_ACTIVITY;
26 
27 import static org.junit.Assume.assumeTrue;
28 
29 import android.platform.test.annotations.Presubmit;
30 import android.server.wm.WindowManagerState.DisplayContent;
31 
32 import androidx.test.filters.FlakyTest;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 /**
38  * Display tests that require a locked keyguard.
39  *
40  * <p>Build/Install/Run:
41  *     atest CtsWindowManagerDeviceTestCases:MultiDisplayLockedKeyguardTests
42  */
43 @Presubmit
44 @android.server.wm.annotation.Group3
45 public class MultiDisplayLockedKeyguardTests extends MultiDisplayTestBase {
46 
47     @Before
48     @Override
setUp()49     public void setUp() throws Exception {
50         super.setUp();
51 
52         assumeTrue(supportsMultiDisplay());
53         assumeTrue(supportsSecureLock());
54     }
55 
56     /**
57      * Test that virtual display content is hidden when device is locked.
58      */
59     @Test
testVirtualDisplayHidesContentWhenLocked()60     public void testVirtualDisplayHidesContentWhenLocked() {
61         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
62         lockScreenSession.setLockCredential();
63 
64         // Create new usual virtual display.
65         final DisplayContent newDisplay = createManagedVirtualDisplaySession()
66                 .setPublicDisplay(true)
67                 .createDisplay();
68         mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);
69 
70         // Launch activity on new secondary display.
71         launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
72         mWmState.assertVisibility(TEST_ACTIVITY, true /* visible */);
73 
74         // Lock the device.
75         lockScreenSession.gotoKeyguard();
76         waitAndAssertActivityState(TEST_ACTIVITY, STATE_STOPPED,
77                 "Expected stopped activity on secondary display ");
78         mWmState.assertVisibility(TEST_ACTIVITY, false /* visible */);
79 
80         // Unlock and check if visibility is back.
81         lockScreenSession.unlockDevice();
82 
83         lockScreenSession.enterAndConfirmLockCredential();
84         mWmState.waitAndAssertKeyguardGone();
85         waitAndAssertActivityState(TEST_ACTIVITY, STATE_RESUMED,
86                 "Expected resumed activity on secondary display");
87         mWmState.assertVisibility(TEST_ACTIVITY, true /* visible */);
88     }
89 
90     /**
91      * Tests that private display cannot show content while device locked.
92      */
93     @Test
testPrivateDisplayHideContentWhenLocked()94     public void testPrivateDisplayHideContentWhenLocked() {
95         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
96         lockScreenSession.setLockCredential();
97 
98         final DisplayContent newDisplay = createManagedVirtualDisplaySession()
99                 .setPublicDisplay(false)
100                 .createDisplay();
101         launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
102 
103         lockScreenSession.gotoKeyguard();
104 
105         waitAndAssertActivityState(TEST_ACTIVITY, STATE_STOPPED,
106                 "Expected stopped activity on private display");
107         mWmState.assertVisibility(TEST_ACTIVITY, false /* visible */);
108     }
109 
110     /**
111      * Tests whether a FLAG_DISMISS_KEYGUARD activity on a secondary display dismisses the keyguard.
112      */
113     @Test
testDismissKeyguard_secondaryDisplay()114     public void testDismissKeyguard_secondaryDisplay() {
115         final LockScreenSession lockScreenSession =
116                 mObjectTracker.manage(new LockScreenSession(FLAG_REMOVE_ACTIVITIES_ON_CLOSE));
117         lockScreenSession.setLockCredential();
118 
119         final DisplayContent newDisplay = createManagedVirtualDisplaySession()
120                 .setPublicDisplay(true)
121                 .createDisplay();
122 
123         lockScreenSession.gotoKeyguard();
124         mWmState.assertKeyguardShowingAndNotOccluded();
125         getLaunchActivityBuilder().setUseInstrumentation()
126                 .setTargetActivity(DISMISS_KEYGUARD_ACTIVITY).setNewTask(true)
127                 .setMultipleTask(true).setDisplayId(newDisplay.mId)
128                 .setWaitForLaunched(false).execute();
129         waitAndAssertActivityState(DISMISS_KEYGUARD_ACTIVITY, STATE_STOPPED,
130                 "Expected stopped activity on secondary display");
131         lockScreenSession.enterAndConfirmLockCredential();
132         mWmState.waitAndAssertKeyguardGone();
133         mWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
134     }
135 
136     @Test
testDismissKeyguard_whileOccluded_secondaryDisplay()137     public void testDismissKeyguard_whileOccluded_secondaryDisplay() {
138         final LockScreenSession lockScreenSession =
139                 mObjectTracker.manage(new LockScreenSession(FLAG_REMOVE_ACTIVITIES_ON_CLOSE));
140         lockScreenSession.setLockCredential();
141 
142         final DisplayContent newDisplay = createManagedVirtualDisplaySession()
143                 .setPublicDisplay(true)
144                 .createDisplay();
145 
146         lockScreenSession.gotoKeyguard();
147         mWmState.assertKeyguardShowingAndNotOccluded();
148         launchActivity(SHOW_WHEN_LOCKED_ACTIVITY);
149         mWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY);
150         mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
151         getLaunchActivityBuilder().setUseInstrumentation()
152                 .setTargetActivity(DISMISS_KEYGUARD_ACTIVITY).setNewTask(true)
153                 .setMultipleTask(true).setDisplayId(newDisplay.mId)
154                 .setWaitForLaunched(false).execute();
155         waitAndAssertActivityState(DISMISS_KEYGUARD_ACTIVITY, STATE_STOPPED,
156                 "Expected stopped activity on secondary display");
157         lockScreenSession.enterAndConfirmLockCredential();
158         mWmState.waitAndAssertKeyguardGone();
159         mWmState.computeState(DISMISS_KEYGUARD_ACTIVITY);
160         mWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
161         mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true);
162     }
163 }
164