blob: d202e16a5ff4dd6798b1eece29c36aa0e7386a16 [file] [log] [blame]
Wale Ogunwale44fbdf52016-11-16 10:18:45 -08001/*
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 Ogunwale44fbdf52016-11-16 10:18:45 -080015 */
16
17package com.android.server.wm;
18
Brett Chabota26eda92018-07-23 13:08:30 -070019import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale68278562017-09-23 17:13:55 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
21import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070022import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080023import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS;
Brett Chabota26eda92018-07-23 13:08:30 -070024import static android.view.View.VISIBLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080025import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Wale Ogunwale34247952017-02-19 11:57:53 -080026import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080027import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080028import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
29import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
30import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
31import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
32import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
33import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
34import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
35import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
36import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Brett Chabota26eda92018-07-23 13:08:30 -070037
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070038import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
39
Vishnu Naire6e2b0f2019-02-21 10:41:00 -080040import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
41
42import static org.mockito.Mockito.mock;
43
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080044
Brett Chabota26eda92018-07-23 13:08:30 -070045import android.content.Context;
46import android.content.res.Configuration;
Brett Chabota26eda92018-07-23 13:08:30 -070047import android.hardware.display.DisplayManagerGlobal;
48import android.testing.DexmakerShareClassLoaderRule;
49import android.util.Log;
50import android.view.Display;
51import android.view.DisplayInfo;
52import android.view.IWindow;
Vishnu Naire6e2b0f2019-02-21 10:41:00 -080053import android.view.Surface;
54import android.view.SurfaceControl.Transaction;
Brett Chabota26eda92018-07-23 13:08:30 -070055import android.view.WindowManager;
56
Jorim Jaggi9bafc712017-01-19 17:28:30 +010057import com.android.server.AttributeCache;
Garfield Tan26835f02019-02-07 14:38:38 -080058import com.android.server.wm.utils.MockTracker;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080059
Brett Chabota26eda92018-07-23 13:08:30 -070060import org.junit.After;
Tadashi G. Takaoka5a108b82018-12-13 17:09:12 +090061import org.junit.AfterClass;
Brett Chabota26eda92018-07-23 13:08:30 -070062import org.junit.Before;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070063import org.junit.BeforeClass;
Brett Chabota26eda92018-07-23 13:08:30 -070064import org.junit.Rule;
65
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090066import java.io.IOException;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080067import java.util.HashSet;
Wale Ogunwale34247952017-02-19 11:57:53 -080068import java.util.LinkedList;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010069
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080070/**
71 * Common base class for window manager unit test classes.
chaviw97d28202018-02-27 16:23:53 -080072 *
73 * Make sure any requests to WM hold the WM lock if needed b/73966377
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080074 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080075class WindowTestsBase {
chaviw578b6fe2018-03-06 16:24:17 -080076 private static final String TAG = WindowTestsBase.class.getSimpleName();
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070077
78 WindowManagerService mWm;
Adrian Roos3150dbf2018-03-28 18:06:52 +020079 private final IWindow mIWindow = new TestIWindow();
80 private Session mMockSession;
Bryce Lee310de9e2017-03-15 10:18:21 -070081 // The default display is removed in {@link #setUp} and then we iterate over all displays to
82 // make sure we don't collide with any existing display. If we run into no other display, the
Bryce Lee6272c7f2017-03-17 17:22:35 -070083 // added display should be treated as default. This cannot be the default display
Wale Ogunwale11cc5162017-04-25 20:29:13 -070084 private static int sNextDisplayId = DEFAULT_DISPLAY + 1;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070085 static int sNextStackId = 1000;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080086
Riddle Hsuf53da812018-08-15 22:00:27 +080087 /** Non-default display. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -070088 DisplayContent mDisplayContent;
89 DisplayInfo mDisplayInfo = new DisplayInfo();
Wale Ogunwale11cc5162017-04-25 20:29:13 -070090 WindowState mWallpaperWindow;
91 WindowState mImeWindow;
92 WindowState mImeDialogWindow;
93 WindowState mStatusBarWindow;
94 WindowState mDockedDividerWindow;
95 WindowState mNavBarWindow;
96 WindowState mAppWindow;
97 WindowState mChildAppWindowAbove;
98 WindowState mChildAppWindowBelow;
99 HashSet<WindowState> mCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800100
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900101 private MockTracker mMockTracker;
102
Riddle Hsu3a4bb612019-01-31 00:02:22 +0800103 /**
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800104 * Spied {@link Transaction} class than can be used to verify calls.
Riddle Hsu3a4bb612019-01-31 00:02:22 +0800105 */
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800106 Transaction mTransaction;
Riddle Hsu3a4bb612019-01-31 00:02:22 +0800107
Adrian Roos3150dbf2018-03-28 18:06:52 +0200108 @Rule
109 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
110 new DexmakerShareClassLoaderRule();
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900111 @Rule
112 public final SystemServicesTestRule mSystemServicesTestRule = new SystemServicesTestRule();
Adrian Roos3150dbf2018-03-28 18:06:52 +0200113
Tadashi G. Takaoka5a108b82018-12-13 17:09:12 +0900114 static WindowState.PowerManagerWrapper sPowerManagerWrapper;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700115
116 @BeforeClass
117 public static void setUpOnceBase() {
118 AttributeCache.init(getInstrumentation().getTargetContext());
Tadashi G. Takaoka5a108b82018-12-13 17:09:12 +0900119
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700120 sPowerManagerWrapper = mock(WindowState.PowerManagerWrapper.class);
121 }
chaviw40234662018-02-07 09:37:16 -0800122
Tadashi G. Takaoka5a108b82018-12-13 17:09:12 +0900123 @AfterClass
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900124 public static void tearDownOnceBase() throws IOException {
125 sPowerManagerWrapper = null;
Tadashi G. Takaoka5a108b82018-12-13 17:09:12 +0900126 }
127
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800128 @Before
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700129 public void setUpBase() {
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900130 mMockTracker = new MockTracker();
131
chaviw578b6fe2018-03-06 16:24:17 -0800132 // If @Before throws an exception, the error isn't logged. This will make sure any failures
133 // in the set up are clear. This can be removed when b/37850063 is fixed.
134 try {
Adrian Roos3150dbf2018-03-28 18:06:52 +0200135 mMockSession = mock(Session.class);
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800136 mTransaction = spy(StubTransaction.class);
chaviw578b6fe2018-03-06 16:24:17 -0800137
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700138 final Context context = getInstrumentation().getTargetContext();
chaviw578b6fe2018-03-06 16:24:17 -0800139
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900140 mWm = mSystemServicesTestRule.getWindowManagerService();
Vishnu Naire6e2b0f2019-02-21 10:41:00 -0800141
142 // Setup factory classes to prevent calls to native code.
143
144 // Return a spied Transaction class than can be used to verify calls.
145 mWm.mTransactionFactory = () -> mTransaction;
146 // Return a SurfaceControl.Builder class that creates mocked SurfaceControl instances.
147 mWm.mSurfaceBuilderFactory = (unused) -> new MockSurfaceControlBuilder();
148 // Return mocked Surface instances.
149 mWm.mSurfaceFactory = () -> mock(Surface.class);
150
chaviw578b6fe2018-03-06 16:24:17 -0800151 beforeCreateDisplay();
152
chaviw578b6fe2018-03-06 16:24:17 -0800153 context.getDisplay().getDisplayInfo(mDisplayInfo);
154 mDisplayContent = createNewDisplay();
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700155 mWm.mDisplayEnabled = true;
156 mWm.mDisplayReady = true;
chaviw578b6fe2018-03-06 16:24:17 -0800157
158 // Set-up some common windows.
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700159 mCommonWindows = new HashSet<>();
160 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800161 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
162 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
lumark90120a82018-08-15 00:33:03 +0800163 mDisplayContent.mInputMethodWindow = mImeWindow;
chaviw97d28202018-02-27 16:23:53 -0800164 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG,
165 "mImeDialogWindow");
166 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
167 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
168 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER,
169 "mDockedDividerWindow");
170 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
171 mChildAppWindowAbove = createCommonWindow(mAppWindow,
172 TYPE_APPLICATION_ATTACHED_DIALOG,
173 "mChildAppWindowAbove");
174 mChildAppWindowBelow = createCommonWindow(mAppWindow,
175 TYPE_APPLICATION_MEDIA_OVERLAY,
176 "mChildAppWindowBelow");
177 }
chaviw578b6fe2018-03-06 16:24:17 -0800178 // Adding a display will cause freezing the display. Make sure to wait until it's
179 // unfrozen to not run into race conditions with the tests.
180 waitUntilHandlersIdle();
181 } catch (Exception e) {
182 Log.e(TAG, "Failed to set up test", e);
183 throw e;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800184 }
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800185 }
186
Robert Carrb1579c82017-09-05 14:54:47 -0700187 void beforeCreateDisplay() {
188 // Called before display is created.
189 }
190
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800191 @After
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700192 public void tearDownBase() {
chaviw578b6fe2018-03-06 16:24:17 -0800193 // If @After throws an exception, the error isn't logged. This will make sure any failures
194 // in the tear down are clear. This can be removed when b/37850063 is fixed.
195 try {
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800196 // Test may schedule to perform surface placement or other messages. Wait until a
197 // stable state to clean up for consistency.
198 waitUntilHandlersIdle();
199
200 final LinkedList<WindowState> nonCommonWindows = new LinkedList<>();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700201
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700202 synchronized (mWm.mGlobalLock) {
203 mWm.mRoot.forAllWindows(w -> {
chaviw578b6fe2018-03-06 16:24:17 -0800204 if (!mCommonWindows.contains(w)) {
205 nonCommonWindows.addLast(w);
206 }
207 }, true /* traverseTopToBottom */);
208
209 while (!nonCommonWindows.isEmpty()) {
210 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700211 }
Wale Ogunwale027f4752017-05-12 10:37:16 -0700212
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700213 for (int i = mWm.mRoot.mChildren.size() - 1; i >= 0; --i) {
214 final DisplayContent displayContent = mWm.mRoot.mChildren.get(i);
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800215 if (!displayContent.isDefaultDisplay) {
216 displayContent.removeImmediately();
217 }
218 }
lumark588a3e82018-07-20 18:53:54 +0800219 // Remove app transition & window freeze timeout callbacks to prevent unnecessary
220 // actions after test.
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700221 mWm.getDefaultDisplayContentLocked().mAppTransition
lumark588a3e82018-07-20 18:53:54 +0800222 .removeAppTransitionTimeoutCallbacks();
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700223 mWm.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
lumarkff0ab692018-11-05 20:32:30 +0800224 mDisplayContent.mInputMethodTarget = null;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800225 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800226
Tadashi G. Takaokaa5f7c2c2019-01-09 12:17:14 +0900227 // Cleaned up everything in Handler.
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900228 mSystemServicesTestRule.cleanupWindowManagerHandlers();
chaviw578b6fe2018-03-06 16:24:17 -0800229 } catch (Exception e) {
230 Log.e(TAG, "Failed to tear down test", e);
231 throw e;
Wale Ogunwale34247952017-02-19 11:57:53 -0800232 }
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900233
234 mMockTracker.close();
235 mMockTracker = null;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800236 }
237
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700238 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700239 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800240 final WindowState win = createWindow(parent, type, name);
241 mCommonWindows.add(win);
242 // Prevent common windows from been IMe targets
243 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
244 return win;
245 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800246 }
247
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100248 /**
249 * Waits until the main handler for WM has processed all messages.
250 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100251 void waitUntilHandlersIdle() {
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900252 mSystemServicesTestRule.waitUntilWindowManagerHandlersIdle();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100253 }
254
Wale Ogunwale68278562017-09-23 17:13:55 -0700255 private WindowToken createWindowToken(
256 DisplayContent dc, int windowingMode, int activityType, int type) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700257 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800258 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
259 return WindowTestUtils.createTestWindowToken(type, dc);
260 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800261
chaviw97d28202018-02-27 16:23:53 -0800262 return createAppWindowToken(dc, windowingMode, activityType);
263 }
chaviwb28de1f2018-03-02 10:42:36 -0800264 }
265
266 AppWindowToken createAppWindowToken(DisplayContent dc, int windowingMode, int activityType) {
Vishnu Naira2977262018-07-26 13:31:26 -0700267 return createTestAppWindowToken(dc, windowingMode, activityType);
268 }
269
270 WindowTestUtils.TestAppWindowToken createTestAppWindowToken(DisplayContent dc, int
271 windowingMode, int activityType) {
Vishnu Nairf77d53d2019-02-20 14:38:50 -0800272 return createTestAppWindowToken(dc, windowingMode, activityType,
273 true /*skipOnParentChanged */);
274 }
275
276 WindowTestUtils.TestAppWindowToken createTestAppWindowToken(DisplayContent dc, int
277 windowingMode, int activityType, boolean skipOnParentChanged) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800278 final TaskStack stack = createTaskStackOnDisplay(windowingMode, activityType, dc);
Andrii Kuliand2765632016-12-12 22:26:34 -0800279 final Task task = createTaskInStack(stack, 0 /* userId */);
chaviwb28de1f2018-03-02 10:42:36 -0800280 final WindowTestUtils.TestAppWindowToken appWindowToken =
Vishnu Nairf77d53d2019-02-20 14:38:50 -0800281 WindowTestUtils.createTestAppWindowToken(dc, skipOnParentChanged);
chaviwb28de1f2018-03-02 10:42:36 -0800282 task.addChild(appWindowToken, 0);
283 return appWindowToken;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800284 }
285
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700286 WindowState createWindow(WindowState parent, int type, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700287 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800288 return (parent == null)
289 ? createWindow(parent, type, mDisplayContent, name)
290 : createWindow(parent, type, parent.mToken, name);
291 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800292 }
293
Michal Karpinskia606a292019-01-12 17:29:52 +0000294 WindowState createWindow(WindowState parent, int type, String name, int ownerId) {
295 synchronized (mWm.mGlobalLock) {
296 return (parent == null)
297 ? createWindow(parent, type, mDisplayContent, name, ownerId)
298 : createWindow(parent, type, parent.mToken, name, ownerId);
299 }
300 }
301
Wale Ogunwale68278562017-09-23 17:13:55 -0700302 WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType,
303 int type, DisplayContent dc, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700304 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800305 final WindowToken token = createWindowToken(dc, windowingMode, activityType, type);
306 return createWindow(parent, type, token, name);
307 }
Winson Chung83471632016-12-13 11:02:12 -0800308 }
309
Jorim Jaggi02886a82016-12-06 09:10:06 -0800310 WindowState createAppWindow(Task task, int type, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700311 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800312 final AppWindowToken token = WindowTestUtils.createTestAppWindowToken(mDisplayContent);
313 task.addChild(token, 0);
314 return createWindow(null, type, token, name);
315 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800316 }
317
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700318 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700319 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800320 final WindowToken token = createWindowToken(
321 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
Michal Karpinskia606a292019-01-12 17:29:52 +0000322 return createWindow(parent, type, token, name, 0 /* ownerId */);
323 }
324 }
325
326 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
327 int ownerId) {
328 synchronized (mWm.mGlobalLock) {
329 final WindowToken token = createWindowToken(
330 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
331 return createWindow(parent, type, token, name, ownerId);
chaviw97d28202018-02-27 16:23:53 -0800332 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800333 }
334
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700335 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800336 boolean ownerCanAddInternalSystemWindow) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700337 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800338 final WindowToken token = createWindowToken(
339 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
340 return createWindow(parent, type, token, name, 0 /* ownerId */,
341 ownerCanAddInternalSystemWindow);
342 }
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800343 }
344
Adrian Roos3150dbf2018-03-28 18:06:52 +0200345 WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700346 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800347 return createWindow(parent, type, token, name, 0 /* ownerId */,
348 false /* ownerCanAddInternalSystemWindow */);
349 }
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800350 }
351
Adrian Roos3150dbf2018-03-28 18:06:52 +0200352 WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
Michal Karpinskia606a292019-01-12 17:29:52 +0000353 int ownerId) {
354 synchronized (mWm.mGlobalLock) {
355 return createWindow(parent, type, token, name, ownerId,
356 false /* ownerCanAddInternalSystemWindow */);
357 }
358 }
359
360 WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
Daichi Hironoda0748d2017-12-13 12:48:59 +0900361 int ownerId, boolean ownerCanAddInternalSystemWindow) {
Adrian Roos3150dbf2018-03-28 18:06:52 +0200362 return createWindow(parent, type, token, name, ownerId, ownerCanAddInternalSystemWindow,
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700363 mWm, mMockSession, mIWindow);
Adrian Roos3150dbf2018-03-28 18:06:52 +0200364 }
365
366 static WindowState createWindow(WindowState parent, int type, WindowToken token,
367 String name, int ownerId, boolean ownerCanAddInternalSystemWindow,
368 WindowManagerService service, Session session, IWindow iWindow) {
Wale Ogunwaledb485de2018-10-29 09:47:07 -0700369 synchronized (service.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800370 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
371 attrs.setTitle(name);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800372
Adrian Roos3150dbf2018-03-28 18:06:52 +0200373 final WindowState w = new WindowState(service, session, iWindow, token, parent,
chaviw97d28202018-02-27 16:23:53 -0800374 OP_NONE,
375 0, attrs, VISIBLE, ownerId, ownerCanAddInternalSystemWindow,
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700376 sPowerManagerWrapper);
chaviw97d28202018-02-27 16:23:53 -0800377 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
378 // adding it to the token...
379 token.addWindow(w);
380 return w;
381 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800382 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800383
Andrii Kuliand2765632016-12-12 22:26:34 -0800384 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700385 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700386 synchronized (mWm.mGlobalLock) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800387 return createTaskStackOnDisplay(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, dc);
chaviw97d28202018-02-27 16:23:53 -0800388 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800389 }
390
Yunfan Chen279f5582018-12-12 15:24:50 -0800391 TaskStack createTaskStackOnDisplay(int windowingMode, int activityType, DisplayContent dc) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700392 synchronized (mWm.mGlobalLock) {
chaviw97d28202018-02-27 16:23:53 -0800393 final Configuration overrideConfig = new Configuration();
394 overrideConfig.windowConfiguration.setWindowingMode(windowingMode);
395 overrideConfig.windowConfiguration.setActivityType(activityType);
396 final int stackId = ++sNextStackId;
Yunfan Chen279f5582018-12-12 15:24:50 -0800397 final TaskStack stack = new TaskStack(mWm, stackId, mock(ActivityStack.class));
398 dc.setStackOnDisplay(stackId, true, stack);
399 stack.onRequestedOverrideConfigurationChanged(overrideConfig);
400 return stack;
chaviw97d28202018-02-27 16:23:53 -0800401 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800402 }
403
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800404 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700405 Task createTaskInStack(TaskStack stack, int userId) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700406 return WindowTestUtils.createTaskInStack(mWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800407 }
408
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800409 /** Creates a {@link DisplayContent} and adds it to the system. */
410 DisplayContent createNewDisplay() {
Chilun8753ad32018-10-09 15:56:45 +0800411 return createNewDisplay(mDisplayInfo);
412 }
413
414 /** Creates a {@link DisplayContent} and adds it to the system. */
415 DisplayContent createNewDisplay(DisplayInfo displayInfo) {
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800416 final int displayId = sNextDisplayId++;
417 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Chilun8753ad32018-10-09 15:56:45 +0800418 displayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700419 synchronized (mWm.mGlobalLock) {
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800420 return new DisplayContent(display, mWm, mock(ActivityDisplay.class));
chaviw97d28202018-02-27 16:23:53 -0800421 }
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800422 }
423
lumark588a3e82018-07-20 18:53:54 +0800424 /**
425 * Creates a {@link DisplayContent} with given display state and adds it to the system.
426 *
lumark588a3e82018-07-20 18:53:54 +0800427 * @param displayState For initializing the state of the display. See
428 * {@link Display#getState()}.
429 */
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800430 DisplayContent createNewDisplay(int displayState) {
lumark588a3e82018-07-20 18:53:54 +0800431 // Leverage main display info & initialize it with display state for given displayId.
432 DisplayInfo displayInfo = new DisplayInfo();
433 displayInfo.copyFrom(mDisplayInfo);
434 displayInfo.state = displayState;
435 final int displayId = sNextDisplayId++;
436 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
437 displayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700438 synchronized (mWm.mGlobalLock) {
lumark588a3e82018-07-20 18:53:54 +0800439 // Display creation is driven by DisplayWindowController via ActivityStackSupervisor.
440 // We skip those steps here.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800441 final ActivityDisplay mockAd = mock(ActivityDisplay.class);
442 return mWm.mRoot.createDisplayContent(display, mockAd);
lumark588a3e82018-07-20 18:53:54 +0800443 }
444 }
445
Bryce Leeaf691c02017-03-20 14:20:22 -0700446 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
447 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
448 WindowToken token) {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700449 synchronized (mWm.mGlobalLock) {
450 return new WindowTestUtils.TestWindowState(mWm, mMockSession, mIWindow, attrs, token);
chaviw97d28202018-02-27 16:23:53 -0800451 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800452 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800453}