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.am; |
| 18 | |
| 19 | import static org.mockito.Mockito.mock; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.doReturn; |
| 21 | import static org.mockito.Mockito.any; |
| 22 | import static org.mockito.Mockito.doAnswer; |
| 23 | |
| 24 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 25 | |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.pm.ActivityInfo; |
| 31 | import android.content.pm.ApplicationInfo; |
| 32 | import android.content.res.Configuration; |
| 33 | import android.graphics.Rect; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 34 | import android.os.HandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 35 | import android.os.Looper; |
| 36 | import android.support.test.InstrumentationRegistry; |
| 37 | import com.android.server.AttributeCache; |
| 38 | import com.android.server.wm.AppWindowContainerController; |
| 39 | import com.android.server.wm.StackWindowController; |
| 40 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 41 | import com.android.server.wm.TaskWindowContainerController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 42 | import com.android.server.wm.WindowManagerService; |
| 43 | import com.android.server.wm.WindowTestUtils; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 44 | import org.junit.After; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.mockito.MockitoAnnotations; |
| 47 | |
| 48 | /** |
| 49 | * A base class to handle common operations in activity related unit tests. |
| 50 | */ |
| 51 | public class ActivityTestsBase { |
| 52 | private final Context mContext = InstrumentationRegistry.getContext(); |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 53 | private HandlerThread mHandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 54 | |
| 55 | // Grabbing an instance of {@link WindowManagerService} creates it if not present so this must |
| 56 | // be called at before any tests. |
| 57 | private final WindowManagerService mWms = WindowTestUtils.getWindowManagerService(mContext); |
| 58 | |
| 59 | @Before |
| 60 | public void setUp() throws Exception { |
| 61 | MockitoAnnotations.initMocks(this); |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 62 | mHandlerThread = new HandlerThread("ActivityTestsBaseThread"); |
| 63 | mHandlerThread.start(); |
| 64 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 65 | |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 66 | @After |
| 67 | public void tearDown() { |
| 68 | mHandlerThread.quitSafely(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | protected ActivityManagerService createActivityManagerService() { |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 72 | final ActivityManagerService service = new TestActivityManagerService(mContext); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 73 | service.mWindowManager = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 74 | return service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 77 | protected static ActivityStack createActivityStack(ActivityManagerService service, |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 78 | int stackId, int displayId, boolean onTop) { |
| 79 | if (service.mStackSupervisor instanceof TestActivityStackSupervisor) { |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 80 | return ((TestActivityStackSupervisor) service.mStackSupervisor) |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 81 | .createTestStack(service, stackId, onTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | protected static ActivityRecord createActivity(ActivityManagerService service, |
| 88 | ComponentName component, TaskRecord task) { |
| 89 | Intent intent = new Intent(); |
| 90 | intent.setComponent(component); |
| 91 | final ActivityInfo aInfo = new ActivityInfo(); |
| 92 | aInfo.applicationInfo = new ApplicationInfo(); |
| 93 | aInfo.applicationInfo.packageName = component.getPackageName(); |
| 94 | AttributeCache.init(service.mContext); |
| 95 | final ActivityRecord activity = new ActivityRecord(service, null /* caller */, |
| 96 | 0 /* launchedFromPid */, 0, null, intent, null, |
| 97 | aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */, |
| 98 | 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */, |
| 99 | service.mStackSupervisor, null /* container */, null /* options */, |
| 100 | null /* sourceRecord */); |
| 101 | activity.mWindowContainerController = mock(AppWindowContainerController.class); |
| 102 | |
| 103 | if (task != null) { |
| 104 | task.addActivityToTop(activity); |
| 105 | } |
| 106 | |
| 107 | return activity; |
| 108 | } |
| 109 | |
| 110 | protected static TaskRecord createTask(ActivityManagerService service, |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 111 | ComponentName component, int stackId) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 112 | final ActivityInfo aInfo = new ActivityInfo(); |
| 113 | aInfo.applicationInfo = new ApplicationInfo(); |
| 114 | aInfo.applicationInfo.packageName = component.getPackageName(); |
| 115 | |
| 116 | Intent intent = new Intent(); |
| 117 | intent.setComponent(component); |
| 118 | |
| 119 | final TaskRecord task = new TaskRecord(service, 0, aInfo, intent /*intent*/, |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 120 | null /*_taskDescription*/, new ActivityManager.TaskThumbnailInfo()); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 121 | final ActivityStack stack = service.mStackSupervisor.getStack(stackId, |
| 122 | true /*createStaticStackIfNeeded*/, true /*onTop*/); |
Andrii Kulian | 3a1619d | 2017-07-07 14:38:09 -0700 | [diff] [blame] | 123 | service.mStackSupervisor.setFocusStackUnchecked("test", stack); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 124 | stack.addTask(task, true, "creating test task"); |
| 125 | task.setStack(stack); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 126 | task.setWindowContainerController(mock(TaskWindowContainerController.class)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 127 | |
| 128 | return task; |
| 129 | } |
| 130 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 131 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 132 | /** |
| 133 | * An {@link ActivityManagerService} subclass which provides a test |
| 134 | * {@link ActivityStackSupervisor}. |
| 135 | */ |
| 136 | protected static class TestActivityManagerService extends ActivityManagerService { |
| 137 | public TestActivityManagerService(Context context) { |
| 138 | super(context); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 139 | mSupportsMultiWindow = true; |
| 140 | mSupportsMultiDisplay = true; |
| 141 | mWindowManager = WindowTestUtils.getWindowManagerService(context); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected ActivityStackSupervisor createStackSupervisor() { |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 146 | return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 152 | * setup not available in the test environment. Also specifies an injector for |
| 153 | */ |
| 154 | protected static class TestActivityStackSupervisor extends ActivityStackSupervisor { |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 155 | private final ActivityDisplay mDisplay; |
| 156 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 157 | public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) { |
| 158 | super(service, looper); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 159 | mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 160 | mDisplay = new ActivityDisplay(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // No home stack is set. |
| 164 | @Override |
| 165 | void moveHomeStackToFront(String reason) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Bryce Lee | 3345c4e | 2017-04-25 07:40:41 -0700 | [diff] [blame] | 168 | @Override |
| 169 | boolean moveHomeStackTaskToTop(String reason) { |
| 170 | return true; |
| 171 | } |
| 172 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 173 | // Invoked during {@link ActivityStack} creation. |
| 174 | @Override |
| 175 | void updateUIDsPresentOnDisplay() { |
| 176 | } |
| 177 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 178 | // Just return the current front task. |
| 179 | @Override |
| 180 | ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) { |
| 181 | return mFocusedStack; |
| 182 | } |
| 183 | |
| 184 | // Called when moving activity to pinned stack. |
| 185 | @Override |
| 186 | void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges, |
| 187 | boolean preserveWindows) { |
| 188 | } |
| 189 | |
| 190 | public <T extends ActivityStack> T createTestStack(ActivityManagerService service, |
| 191 | int stackId, boolean onTop) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 192 | final TestActivityContainer container = |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 193 | new TestActivityContainer(service, stackId, mDisplay, onTop); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 194 | mActivityContainers.put(stackId, container); |
| 195 | return (T) container.getStack(); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected <T extends ActivityStack> T getStack(int stackId, |
| 200 | boolean createStaticStackIfNeeded, boolean createOnTop) { |
| 201 | final T stack = super.getStack(stackId, createStaticStackIfNeeded, createOnTop); |
| 202 | |
| 203 | if (stack != null || !createStaticStackIfNeeded) { |
| 204 | return stack; |
| 205 | } |
| 206 | |
| 207 | return createTestStack(mService, stackId, createOnTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private class TestActivityContainer extends ActivityContainer { |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 211 | private final ActivityManagerService mService; |
| 212 | |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 213 | private boolean mOnTop; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 214 | private int mStackId; |
| 215 | private ActivityStack mStack; |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 216 | |
| 217 | TestActivityContainer(ActivityManagerService service, int stackId, |
| 218 | ActivityDisplay activityDisplay, boolean onTop) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 219 | super(stackId, activityDisplay, onTop); |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 220 | mService = service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | protected void createStack(int stackId, boolean onTop) { |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 225 | // normally stack creation is done here. However we need to do it on demand since |
| 226 | // we cannot set {@link mService} by the time the super constructor calling this |
| 227 | // method is invoked. |
| 228 | mOnTop = onTop; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 229 | mStackId = stackId; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 232 | public ActivityStack getStack() { |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 233 | if (mStack == null) { |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 234 | final RecentTasks recents = |
| 235 | new RecentTasks(mService, mService.mStackSupervisor); |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 236 | if (mStackId == ActivityManager.StackId.PINNED_STACK_ID) { |
| 237 | mStack = new PinnedActivityStack(this, recents, mOnTop) { |
| 238 | @Override |
Winson Chung | a71febe | 2017-05-22 11:14:22 -0700 | [diff] [blame] | 239 | Rect getDefaultPictureInPictureBounds(float aspectRatio) { |
Winson Chung | 9c84441 | 2017-04-27 17:36:41 -0700 | [diff] [blame] | 240 | return new Rect(50, 50, 100, 100); |
| 241 | } |
| 242 | }; |
| 243 | } else { |
| 244 | mStack = new TestActivityStack(this, recents, mOnTop); |
| 245 | } |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 248 | return mStack; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 253 | private static WindowManagerService prepareMockWindowManager() { |
| 254 | final WindowManagerService service = mock(WindowManagerService.class); |
| 255 | |
| 256 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 257 | final Runnable runnable = invocationOnMock.<Runnable>getArgument(0); |
| 258 | if (runnable != null) { |
| 259 | runnable.run(); |
| 260 | } |
| 261 | return null; |
| 262 | }).when(service).inSurfaceTransaction(any()); |
| 263 | |
| 264 | return service; |
| 265 | } |
| 266 | |
| 267 | protected interface ActivityStackReporter { |
| 268 | int onActivityRemovedFromStackInvocationCount(); |
| 269 | } |
| 270 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 271 | /** |
| 272 | * Override of {@link ActivityStack} that tracks test metrics, such as the number of times a |
| 273 | * method is called. Note that its functionality depends on the implementations of the |
| 274 | * construction arguments. |
| 275 | */ |
| 276 | protected static class TestActivityStack<T extends StackWindowController> |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 277 | extends ActivityStack<T> implements ActivityStackReporter { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 278 | private int mOnActivityRemovedFromStackCount = 0; |
| 279 | private T mContainerController; |
| 280 | TestActivityStack(ActivityStackSupervisor.ActivityContainer activityContainer, |
| 281 | RecentTasks recentTasks, boolean onTop) { |
| 282 | super(activityContainer, recentTasks, onTop); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | void onActivityRemovedFromStack(ActivityRecord r) { |
| 287 | mOnActivityRemovedFromStackCount++; |
| 288 | super.onActivityRemovedFromStack(r); |
| 289 | } |
| 290 | |
| 291 | // Returns the number of times {@link #onActivityRemovedFromStack} has been called |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 292 | @Override |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 293 | public int onActivityRemovedFromStackInvocationCount() { |
| 294 | return mOnActivityRemovedFromStackCount; |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | protected T createStackWindowController(int displayId, boolean onTop, |
| 299 | Rect outBounds) { |
| 300 | mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController(); |
| 301 | return mContainerController; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | T getWindowContainerController() { |
| 306 | return mContainerController; |
| 307 | } |
| 308 | } |
| 309 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 310 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 311 | protected static class ActivityStackBuilder { |
| 312 | private boolean mOnTop = true; |
| 313 | private int mStackId = 0; |
| 314 | private int mDisplayId = 1; |
| 315 | |
| 316 | private final ActivityManagerService mService; |
| 317 | |
| 318 | public ActivityStackBuilder(ActivityManagerService ams) { |
| 319 | mService = ams; |
| 320 | } |
| 321 | |
| 322 | public ActivityStackBuilder setOnTop(boolean onTop) { |
| 323 | mOnTop = onTop; |
| 324 | return this; |
| 325 | } |
| 326 | |
| 327 | public ActivityStackBuilder setStackId(int id) { |
| 328 | mStackId = id; |
| 329 | return this; |
| 330 | } |
| 331 | |
| 332 | public ActivityStackBuilder setDisplayId(int id) { |
| 333 | mDisplayId = id; |
| 334 | return this; |
| 335 | } |
| 336 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 337 | public ActivityStack build() { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 338 | return createActivityStack(mService, mStackId, mDisplayId, mOnTop); |
| 339 | } |
| 340 | } |
| 341 | } |