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