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.UiDeviceUtils.pressBackButton;
20 import static android.server.wm.app.Components.DISMISS_KEYGUARD_ACTIVITY;
21 import static android.view.Display.DEFAULT_DISPLAY;
22 
23 import static org.junit.Assume.assumeTrue;
24 
25 import android.platform.test.annotations.Presubmit;
26 import android.server.wm.WindowManagerState.DisplayContent;
27 import android.util.Size;
28 
29 import androidx.test.filters.FlakyTest;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 
34 /**
35  * Display tests that require a keyguard.
36  *
37  * <p>Build/Install/Run:
38  *     atest CtsWindowManagerDeviceTestCases:MultiDisplayKeyguardTests
39  */
40 @Presubmit
41 @android.server.wm.annotation.Group3
42 public class MultiDisplayKeyguardTests extends MultiDisplayTestBase {
43 
44     @Before
45     @Override
setUp()46     public void setUp() throws Exception {
47         super.setUp();
48 
49         assumeTrue(supportsMultiDisplay());
50         assumeTrue(supportsInsecureLock());
51     }
52 
53     /**
54      * Tests whether a FLAG_DISMISS_KEYGUARD activity on a secondary display is visible (for an
55      * insecure keyguard).
56      */
57     @Test
testDismissKeyguardActivity_secondaryDisplay()58     public void testDismissKeyguardActivity_secondaryDisplay() {
59         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
60         final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();
61 
62         lockScreenSession.gotoKeyguard();
63         mWmState.assertKeyguardShowingAndNotOccluded();
64         launchActivityOnDisplay(DISMISS_KEYGUARD_ACTIVITY, newDisplay.mId);
65         mWmState.waitForKeyguardShowingAndNotOccluded();
66         mWmState.assertKeyguardShowingAndNotOccluded();
67         mWmState.assertVisibility(DISMISS_KEYGUARD_ACTIVITY, true);
68     }
69 
70     /**
71      * Tests keyguard dialog shows on secondary display.
72      */
73     @Test
testShowKeyguardDialogOnSecondaryDisplay()74     public void testShowKeyguardDialogOnSecondaryDisplay() {
75         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
76         final DisplayContent publicDisplay = createManagedVirtualDisplaySession()
77                 .setPublicDisplay(true)
78                 .createDisplay();
79 
80         lockScreenSession.gotoKeyguard();
81         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
82 
83         // Keyguard dialog mustn't be removed when press back key
84         pressBackButton();
85         mWmState.computeState();
86         mWmState.assertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
87     }
88 
89     /**
90      * Tests keyguard dialog should exist after secondary display changed.
91      */
92     @Test
testShowKeyguardDialogSecondaryDisplayChange()93     public void testShowKeyguardDialogSecondaryDisplayChange() {
94         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
95         final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
96 
97         final DisplayContent publicDisplay = virtualDisplaySession
98                 .setPublicDisplay(true)
99                 .createDisplay();
100 
101         lockScreenSession.gotoKeyguard();
102         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
103 
104         // By default, a Presentation object should be dismissed if the DisplayMetrics changed.
105         // But this rule should not apply to KeyguardPresentation.
106         virtualDisplaySession.resizeDisplay();
107         mWmState.computeState();
108         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
109     }
110 
111     /**
112      * Tests keyguard dialog should exist after default display changed.
113      */
114     @Test
testShowKeyguardDialogDefaultDisplayChange()115     public void testShowKeyguardDialogDefaultDisplayChange() {
116         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
117         final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
118         final DisplayMetricsSession displayMetricsSession =
119                 createManagedDisplayMetricsSession(DEFAULT_DISPLAY);
120 
121         // Use simulate display instead of virtual display, because VirtualDisplayActivity will
122         // relaunch after configuration change.
123         final DisplayContent publicDisplay = virtualDisplaySession
124                 .setSimulateDisplay(true)
125                 .createDisplay();
126 
127         lockScreenSession.gotoKeyguard();
128         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
129 
130         // Unlock then lock again, to ensure the display metrics has updated.
131         lockScreenSession.wakeUpDevice().unlockDevice();
132         // Overriding the display metrics on the default display should not affect Keyguard to show
133         // on secondary display.
134         final ReportedDisplayMetrics originalDisplayMetrics =
135                 displayMetricsSession.getInitialDisplayMetrics();
136         final Size overrideSize = new Size(
137                 (int) (originalDisplayMetrics.physicalSize.getWidth() * 1.5),
138                 (int) (originalDisplayMetrics.physicalSize.getHeight() * 1.5));
139         final Integer overrideDensity = (int) (originalDisplayMetrics.physicalDensity * 1.1);
140         displayMetricsSession.overrideDisplayMetrics(overrideSize, overrideDensity);
141 
142         lockScreenSession.gotoKeyguard();
143         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
144     }
145 
146     /**
147      * Tests keyguard dialog cannot be shown on private display.
148      */
149     @Test
testNoKeyguardDialogOnPrivateDisplay()150     public void testNoKeyguardDialogOnPrivateDisplay() {
151         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
152         final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
153 
154         final DisplayContent privateDisplay =
155                 virtualDisplaySession.setPublicDisplay(false).createDisplay();
156         final DisplayContent publicDisplay =
157                 virtualDisplaySession.setPublicDisplay(true).createDisplay();
158 
159         lockScreenSession.gotoKeyguard();
160         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(publicDisplay.mId);
161         mWmState.assertKeyguardGoneOnSecondaryDisplay(privateDisplay.mId);
162     }
163 
164     @Test
testUnlockScreen_secondDisplayChanged_dismissesKeyguardOnUnlock()165     public void testUnlockScreen_secondDisplayChanged_dismissesKeyguardOnUnlock() {
166         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
167         final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
168         lockScreenSession.setLockCredential();
169 
170         // Create second screen
171         final DisplayContent secondDisplay = virtualDisplaySession
172                 .setPublicDisplay(true)
173                 .createDisplay();
174         final int secondDisplayId = secondDisplay.mId;
175 
176         // Lock screen. Keyguard should be shown on the second display
177         lockScreenSession.gotoKeyguard();
178         mWmState.assertKeyguardShowingAndNotOccluded();
179         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(secondDisplayId);
180 
181         // Change second display. Keyguard should still be shown on the second display
182         virtualDisplaySession.resizeDisplay();
183         mWmState.computeState();
184         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(secondDisplayId);
185 
186         // Unlock device. Keyguard should be dismissed on the second display
187         lockScreenSession.unlockDevice();
188         lockScreenSession.enterAndConfirmLockCredential();
189         mWmState.waitAndAssertKeyguardGone();
190         mWmState.waitAndAssertKeyguardGoneOnSecondaryDisplay(secondDisplayId);
191     }
192 
193     @Test
194     @FlakyTest(bugId = 185566696)
testUnlockScreen_decoredSystemDisplayChanged_dismissesKeyguardOnUnlock()195     public void testUnlockScreen_decoredSystemDisplayChanged_dismissesKeyguardOnUnlock() {
196         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
197         final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
198         lockScreenSession.setLockCredential();
199 
200         // Create decored system screen
201         final DisplayContent decoredSystemDisplay = virtualDisplaySession
202                 .setSimulateDisplay(true)
203                 .setShowSystemDecorations(true)
204                 .createDisplay();
205         final int decoredSystemDisplayId = decoredSystemDisplay.mId;
206 
207         // Lock screen. Keyguard should be shown on the decored system display
208         lockScreenSession.gotoKeyguard();
209         mWmState.assertKeyguardShowingAndNotOccluded();
210         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(decoredSystemDisplayId);
211 
212         // Resize decored display. Keyguard should still be shown on the decored system display
213         final ReportedDisplayMetrics displayMetrics =
214                 ReportedDisplayMetrics.getDisplayMetrics(decoredSystemDisplayId);
215         final Size overrideSize = new Size(
216                 (int) (displayMetrics.physicalSize.getWidth() * 0.5),
217                 (int) (displayMetrics.physicalSize.getHeight() * 0.5));
218         displayMetrics.setDisplayMetrics(overrideSize, displayMetrics.physicalDensity);
219         mWmState.computeState();
220         mWmState.waitAndAssertKeyguardShownOnSecondaryDisplay(decoredSystemDisplayId);
221 
222         // Unlock device. Keyguard should be dismissed on the decored system display
223         lockScreenSession.unlockDevice();
224         lockScreenSession.enterAndConfirmLockCredential();
225         mWmState.waitAndAssertKeyguardGone();
226         mWmState.waitAndAssertKeyguardGoneOnSecondaryDisplay(decoredSystemDisplayId);
227     }
228 }
229