blob: 6618a699bee9add8803a14087be68ba6897ef59f [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
14 * limitations under the License
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale11cc5162017-04-25 20:29:13 -070019import static android.view.Display.DEFAULT_DISPLAY;
David Stevens46939562017-03-24 13:04:00 -070020import static android.view.View.VISIBLE;
21
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080022import android.graphics.Rect;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080023import android.hardware.display.DisplayManagerGlobal;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080024import android.view.Display;
25import android.view.DisplayInfo;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080026import org.junit.Assert;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080027import org.junit.After;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080028import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010029import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080030
31import android.content.Context;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080032import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080033import android.view.IWindow;
34import android.view.WindowManager;
35
36import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
Winson Chung83471632016-12-13 11:02:12 -080037import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080038import static android.app.AppOpsManager.OP_NONE;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080039import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080040import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Wale Ogunwale34247952017-02-19 11:57:53 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080042import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080043import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
44import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
45import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
46import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
47import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
48import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
49import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
50import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
51import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080052import static org.mockito.Mockito.mock;
53
Jorim Jaggi9bafc712017-01-19 17:28:30 +010054import com.android.server.AttributeCache;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080055
56import java.util.HashSet;
Wale Ogunwale34247952017-02-19 11:57:53 -080057import java.util.LinkedList;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010058
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080059/**
60 * Common base class for window manager unit test classes.
61 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080062class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080063 static WindowManagerService sWm = null;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070064 private static final IWindow sIWindow = new TestIWindow();
65 private static final Session sMockSession = mock(Session.class);
Bryce Lee310de9e2017-03-15 10:18:21 -070066 // The default display is removed in {@link #setUp} and then we iterate over all displays to
67 // 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 -070068 // added display should be treated as default. This cannot be the default display
Wale Ogunwale11cc5162017-04-25 20:29:13 -070069 private static int sNextDisplayId = DEFAULT_DISPLAY + 1;
Bryce Leeaf691c02017-03-20 14:20:22 -070070 private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080071
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080072 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070073 DisplayContent mDisplayContent;
74 DisplayInfo mDisplayInfo = new DisplayInfo();
75 WindowLayersController mLayersController;
76 WindowState mWallpaperWindow;
77 WindowState mImeWindow;
78 WindowState mImeDialogWindow;
79 WindowState mStatusBarWindow;
80 WindowState mDockedDividerWindow;
81 WindowState mNavBarWindow;
82 WindowState mAppWindow;
83 WindowState mChildAppWindowAbove;
84 WindowState mChildAppWindowBelow;
85 HashSet<WindowState> mCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080086
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080087 @Before
88 public void setUp() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070089 if (!sOneTimeSetupDone) {
90 sOneTimeSetupDone = true;
91 MockitoAnnotations.initMocks(this);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080092 }
Wale Ogunwale11cc5162017-04-25 20:29:13 -070093
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080094 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010095 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080096 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale11cc5162017-04-25 20:29:13 -070097 mLayersController = new WindowLayersController(sWm);
Bryce Lee6272c7f2017-03-17 17:22:35 -070098
Wale Ogunwale11cc5162017-04-25 20:29:13 -070099 context.getDisplay().getDisplayInfo(mDisplayInfo);
100 mDisplayContent = createNewDisplay();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100101 sWm.mDisplayEnabled = true;
102 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800103
104 // Set-up some common windows.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700105 mCommonWindows = new HashSet();
106 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
107 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
108 sWm.mInputMethodWindow = mImeWindow;
109 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
110 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
111 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
112 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
113 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
114 mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
115 "mChildAppWindowAbove");
116 mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
117 "mChildAppWindowBelow");
Jorim Jaggi153badb2017-05-24 13:18:28 +0200118
119 // Adding a display will cause freezing the display. Make sure to wait until it's unfrozen
120 // to not run into race conditions with the tests.
121 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800122 }
123
124 @After
125 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800126 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700127
128 synchronized (sWm.mWindowMap) {
129 sWm.mRoot.forAllWindows(w -> {
130 if (!mCommonWindows.contains(w)) {
131 nonCommonWindows.addLast(w);
132 }
133 }, true /* traverseTopToBottom */);
134
135 while (!nonCommonWindows.isEmpty()) {
136 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800137 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800138
Wale Ogunwale027f4752017-05-12 10:37:16 -0700139 mDisplayContent.removeImmediately();
140 sWm.mInputMethodTarget = null;
Wale Ogunwale34247952017-02-19 11:57:53 -0800141 }
Jorim Jaggi153badb2017-05-24 13:18:28 +0200142
143 // Wait until everything is really cleaned up.
144 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800145 }
146
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700147 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800148 final WindowState win = createWindow(parent, type, name);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700149 mCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800150 // Prevent common windows from been IMe targets
151 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800152 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800153 }
154
155 /** Asserts that the first entry is greater than the second entry. */
156 void assertGreaterThan(int first, int second) throws Exception {
157 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
158 }
159
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100160 /**
161 * Waits until the main handler for WM has processed all messages.
162 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100163 void waitUntilHandlersIdle() {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100164 sWm.mH.runWithScissors(() -> { }, 0);
Jorim Jaggied7993b2017-03-28 18:50:01 +0100165 sWm.mAnimationHandler.runWithScissors(() -> { }, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100166 }
167
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700168 private WindowToken createWindowToken(DisplayContent dc, int stackId, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800169 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700170 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800171 }
172
Winson Chung83471632016-12-13 11:02:12 -0800173 final TaskStack stack = stackId == INVALID_STACK_ID
174 ? createTaskStackOnDisplay(dc)
175 : createStackControllerOnStackOnDisplay(stackId, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800176 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700177 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800178 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800179 return token;
180 }
181
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700182 WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800183 return (parent == null)
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700184 ? createWindow(parent, type, mDisplayContent, name)
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800185 : createWindow(parent, type, parent.mToken, name);
186 }
187
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700188 WindowState createWindowOnStack(WindowState parent, int stackId, int type,
Winson Chung83471632016-12-13 11:02:12 -0800189 DisplayContent dc, String name) {
190 final WindowToken token = createWindowToken(dc, stackId, type);
191 return createWindow(parent, type, token, name);
192 }
193
Jorim Jaggi02886a82016-12-06 09:10:06 -0800194 WindowState createAppWindow(Task task, int type, String name) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700195 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(mDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800196 task.addChild(token, 0);
197 return createWindow(null, type, token, name);
198 }
199
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700200 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Winson Chung83471632016-12-13 11:02:12 -0800201 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800202 return createWindow(parent, type, token, name);
203 }
204
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700205 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800206 boolean ownerCanAddInternalSystemWindow) {
Winson Chung83471632016-12-13 11:02:12 -0800207 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800208 return createWindow(parent, type, token, name, ownerCanAddInternalSystemWindow);
209 }
210
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100211 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800212 return createWindow(parent, type, token, name, false /* ownerCanAddInternalSystemWindow */);
213 }
214
215 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
216 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800217 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
218 attrs.setTitle(name);
219
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800220 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
David Stevens46939562017-03-24 13:04:00 -0700221 0, attrs, VISIBLE, 0, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800222 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
223 // adding it to the token...
224 token.addWindow(w);
225 return w;
226 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800227
Andrii Kuliand2765632016-12-12 22:26:34 -0800228 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700229 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800230 return createStackControllerOnDisplay(dc).mContainer;
231 }
232
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700233 StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800234 final int stackId = ++sNextStackId;
Winson Chung83471632016-12-13 11:02:12 -0800235 return createStackControllerOnStackOnDisplay(stackId, dc);
236 }
237
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700238 StackWindowController createStackControllerOnStackOnDisplay(int stackId,
Winson Chung83471632016-12-13 11:02:12 -0800239 DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800240 return new StackWindowController(stackId, null, dc.getDisplayId(),
241 true /* onTop */, new Rect(), sWm);
Andrii Kuliand2765632016-12-12 22:26:34 -0800242 }
243
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800244 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700245 Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700246 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800247 }
248
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800249 /** Creates a {@link DisplayContent} and adds it to the system. */
250 DisplayContent createNewDisplay() {
251 final int displayId = sNextDisplayId++;
252 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700253 mDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
254 return new DisplayContent(display, sWm, mLayersController, new WallpaperController(sWm));
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800255 }
256
Bryce Leeaf691c02017-03-20 14:20:22 -0700257 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
258 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
259 WindowToken token) {
260 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800261 }
262
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800263}