Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Configuration; |
| 22 | import android.graphics.Rect; |
| 23 | import android.os.Binder; |
| 24 | import android.os.IBinder; |
| 25 | import android.view.IApplicationToken; |
| 26 | import android.view.IWindow; |
| 27 | import android.view.WindowManager; |
| 28 | |
| 29 | import static android.app.AppOpsManager.OP_NONE; |
| 30 | import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE; |
| 31 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 32 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 33 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.any; |
| 35 | import static org.mockito.Mockito.anyBoolean; |
| 36 | import static org.mockito.Mockito.anyFloat; |
| 37 | import static org.mockito.Mockito.doAnswer; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 38 | import static org.mockito.Mockito.mock; |
Bryce Lee | 2b17afd | 2017-09-21 10:38:20 -0700 | [diff] [blame] | 39 | import static org.mockito.Mockito.when; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 40 | |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 41 | import org.mockito.invocation.InvocationOnMock; |
| 42 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 43 | /** |
| 44 | * A collection of static functions that can be referenced by other test packages to provide access |
| 45 | * to WindowManager related test functionality. |
| 46 | */ |
| 47 | public class WindowTestUtils { |
| 48 | public static int sNextTaskId = 0; |
| 49 | |
| 50 | /** |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 51 | * Retrieves an instance of a mock {@link WindowManagerService}. |
| 52 | */ |
| 53 | public static WindowManagerService getMockWindowManagerService() { |
Bryce Lee | 2b17afd | 2017-09-21 10:38:20 -0700 | [diff] [blame] | 54 | final WindowManagerService service = mock(WindowManagerService.class); |
| 55 | final WindowHashMap windowMap = new WindowHashMap(); |
| 56 | when(service.getWindowManagerLock()).thenReturn(windowMap); |
| 57 | return service; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 61 | * Creates a mock instance of {@link StackWindowController}. |
| 62 | */ |
| 63 | public static StackWindowController createMockStackWindowContainerController() { |
| 64 | StackWindowController controller = mock(StackWindowController.class); |
| 65 | controller.mContainer = mock(TestTaskStack.class); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 66 | |
| 67 | // many components rely on the {@link StackWindowController#adjustConfigurationForBounds} |
| 68 | // to properly set bounds values in the configuration. We must mimick those actions here. |
| 69 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 70 | final Configuration config = invocationOnMock.<Configuration>getArgument(7); |
| 71 | final Rect bounds = invocationOnMock.<Rect>getArgument(0); |
| 72 | config.windowConfiguration.setBounds(bounds); |
| 73 | return null; |
| 74 | }).when(controller).adjustConfigurationForBounds(any(), any(), any(), any(), |
| 75 | anyBoolean(), anyBoolean(), anyFloat(), any(), any()); |
| 76 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 77 | return controller; |
| 78 | } |
| 79 | |
| 80 | /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */ |
| 81 | public static Task createTaskInStack(WindowManagerService service, TaskStack stack, |
| 82 | int userId) { |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 83 | synchronized (service.mWindowMap) { |
| 84 | final Task newTask = new Task(sNextTaskId++, stack, userId, service, 0, false, |
| 85 | new ActivityManager.TaskDescription(), null); |
| 86 | stack.addTask(newTask, POSITION_TOP); |
| 87 | return newTask; |
| 88 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * An extension of {@link TestTaskStack}, which overrides package scoped methods that would not |
| 93 | * normally be mocked out. |
| 94 | */ |
| 95 | public static class TestTaskStack extends TaskStack { |
| 96 | TestTaskStack(WindowManagerService service, int stackId) { |
Wale Ogunwale | 704a3c0 | 2017-09-18 15:30:52 -0700 | [diff] [blame] | 97 | super(service, stackId, null); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | void addTask(Task task, int position, boolean showForAllUsers, boolean moveParents) { |
| 102 | // Do nothing. |
| 103 | } |
| 104 | } |
| 105 | |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 106 | static TestAppWindowToken createTestAppWindowToken(DisplayContent dc) { |
| 107 | synchronized (dc.mService.mWindowMap) { |
| 108 | return new TestAppWindowToken(dc); |
| 109 | } |
| 110 | } |
| 111 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 112 | /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */ |
| 113 | public static class TestAppWindowToken extends AppWindowToken { |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 114 | boolean mOnTop = false; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 115 | |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 116 | private TestAppWindowToken(DisplayContent dc) { |
Steven Timotius | af03df6 | 2017-07-18 16:56:43 -0700 | [diff] [blame] | 117 | super(dc.mService, new IApplicationToken.Stub() { |
| 118 | public String getName() {return null;} |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 119 | }, false, dc, true /* fillsParent */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | TestAppWindowToken(WindowManagerService service, IApplicationToken token, |
| 123 | boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos, |
| 124 | boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation, |
| 125 | int rotationAnimationHint, int configChanges, boolean launchTaskBehind, |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 126 | boolean alwaysFocusable, AppWindowContainerController controller) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 127 | super(service, token, voiceInteraction, dc, inputDispatchingTimeoutNanos, fullscreen, |
| 128 | showForAllUsers, targetSdk, orientation, rotationAnimationHint, configChanges, |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 129 | launchTaskBehind, alwaysFocusable, controller); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int getWindowsCount() { |
| 133 | return mChildren.size(); |
| 134 | } |
| 135 | |
| 136 | boolean hasWindow(WindowState w) { |
| 137 | return mChildren.contains(w); |
| 138 | } |
| 139 | |
| 140 | WindowState getFirstChild() { |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 141 | return mChildren.peekFirst(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | WindowState getLastChild() { |
Jorim Jaggi | 612bb88 | 2017-05-16 17:11:18 +0200 | [diff] [blame] | 145 | return mChildren.peekLast(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int positionInParent() { |
| 149 | return getParent().mChildren.indexOf(this); |
| 150 | } |
Bryce Lee | 00d586d | 2017-07-28 20:48:43 -0700 | [diff] [blame] | 151 | |
| 152 | void setIsOnTop(boolean onTop) { |
| 153 | mOnTop = onTop; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | boolean isOnTop() { |
| 158 | return mOnTop; |
| 159 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 160 | } |
| 161 | |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 162 | static TestWindowToken createTestWindowToken(int type, DisplayContent dc) { |
| 163 | return createTestWindowToken(type, dc, false /* persistOnEmpty */); |
| 164 | } |
| 165 | |
| 166 | static TestWindowToken createTestWindowToken(int type, DisplayContent dc, |
| 167 | boolean persistOnEmpty) { |
| 168 | synchronized (dc.mService.mWindowMap) { |
| 169 | return new TestWindowToken(type, dc, persistOnEmpty); |
| 170 | } |
| 171 | } |
| 172 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 173 | /* Used so we can gain access to some protected members of the {@link WindowToken} class */ |
| 174 | public static class TestWindowToken extends WindowToken { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 175 | |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 176 | private TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 177 | super(dc.mService, mock(IBinder.class), type, persistOnEmpty, dc, |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 178 | false /* ownerCanManageAppTokens */); |
| 179 | } |
| 180 | |
| 181 | int getWindowsCount() { |
| 182 | return mChildren.size(); |
| 183 | } |
| 184 | |
| 185 | boolean hasWindow(WindowState w) { |
| 186 | return mChildren.contains(w); |
| 187 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Used so we can gain access to some protected members of the {@link Task} class */ |
| 191 | public static class TestTask extends Task { |
| 192 | boolean mShouldDeferRemoval = false; |
| 193 | boolean mOnDisplayChangedCalled = false; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 194 | private boolean mIsAnimating = false; |
| 195 | |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 196 | TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 197 | int resizeMode, boolean supportsPictureInPicture, |
Wale Ogunwale | 6fbde9f | 2017-08-24 07:24:12 -0700 | [diff] [blame] | 198 | TaskWindowContainerController controller) { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 199 | super(taskId, stack, userId, service, resizeMode, supportsPictureInPicture, |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 200 | new ActivityManager.TaskDescription(), controller); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | boolean shouldDeferRemoval() { |
| 204 | return mShouldDeferRemoval; |
| 205 | } |
| 206 | |
| 207 | int positionInParent() { |
| 208 | return getParent().mChildren.indexOf(this); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | void onDisplayChanged(DisplayContent dc) { |
| 213 | super.onDisplayChanged(dc); |
| 214 | mOnDisplayChangedCalled = true; |
| 215 | } |
| 216 | |
| 217 | @Override |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 218 | boolean isSelfAnimating() { |
| 219 | return mIsAnimating; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void setLocalIsAnimating(boolean isAnimating) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 223 | mIsAnimating = isAnimating; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Used so we can gain access to some protected members of {@link TaskWindowContainerController} |
| 229 | * class. |
| 230 | */ |
| 231 | public static class TestTaskWindowContainerController extends TaskWindowContainerController { |
| 232 | |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 233 | TestTaskWindowContainerController(WindowTestsBase testsBase) { |
| 234 | this(testsBase.createStackControllerOnDisplay(testsBase.mDisplayContent)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | TestTaskWindowContainerController(StackWindowController stackController) { |
| 238 | super(sNextTaskId++, new TaskWindowContainerListener() { |
| 239 | @Override |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 240 | public void registerConfigurationChangeListener( |
| 241 | ConfigurationContainerListener listener) { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void unregisterConfigurationChangeListener( |
| 247 | ConfigurationContainerListener listener) { |
| 248 | |
| 249 | } |
| 250 | |
| 251 | @Override |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 252 | public void onSnapshotChanged(ActivityManager.TaskSnapshot snapshot) { |
| 253 | |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void requestResize(Rect bounds, int resizeMode) { |
| 258 | |
| 259 | } |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 260 | }, stackController, 0 /* userId */, null /* bounds */, RESIZE_MODE_UNRESIZEABLE, |
Wale Ogunwale | 6fbde9f | 2017-08-24 07:24:12 -0700 | [diff] [blame] | 261 | false /* supportsPictureInPicture */, true /* toTop*/, |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 262 | true /* showForAllUsers */, new ActivityManager.TaskDescription(), |
| 263 | stackController.mService); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 267 | TestTask createTask(int taskId, TaskStack stack, int userId, int resizeMode, |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 268 | boolean supportsPictureInPicture, ActivityManager.TaskDescription taskDescription) { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 269 | return new TestTask(taskId, stack, userId, mService, resizeMode, |
Wale Ogunwale | 6fbde9f | 2017-08-24 07:24:12 -0700 | [diff] [blame] | 270 | supportsPictureInPicture, this); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | public static class TestAppWindowContainerController extends AppWindowContainerController { |
| 275 | |
| 276 | final IApplicationToken mToken; |
| 277 | |
| 278 | TestAppWindowContainerController(TestTaskWindowContainerController taskController) { |
| 279 | this(taskController, new TestIApplicationToken()); |
| 280 | } |
| 281 | |
| 282 | TestAppWindowContainerController(TestTaskWindowContainerController taskController, |
| 283 | IApplicationToken token) { |
| 284 | super(taskController, token, null /* listener */, 0 /* index */, |
| 285 | SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */, |
| 286 | true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */, |
| 287 | false /* launchTaskBehind */, false /* alwaysFocusable */, |
| 288 | 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */, |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 289 | 0 /* inputDispatchingTimeoutNanos */, taskController.mService); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 290 | mToken = token; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | AppWindowToken createAppWindow(WindowManagerService service, IApplicationToken token, |
| 295 | boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos, |
| 296 | boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation, |
| 297 | int rotationAnimationHint, int configChanges, boolean launchTaskBehind, |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 298 | boolean alwaysFocusable, AppWindowContainerController controller) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 299 | return new TestAppWindowToken(service, token, voiceInteraction, dc, |
| 300 | inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdk, |
| 301 | orientation, |
| 302 | rotationAnimationHint, configChanges, launchTaskBehind, alwaysFocusable, |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 303 | controller); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 306 | AppWindowToken getAppWindowToken(DisplayContent dc) { |
| 307 | return (AppWindowToken) dc.getWindowToken(mToken.asBinder()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | public static class TestIApplicationToken implements IApplicationToken { |
| 312 | |
| 313 | private final Binder mBinder = new Binder(); |
| 314 | @Override |
| 315 | public IBinder asBinder() { |
| 316 | return mBinder; |
| 317 | } |
Steven Timotius | af03df6 | 2017-07-18 16:56:43 -0700 | [diff] [blame] | 318 | @Override |
| 319 | public String getName() { |
| 320 | return null; |
| 321 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** Used to track resize reports. */ |
| 325 | public static class TestWindowState extends WindowState { |
| 326 | boolean resizeReported; |
| 327 | |
| 328 | TestWindowState(WindowManagerService service, Session session, IWindow window, |
| 329 | WindowManager.LayoutParams attrs, WindowToken token) { |
| 330 | super(service, session, window, token, null, OP_NONE, 0, attrs, 0, 0, |
| 331 | false /* ownerCanAddInternalSystemWindow */); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | void reportResized() { |
| 336 | super.reportResized(); |
| 337 | resizeReported = true; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public boolean isGoneForLayoutLw() { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | void updateResizingWindowIfNeeded() { |
| 347 | // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive |
| 348 | // the system that it can actually update the window. |
| 349 | boolean hadSurface = mHasSurface; |
| 350 | mHasSurface = true; |
| 351 | |
| 352 | super.updateResizingWindowIfNeeded(); |
| 353 | |
| 354 | mHasSurface = hadSurface; |
| 355 | } |
| 356 | } |
| 357 | } |