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