blob: 0d40c5e25acb337edcc4e4b21b9827e1939ac3b3 [file] [log] [blame]
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001/*
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
17package com.android.server.wm;
18
Wale Ogunwale68278562017-09-23 17:13:55 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
Kazuki Takise048e2662018-06-27 17:05:11 +090020import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
21import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale68278562017-09-23 17:13:55 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Andrii Kulian92c9a942017-10-10 00:41:41 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
24import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
25import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale34247952017-02-19 11:57:53 -080026import static android.view.Display.DEFAULT_DISPLAY;
Adrian Roos24264212018-02-19 16:26:15 +010027import static android.view.DisplayCutout.fromBoundingRect;
Wale Ogunwale34247952017-02-19 11:57:53 -080028import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
29import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
30import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Andrii Kulian92c9a942017-10-10 00:41:41 -070031import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwale34247952017-02-19 11:57:53 -080032import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
Brett Chabota26eda92018-07-23 13:08:30 -070033
David Stevens46939562017-03-24 13:04:00 -070034import static com.android.server.wm.WindowContainer.POSITION_TOP;
chaviwebcbc342018-02-07 13:19:00 -080035
Adrian Roos5251b1d2018-03-23 18:57:43 +010036import static org.hamcrest.Matchers.is;
Wale Ogunwale34247952017-02-19 11:57:53 -080037import static org.junit.Assert.assertEquals;
Andrii Kulianf0379de2018-03-14 16:24:07 -070038import static org.junit.Assert.assertFalse;
Adrian Roos5251b1d2018-03-23 18:57:43 +010039import static org.junit.Assert.assertThat;
Wale Ogunwale34247952017-02-19 11:57:53 -080040import static org.junit.Assert.assertTrue;
Riddle Hsu654a6f92018-07-13 22:59:36 +080041import static org.mockito.Mockito.doNothing;
42import static org.mockito.Mockito.spy;
Andrii Kulianf0379de2018-03-14 16:24:07 -070043import static org.mockito.Mockito.times;
44import static org.mockito.Mockito.verify;
Wale Ogunwale34247952017-02-19 11:57:53 -080045
Andrii Kulian92c9a942017-10-10 00:41:41 -070046import android.annotation.SuppressLint;
Andrii Kuliand68501e2017-01-10 22:57:27 -080047import android.content.res.Configuration;
Adrian Roos1cf585052018-01-03 18:43:27 +010048import android.graphics.Rect;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070049import android.os.SystemClock;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070050import android.platform.test.annotations.Presubmit;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070051import android.util.DisplayMetrics;
Andrii Kulian0214ed92017-05-16 13:44:05 -070052import android.util.SparseIntArray;
Adrian Roos1cf585052018-01-03 18:43:27 +010053import android.view.DisplayCutout;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080054import android.view.Gravity;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070055import android.view.MotionEvent;
Adrian Roos1cf585052018-01-03 18:43:27 +010056import android.view.Surface;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070057
Brett Chabota26eda92018-07-23 13:08:30 -070058import androidx.test.filters.FlakyTest;
59import androidx.test.filters.SmallTest;
60import androidx.test.runner.AndroidJUnit4;
61
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010062import com.android.server.wm.utils.WmDisplayCutout;
63
Brett Chabota26eda92018-07-23 13:08:30 -070064import org.junit.Test;
65import org.junit.runner.RunWith;
66
Adrian Roos0f9368c2018-04-08 10:59:08 -070067import java.util.ArrayList;
Wale Ogunwale34247952017-02-19 11:57:53 -080068import java.util.Arrays;
Adrian Roos0f9368c2018-04-08 10:59:08 -070069import java.util.Collections;
Wale Ogunwale34247952017-02-19 11:57:53 -080070import java.util.LinkedList;
71import java.util.List;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070072
73/**
74 * Tests for the {@link DisplayContent} class.
75 *
76 * Build/Install/Run:
Adrian Roos1cf585052018-01-03 18:43:27 +010077 * atest com.android.server.wm.DisplayContentTests
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070078 */
79@SmallTest
80@Presubmit
81@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080082public class DisplayContentTests extends WindowTestsBase {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070083
84 @Test
Adrian Roos0f9368c2018-04-08 10:59:08 -070085 @FlakyTest(bugId = 77772044)
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070086 public void testForAllWindows() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080087 final WindowState exitingAppWindow = createWindow(null, TYPE_BASE_APPLICATION,
Wale Ogunwale11cc5162017-04-25 20:29:13 -070088 mDisplayContent, "exiting app");
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070089 final AppWindowToken exitingAppToken = exitingAppWindow.mAppToken;
90 exitingAppToken.mIsExiting = true;
Bryce Lee6d410262017-02-28 15:30:17 -080091 exitingAppToken.getTask().mStack.mExitingAppTokens.add(exitingAppToken);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070092
Wale Ogunwale34247952017-02-19 11:57:53 -080093 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -070094 mWallpaperWindow,
Wale Ogunwale34247952017-02-19 11:57:53 -080095 exitingAppWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -070096 mChildAppWindowBelow,
97 mAppWindow,
98 mChildAppWindowAbove,
99 mDockedDividerWindow,
100 mStatusBarWindow,
101 mNavBarWindow,
102 mImeWindow,
103 mImeDialogWindow));
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800104 }
105
106 @Test
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800107 public void testForAllWindows_WithAppImeTarget() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800108 final WindowState imeAppTarget =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700109 createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "imeAppTarget");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800110
111 sWm.mInputMethodTarget = imeAppTarget;
112
Wale Ogunwale34247952017-02-19 11:57:53 -0800113 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700114 mWallpaperWindow,
115 mChildAppWindowBelow,
116 mAppWindow,
117 mChildAppWindowAbove,
Wale Ogunwale34247952017-02-19 11:57:53 -0800118 imeAppTarget,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700119 mImeWindow,
120 mImeDialogWindow,
121 mDockedDividerWindow,
122 mStatusBarWindow,
123 mNavBarWindow));
Wale Ogunwale34247952017-02-19 11:57:53 -0800124 }
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800125
Wale Ogunwale34247952017-02-19 11:57:53 -0800126 @Test
127 public void testForAllWindows_WithChildWindowImeTarget() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700128 sWm.mInputMethodTarget = mChildAppWindowAbove;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800129
Wale Ogunwale34247952017-02-19 11:57:53 -0800130 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700131 mWallpaperWindow,
132 mChildAppWindowBelow,
133 mAppWindow,
134 mChildAppWindowAbove,
135 mImeWindow,
136 mImeDialogWindow,
137 mDockedDividerWindow,
138 mStatusBarWindow,
139 mNavBarWindow));
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700140 }
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800141
142 @Test
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800143 public void testForAllWindows_WithStatusBarImeTarget() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700144 sWm.mInputMethodTarget = mStatusBarWindow;
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800145
Wale Ogunwale34247952017-02-19 11:57:53 -0800146 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700147 mWallpaperWindow,
148 mChildAppWindowBelow,
149 mAppWindow,
150 mChildAppWindowAbove,
151 mDockedDividerWindow,
152 mStatusBarWindow,
153 mImeWindow,
154 mImeDialogWindow,
155 mNavBarWindow));
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800156 }
157
158 @Test
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800159 public void testForAllWindows_WithInBetweenWindowToken() throws Exception {
160 // This window is set-up to be z-ordered between some windows that go in the same token like
161 // the nav bar and status bar.
162 final WindowState voiceInteractionWindow = createWindow(null, TYPE_VOICE_INTERACTION,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700163 mDisplayContent, "voiceInteractionWindow");
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800164
Wale Ogunwale34247952017-02-19 11:57:53 -0800165 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700166 mWallpaperWindow,
167 mChildAppWindowBelow,
168 mAppWindow,
169 mChildAppWindowAbove,
170 mDockedDividerWindow,
Wale Ogunwale34247952017-02-19 11:57:53 -0800171 voiceInteractionWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700172 mStatusBarWindow,
173 mNavBarWindow,
174 mImeWindow,
175 mImeDialogWindow));
Wale Ogunwale34247952017-02-19 11:57:53 -0800176 }
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800177
Wale Ogunwale34247952017-02-19 11:57:53 -0800178 @Test
179 public void testComputeImeTarget() throws Exception {
180 // Verify that an app window can be an ime target.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700181 final WindowState appWin = createWindow(null, TYPE_APPLICATION, mDisplayContent, "appWin");
Wale Ogunwale34247952017-02-19 11:57:53 -0800182 appWin.setHasSurface(true);
183 assertTrue(appWin.canBeImeTarget());
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700184 WindowState imeTarget = mDisplayContent.computeImeTarget(false /* updateImeTarget */);
Wale Ogunwale34247952017-02-19 11:57:53 -0800185 assertEquals(appWin, imeTarget);
chaviwebcbc342018-02-07 13:19:00 -0800186 appWin.mHidden = false;
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800187
Wale Ogunwale34247952017-02-19 11:57:53 -0800188 // Verify that an child window can be an ime target.
189 final WindowState childWin = createWindow(appWin,
190 TYPE_APPLICATION_ATTACHED_DIALOG, "childWin");
191 childWin.setHasSurface(true);
192 assertTrue(childWin.canBeImeTarget());
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700193 imeTarget = mDisplayContent.computeImeTarget(false /* updateImeTarget */);
Wale Ogunwale34247952017-02-19 11:57:53 -0800194 assertEquals(childWin, imeTarget);
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800195 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800196
Andrii Kuliand68501e2017-01-10 22:57:27 -0800197 /**
198 * This tests stack movement between displays and proper stack's, task's and app token's display
199 * container references updates.
200 */
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800201 @Test
202 public void testMoveStackBetweenDisplays() throws Exception {
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800203 // Create a second display.
204 final DisplayContent dc = createNewDisplay();
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800205
206 // Add stack with activity.
207 final TaskStack stack = createTaskStackOnDisplay(dc);
208 assertEquals(dc.getDisplayId(), stack.getDisplayContent().getDisplayId());
209 assertEquals(dc, stack.getParent().getParent());
210 assertEquals(dc, stack.getDisplayContent());
211
212 final Task task = createTaskInStack(stack, 0 /* userId */);
chaviw97d28202018-02-27 16:23:53 -0800213 final WindowTestUtils.TestAppWindowToken token = WindowTestUtils.createTestAppWindowToken(
214 dc);
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800215 task.addChild(token, 0);
216 assertEquals(dc, task.getDisplayContent());
217 assertEquals(dc, token.getDisplayContent());
218
219 // Move stack to first display.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700220 mDisplayContent.moveStackToDisplay(stack, true /* onTop */);
221 assertEquals(mDisplayContent.getDisplayId(), stack.getDisplayContent().getDisplayId());
222 assertEquals(mDisplayContent, stack.getParent().getParent());
223 assertEquals(mDisplayContent, stack.getDisplayContent());
224 assertEquals(mDisplayContent, task.getDisplayContent());
225 assertEquals(mDisplayContent, token.getDisplayContent());
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800226 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800227
228 /**
229 * This tests override configuration updates for display content.
230 */
231 @Test
232 public void testDisplayOverrideConfigUpdate() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700233 final int displayId = mDisplayContent.getDisplayId();
234 final Configuration currentOverrideConfig = mDisplayContent.getOverrideConfiguration();
Andrii Kuliand68501e2017-01-10 22:57:27 -0800235
236 // Create new, slightly changed override configuration and apply it to the display.
237 final Configuration newOverrideConfig = new Configuration(currentOverrideConfig);
238 newOverrideConfig.densityDpi += 120;
239 newOverrideConfig.fontScale += 0.3;
240
241 sWm.setNewDisplayOverrideConfiguration(newOverrideConfig, displayId);
242
243 // Check that override config is applied.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700244 assertEquals(newOverrideConfig, mDisplayContent.getOverrideConfiguration());
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800245 }
246
247 /**
248 * This tests global configuration updates when default display config is updated.
249 */
250 @Test
251 public void testDefaultDisplayOverrideConfigUpdate() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700252 final Configuration currentConfig = mDisplayContent.getConfiguration();
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800253
254 // Create new, slightly changed override configuration and apply it to the display.
Andrii Kuliana95bfff2017-03-30 19:00:41 -0700255 final Configuration newOverrideConfig = new Configuration(currentConfig);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800256 newOverrideConfig.densityDpi += 120;
257 newOverrideConfig.fontScale += 0.3;
258
259 sWm.setNewDisplayOverrideConfiguration(newOverrideConfig, DEFAULT_DISPLAY);
Andrii Kuliand68501e2017-01-10 22:57:27 -0800260
261 // Check that global configuration is updated, as we've updated default display's config.
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800262 Configuration globalConfig = sWm.mRoot.getConfiguration();
Andrii Kuliand68501e2017-01-10 22:57:27 -0800263 assertEquals(newOverrideConfig.densityDpi, globalConfig.densityDpi);
264 assertEquals(newOverrideConfig.fontScale, globalConfig.fontScale, 0.1 /* delta */);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800265
266 // Return back to original values.
Andrii Kuliana95bfff2017-03-30 19:00:41 -0700267 sWm.setNewDisplayOverrideConfiguration(currentConfig, DEFAULT_DISPLAY);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800268 globalConfig = sWm.mRoot.getConfiguration();
Andrii Kuliana95bfff2017-03-30 19:00:41 -0700269 assertEquals(currentConfig.densityDpi, globalConfig.densityDpi);
270 assertEquals(currentConfig.fontScale, globalConfig.fontScale, 0.1 /* delta */);
Andrii Kuliand68501e2017-01-10 22:57:27 -0800271 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800272
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700273 /**
274 * Tests tapping on a stack in different display results in window gaining focus.
275 */
276 @Test
277 public void testInputEventBringsCorrectDisplayInFocus() throws Exception {
278 DisplayContent dc0 = sWm.getDefaultDisplayContentLocked();
279 // Create a second display
280 final DisplayContent dc1 = createNewDisplay();
281
282 // Add stack with activity.
283 final TaskStack stack0 = createTaskStackOnDisplay(dc0);
284 final Task task0 = createTaskInStack(stack0, 0 /* userId */);
285 final WindowTestUtils.TestAppWindowToken token =
chaviw97d28202018-02-27 16:23:53 -0800286 WindowTestUtils.createTestAppWindowToken(dc0);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700287 task0.addChild(token, 0);
288 dc0.mTapDetector = new TaskTapPointerEventListener(sWm, dc0);
289 sWm.registerPointerEventListener(dc0.mTapDetector);
290 final TaskStack stack1 = createTaskStackOnDisplay(dc1);
291 final Task task1 = createTaskInStack(stack1, 0 /* userId */);
292 final WindowTestUtils.TestAppWindowToken token1 =
chaviw97d28202018-02-27 16:23:53 -0800293 WindowTestUtils.createTestAppWindowToken(dc0);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700294 task1.addChild(token1, 0);
295 dc1.mTapDetector = new TaskTapPointerEventListener(sWm, dc0);
296 sWm.registerPointerEventListener(dc1.mTapDetector);
297
298 // tap on primary display (by sending ACTION_DOWN followed by ACTION_UP)
299 DisplayMetrics dm0 = dc0.getDisplayMetrics();
300 dc0.mTapDetector.onPointerEvent(
301 createTapEvent(dm0.widthPixels / 2, dm0.heightPixels / 2, true));
302 dc0.mTapDetector.onPointerEvent(
303 createTapEvent(dm0.widthPixels / 2, dm0.heightPixels / 2, false));
304
305 // Check focus is on primary display.
306 assertEquals(sWm.mCurrentFocus, dc0.findFocusedWindow());
307
308 // Tap on secondary display
309 DisplayMetrics dm1 = dc1.getDisplayMetrics();
310 dc1.mTapDetector.onPointerEvent(
311 createTapEvent(dm1.widthPixels / 2, dm1.heightPixels / 2, true));
312 dc1.mTapDetector.onPointerEvent(
313 createTapEvent(dm1.widthPixels / 2, dm1.heightPixels / 2, false));
314
315 // Check focus is on secondary.
316 assertEquals(sWm.mCurrentFocus, dc1.findFocusedWindow());
317 }
318
David Stevens46939562017-03-24 13:04:00 -0700319 @Test
320 public void testFocusedWindowMultipleDisplays() throws Exception {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700321 // Create a focusable window and check that focus is calculated correctly
David Stevens46939562017-03-24 13:04:00 -0700322 final WindowState window1 =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700323 createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "window1");
David Stevens46939562017-03-24 13:04:00 -0700324 assertEquals(window1, sWm.mRoot.computeFocusedWindow());
325
326 // Check that a new display doesn't affect focus
327 final DisplayContent dc = createNewDisplay();
328 assertEquals(window1, sWm.mRoot.computeFocusedWindow());
329
330 // Add a window to the second display, and it should be focused
331 final WindowState window2 = createWindow(null, TYPE_BASE_APPLICATION, dc, "window2");
332 assertEquals(window2, sWm.mRoot.computeFocusedWindow());
333
334 // Move the first window to the to including parents, and make sure focus is updated
335 window1.getParent().positionChildAt(POSITION_TOP, window1, true);
336 assertEquals(window1, sWm.mRoot.computeFocusedWindow());
337 }
338
Adrian Roos4163d622018-05-22 16:56:35 +0200339 @Test
340 public void testKeyguard_preventsSecondaryDisplayFocus() throws Exception {
341 final WindowState keyguard = createWindow(null, TYPE_STATUS_BAR,
342 sWm.getDefaultDisplayContentLocked(), "keyguard");
343 assertEquals(keyguard, sWm.mRoot.computeFocusedWindow());
344
345 // Add a window to a second display, and it should be focused
346 final DisplayContent dc = createNewDisplay();
347 final WindowState win = createWindow(null, TYPE_BASE_APPLICATION, dc, "win");
348 assertEquals(win, sWm.mRoot.computeFocusedWindow());
349
350 mWmRule.getWindowManagerPolicy().keyguardShowingAndNotOccluded = true;
351 assertEquals(keyguard, sWm.mRoot.computeFocusedWindow());
352 }
353
Bryce Lee27cec322017-03-21 09:41:37 -0700354 /**
355 * This tests setting the maximum ui width on a display.
356 */
357 @Test
358 public void testMaxUiWidth() throws Exception {
Riddle Hsu654a6f92018-07-13 22:59:36 +0800359 // Prevent base display metrics for test from being updated to the value of real display.
360 final DisplayContent displayContent = createDisplayNoUpdateDisplayInfo();
Bryce Lee27cec322017-03-21 09:41:37 -0700361 final int baseWidth = 1440;
362 final int baseHeight = 2560;
363 final int baseDensity = 300;
364
Riddle Hsu654a6f92018-07-13 22:59:36 +0800365 displayContent.updateBaseDisplayMetrics(baseWidth, baseHeight, baseDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700366
367 final int maxWidth = 300;
368 final int resultingHeight = (maxWidth * baseHeight) / baseWidth;
369 final int resultingDensity = (maxWidth * baseDensity) / baseWidth;
370
Riddle Hsu654a6f92018-07-13 22:59:36 +0800371 displayContent.setMaxUiWidth(maxWidth);
372 verifySizes(displayContent, maxWidth, resultingHeight, resultingDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700373
374 // Assert setting values again does not change;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800375 displayContent.updateBaseDisplayMetrics(baseWidth, baseHeight, baseDensity);
376 verifySizes(displayContent, maxWidth, resultingHeight, resultingDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700377
378 final int smallerWidth = 200;
379 final int smallerHeight = 400;
380 final int smallerDensity = 100;
381
382 // Specify smaller dimension, verify that it is honored
Riddle Hsu654a6f92018-07-13 22:59:36 +0800383 displayContent.updateBaseDisplayMetrics(smallerWidth, smallerHeight, smallerDensity);
384 verifySizes(displayContent, smallerWidth, smallerHeight, smallerDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700385
386 // Verify that setting the max width to a greater value than the base width has no effect
Riddle Hsu654a6f92018-07-13 22:59:36 +0800387 displayContent.setMaxUiWidth(maxWidth);
388 verifySizes(displayContent, smallerWidth, smallerHeight, smallerDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700389 }
390
Bryce Lee48f4b572017-04-10 10:54:15 -0700391 /**
Kazuki Takise148d00a2018-05-31 15:32:19 +0900392 * This test enforces that alwaysOnTop stack is placed at proper position.
Bryce Lee48f4b572017-04-10 10:54:15 -0700393 */
394 @Test
Kazuki Takise148d00a2018-05-31 15:32:19 +0900395 public void testAlwaysOnTopStackLocation() {
Kazuki Takise048e2662018-06-27 17:05:11 +0900396 final TaskStack alwaysOnTopStack = createStackControllerOnStackOnDisplay(
397 WINDOWING_MODE_FREEFORM, ACTIVITY_TYPE_STANDARD, mDisplayContent).mContainer;
Kazuki Takise4e7e7892018-06-19 13:55:23 +0900398 final Task task = createTaskInStack(alwaysOnTopStack, 0 /* userId */);
Kazuki Takise148d00a2018-05-31 15:32:19 +0900399 alwaysOnTopStack.setAlwaysOnTop(true);
Riddle Hsu57831b52018-07-27 00:31:48 +0800400 mDisplayContent.positionStackAt(POSITION_TOP, alwaysOnTopStack,
401 false /* includingParents */);
Kazuki Takise148d00a2018-05-31 15:32:19 +0900402 assertTrue(alwaysOnTopStack.isAlwaysOnTop());
Kazuki Takise4e7e7892018-06-19 13:55:23 +0900403 // Ensure always on top state is synced to the children of the stack.
404 assertTrue(alwaysOnTopStack.getTopChild().isAlwaysOnTop());
Kazuki Takise148d00a2018-05-31 15:32:19 +0900405 assertEquals(alwaysOnTopStack, mDisplayContent.getTopStack());
406
Wale Ogunwale61911492017-10-11 08:50:50 -0700407 final TaskStack pinnedStack = createStackControllerOnStackOnDisplay(
408 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, mDisplayContent).mContainer;
Wale Ogunwale61911492017-10-11 08:50:50 -0700409 assertEquals(pinnedStack, mDisplayContent.getPinnedStack());
410 assertEquals(pinnedStack, mDisplayContent.getTopStack());
Kazuki Takise148d00a2018-05-31 15:32:19 +0900411
Kazuki Takise048e2662018-06-27 17:05:11 +0900412 final TaskStack anotherAlwaysOnTopStack = createStackControllerOnStackOnDisplay(
413 WINDOWING_MODE_FREEFORM, ACTIVITY_TYPE_STANDARD, mDisplayContent).mContainer;
Kazuki Takise148d00a2018-05-31 15:32:19 +0900414 anotherAlwaysOnTopStack.setAlwaysOnTop(true);
Riddle Hsu57831b52018-07-27 00:31:48 +0800415 mDisplayContent.positionStackAt(POSITION_TOP, anotherAlwaysOnTopStack,
416 false /* includingParents */);
Kazuki Takise148d00a2018-05-31 15:32:19 +0900417 assertTrue(anotherAlwaysOnTopStack.isAlwaysOnTop());
418 int topPosition = mDisplayContent.getStacks().size() - 1;
419 // Ensure the new alwaysOnTop stack is put below the pinned stack, but on top of the
420 // existing alwaysOnTop stack.
421 assertEquals(anotherAlwaysOnTopStack, mDisplayContent.getStacks().get(topPosition - 1));
422
Kazuki Takise048e2662018-06-27 17:05:11 +0900423 final TaskStack nonAlwaysOnTopStack = createStackControllerOnStackOnDisplay(
424 WINDOWING_MODE_FREEFORM, ACTIVITY_TYPE_STANDARD, mDisplayContent).mContainer;
Kazuki Takise148d00a2018-05-31 15:32:19 +0900425 assertEquals(mDisplayContent, nonAlwaysOnTopStack.getDisplayContent());
426 topPosition = mDisplayContent.getStacks().size() - 1;
427 // Ensure the non-alwaysOnTop stack is put below the three alwaysOnTop stacks, but above the
428 // existing other non-alwaysOnTop stacks.
429 assertEquals(nonAlwaysOnTopStack, mDisplayContent.getStacks().get(topPosition - 3));
Kazuki Takisef85197b2018-06-18 18:18:36 +0900430
431 anotherAlwaysOnTopStack.setAlwaysOnTop(false);
Riddle Hsu57831b52018-07-27 00:31:48 +0800432 mDisplayContent.positionStackAt(POSITION_TOP, anotherAlwaysOnTopStack,
433 false /* includingParents */);
Kazuki Takisef85197b2018-06-18 18:18:36 +0900434 assertFalse(anotherAlwaysOnTopStack.isAlwaysOnTop());
Kazuki Takisef85197b2018-06-18 18:18:36 +0900435 // Ensure, when always on top is turned off for a stack, the stack is put just below all
436 // other always on top stacks.
437 assertEquals(anotherAlwaysOnTopStack, mDisplayContent.getStacks().get(topPosition - 2));
Kazuki Takise048e2662018-06-27 17:05:11 +0900438 anotherAlwaysOnTopStack.setAlwaysOnTop(true);
439
440 // Ensure always on top state changes properly when windowing mode changes.
441 anotherAlwaysOnTopStack.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
442 assertFalse(anotherAlwaysOnTopStack.isAlwaysOnTop());
443 assertEquals(anotherAlwaysOnTopStack, mDisplayContent.getStacks().get(topPosition - 2));
444 anotherAlwaysOnTopStack.setWindowingMode(WINDOWING_MODE_FREEFORM);
445 assertTrue(anotherAlwaysOnTopStack.isAlwaysOnTop());
446 assertEquals(anotherAlwaysOnTopStack, mDisplayContent.getStacks().get(topPosition - 1));
Bryce Lee48f4b572017-04-10 10:54:15 -0700447 }
448
Andrii Kulian0214ed92017-05-16 13:44:05 -0700449 /**
450 * Test that WM does not report displays to AM that are pending to be removed.
451 */
452 @Test
453 public void testDontReportDeferredRemoval() {
454 // Create a display and add an animating window to it.
455 final DisplayContent dc = createNewDisplay();
456 final WindowState window = createWindow(null /* parent */, TYPE_BASE_APPLICATION, dc, "w");
457 window.mAnimatingExit = true;
458 // Request display removal, it should be deferred.
459 dc.removeIfPossible();
460 // Request ordered display ids from WM.
461 final SparseIntArray orderedDisplayIds = new SparseIntArray();
462 sWm.getDisplaysInFocusOrder(orderedDisplayIds);
463 // Make sure that display that is marked for removal is not reported.
464 assertEquals(-1, orderedDisplayIds.indexOfValue(dc.getDisplayId()));
465 }
466
Andrii Kulian92c9a942017-10-10 00:41:41 -0700467 @Test
Adrian Roos1cf585052018-01-03 18:43:27 +0100468 public void testDisplayCutout_rot0() throws Exception {
469 synchronized (sWm.getWindowManagerLock()) {
470 final DisplayContent dc = createNewDisplay();
471 dc.mInitialDisplayWidth = 200;
472 dc.mInitialDisplayHeight = 400;
Adrian Roos24264212018-02-19 16:26:15 +0100473 Rect r = new Rect(80, 0, 120, 10);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100474 final DisplayCutout cutout = new WmDisplayCutout(
475 fromBoundingRect(r.left, r.top, r.right, r.bottom), null)
476 .computeSafeInsets(200, 400).getDisplayCutout();
Adrian Roos1cf585052018-01-03 18:43:27 +0100477
478 dc.mInitialDisplayCutout = cutout;
479 dc.setRotation(Surface.ROTATION_0);
480 dc.computeScreenConfiguration(new Configuration()); // recomputes dc.mDisplayInfo.
481
482 assertEquals(cutout, dc.getDisplayInfo().displayCutout);
483 }
484 }
485
486 @Test
487 public void testDisplayCutout_rot90() throws Exception {
488 synchronized (sWm.getWindowManagerLock()) {
Riddle Hsu654a6f92018-07-13 22:59:36 +0800489 // Prevent mInitialDisplayCutout from being updated from real display (e.g. null
490 // if the device has no cutout).
491 final DisplayContent dc = createDisplayNoUpdateDisplayInfo();
492 // Rotation may use real display info to compute bound, so here also uses the
493 // same width and height.
494 final int displayWidth = dc.mInitialDisplayWidth;
495 final int displayHeight = dc.mInitialDisplayHeight;
496 final int cutoutWidth = 40;
497 final int cutoutHeight = 10;
498 final int left = (displayWidth - cutoutWidth) / 2;
499 final int top = 0;
500 final int right = (displayWidth + cutoutWidth) / 2;
501 final int bottom = cutoutHeight;
502
503 final Rect r1 = new Rect(left, top, right, bottom);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100504 final DisplayCutout cutout = new WmDisplayCutout(
505 fromBoundingRect(r1.left, r1.top, r1.right, r1.bottom), null)
Riddle Hsu654a6f92018-07-13 22:59:36 +0800506 .computeSafeInsets(displayWidth, displayHeight).getDisplayCutout();
Adrian Roos1cf585052018-01-03 18:43:27 +0100507
508 dc.mInitialDisplayCutout = cutout;
509 dc.setRotation(Surface.ROTATION_90);
510 dc.computeScreenConfiguration(new Configuration()); // recomputes dc.mDisplayInfo.
511
Riddle Hsu654a6f92018-07-13 22:59:36 +0800512 // ----o---------- -------------
513 // | | | | |
514 // | ------o | o---
515 // | | | |
516 // | | -> | |
517 // | | ---o
518 // | | |
519 // | | -------------
520 final Rect r = new Rect(top, left, bottom, right);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100521 assertEquals(new WmDisplayCutout(
522 fromBoundingRect(r.left, r.top, r.right, r.bottom), null)
Riddle Hsu654a6f92018-07-13 22:59:36 +0800523 .computeSafeInsets(displayHeight, displayWidth)
524 .getDisplayCutout(), dc.getDisplayInfo().displayCutout);
Adrian Roos1cf585052018-01-03 18:43:27 +0100525 }
526 }
527
528 @Test
Adrian Roos5251b1d2018-03-23 18:57:43 +0100529 public void testLayoutSeq_assignedDuringLayout() throws Exception {
530 synchronized (sWm.getWindowManagerLock()) {
531
532 final DisplayContent dc = createNewDisplay();
533 final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, dc, "w");
534
535 dc.setLayoutNeeded();
536 dc.performLayout(true /* initial */, false /* updateImeWindows */);
537
538 assertThat(win.mLayoutSeq, is(dc.mLayoutSeq));
539 }
540 }
541
542 @Test
Andrii Kulian92c9a942017-10-10 00:41:41 -0700543 @SuppressLint("InlinedApi")
544 public void testOrientationDefinedByKeyguard() {
545 final DisplayContent dc = createNewDisplay();
546 // Create a window that requests landscape orientation. It will define device orientation
547 // by default.
548 final WindowState window = createWindow(null /* parent */, TYPE_BASE_APPLICATION, dc, "w");
549 window.mAppToken.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
550
551 final WindowState keyguard = createWindow(null, TYPE_STATUS_BAR, dc, "keyguard");
552 keyguard.mHasSurface = true;
553 keyguard.mAttrs.screenOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
554
555 assertEquals("Screen orientation must be defined by the app window by default",
556 SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
557
558 keyguard.mAttrs.screenOrientation = SCREEN_ORIENTATION_PORTRAIT;
559 assertEquals("Visible keyguard must influence device orientation",
560 SCREEN_ORIENTATION_PORTRAIT, dc.getOrientation());
561
562 sWm.setKeyguardGoingAway(true);
563 assertEquals("Keyguard that is going away must not influence device orientation",
564 SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
565 }
566
Andrii Kulianf0379de2018-03-14 16:24:07 -0700567 @Test
568 public void testDisableDisplayInfoOverrideFromWindowManager() {
569 final DisplayContent dc = createNewDisplay();
570
571 assertTrue(dc.mShouldOverrideDisplayConfiguration);
572 sWm.dontOverrideDisplayInfo(dc.getDisplayId());
573
574 assertFalse(dc.mShouldOverrideDisplayConfiguration);
575 verify(sWm.mDisplayManagerInternal, times(1))
576 .setDisplayInfoOverrideFromWindowManager(dc.getDisplayId(), null);
577 }
578
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800579 @Test
580 public void testGetPreferredOptionsPanelGravityFromDifferentDisplays() {
581 final DisplayContent portraitDisplay = createNewDisplay();
582 portraitDisplay.mInitialDisplayHeight = 2000;
583 portraitDisplay.mInitialDisplayWidth = 1000;
584
585 portraitDisplay.setRotation(Surface.ROTATION_0);
586 assertFalse(isOptionsPanelAtRight(portraitDisplay.getDisplayId()));
587 portraitDisplay.setRotation(Surface.ROTATION_90);
588 assertTrue(isOptionsPanelAtRight(portraitDisplay.getDisplayId()));
589
590 final DisplayContent landscapeDisplay = createNewDisplay();
591 landscapeDisplay.mInitialDisplayHeight = 1000;
592 landscapeDisplay.mInitialDisplayWidth = 2000;
593
594 landscapeDisplay.setRotation(Surface.ROTATION_0);
595 assertTrue(isOptionsPanelAtRight(landscapeDisplay.getDisplayId()));
596 landscapeDisplay.setRotation(Surface.ROTATION_90);
597 assertFalse(isOptionsPanelAtRight(landscapeDisplay.getDisplayId()));
598 }
599
600 private boolean isOptionsPanelAtRight(int displayId) {
601 return (sWm.getPreferredOptionsPanelGravity(displayId) & Gravity.RIGHT) == Gravity.RIGHT;
602 }
603
Bryce Lee27cec322017-03-21 09:41:37 -0700604 private static void verifySizes(DisplayContent displayContent, int expectedBaseWidth,
605 int expectedBaseHeight, int expectedBaseDensity) {
606 assertEquals(displayContent.mBaseDisplayWidth, expectedBaseWidth);
607 assertEquals(displayContent.mBaseDisplayHeight, expectedBaseHeight);
608 assertEquals(displayContent.mBaseDisplayDensity, expectedBaseDensity);
609 }
610
Riddle Hsu654a6f92018-07-13 22:59:36 +0800611 /**
612 * Create DisplayContent that does not update display base/initial values from device to keep
613 * the values set by test.
614 */
615 private DisplayContent createDisplayNoUpdateDisplayInfo() {
616 final DisplayContent displayContent = spy(createNewDisplay());
617 doNothing().when(displayContent).updateDisplayInfo();
618 return displayContent;
619 }
620
Adrian Roos0f9368c2018-04-08 10:59:08 -0700621 private void assertForAllWindowsOrder(List<WindowState> expectedWindowsBottomToTop) {
622 final LinkedList<WindowState> actualWindows = new LinkedList<>();
Wale Ogunwale34247952017-02-19 11:57:53 -0800623
624 // Test forward traversal.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700625 mDisplayContent.forAllWindows(actualWindows::addLast, false /* traverseTopToBottom */);
Adrian Roos0f9368c2018-04-08 10:59:08 -0700626 assertThat("bottomToTop", actualWindows, is(expectedWindowsBottomToTop));
627
628 actualWindows.clear();
Wale Ogunwale34247952017-02-19 11:57:53 -0800629
630 // Test backward traversal.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700631 mDisplayContent.forAllWindows(actualWindows::addLast, true /* traverseTopToBottom */);
Adrian Roos0f9368c2018-04-08 10:59:08 -0700632 assertThat("topToBottom", actualWindows, is(reverseList(expectedWindowsBottomToTop)));
633 }
634
635 private static List<WindowState> reverseList(List<WindowState> list) {
636 final ArrayList<WindowState> result = new ArrayList<>(list);
637 Collections.reverse(result);
638 return result;
Wale Ogunwale34247952017-02-19 11:57:53 -0800639 }
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700640
641 private MotionEvent createTapEvent(float x, float y, boolean isDownEvent) {
642 final long downTime = SystemClock.uptimeMillis();
643 final long eventTime = SystemClock.uptimeMillis() + 100;
644 final int metaState = 0;
645
646 return MotionEvent.obtain(
647 downTime,
648 eventTime,
649 isDownEvent ? MotionEvent.ACTION_DOWN : MotionEvent.ACTION_UP,
650 x,
651 y,
652 metaState);
653 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700654}