blob: 38b3d76b447dbef939bf9a65e3a6d79c8a7613f9 [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
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070014 * limitations under the License.
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070015 */
16
17package com.android.server.wm;
18
Garfield Tan98eead32019-11-18 13:24:53 -080019import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Andrii Kulian92c9a942017-10-10 00:41:41 -070020import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
21import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
22import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Tiger Huang86e6d072019-05-02 20:23:47 +080023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080024import static android.os.Build.VERSION_CODES.P;
25import static android.os.Build.VERSION_CODES.Q;
Wale Ogunwale34247952017-02-19 11:57:53 -080026import static android.view.Display.DEFAULT_DISPLAY;
Issei Suzuki43190bd2018-08-20 17:28:41 +020027import static android.view.DisplayCutout.BOUNDS_POSITION_LEFT;
28import static android.view.DisplayCutout.BOUNDS_POSITION_TOP;
Adrian Roos24264212018-02-19 16:26:15 +010029import static android.view.DisplayCutout.fromBoundingRect;
Riddle Hsu9239d7c62020-02-20 01:35:56 +080030import static android.view.Surface.ROTATION_0;
Evan Rosky69cace42019-09-20 16:28:13 -070031import static android.view.Surface.ROTATION_90;
Adrian Roos019a52b2019-07-02 16:47:44 +020032import static android.view.View.SYSTEM_UI_FLAG_FULLSCREEN;
33import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
34import static android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Adrian Roos4ffc8972019-02-07 20:45:11 +010035import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
36import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
Adrian Roos019a52b2019-07-02 16:47:44 +020037import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
38import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
Wale Ogunwale34247952017-02-19 11:57:53 -080039import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
40import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
Tiger Huang86e6d072019-05-02 20:23:47 +080041import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
Wale Ogunwale34247952017-02-19 11:57:53 -080042import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
wilsonshihe8321942019-10-18 18:39:46 +080043import static android.view.WindowManager.LayoutParams.TYPE_NOTIFICATION_SHADE;
Andrii Kulian92c9a942017-10-10 00:41:41 -070044import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwale34247952017-02-19 11:57:53 -080045import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
Riddle Hsub2297ad2019-07-26 23:37:25 -060046import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Brett Chabota26eda92018-07-23 13:08:30 -070047
Garfield Tan90b04282018-12-11 14:04:42 -080048import static com.android.dx.mockito.inline.extended.ExtendedMockito.any;
49import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyBoolean;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090050import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
Riddle Hsu6d6f67c2019-03-14 16:54:26 +080051import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
Garfield Tan90b04282018-12-11 14:04:42 -080052import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
53import static com.android.dx.mockito.inline.extended.ExtendedMockito.never;
Riddle Hsub2297ad2019-07-26 23:37:25 -060054import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset;
Garfield Tan90b04282018-12-11 14:04:42 -080055import static com.android.dx.mockito.inline.extended.ExtendedMockito.same;
Riddle Hsu6d6f67c2019-03-14 16:54:26 +080056import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090057import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
58import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
Riddle Hsufc246d12020-03-20 18:28:11 +080059import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_APP_TRANSITION;
David Stevens46939562017-03-24 13:04:00 -070060import static com.android.server.wm.WindowContainer.POSITION_TOP;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080061import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
chaviwebcbc342018-02-07 13:19:00 -080062
Riddle Hsu12c05452020-01-09 00:39:52 +080063import static com.google.common.truth.Truth.assertThat;
64
Adrian Roos5251b1d2018-03-23 18:57:43 +010065import static org.hamcrest.Matchers.is;
Wale Ogunwale34247952017-02-19 11:57:53 -080066import static org.junit.Assert.assertEquals;
Andrii Kulianf0379de2018-03-14 16:24:07 -070067import static org.junit.Assert.assertFalse;
Arthur Hungbe5ce212018-09-13 18:41:56 +080068import static org.junit.Assert.assertNotNull;
lumarkff0ab692018-11-05 20:32:30 +080069import static org.junit.Assert.assertNull;
Adrian Roos5251b1d2018-03-23 18:57:43 +010070import static org.junit.Assert.assertThat;
Wale Ogunwale34247952017-02-19 11:57:53 -080071import static org.junit.Assert.assertTrue;
Evan Rosky69cace42019-09-20 16:28:13 -070072import static org.mockito.ArgumentMatchers.anyInt;
Tarandeep Singha6f35612019-01-11 19:50:46 -080073import static org.mockito.ArgumentMatchers.eq;
Wale Ogunwale34247952017-02-19 11:57:53 -080074
Andrii Kulian92c9a942017-10-10 00:41:41 -070075import android.annotation.SuppressLint;
Tarandeep Singha6f35612019-01-11 19:50:46 -080076import android.app.WindowConfiguration;
Andrii Kuliand68501e2017-01-10 22:57:27 -080077import android.content.res.Configuration;
Adrian Roos1cf585052018-01-03 18:43:27 +010078import android.graphics.Rect;
Adrian Roos4ffc8972019-02-07 20:45:11 +010079import android.graphics.Region;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -050080import android.metrics.LogMaker;
Evan Rosky69cace42019-09-20 16:28:13 -070081import android.os.RemoteException;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070082import android.os.SystemClock;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070083import android.platform.test.annotations.Presubmit;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070084import android.util.DisplayMetrics;
Adrian Roos1cf585052018-01-03 18:43:27 +010085import android.view.DisplayCutout;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080086import android.view.Gravity;
Evan Rosky69cace42019-09-20 16:28:13 -070087import android.view.IDisplayWindowRotationCallback;
88import android.view.IDisplayWindowRotationController;
Adrian Roos4ffc8972019-02-07 20:45:11 +010089import android.view.ISystemGestureExclusionListener;
Garfield Tana3f19032019-11-19 18:04:50 -080090import android.view.IWindowManager;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070091import android.view.MotionEvent;
Adrian Roos1cf585052018-01-03 18:43:27 +010092import android.view.Surface;
Tarandeep Singha6f35612019-01-11 19:50:46 -080093import android.view.ViewRootImpl;
Riddle Hsu12c05452020-01-09 00:39:52 +080094import android.view.WindowManager;
Tarandeep Singha6f35612019-01-11 19:50:46 -080095import android.view.test.InsetsModeSession;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070096
Brett Chabota26eda92018-07-23 13:08:30 -070097import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070098
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -050099import com.android.internal.logging.MetricsLogger;
100import com.android.internal.logging.nano.MetricsProto;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100101import com.android.server.wm.utils.WmDisplayCutout;
102
Brett Chabota26eda92018-07-23 13:08:30 -0700103import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +0800104import org.junit.runner.RunWith;
Garfield Tan90b04282018-12-11 14:04:42 -0800105import org.mockito.ArgumentCaptor;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500106import org.mockito.Mockito;
Brett Chabota26eda92018-07-23 13:08:30 -0700107
Adrian Roos0f9368c2018-04-08 10:59:08 -0700108import java.util.ArrayList;
Wale Ogunwale34247952017-02-19 11:57:53 -0800109import java.util.Arrays;
Adrian Roos0f9368c2018-04-08 10:59:08 -0700110import java.util.Collections;
Wale Ogunwale34247952017-02-19 11:57:53 -0800111import java.util.LinkedList;
112import java.util.List;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700113
114/**
115 * Tests for the {@link DisplayContent} class.
116 *
117 * Build/Install/Run:
Yunfan Chen6dd9a622019-02-18 15:12:33 +0900118 * atest WmTests:DisplayContentTests
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700119 */
120@SmallTest
121@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +0800122@RunWith(WindowTestRunner.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800123public class DisplayContentTests extends WindowTestsBase {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700124
125 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700126 public void testForAllWindows() {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800127 final WindowState exitingAppWindow = createWindow(null, TYPE_BASE_APPLICATION,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700128 mDisplayContent, "exiting app");
Garfield Tane8d84ab2019-10-11 09:49:40 -0700129 final ActivityRecord exitingApp = exitingAppWindow.mActivityRecord;
Yunfan Chen6dd9a622019-02-18 15:12:33 +0900130 // Wait until everything in animation handler get executed to prevent the exiting window
131 // from being removed during WindowSurfacePlacer Traversal.
132 waitUntilHandlersIdle();
133
Garfield Tane8d84ab2019-10-11 09:49:40 -0700134 exitingApp.mIsExiting = true;
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800135 exitingApp.getTask().getStack().mExitingActivities.add(exitingApp);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700136
Wale Ogunwale34247952017-02-19 11:57:53 -0800137 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700138 mWallpaperWindow,
Wale Ogunwale34247952017-02-19 11:57:53 -0800139 exitingAppWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700140 mChildAppWindowBelow,
141 mAppWindow,
142 mChildAppWindowAbove,
143 mDockedDividerWindow,
Adrian Roos22a20a82019-10-23 19:05:33 +0200144 mImeWindow,
145 mImeDialogWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700146 mStatusBarWindow,
wilsonshihe8321942019-10-18 18:39:46 +0800147 mNotificationShadeWindow,
Adrian Roos22a20a82019-10-23 19:05:33 +0200148 mNavBarWindow));
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800149 }
150
151 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700152 public void testForAllWindows_WithAppImeTarget() {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800153 final WindowState imeAppTarget =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700154 createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "imeAppTarget");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800155
lumarkff0ab692018-11-05 20:32:30 +0800156 mDisplayContent.mInputMethodTarget = imeAppTarget;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800157
Wale Ogunwale34247952017-02-19 11:57:53 -0800158 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700159 mWallpaperWindow,
160 mChildAppWindowBelow,
161 mAppWindow,
162 mChildAppWindowAbove,
Wale Ogunwale34247952017-02-19 11:57:53 -0800163 imeAppTarget,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700164 mImeWindow,
165 mImeDialogWindow,
166 mDockedDividerWindow,
167 mStatusBarWindow,
wilsonshihe8321942019-10-18 18:39:46 +0800168 mNotificationShadeWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700169 mNavBarWindow));
Wale Ogunwale34247952017-02-19 11:57:53 -0800170 }
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800171
Wale Ogunwale34247952017-02-19 11:57:53 -0800172 @Test
lumarkff0ab692018-11-05 20:32:30 +0800173 public void testForAllWindows_WithChildWindowImeTarget() throws Exception {
174 mDisplayContent.mInputMethodTarget = mChildAppWindowAbove;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800175
Wale Ogunwale34247952017-02-19 11:57:53 -0800176 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700177 mWallpaperWindow,
178 mChildAppWindowBelow,
179 mAppWindow,
180 mChildAppWindowAbove,
181 mImeWindow,
182 mImeDialogWindow,
183 mDockedDividerWindow,
184 mStatusBarWindow,
wilsonshihe8321942019-10-18 18:39:46 +0800185 mNotificationShadeWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700186 mNavBarWindow));
Wale Ogunwaleb783fd82016-11-04 09:51:54 -0700187 }
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800188
189 @Test
lumarkff0ab692018-11-05 20:32:30 +0800190 public void testForAllWindows_WithStatusBarImeTarget() throws Exception {
191 mDisplayContent.mInputMethodTarget = mStatusBarWindow;
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800192
Wale Ogunwale34247952017-02-19 11:57:53 -0800193 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700194 mWallpaperWindow,
195 mChildAppWindowBelow,
196 mAppWindow,
197 mChildAppWindowAbove,
198 mDockedDividerWindow,
199 mStatusBarWindow,
200 mImeWindow,
201 mImeDialogWindow,
wilsonshihe8321942019-10-18 18:39:46 +0800202 mNotificationShadeWindow,
203 mNavBarWindow));
204 }
205
206 @Test
207 public void testForAllWindows_WithNotificationShadeImeTarget() throws Exception {
208 mDisplayContent.mInputMethodTarget = mNotificationShadeWindow;
209
210 assertForAllWindowsOrder(Arrays.asList(
211 mWallpaperWindow,
212 mChildAppWindowBelow,
213 mAppWindow,
214 mChildAppWindowAbove,
215 mDockedDividerWindow,
216 mStatusBarWindow,
217 mNotificationShadeWindow,
218 mImeWindow,
219 mImeDialogWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700220 mNavBarWindow));
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800221 }
222
223 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700224 public void testForAllWindows_WithInBetweenWindowToken() {
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800225 // This window is set-up to be z-ordered between some windows that go in the same token like
226 // the nav bar and status bar.
227 final WindowState voiceInteractionWindow = createWindow(null, TYPE_VOICE_INTERACTION,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700228 mDisplayContent, "voiceInteractionWindow");
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800229
Wale Ogunwale34247952017-02-19 11:57:53 -0800230 assertForAllWindowsOrder(Arrays.asList(
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700231 mWallpaperWindow,
232 mChildAppWindowBelow,
233 mAppWindow,
234 mChildAppWindowAbove,
235 mDockedDividerWindow,
Wale Ogunwale34247952017-02-19 11:57:53 -0800236 voiceInteractionWindow,
Adrian Roos22a20a82019-10-23 19:05:33 +0200237 mImeWindow,
238 mImeDialogWindow,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700239 mStatusBarWindow,
wilsonshihe8321942019-10-18 18:39:46 +0800240 mNotificationShadeWindow,
Adrian Roos22a20a82019-10-23 19:05:33 +0200241 mNavBarWindow));
Wale Ogunwale34247952017-02-19 11:57:53 -0800242 }
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800243
Wale Ogunwale34247952017-02-19 11:57:53 -0800244 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700245 public void testComputeImeTarget() {
Wale Ogunwale34247952017-02-19 11:57:53 -0800246 // Verify that an app window can be an ime target.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700247 final WindowState appWin = createWindow(null, TYPE_APPLICATION, mDisplayContent, "appWin");
Wale Ogunwale34247952017-02-19 11:57:53 -0800248 appWin.setHasSurface(true);
249 assertTrue(appWin.canBeImeTarget());
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700250 WindowState imeTarget = mDisplayContent.computeImeTarget(false /* updateImeTarget */);
Wale Ogunwale34247952017-02-19 11:57:53 -0800251 assertEquals(appWin, imeTarget);
chaviwebcbc342018-02-07 13:19:00 -0800252 appWin.mHidden = false;
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800253
Wale Ogunwale34247952017-02-19 11:57:53 -0800254 // Verify that an child window can be an ime target.
255 final WindowState childWin = createWindow(appWin,
256 TYPE_APPLICATION_ATTACHED_DIALOG, "childWin");
257 childWin.setHasSurface(true);
258 assertTrue(childWin.canBeImeTarget());
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700259 imeTarget = mDisplayContent.computeImeTarget(false /* updateImeTarget */);
Wale Ogunwale34247952017-02-19 11:57:53 -0800260 assertEquals(childWin, imeTarget);
Wale Ogunwale5d7e7f12016-12-12 14:47:05 -0800261 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800262
Andrii Kuliand68501e2017-01-10 22:57:27 -0800263 /**
264 * This tests stack movement between displays and proper stack's, task's and app token's display
265 * container references updates.
266 */
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800267 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700268 public void testMoveStackBetweenDisplays() {
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800269 // Create a second display.
270 final DisplayContent dc = createNewDisplay();
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800271
272 // Add stack with activity.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700273 final ActivityStack stack = createTaskStackOnDisplay(dc);
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800274 assertEquals(dc.getDisplayId(), stack.getDisplayContent().getDisplayId());
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800275 assertEquals(dc, stack.getDisplayContent());
276
277 final Task task = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700278 final ActivityRecord activity = WindowTestUtils.createTestActivityRecord(dc);
279 task.addChild(activity, 0);
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800280 assertEquals(dc, task.getDisplayContent());
Garfield Tane8d84ab2019-10-11 09:49:40 -0700281 assertEquals(dc, activity.getDisplayContent());
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800282
283 // Move stack to first display.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700284 mDisplayContent.moveStackToDisplay(stack, true /* onTop */);
285 assertEquals(mDisplayContent.getDisplayId(), stack.getDisplayContent().getDisplayId());
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700286 assertEquals(mDisplayContent, stack.getDisplayContent());
287 assertEquals(mDisplayContent, task.getDisplayContent());
Garfield Tane8d84ab2019-10-11 09:49:40 -0700288 assertEquals(mDisplayContent, activity.getDisplayContent());
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800289 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800290
291 /**
292 * This tests override configuration updates for display content.
293 */
294 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700295 public void testDisplayOverrideConfigUpdate() {
Evan Roskydfe3da72018-10-26 17:21:06 -0700296 final Configuration currentOverrideConfig =
297 mDisplayContent.getRequestedOverrideConfiguration();
Andrii Kuliand68501e2017-01-10 22:57:27 -0800298
299 // Create new, slightly changed override configuration and apply it to the display.
300 final Configuration newOverrideConfig = new Configuration(currentOverrideConfig);
301 newOverrideConfig.densityDpi += 120;
302 newOverrideConfig.fontScale += 0.3;
303
Evan Roskye747c3e2018-10-30 20:06:41 -0700304 mWm.setNewDisplayOverrideConfiguration(newOverrideConfig, mDisplayContent);
Andrii Kuliand68501e2017-01-10 22:57:27 -0800305
306 // Check that override config is applied.
Evan Roskydfe3da72018-10-26 17:21:06 -0700307 assertEquals(newOverrideConfig, mDisplayContent.getRequestedOverrideConfiguration());
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800308 }
309
310 /**
311 * This tests global configuration updates when default display config is updated.
312 */
313 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700314 public void testDefaultDisplayOverrideConfigUpdate() {
Evan Roskye747c3e2018-10-30 20:06:41 -0700315 DisplayContent defaultDisplay = mWm.mRoot.getDisplayContent(DEFAULT_DISPLAY);
316 final Configuration currentConfig = defaultDisplay.getConfiguration();
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800317
318 // Create new, slightly changed override configuration and apply it to the display.
Andrii Kuliana95bfff2017-03-30 19:00:41 -0700319 final Configuration newOverrideConfig = new Configuration(currentConfig);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800320 newOverrideConfig.densityDpi += 120;
321 newOverrideConfig.fontScale += 0.3;
322
Evan Roskye747c3e2018-10-30 20:06:41 -0700323 mWm.setNewDisplayOverrideConfiguration(newOverrideConfig, defaultDisplay);
Andrii Kuliand68501e2017-01-10 22:57:27 -0800324
325 // Check that global configuration is updated, as we've updated default display's config.
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700326 Configuration globalConfig = mWm.mRoot.getConfiguration();
Andrii Kuliand68501e2017-01-10 22:57:27 -0800327 assertEquals(newOverrideConfig.densityDpi, globalConfig.densityDpi);
328 assertEquals(newOverrideConfig.fontScale, globalConfig.fontScale, 0.1 /* delta */);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800329
330 // Return back to original values.
Evan Roskye747c3e2018-10-30 20:06:41 -0700331 mWm.setNewDisplayOverrideConfiguration(currentConfig, defaultDisplay);
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700332 globalConfig = mWm.mRoot.getConfiguration();
Andrii Kuliana95bfff2017-03-30 19:00:41 -0700333 assertEquals(currentConfig.densityDpi, globalConfig.densityDpi);
334 assertEquals(currentConfig.fontScale, globalConfig.fontScale, 0.1 /* delta */);
Andrii Kuliand68501e2017-01-10 22:57:27 -0800335 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800336
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700337 /**
338 * Tests tapping on a stack in different display results in window gaining focus.
339 */
340 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700341 public void testInputEventBringsCorrectDisplayInFocus() {
342 DisplayContent dc0 = mWm.getDefaultDisplayContentLocked();
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700343 // Create a second display
344 final DisplayContent dc1 = createNewDisplay();
345
346 // Add stack with activity.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700347 final ActivityStack stack0 = createTaskStackOnDisplay(dc0);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700348 final Task task0 = createTaskInStack(stack0, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700349 final ActivityRecord activity =
350 WindowTestUtils.createTestActivityRecord(dc0);
351 task0.addChild(activity, 0);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800352 dc0.configureDisplayPolicy();
353 assertNotNull(dc0.mTapDetector);
354
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700355 final ActivityStack stack1 = createTaskStackOnDisplay(dc1);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700356 final Task task1 = createTaskInStack(stack1, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700357 final ActivityRecord activity1 =
358 WindowTestUtils.createTestActivityRecord(dc0);
359 task1.addChild(activity1, 0);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800360 dc1.configureDisplayPolicy();
361 assertNotNull(dc1.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700362
Arthur Hungbe5ce212018-09-13 18:41:56 +0800363 // tap on primary display.
364 tapOnDisplay(dc0);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700365 // Check focus is on primary display.
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700366 assertEquals(mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus,
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800367 dc0.findFocusedWindow());
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700368
Arthur Hungbe5ce212018-09-13 18:41:56 +0800369 // Tap on secondary display.
370 tapOnDisplay(dc1);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700371 // Check focus is on secondary.
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700372 assertEquals(mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus,
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800373 dc1.findFocusedWindow());
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700374 }
375
David Stevens46939562017-03-24 13:04:00 -0700376 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700377 public void testFocusedWindowMultipleDisplays() {
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800378 doTestFocusedWindowMultipleDisplays(false /* perDisplayFocusEnabled */, Q);
379 }
380
381 @Test
382 public void testFocusedWindowMultipleDisplaysPerDisplayFocusEnabled() {
383 doTestFocusedWindowMultipleDisplays(true /* perDisplayFocusEnabled */, Q);
384 }
385
386 @Test
387 public void testFocusedWindowMultipleDisplaysPerDisplayFocusEnabledLegacyApp() {
388 doTestFocusedWindowMultipleDisplays(true /* perDisplayFocusEnabled */, P);
389 }
390
391 private void doTestFocusedWindowMultipleDisplays(boolean perDisplayFocusEnabled,
392 int targetSdk) {
393 mWm.mPerDisplayFocusEnabled = perDisplayFocusEnabled;
394
Andrii Kulian0214ed92017-05-16 13:44:05 -0700395 // Create a focusable window and check that focus is calculated correctly
David Stevens46939562017-03-24 13:04:00 -0700396 final WindowState window1 =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700397 createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "window1");
Garfield Tane8d84ab2019-10-11 09:49:40 -0700398 window1.mActivityRecord.mTargetSdk = targetSdk;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800399 updateFocusedWindow();
400 assertTrue(window1.isFocused());
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700401 assertEquals(window1, mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus);
David Stevens46939562017-03-24 13:04:00 -0700402
403 // Check that a new display doesn't affect focus
404 final DisplayContent dc = createNewDisplay();
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800405 updateFocusedWindow();
406 assertTrue(window1.isFocused());
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700407 assertEquals(window1, mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus);
David Stevens46939562017-03-24 13:04:00 -0700408
409 // Add a window to the second display, and it should be focused
410 final WindowState window2 = createWindow(null, TYPE_BASE_APPLICATION, dc, "window2");
Garfield Tane8d84ab2019-10-11 09:49:40 -0700411 window2.mActivityRecord.mTargetSdk = targetSdk;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800412 updateFocusedWindow();
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800413 assertTrue(window2.isFocused());
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800414 assertEquals(perDisplayFocusEnabled && targetSdk >= Q, window1.isFocused());
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700415 assertEquals(window2, mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus);
David Stevens46939562017-03-24 13:04:00 -0700416
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800417 // Move the first window to top including parents, and make sure focus is updated
David Stevens46939562017-03-24 13:04:00 -0700418 window1.getParent().positionChildAt(POSITION_TOP, window1, true);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800419 updateFocusedWindow();
420 assertTrue(window1.isFocused());
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800421 assertEquals(perDisplayFocusEnabled && targetSdk >= Q, window2.isFocused());
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700422 assertEquals(window1, mWm.mRoot.getTopFocusedDisplayContent().mCurrentFocus);
Louis Changa9350fe2019-04-25 17:14:20 +0800423
424 // Make sure top focused display not changed if there is a focused app.
Issei Suzuki1669ea42019-11-06 14:20:59 +0100425 window1.mActivityRecord.mVisibleRequested = false;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700426 window1.getDisplayContent().setFocusedApp(window1.mActivityRecord);
Louis Changa9350fe2019-04-25 17:14:20 +0800427 updateFocusedWindow();
428 assertTrue(!window1.isFocused());
429 assertEquals(window1.getDisplayId(),
430 mWm.mRoot.getTopFocusedDisplayContent().getDisplayId());
Adrian Roos4163d622018-05-22 16:56:35 +0200431 }
432
Riddle Hsub2297ad2019-07-26 23:37:25 -0600433 @Test
434 public void testShouldWaitForSystemDecorWindowsOnBoot_OnDefaultDisplay() {
435 mWm.mSystemBooted = true;
436 final DisplayContent defaultDisplay = mWm.getDefaultDisplayContentLocked();
437 final WindowState[] windows = createNotDrawnWindowsOn(defaultDisplay,
438 TYPE_WALLPAPER, TYPE_APPLICATION);
439
440 // Verify waiting for windows to be drawn.
441 assertTrue(defaultDisplay.shouldWaitForSystemDecorWindowsOnBoot());
442
443 // Verify not waiting for drawn windows.
444 makeWindowsDrawn(windows);
445 assertFalse(defaultDisplay.shouldWaitForSystemDecorWindowsOnBoot());
446 }
447
448 @Test
449 public void testShouldWaitForSystemDecorWindowsOnBoot_OnSecondaryDisplay() {
450 mWm.mSystemBooted = true;
451 final DisplayContent secondaryDisplay = createNewDisplay();
452 final WindowState[] windows = createNotDrawnWindowsOn(secondaryDisplay,
453 TYPE_WALLPAPER, TYPE_APPLICATION);
454
455 // Verify not waiting for display without system decorations.
456 doReturn(false).when(secondaryDisplay).supportsSystemDecorations();
457 assertFalse(secondaryDisplay.shouldWaitForSystemDecorWindowsOnBoot());
458
459 // Verify waiting for non-drawn windows on display with system decorations.
460 reset(secondaryDisplay);
461 doReturn(true).when(secondaryDisplay).supportsSystemDecorations();
462 assertTrue(secondaryDisplay.shouldWaitForSystemDecorWindowsOnBoot());
463
464 // Verify not waiting for drawn windows on display with system decorations.
465 makeWindowsDrawn(windows);
466 assertFalse(secondaryDisplay.shouldWaitForSystemDecorWindowsOnBoot());
467 }
468
469 private WindowState[] createNotDrawnWindowsOn(DisplayContent displayContent, int... types) {
470 final WindowState[] windows = new WindowState[types.length];
471 for (int i = 0; i < types.length; i++) {
472 final int type = types[i];
473 windows[i] = createWindow(null /* parent */, type, displayContent, "window-" + type);
474 windows[i].mHasSurface = false;
475 }
476 return windows;
477 }
478
479 private static void makeWindowsDrawn(WindowState[] windows) {
480 for (WindowState window : windows) {
481 window.mHasSurface = true;
482 window.mWinAnimator.mDrawState = WindowStateAnimator.HAS_DRAWN;
483 }
484 }
485
Bryce Lee27cec322017-03-21 09:41:37 -0700486 /**
487 * This tests setting the maximum ui width on a display.
488 */
489 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700490 public void testMaxUiWidth() {
Riddle Hsu654a6f92018-07-13 22:59:36 +0800491 // Prevent base display metrics for test from being updated to the value of real display.
492 final DisplayContent displayContent = createDisplayNoUpdateDisplayInfo();
Bryce Lee27cec322017-03-21 09:41:37 -0700493 final int baseWidth = 1440;
494 final int baseHeight = 2560;
495 final int baseDensity = 300;
496
Riddle Hsu654a6f92018-07-13 22:59:36 +0800497 displayContent.updateBaseDisplayMetrics(baseWidth, baseHeight, baseDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700498
499 final int maxWidth = 300;
500 final int resultingHeight = (maxWidth * baseHeight) / baseWidth;
501 final int resultingDensity = (maxWidth * baseDensity) / baseWidth;
502
Riddle Hsu654a6f92018-07-13 22:59:36 +0800503 displayContent.setMaxUiWidth(maxWidth);
504 verifySizes(displayContent, maxWidth, resultingHeight, resultingDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700505
506 // Assert setting values again does not change;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800507 displayContent.updateBaseDisplayMetrics(baseWidth, baseHeight, baseDensity);
508 verifySizes(displayContent, maxWidth, resultingHeight, resultingDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700509
510 final int smallerWidth = 200;
511 final int smallerHeight = 400;
512 final int smallerDensity = 100;
513
514 // Specify smaller dimension, verify that it is honored
Riddle Hsu654a6f92018-07-13 22:59:36 +0800515 displayContent.updateBaseDisplayMetrics(smallerWidth, smallerHeight, smallerDensity);
516 verifySizes(displayContent, smallerWidth, smallerHeight, smallerDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700517
518 // Verify that setting the max width to a greater value than the base width has no effect
Riddle Hsu654a6f92018-07-13 22:59:36 +0800519 displayContent.setMaxUiWidth(maxWidth);
520 verifySizes(displayContent, smallerWidth, smallerHeight, smallerDensity);
Bryce Lee27cec322017-03-21 09:41:37 -0700521 }
522
Andrii Kulian92c9a942017-10-10 00:41:41 -0700523 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700524 public void testDisplayCutout_rot0() {
Riddle Hsu73f53572019-09-23 23:13:01 +0800525 final DisplayContent dc = createNewDisplay();
526 dc.mInitialDisplayWidth = 200;
527 dc.mInitialDisplayHeight = 400;
528 final Rect r = new Rect(80, 0, 120, 10);
529 final DisplayCutout cutout = new WmDisplayCutout(
530 fromBoundingRect(r.left, r.top, r.right, r.bottom, BOUNDS_POSITION_TOP), null)
531 .computeSafeInsets(200, 400).getDisplayCutout();
Adrian Roos1cf585052018-01-03 18:43:27 +0100532
Riddle Hsu73f53572019-09-23 23:13:01 +0800533 dc.mInitialDisplayCutout = cutout;
534 dc.getDisplayRotation().setRotation(Surface.ROTATION_0);
535 dc.computeScreenConfiguration(new Configuration()); // recomputes dc.mDisplayInfo.
Adrian Roos1cf585052018-01-03 18:43:27 +0100536
Riddle Hsu73f53572019-09-23 23:13:01 +0800537 assertEquals(cutout, dc.getDisplayInfo().displayCutout);
Adrian Roos1cf585052018-01-03 18:43:27 +0100538 }
539
540 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700541 public void testDisplayCutout_rot90() {
Riddle Hsu73f53572019-09-23 23:13:01 +0800542 // Prevent mInitialDisplayCutout from being updated from real display (e.g. null
543 // if the device has no cutout).
544 final DisplayContent dc = createDisplayNoUpdateDisplayInfo();
Garfield Tan7cb30142019-12-20 16:43:06 -0800545 // This test assumes it's a top cutout on a portrait display, so if it happens to be a
546 // landscape display let's rotate it.
547 if (dc.mInitialDisplayHeight < dc.mInitialDisplayWidth) {
548 int tmp = dc.mInitialDisplayHeight;
549 dc.mInitialDisplayHeight = dc.mInitialDisplayWidth;
550 dc.mInitialDisplayWidth = tmp;
551 }
Riddle Hsu73f53572019-09-23 23:13:01 +0800552 // Rotation may use real display info to compute bound, so here also uses the
553 // same width and height.
554 final int displayWidth = dc.mInitialDisplayWidth;
555 final int displayHeight = dc.mInitialDisplayHeight;
556 final int cutoutWidth = 40;
557 final int cutoutHeight = 10;
558 final int left = (displayWidth - cutoutWidth) / 2;
559 final int top = 0;
560 final int right = (displayWidth + cutoutWidth) / 2;
561 final int bottom = cutoutHeight;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800562
Riddle Hsu73f53572019-09-23 23:13:01 +0800563 final Rect r1 = new Rect(left, top, right, bottom);
564 final DisplayCutout cutout = new WmDisplayCutout(
565 fromBoundingRect(r1.left, r1.top, r1.right, r1.bottom, BOUNDS_POSITION_TOP), null)
566 .computeSafeInsets(displayWidth, displayHeight).getDisplayCutout();
Adrian Roos1cf585052018-01-03 18:43:27 +0100567
Riddle Hsu73f53572019-09-23 23:13:01 +0800568 dc.mInitialDisplayCutout = cutout;
569 dc.getDisplayRotation().setRotation(Surface.ROTATION_90);
570 dc.computeScreenConfiguration(new Configuration()); // recomputes dc.mDisplayInfo.
Adrian Roos1cf585052018-01-03 18:43:27 +0100571
Riddle Hsu73f53572019-09-23 23:13:01 +0800572 // ----o---------- -------------
573 // | | | | |
574 // | ------o | o---
575 // | | | |
576 // | | -> | |
577 // | | ---o
578 // | | |
579 // | | -------------
580 final Rect r = new Rect(top, left, bottom, right);
581 assertEquals(new WmDisplayCutout(
582 fromBoundingRect(r.left, r.top, r.right, r.bottom, BOUNDS_POSITION_LEFT), null)
583 .computeSafeInsets(displayHeight, displayWidth).getDisplayCutout(),
584 dc.getDisplayInfo().displayCutout);
Adrian Roos1cf585052018-01-03 18:43:27 +0100585 }
586
587 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700588 public void testLayoutSeq_assignedDuringLayout() {
Riddle Hsu73f53572019-09-23 23:13:01 +0800589 final DisplayContent dc = createNewDisplay();
590 final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, dc, "w");
Adrian Roos5251b1d2018-03-23 18:57:43 +0100591
Riddle Hsu12c05452020-01-09 00:39:52 +0800592 performLayout(dc);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100593
Riddle Hsu73f53572019-09-23 23:13:01 +0800594 assertThat(win.mLayoutSeq, is(dc.mLayoutSeq));
Adrian Roos5251b1d2018-03-23 18:57:43 +0100595 }
596
597 @Test
Andrii Kulian92c9a942017-10-10 00:41:41 -0700598 @SuppressLint("InlinedApi")
599 public void testOrientationDefinedByKeyguard() {
600 final DisplayContent dc = createNewDisplay();
Garfield Tan90b04282018-12-11 14:04:42 -0800601
602 // When display content is created its configuration is not yet initialized, which could
603 // cause unnecessary configuration propagation, so initialize it here.
604 final Configuration config = new Configuration();
605 dc.computeScreenConfiguration(config);
606 dc.onRequestedOverrideConfigurationChanged(config);
607
Andrii Kulian92c9a942017-10-10 00:41:41 -0700608 // Create a window that requests landscape orientation. It will define device orientation
609 // by default.
610 final WindowState window = createWindow(null /* parent */, TYPE_BASE_APPLICATION, dc, "w");
Garfield Tane8d84ab2019-10-11 09:49:40 -0700611 window.mActivityRecord.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Andrii Kulian92c9a942017-10-10 00:41:41 -0700612
wilsonshihe8321942019-10-18 18:39:46 +0800613 final WindowState keyguard = createWindow(null, TYPE_NOTIFICATION_SHADE , dc, "keyguard");
Andrii Kulian92c9a942017-10-10 00:41:41 -0700614 keyguard.mHasSurface = true;
615 keyguard.mAttrs.screenOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
616
617 assertEquals("Screen orientation must be defined by the app window by default",
618 SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
619
620 keyguard.mAttrs.screenOrientation = SCREEN_ORIENTATION_PORTRAIT;
621 assertEquals("Visible keyguard must influence device orientation",
622 SCREEN_ORIENTATION_PORTRAIT, dc.getOrientation());
623
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700624 mWm.setKeyguardGoingAway(true);
Andrii Kulian92c9a942017-10-10 00:41:41 -0700625 assertEquals("Keyguard that is going away must not influence device orientation",
626 SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
627 }
628
Andrii Kulianf0379de2018-03-14 16:24:07 -0700629 @Test
Tiger Huang86e6d072019-05-02 20:23:47 +0800630 public void testOrientationForAspectRatio() {
631 final DisplayContent dc = createNewDisplay();
632
633 // When display content is created its configuration is not yet initialized, which could
634 // cause unnecessary configuration propagation, so initialize it here.
635 final Configuration config = new Configuration();
636 dc.computeScreenConfiguration(config);
637 dc.onRequestedOverrideConfigurationChanged(config);
638
639 // Create a window that requests a fixed orientation. It will define device orientation
640 // by default.
641 final WindowState window = createWindow(null /* parent */, TYPE_APPLICATION_OVERLAY, dc,
642 "window");
643 window.mHasSurface = true;
644 window.mAttrs.screenOrientation = SCREEN_ORIENTATION_LANDSCAPE;
645
646 // --------------------------------
647 // Test non-close-to-square display
648 // --------------------------------
649 dc.mBaseDisplayWidth = 1000;
650 dc.mBaseDisplayHeight = (int) (dc.mBaseDisplayWidth * dc.mCloseToSquareMaxAspectRatio * 2f);
651 dc.configureDisplayPolicy();
652
653 assertEquals("Screen orientation must be defined by the window by default.",
654 window.mAttrs.screenOrientation, dc.getOrientation());
655
656 // ----------------------------
657 // Test close-to-square display
658 // ----------------------------
659 dc.mBaseDisplayHeight = dc.mBaseDisplayWidth;
660 dc.configureDisplayPolicy();
661
662 assertEquals("Screen orientation must be SCREEN_ORIENTATION_USER.",
663 SCREEN_ORIENTATION_USER, dc.getOrientation());
664 }
665
666 @Test
Andrii Kulianf0379de2018-03-14 16:24:07 -0700667 public void testDisableDisplayInfoOverrideFromWindowManager() {
668 final DisplayContent dc = createNewDisplay();
669
670 assertTrue(dc.mShouldOverrideDisplayConfiguration);
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700671 mWm.dontOverrideDisplayInfo(dc.getDisplayId());
Andrii Kulianf0379de2018-03-14 16:24:07 -0700672
673 assertFalse(dc.mShouldOverrideDisplayConfiguration);
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700674 verify(mWm.mDisplayManagerInternal, times(1))
Andrii Kulianf0379de2018-03-14 16:24:07 -0700675 .setDisplayInfoOverrideFromWindowManager(dc.getDisplayId(), null);
676 }
677
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800678 @Test
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800679 public void testClearLastFocusWhenReparentingFocusedWindow() {
680 final DisplayContent defaultDisplay = mWm.getDefaultDisplayContentLocked();
681 final WindowState window = createWindow(null /* parent */, TYPE_BASE_APPLICATION,
682 defaultDisplay, "window");
683 defaultDisplay.mLastFocus = window;
684 mDisplayContent.mCurrentFocus = window;
685 mDisplayContent.reParentWindowToken(window.mToken);
686
687 assertNull(defaultDisplay.mLastFocus);
688 }
689
690 @Test
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800691 public void testGetPreferredOptionsPanelGravityFromDifferentDisplays() {
692 final DisplayContent portraitDisplay = createNewDisplay();
693 portraitDisplay.mInitialDisplayHeight = 2000;
694 portraitDisplay.mInitialDisplayWidth = 1000;
695
Riddle Hsuccf09402019-08-13 00:33:06 +0800696 portraitDisplay.getDisplayRotation().setRotation(Surface.ROTATION_0);
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800697 assertFalse(isOptionsPanelAtRight(portraitDisplay.getDisplayId()));
Evan Rosky69cace42019-09-20 16:28:13 -0700698 portraitDisplay.getDisplayRotation().setRotation(ROTATION_90);
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800699 assertTrue(isOptionsPanelAtRight(portraitDisplay.getDisplayId()));
700
701 final DisplayContent landscapeDisplay = createNewDisplay();
702 landscapeDisplay.mInitialDisplayHeight = 1000;
703 landscapeDisplay.mInitialDisplayWidth = 2000;
704
Riddle Hsuccf09402019-08-13 00:33:06 +0800705 landscapeDisplay.getDisplayRotation().setRotation(Surface.ROTATION_0);
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800706 assertTrue(isOptionsPanelAtRight(landscapeDisplay.getDisplayId()));
Evan Rosky69cace42019-09-20 16:28:13 -0700707 landscapeDisplay.getDisplayRotation().setRotation(ROTATION_90);
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800708 assertFalse(isOptionsPanelAtRight(landscapeDisplay.getDisplayId()));
709 }
710
lumarkff0ab692018-11-05 20:32:30 +0800711 @Test
712 public void testInputMethodTargetUpdateWhenSwitchingOnDisplays() {
713 final DisplayContent newDisplay = createNewDisplay();
714
715 final WindowState appWin = createWindow(null, TYPE_APPLICATION, mDisplayContent, "appWin");
716 final WindowState appWin1 = createWindow(null, TYPE_APPLICATION, newDisplay, "appWin1");
717 appWin.setHasSurface(true);
718 appWin1.setHasSurface(true);
719
720 // Set current input method window on default display, make sure the input method target
721 // is appWin & null on the other display.
722 mDisplayContent.setInputMethodWindowLocked(mImeWindow);
723 newDisplay.setInputMethodWindowLocked(null);
Wale Ogunwale3198da42019-10-10 14:45:03 +0200724 assertEquals("appWin should be IME target window",
725 appWin, mDisplayContent.mInputMethodTarget);
lumarkff0ab692018-11-05 20:32:30 +0800726 assertNull("newDisplay Ime target: ", newDisplay.mInputMethodTarget);
727
728 // Switch input method window on new display & make sure the input method target also
729 // switched as expected.
730 newDisplay.setInputMethodWindowLocked(mImeWindow);
731 mDisplayContent.setInputMethodWindowLocked(null);
Wale Ogunwale3198da42019-10-10 14:45:03 +0200732 assertEquals("appWin1 should be IME target window", appWin1, newDisplay.mInputMethodTarget);
lumarkff0ab692018-11-05 20:32:30 +0800733 assertNull("default display Ime target: ", mDisplayContent.mInputMethodTarget);
734 }
735
Garfield Tan90b04282018-12-11 14:04:42 -0800736 @Test
Garfield Tan98eead32019-11-18 13:24:53 -0800737 public void testAllowsTopmostFullscreenOrientation() {
738 final DisplayContent dc = createNewDisplay();
739 dc.getDisplayRotation().setFixedToUserRotation(
Garfield Tana3f19032019-11-19 18:04:50 -0800740 IWindowManager.FIXED_TO_USER_ROTATION_DISABLED);
Garfield Tan98eead32019-11-18 13:24:53 -0800741
742 final ActivityStack stack =
Louis Chang149d5c82019-12-30 09:47:39 +0800743 new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootWindowContainer)
Louis Chang2453d062019-11-19 22:30:48 +0800744 .setDisplay(dc)
Garfield Tan98eead32019-11-18 13:24:53 -0800745 .build();
746 doReturn(true).when(stack).isVisible();
747
748 final ActivityStack freeformStack =
Louis Chang149d5c82019-12-30 09:47:39 +0800749 new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootWindowContainer)
Louis Chang2453d062019-11-19 22:30:48 +0800750 .setDisplay(dc)
Garfield Tan98eead32019-11-18 13:24:53 -0800751 .setWindowingMode(WINDOWING_MODE_FREEFORM)
752 .build();
753 doReturn(true).when(freeformStack).isVisible();
754 freeformStack.getTopChild().setBounds(100, 100, 300, 400);
755
756 assertTrue(dc.isStackVisible(WINDOWING_MODE_FREEFORM));
757
758 freeformStack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
759 stack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_PORTRAIT);
760 assertEquals(SCREEN_ORIENTATION_PORTRAIT, dc.getOrientation());
761
762 stack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
763 freeformStack.getTopNonFinishingActivity().setOrientation(SCREEN_ORIENTATION_PORTRAIT);
764 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, dc.getOrientation());
765 }
766
767 @Test
Garfield Tan90b04282018-12-11 14:04:42 -0800768 public void testOnDescendantOrientationRequestChanged() {
Garfield Tan49dae102019-02-04 09:51:59 -0800769 final DisplayContent dc = createNewDisplay();
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700770 dc.getDisplayRotation().setFixedToUserRotation(
Garfield Tana3f19032019-11-19 18:04:50 -0800771 IWindowManager.FIXED_TO_USER_ROTATION_DISABLED);
Garfield Tan49dae102019-02-04 09:51:59 -0800772 final int newOrientation = dc.getLastOrientation() == SCREEN_ORIENTATION_LANDSCAPE
773 ? SCREEN_ORIENTATION_PORTRAIT
774 : SCREEN_ORIENTATION_LANDSCAPE;
Garfield Tan90b04282018-12-11 14:04:42 -0800775
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700776 final ActivityStack stack =
Louis Chang149d5c82019-12-30 09:47:39 +0800777 new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootWindowContainer)
Louis Chang2453d062019-11-19 22:30:48 +0800778 .setDisplay(dc).build();
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900779 final ActivityRecord activity = stack.getTopMostTask().getTopNonFinishingActivity();
Garfield Tan90b04282018-12-11 14:04:42 -0800780
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700781 activity.setRequestedOrientation(newOrientation);
Garfield Tan90b04282018-12-11 14:04:42 -0800782
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700783 final int expectedOrientation = newOrientation == SCREEN_ORIENTATION_PORTRAIT
784 ? Configuration.ORIENTATION_PORTRAIT
785 : Configuration.ORIENTATION_LANDSCAPE;
Evan Rosky01775072019-09-11 17:28:07 -0700786 assertEquals(expectedOrientation, dc.getConfiguration().orientation);
Garfield Tan90b04282018-12-11 14:04:42 -0800787 }
788
789 @Test
790 public void testOnDescendantOrientationRequestChanged_FrozenToUserRotation() {
Garfield Tan49dae102019-02-04 09:51:59 -0800791 final DisplayContent dc = createNewDisplay();
Garfield Tan7fbca052019-02-19 10:45:35 -0800792 dc.getDisplayRotation().setFixedToUserRotation(
Garfield Tana3f19032019-11-19 18:04:50 -0800793 IWindowManager.FIXED_TO_USER_ROTATION_ENABLED);
Garfield Tan49dae102019-02-04 09:51:59 -0800794 final int newOrientation = dc.getLastOrientation() == SCREEN_ORIENTATION_LANDSCAPE
795 ? SCREEN_ORIENTATION_PORTRAIT
796 : SCREEN_ORIENTATION_LANDSCAPE;
Garfield Tan90b04282018-12-11 14:04:42 -0800797
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700798 final ActivityStack stack =
Louis Chang149d5c82019-12-30 09:47:39 +0800799 new ActivityTestsBase.StackBuilder(mWm.mAtmService.mRootWindowContainer)
Louis Chang2453d062019-11-19 22:30:48 +0800800 .setDisplay(dc).build();
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900801 final ActivityRecord activity = stack.getTopMostTask().getTopNonFinishingActivity();
Garfield Tan90b04282018-12-11 14:04:42 -0800802
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700803 activity.setRequestedOrientation(newOrientation);
Garfield Tan90b04282018-12-11 14:04:42 -0800804
Louis Chang677921f2019-12-06 16:44:24 +0800805 verify(dc, never()).updateDisplayOverrideConfigurationLocked(any(), eq(activity),
806 anyBoolean(), same(null));
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700807 assertEquals(dc.getDisplayRotation().getUserRotation(), dc.getRotation());
Garfield Tan90b04282018-12-11 14:04:42 -0800808 }
809
Tarandeep Singha6f35612019-01-11 19:50:46 -0800810 @Test
811 public void testComputeImeParent_app() throws Exception {
812 try (final InsetsModeSession session =
813 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_IME)) {
814 final DisplayContent dc = createNewDisplay();
815 dc.mInputMethodTarget = createWindow(null, TYPE_BASE_APPLICATION, "app");
Garfield Tane8d84ab2019-10-11 09:49:40 -0700816 assertEquals(dc.mInputMethodTarget.mActivityRecord.getSurfaceControl(),
Tarandeep Singha6f35612019-01-11 19:50:46 -0800817 dc.computeImeParent());
818 }
819 }
820
821 @Test
822 public void testComputeImeParent_app_notFullscreen() throws Exception {
823 try (final InsetsModeSession session =
824 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_IME)) {
825 final DisplayContent dc = createNewDisplay();
826 dc.mInputMethodTarget = createWindow(null, TYPE_STATUS_BAR, "app");
827 dc.mInputMethodTarget.setWindowingMode(
828 WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
829 assertEquals(dc.getWindowingLayer(), dc.computeImeParent());
830 }
831 }
832
833 @Test
Riddle Hsu6d6f67c2019-03-14 16:54:26 +0800834 public void testComputeImeParent_app_notMatchParentBounds() {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700835 spyOn(mAppWindow.mActivityRecord);
836 doReturn(false).when(mAppWindow.mActivityRecord).matchParentBounds();
Riddle Hsu6d6f67c2019-03-14 16:54:26 +0800837 mDisplayContent.mInputMethodTarget = mAppWindow;
838 // The surface parent of IME should be the display instead of app window.
839 assertEquals(mDisplayContent.getWindowingLayer(), mDisplayContent.computeImeParent());
840 }
841
842 @Test
Tarandeep Singha6f35612019-01-11 19:50:46 -0800843 public void testComputeImeParent_noApp() throws Exception {
844 try (final InsetsModeSession session =
845 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_IME)) {
846 final DisplayContent dc = createNewDisplay();
847 dc.mInputMethodTarget = createWindow(null, TYPE_STATUS_BAR, "statusBar");
848 assertEquals(dc.getWindowingLayer(), dc.computeImeParent());
849 }
850 }
851
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500852 @Test
Adrian Roos4ffc8972019-02-07 20:45:11 +0100853 public void testUpdateSystemGestureExclusion() throws Exception {
854 final DisplayContent dc = createNewDisplay();
855 final WindowState win = createWindow(null, TYPE_BASE_APPLICATION, dc, "win");
856 win.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
857 win.setSystemGestureExclusion(Collections.singletonList(new Rect(10, 20, 30, 40)));
858
Riddle Hsu12c05452020-01-09 00:39:52 +0800859 performLayout(dc);
Adrian Roos4ffc8972019-02-07 20:45:11 +0100860
861 win.setHasSurface(true);
862 dc.updateSystemGestureExclusion();
863
Riddle Hsu73f53572019-09-23 23:13:01 +0800864 final boolean[] invoked = { false };
Adrian Roos4ffc8972019-02-07 20:45:11 +0100865 final ISystemGestureExclusionListener.Stub verifier =
866 new ISystemGestureExclusionListener.Stub() {
867 @Override
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200868 public void onSystemGestureExclusionChanged(int displayId, Region actual,
869 Region unrestricted) {
Adrian Roos4ffc8972019-02-07 20:45:11 +0100870 Region expected = Region.obtain();
871 expected.set(10, 20, 30, 40);
872 assertEquals(expected, actual);
Riddle Hsu73f53572019-09-23 23:13:01 +0800873 invoked[0] = true;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100874 }
875 };
876 try {
877 dc.registerSystemGestureExclusionListener(verifier);
878 } finally {
879 dc.unregisterSystemGestureExclusionListener(verifier);
880 }
Riddle Hsu73f53572019-09-23 23:13:01 +0800881 assertTrue("SystemGestureExclusionListener was not invoked", invoked[0]);
Adrian Roos4ffc8972019-02-07 20:45:11 +0100882 }
883
884 @Test
885 public void testCalculateSystemGestureExclusion() throws Exception {
886 final DisplayContent dc = createNewDisplay();
887 final WindowState win = createWindow(null, TYPE_BASE_APPLICATION, dc, "win");
888 win.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
889 win.setSystemGestureExclusion(Collections.singletonList(new Rect(10, 20, 30, 40)));
890
891 final WindowState win2 = createWindow(null, TYPE_APPLICATION, dc, "win2");
892 win2.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
893 win2.setSystemGestureExclusion(Collections.singletonList(new Rect(20, 30, 40, 50)));
894
Riddle Hsu12c05452020-01-09 00:39:52 +0800895 performLayout(dc);
Adrian Roos4ffc8972019-02-07 20:45:11 +0100896
897 win.setHasSurface(true);
898 win2.setHasSurface(true);
899
900 final Region expected = Region.obtain();
901 expected.set(20, 30, 40, 50);
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200902 assertEquals(expected, calculateSystemGestureExclusion(dc));
903 }
904
905 private Region calculateSystemGestureExclusion(DisplayContent dc) {
906 Region out = Region.obtain();
907 Region unrestricted = Region.obtain();
908 dc.calculateSystemGestureExclusion(out, unrestricted);
909 return out;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100910 }
911
912 @Test
Adrian Roosb1063792019-06-28 12:10:51 +0200913 public void testCalculateSystemGestureExclusion_modal() throws Exception {
914 final DisplayContent dc = createNewDisplay();
915 final WindowState win = createWindow(null, TYPE_BASE_APPLICATION, dc, "base");
916 win.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
917 win.setSystemGestureExclusion(Collections.singletonList(new Rect(0, 0, 1000, 1000)));
918
919 final WindowState win2 = createWindow(null, TYPE_APPLICATION, dc, "modal");
920 win2.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
921 win2.getAttrs().privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION;
922 win2.getAttrs().width = 10;
923 win2.getAttrs().height = 10;
924 win2.setSystemGestureExclusion(Collections.emptyList());
925
Riddle Hsu12c05452020-01-09 00:39:52 +0800926 performLayout(dc);
Adrian Roosb1063792019-06-28 12:10:51 +0200927
928 win.setHasSurface(true);
929 win2.setHasSurface(true);
930
931 final Region expected = Region.obtain();
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200932 assertEquals(expected, calculateSystemGestureExclusion(dc));
Adrian Roosb1063792019-06-28 12:10:51 +0200933 }
934
935 @Test
Adrian Roos019a52b2019-07-02 16:47:44 +0200936 public void testCalculateSystemGestureExclusion_immersiveStickyLegacyWindow() throws Exception {
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200937 mWm.mConstants.mSystemGestureExcludedByPreQStickyImmersive = true;
Adrian Roos019a52b2019-07-02 16:47:44 +0200938
Riddle Hsu73f53572019-09-23 23:13:01 +0800939 final DisplayContent dc = createNewDisplay();
940 final WindowState win = createWindow(null, TYPE_BASE_APPLICATION, dc, "win");
941 win.getAttrs().flags |= FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
942 win.getAttrs().layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
943 win.getAttrs().privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION;
944 win.getAttrs().subtreeSystemUiVisibility = win.mSystemUiVisibility =
945 SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_HIDE_NAVIGATION
946 | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700947 win.mActivityRecord.mTargetSdk = P;
Adrian Roos019a52b2019-07-02 16:47:44 +0200948
Riddle Hsu12c05452020-01-09 00:39:52 +0800949 performLayout(dc);
Adrian Roos019a52b2019-07-02 16:47:44 +0200950
Riddle Hsu73f53572019-09-23 23:13:01 +0800951 win.setHasSurface(true);
Adrian Roos019a52b2019-07-02 16:47:44 +0200952
Riddle Hsu73f53572019-09-23 23:13:01 +0800953 final Region expected = Region.obtain();
954 expected.set(dc.getBounds());
955 assertEquals(expected, calculateSystemGestureExclusion(dc));
Adrian Roos019a52b2019-07-02 16:47:44 +0200956
Riddle Hsu73f53572019-09-23 23:13:01 +0800957 win.setHasSurface(false);
Adrian Roos019a52b2019-07-02 16:47:44 +0200958 }
959
960 @Test
Riddle Hsu12c05452020-01-09 00:39:52 +0800961 public void testRequestResizeForEmptyFrames() {
962 final WindowState win = mChildAppWindowAbove;
963 makeWindowVisible(win, win.getParentWindow());
964 win.setRequestedSize(mDisplayContent.mBaseDisplayWidth, 0 /* height */);
965 win.mAttrs.width = win.mAttrs.height = WindowManager.LayoutParams.WRAP_CONTENT;
966 win.mAttrs.gravity = Gravity.CENTER;
967 performLayout(mDisplayContent);
968
969 // The frame is empty because the requested height is zero.
970 assertTrue(win.getFrameLw().isEmpty());
971 // The window should be scheduled to resize then the client may report a new non-empty size.
972 win.updateResizingWindowIfNeeded();
973 assertThat(mWm.mResizingWindows).contains(win);
974 }
975
976 @Test
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500977 public void testOrientationChangeLogging() {
978 MetricsLogger mockLogger = mock(MetricsLogger.class);
979 Configuration oldConfig = new Configuration();
980 oldConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
981
982 Configuration newConfig = new Configuration();
983 newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;
Riddle Hsu73f53572019-09-23 23:13:01 +0800984 final DisplayContent displayContent = createNewDisplay();
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500985 Mockito.doReturn(mockLogger).when(displayContent).getMetricsLogger();
986 Mockito.doReturn(oldConfig).doReturn(newConfig).when(displayContent).getConfiguration();
Louis Chang2453d062019-11-19 22:30:48 +0800987 doNothing().when(displayContent).preOnConfigurationChanged();
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500988
989 displayContent.onConfigurationChanged(newConfig);
990
991 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
992 verify(mockLogger).write(logMakerCaptor.capture());
993 assertThat(logMakerCaptor.getValue().getCategory(),
994 is(MetricsProto.MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED));
995 assertThat(logMakerCaptor.getValue().getSubtype(),
996 is(Configuration.ORIENTATION_PORTRAIT));
997 }
998
Evan Rosky69cace42019-09-20 16:28:13 -0700999 @Test
Riddle Hsu6f548e92020-01-13 13:34:09 +08001000 public void testApplyTopFixedRotationTransform() {
1001 mWm.mIsFixedRotationTransformEnabled = true;
1002 final Configuration config90 = new Configuration();
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001003 mDisplayContent.computeScreenConfiguration(config90, ROTATION_90);
Riddle Hsu6f548e92020-01-13 13:34:09 +08001004
1005 final Configuration config = new Configuration();
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001006 mDisplayContent.getDisplayRotation().setRotation(ROTATION_0);
Riddle Hsu6f548e92020-01-13 13:34:09 +08001007 mDisplayContent.computeScreenConfiguration(config);
1008 mDisplayContent.onRequestedOverrideConfigurationChanged(config);
1009
Riddle Hsufc246d12020-03-20 18:28:11 +08001010 final ActivityRecord closingApp = new ActivityTestsBase.StackBuilder(mWm.mRoot)
1011 .setDisplay(mDisplayContent).setOnTop(false).build().getTopMostActivity();
1012 closingApp.nowVisible = true;
1013 closingApp.startAnimation(closingApp.getPendingTransaction(), mock(AnimationAdapter.class),
1014 false /* hidden */, ANIMATION_TYPE_APP_TRANSITION);
1015 assertTrue(closingApp.isAnimating());
1016
Riddle Hsu6f548e92020-01-13 13:34:09 +08001017 final ActivityRecord app = mAppWindow.mActivityRecord;
1018 mDisplayContent.prepareAppTransition(WindowManager.TRANSIT_ACTIVITY_OPEN,
1019 false /* alwaysKeepCurrent */);
1020 mDisplayContent.mOpeningApps.add(app);
1021 app.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
1022
1023 assertTrue(app.isFixedRotationTransforming());
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001024 assertTrue(mDisplayContent.getDisplayRotation().shouldRotateSeamlessly(
1025 ROTATION_0 /* oldRotation */, ROTATION_90 /* newRotation */,
1026 false /* forceUpdate */));
Riddle Hsuf41034c2020-03-19 13:10:46 +08001027
1028 final Rect outFrame = new Rect();
1029 final Rect outInsets = new Rect();
1030 final Rect outStableInsets = new Rect();
1031 final Rect outSurfaceInsets = new Rect();
1032 mAppWindow.getAnimationFrames(outFrame, outInsets, outStableInsets, outSurfaceInsets);
1033 // The animation frames should not be rotated because display hasn't rotated.
1034 assertEquals(mDisplayContent.getBounds(), outFrame);
1035
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001036 // The display should keep current orientation and the rotated configuration should apply
1037 // to the activity.
Riddle Hsu6f548e92020-01-13 13:34:09 +08001038 assertEquals(config.orientation, mDisplayContent.getConfiguration().orientation);
1039 assertEquals(config90.orientation, app.getConfiguration().orientation);
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001040 assertEquals(config90.windowConfiguration.getBounds(), app.getBounds());
Riddle Hsu6f548e92020-01-13 13:34:09 +08001041
Riddle Hsu6da58ac2020-04-02 01:04:40 +08001042 // Force the negative offset to verify it can be updated.
1043 mWallpaperWindow.mWinAnimator.mXOffset = mWallpaperWindow.mWinAnimator.mYOffset = -1;
1044 assertTrue(mDisplayContent.mWallpaperController.updateWallpaperOffset(mWallpaperWindow,
1045 false /* sync */));
1046 assertThat(mWallpaperWindow.mWinAnimator.mXOffset).isGreaterThan(-1);
1047 assertThat(mWallpaperWindow.mWinAnimator.mYOffset).isGreaterThan(-1);
1048
Riddle Hsu6f548e92020-01-13 13:34:09 +08001049 mDisplayContent.mAppTransition.notifyAppTransitionFinishedLocked(app.token);
1050
Riddle Hsufc246d12020-03-20 18:28:11 +08001051 // The animation in old rotation should be cancelled.
1052 assertFalse(closingApp.isAnimating());
Riddle Hsu9239d7c62020-02-20 01:35:56 +08001053 // The display should be rotated after the launch is finished.
Riddle Hsu6f548e92020-01-13 13:34:09 +08001054 assertFalse(app.hasFixedRotationTransform());
1055 assertEquals(config90.orientation, mDisplayContent.getConfiguration().orientation);
1056 }
1057
1058 @Test
Evan Rosky69cace42019-09-20 16:28:13 -07001059 public void testRemoteRotation() {
1060 DisplayContent dc = createNewDisplay();
1061
1062 final DisplayRotation dr = dc.getDisplayRotation();
1063 Mockito.doCallRealMethod().when(dr).updateRotationUnchecked(anyBoolean());
1064 Mockito.doReturn(ROTATION_90).when(dr).rotationForOrientation(anyInt(), anyInt());
1065 final boolean[] continued = new boolean[1];
Louis Chang2453d062019-11-19 22:30:48 +08001066 // TODO(display-merge): Remove cast
Evan Rosky69cace42019-09-20 16:28:13 -07001067 Mockito.doAnswer(
1068 invocation -> {
1069 continued[0] = true;
1070 return true;
Louis Chang677921f2019-12-06 16:44:24 +08001071 }).when(dc).updateDisplayOverrideConfigurationLocked();
Evan Rosky69cace42019-09-20 16:28:13 -07001072 final boolean[] called = new boolean[1];
1073 mWm.mDisplayRotationController =
1074 new IDisplayWindowRotationController.Stub() {
1075 @Override
1076 public void onRotateDisplay(int displayId, int fromRotation, int toRotation,
1077 IDisplayWindowRotationCallback callback) {
1078 called[0] = true;
1079
1080 try {
1081 callback.continueRotateDisplay(toRotation, null);
1082 } catch (RemoteException e) {
1083 assertTrue(false);
1084 }
1085 }
1086 };
1087
1088 // kill any existing rotation animation (vestigial from test setup).
1089 dc.setRotationAnimation(null);
1090
1091 mWm.updateRotation(true /* alwaysSendConfiguration */, false /* forceRelayout */);
1092 assertTrue(called[0]);
1093 waitUntilHandlersIdle();
1094 assertTrue(continued[0]);
1095 }
1096
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001097 @Test
1098 public void testGetOrCreateRootHomeTask_defaultDisplay() {
1099 DisplayContent defaultDisplay = mWm.mRoot.getDisplayContent(DEFAULT_DISPLAY);
Andrii Kulian86d676c2020-03-27 19:34:54 -07001100 TaskDisplayArea defaultTaskDisplayArea = defaultDisplay.mTaskContainers;
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001101
1102 // Remove the current home stack if it exists so a new one can be created below.
Andrii Kulian86d676c2020-03-27 19:34:54 -07001103 ActivityStack homeTask = defaultTaskDisplayArea.getRootHomeTask();
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001104 if (homeTask != null) {
Andrii Kulian86d676c2020-03-27 19:34:54 -07001105 defaultTaskDisplayArea.removeChild(homeTask);
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001106 }
Andrii Kulian86d676c2020-03-27 19:34:54 -07001107 assertNull(defaultTaskDisplayArea.getRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001108
Andrii Kulian86d676c2020-03-27 19:34:54 -07001109 assertNotNull(defaultTaskDisplayArea.getOrCreateRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001110 }
1111
1112 @Test
1113 public void testGetOrCreateRootHomeTask_supportedSecondaryDisplay() {
1114 DisplayContent display = createNewDisplay();
1115 doReturn(true).when(display).supportsSystemDecorations();
1116 doReturn(false).when(display).isUntrustedVirtualDisplay();
1117
1118 // Remove the current home stack if it exists so a new one can be created below.
Andrii Kulian86d676c2020-03-27 19:34:54 -07001119 TaskDisplayArea taskDisplayArea = display.mTaskContainers;
1120 ActivityStack homeTask = taskDisplayArea.getRootHomeTask();
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001121 if (homeTask != null) {
Andrii Kulian86d676c2020-03-27 19:34:54 -07001122 taskDisplayArea.removeChild(homeTask);
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001123 }
Andrii Kulian86d676c2020-03-27 19:34:54 -07001124 assertNull(taskDisplayArea.getRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001125
Andrii Kulian86d676c2020-03-27 19:34:54 -07001126 assertNotNull(taskDisplayArea.getOrCreateRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001127 }
1128
1129 @Test
1130 public void testGetOrCreateRootHomeTask_unsupportedSystemDecorations() {
1131 DisplayContent display = createNewDisplay();
Andrii Kulian86d676c2020-03-27 19:34:54 -07001132 TaskDisplayArea taskDisplayArea = display.mTaskContainers;
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001133 doReturn(false).when(display).supportsSystemDecorations();
1134
Andrii Kulian86d676c2020-03-27 19:34:54 -07001135 assertNull(taskDisplayArea.getRootHomeTask());
1136 assertNull(taskDisplayArea.getOrCreateRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001137 }
1138
1139 @Test
1140 public void testGetOrCreateRootHomeTask_untrustedVirtualDisplay() {
1141 DisplayContent display = createNewDisplay();
Andrii Kulian86d676c2020-03-27 19:34:54 -07001142 TaskDisplayArea taskDisplayArea = display.mTaskContainers;
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001143 doReturn(true).when(display).isUntrustedVirtualDisplay();
1144
Andrii Kulian86d676c2020-03-27 19:34:54 -07001145 assertNull(taskDisplayArea.getRootHomeTask());
1146 assertNull(taskDisplayArea.getOrCreateRootHomeTask());
Darryl L Johnson3388bd22019-12-19 17:38:41 -08001147 }
1148
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001149 private boolean isOptionsPanelAtRight(int displayId) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -07001150 return (mWm.getPreferredOptionsPanelGravity(displayId) & Gravity.RIGHT) == Gravity.RIGHT;
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001151 }
1152
Bryce Lee27cec322017-03-21 09:41:37 -07001153 private static void verifySizes(DisplayContent displayContent, int expectedBaseWidth,
1154 int expectedBaseHeight, int expectedBaseDensity) {
1155 assertEquals(displayContent.mBaseDisplayWidth, expectedBaseWidth);
1156 assertEquals(displayContent.mBaseDisplayHeight, expectedBaseHeight);
1157 assertEquals(displayContent.mBaseDisplayDensity, expectedBaseDensity);
1158 }
1159
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001160 private void updateFocusedWindow() {
Riddle Hsu73f53572019-09-23 23:13:01 +08001161 mWm.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, false /* updateInputWindows */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001162 }
1163
Riddle Hsu12c05452020-01-09 00:39:52 +08001164 private void performLayout(DisplayContent dc) {
1165 dc.setLayoutNeeded();
1166 dc.performLayout(true /* initial */, false /* updateImeWindows */);
1167 }
1168
Riddle Hsu654a6f92018-07-13 22:59:36 +08001169 /**
1170 * Create DisplayContent that does not update display base/initial values from device to keep
1171 * the values set by test.
1172 */
1173 private DisplayContent createDisplayNoUpdateDisplayInfo() {
Riddle Hsu73f53572019-09-23 23:13:01 +08001174 final DisplayContent displayContent = createNewDisplay();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001175 doNothing().when(displayContent).updateDisplayInfo();
1176 return displayContent;
1177 }
1178
Adrian Roos0f9368c2018-04-08 10:59:08 -07001179 private void assertForAllWindowsOrder(List<WindowState> expectedWindowsBottomToTop) {
1180 final LinkedList<WindowState> actualWindows = new LinkedList<>();
Wale Ogunwale34247952017-02-19 11:57:53 -08001181
1182 // Test forward traversal.
Wale Ogunwale11cc5162017-04-25 20:29:13 -07001183 mDisplayContent.forAllWindows(actualWindows::addLast, false /* traverseTopToBottom */);
Adrian Roos0f9368c2018-04-08 10:59:08 -07001184 assertThat("bottomToTop", actualWindows, is(expectedWindowsBottomToTop));
1185
1186 actualWindows.clear();
Wale Ogunwale34247952017-02-19 11:57:53 -08001187
1188 // Test backward traversal.
Wale Ogunwale11cc5162017-04-25 20:29:13 -07001189 mDisplayContent.forAllWindows(actualWindows::addLast, true /* traverseTopToBottom */);
Adrian Roos0f9368c2018-04-08 10:59:08 -07001190 assertThat("topToBottom", actualWindows, is(reverseList(expectedWindowsBottomToTop)));
1191 }
1192
1193 private static List<WindowState> reverseList(List<WindowState> list) {
1194 final ArrayList<WindowState> result = new ArrayList<>(list);
1195 Collections.reverse(result);
1196 return result;
Wale Ogunwale34247952017-02-19 11:57:53 -08001197 }
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -07001198
Arthur Hungbe5ce212018-09-13 18:41:56 +08001199 private void tapOnDisplay(final DisplayContent dc) {
1200 final DisplayMetrics dm = dc.getDisplayMetrics();
1201 final float x = dm.widthPixels / 2;
1202 final float y = dm.heightPixels / 2;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -07001203 final long downTime = SystemClock.uptimeMillis();
1204 final long eventTime = SystemClock.uptimeMillis() + 100;
Arthur Hungbe5ce212018-09-13 18:41:56 +08001205 // sending ACTION_DOWN
1206 final MotionEvent downEvent = MotionEvent.obtain(
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -07001207 downTime,
Arthur Hungbe5ce212018-09-13 18:41:56 +08001208 downTime,
1209 MotionEvent.ACTION_DOWN,
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -07001210 x,
1211 y,
Arthur Hungbe5ce212018-09-13 18:41:56 +08001212 0 /*metaState*/);
1213 downEvent.setDisplayId(dc.getDisplayId());
1214 dc.mTapDetector.onPointerEvent(downEvent);
1215
1216 // sending ACTION_UP
1217 final MotionEvent upEvent = MotionEvent.obtain(
1218 downTime,
1219 eventTime,
1220 MotionEvent.ACTION_UP,
1221 x,
1222 y,
1223 0 /*metaState*/);
1224 upEvent.setDisplayId(dc.getDisplayId());
1225 dc.mTapDetector.onPointerEvent(upEvent);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -07001226 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001227}