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