blob: 7c25e43c11aac8e144026c78979c14b77273dd64 [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
Jorim Jaggi9bafc712017-01-19 17:28:30 +010019import android.app.ActivityManagerInternal;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080020import android.content.res.Configuration;
21import android.graphics.Rect;
22import android.os.Binder;
23import android.view.IApplicationToken;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080024import org.junit.Assert;
25import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010026import org.mockito.Mock;
27import org.mockito.Mockito;
28import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080029
30import android.content.Context;
31import android.os.IBinder;
32import 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;
37import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080038import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080039import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080040import static android.content.res.Configuration.EMPTY;
41import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
42import 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 Ogunwalee1fe7fa22016-12-15 18:27:00 -080052import static com.android.server.wm.WindowContainer.POSITION_TOP;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080053import static org.mockito.Mockito.mock;
54
Jorim Jaggi9bafc712017-01-19 17:28:30 +010055import com.android.server.AttributeCache;
56import com.android.server.LocalServices;
57
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080058/**
59 * Common base class for window manager unit test classes.
60 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080061class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080062 static WindowManagerService sWm = null;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010063 static TestWindowManagerPolicy sPolicy = null;
64 private final static IWindow sIWindow = new TestIWindow();
65 private final static Session sMockSession = mock(Session.class);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080066 static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080067 private static int sNextTaskId = 0;
68
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080069 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080070 static DisplayContent sDisplayContent;
71 static WindowLayersController sLayersController;
72 static WindowState sWallpaperWindow;
73 static WindowState sImeWindow;
74 static WindowState sImeDialogWindow;
75 static WindowState sStatusBarWindow;
76 static WindowState sDockedDividerWindow;
77 static WindowState sNavBarWindow;
78 static WindowState sAppWindow;
79 static WindowState sChildAppWindowAbove;
80 static WindowState sChildAppWindowBelow;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010081 static @Mock ActivityManagerInternal sMockAm;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080082
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080083 @Before
84 public void setUp() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080085 if (sOneTimeSetupDone) {
Jorim Jaggi9bafc712017-01-19 17:28:30 +010086 Mockito.reset(sMockAm);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080087 return;
88 }
89 sOneTimeSetupDone = true;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010090 MockitoAnnotations.initMocks(this);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080091 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010092 LocalServices.addService(ActivityManagerInternal.class, sMockAm);
93 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080094 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Jorim Jaggi9bafc712017-01-19 17:28:30 +010095 sPolicy = (TestWindowManagerPolicy) sWm.mPolicy;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080096 sLayersController = new WindowLayersController(sWm);
97 sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController,
98 new WallpaperController(sWm));
Andrii Kuliand2765632016-12-12 22:26:34 -080099 sWm.mRoot.addChild(sDisplayContent, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100100 sWm.mDisplayEnabled = true;
101 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800102
103 // Set-up some common windows.
104 sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow");
105 sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow");
106 sImeDialogWindow =
107 createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow");
108 sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow");
109 sNavBarWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800110 createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800111 sDockedDividerWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800112 createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800113 sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow");
114 sChildAppWindowAbove = createWindow(sAppWindow,
115 TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove");
116 sChildAppWindowBelow = createWindow(sAppWindow,
117 TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow");
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800118 }
119
120 /** Asserts that the first entry is greater than the second entry. */
121 void assertGreaterThan(int first, int second) throws Exception {
122 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
123 }
124
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100125 /**
126 * Waits until the main handler for WM has processed all messages.
127 */
128 void waitUntilHandlerIdle() {
129 sWm.mH.runWithScissors(() -> { }, 0);
130 }
131
132 private static WindowToken createWindowToken(DisplayContent dc, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800133 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800134 return new TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800135 }
136
Andrii Kuliand2765632016-12-12 22:26:34 -0800137 final TaskStack stack = createTaskStackOnDisplay(dc);
138 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800139 final TestAppWindowToken token = new TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800140 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800141 return token;
142 }
143
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100144 static WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800145 return (parent == null)
146 ? createWindow(parent, type, sDisplayContent, name)
147 : createWindow(parent, type, parent.mToken, name);
148 }
149
Jorim Jaggi02886a82016-12-06 09:10:06 -0800150 WindowState createAppWindow(Task task, int type, String name) {
151 final AppWindowToken token = new TestAppWindowToken(sDisplayContent);
152 task.addChild(token, 0);
153 return createWindow(null, type, token, name);
154 }
155
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100156 static WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800157 final WindowToken token = createWindowToken(dc, type);
158 return createWindow(parent, type, token, name);
159 }
160
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100161 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800162 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
163 attrs.setTitle(name);
164
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100165 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800166 0, attrs, 0, 0);
167 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
168 // adding it to the token...
169 token.addWindow(w);
170 return w;
171 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800172
Andrii Kuliand2765632016-12-12 22:26:34 -0800173 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100174 static TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800175 final int stackId = sNextStackId++;
176 dc.addStackToDisplay(stackId, true);
177 return sWm.mStackIdToStack.get(stackId);
178 }
179
180 /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100181 static Task createTaskInStack(TaskStack stack, int userId) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800182 final Task newTask = new Task(sNextTaskId++, stack, userId, sWm, null, EMPTY, false, 0,
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800183 false, null);
184 stack.addTask(newTask, POSITION_TOP);
Andrii Kuliand2765632016-12-12 22:26:34 -0800185 return newTask;
186 }
187
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800188 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100189 static class TestWindowToken extends WindowToken {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800190
191 TestWindowToken(int type, DisplayContent dc) {
192 this(type, dc, false /* persistOnEmpty */);
193 }
194
195 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
196 super(sWm, mock(IBinder.class), type, persistOnEmpty, dc);
197 }
198
199 int getWindowsCount() {
200 return mChildren.size();
201 }
202
203 boolean hasWindow(WindowState w) {
204 return mChildren.contains(w);
205 }
206 }
207
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800208 /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100209 static class TestAppWindowToken extends AppWindowToken {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800210
211 TestAppWindowToken(DisplayContent dc) {
212 super(sWm, null, false, dc);
213 }
214
215 int getWindowsCount() {
216 return mChildren.size();
217 }
218
219 boolean hasWindow(WindowState w) {
220 return mChildren.contains(w);
221 }
222
223 WindowState getFirstChild() {
224 return mChildren.getFirst();
225 }
226
227 WindowState getLastChild() {
228 return mChildren.getLast();
229 }
230 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800231
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800232 /* Used so we can gain access to some protected members of the {@link Task} class */
233 class TestTask extends Task {
234
235 boolean mShouldDeferRemoval = false;
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800236 boolean mOnDisplayChangedCalled = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800237
238 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
239 Configuration overrideConfig, boolean isOnTopLauncher, int resizeMode,
240 boolean homeTask, TaskWindowContainerController controller) {
241 super(taskId, stack, userId, service, bounds, overrideConfig, isOnTopLauncher,
242 resizeMode, homeTask, controller);
243 }
244
245 boolean shouldDeferRemoval() {
246 return mShouldDeferRemoval;
247 }
248
249 int positionInParent() {
250 return getParent().mChildren.indexOf(this);
251 }
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800252
253 @Override
254 void onDisplayChanged(DisplayContent dc) {
255 super.onDisplayChanged(dc);
256 mOnDisplayChangedCalled = true;
257 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800258 }
259
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800260 /**
261 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
262 * class.
263 */
264 class TestTaskWindowContainerController extends TaskWindowContainerController {
265
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800266 TestTaskWindowContainerController() {
267 this(createTaskStackOnDisplay(sDisplayContent).mStackId);
268 }
269
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800270 TestTaskWindowContainerController(int stackId) {
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100271 super(sNextTaskId++, snapshot -> {}, stackId, 0 /* userId */, null /* bounds */,
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800272 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/,
273 false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */);
274 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800275
276 @Override
277 TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds,
278 Configuration overrideConfig, int resizeMode, boolean homeTask,
279 boolean isOnTopLauncher) {
280 return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig,
281 isOnTopLauncher, resizeMode, homeTask, this);
282 }
283 }
284
285 class TestAppWindowContainerController extends AppWindowContainerController {
286
287 final IApplicationToken mToken;
288
289 TestAppWindowContainerController(TestTaskWindowContainerController taskController) {
290 this(taskController, new TestIApplicationToken());
291 }
292
293 TestAppWindowContainerController(TestTaskWindowContainerController taskController,
294 IApplicationToken token) {
295 super(taskController, token, null /* listener */, 0 /* index */,
296 SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */,
297 true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */,
298 false /* launchTaskBehind */, false /* alwaysFocusable */,
299 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */,
300 0 /* inputDispatchingTimeoutNanos */, sWm);
301 mToken = token;
302 }
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100303
304 AppWindowToken getAppWindowToken() {
305 return (AppWindowToken) sDisplayContent.getWindowToken(mToken.asBinder());
306 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800307 }
308
309 class TestIApplicationToken implements IApplicationToken {
310
311 private final Binder mBinder = new Binder();
312 @Override
313 public IBinder asBinder() {
314 return mBinder;
315 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800316 }
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800317
318 /** Used to track resize reports. */
319 class TestWindowState extends WindowState {
320 boolean resizeReported;
321
322 TestWindowState(WindowManager.LayoutParams attrs, WindowToken token) {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100323 super(sWm, sMockSession, sIWindow, token, null, OP_NONE, 0, attrs, 0, 0);
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800324 }
325
326 @Override
327 void reportResized() {
328 super.reportResized();
329 resizeReported = true;
330 }
331
332 @Override
333 public boolean isGoneForLayoutLw() {
334 return false;
335 }
336
337 @Override
338 void updateResizingWindowIfNeeded() {
339 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
340 // the system that it can actually update the window.
341 boolean hadSurface = mHasSurface;
342 mHasSurface = true;
343
344 super.updateResizingWindowIfNeeded();
345
346 mHasSurface = hadSurface;
347 }
348 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800349}