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 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
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 | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 79 | protected static ActivityRecord createActivity(ActivityManagerService service, |
| 80 | ComponentName component, TaskRecord task) { |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 81 | return createActivity(service, component, task, 0 /* userId */); |
| 82 | } |
| 83 | |
| 84 | protected static ActivityRecord createActivity(ActivityManagerService service, |
| 85 | ComponentName component, TaskRecord task, int uid) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 86 | Intent intent = new Intent(); |
| 87 | intent.setComponent(component); |
| 88 | final ActivityInfo aInfo = new ActivityInfo(); |
| 89 | aInfo.applicationInfo = new ApplicationInfo(); |
| 90 | aInfo.applicationInfo.packageName = component.getPackageName(); |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 91 | aInfo.applicationInfo.uid = uid; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 92 | AttributeCache.init(service.mContext); |
| 93 | final ActivityRecord activity = new ActivityRecord(service, null /* caller */, |
| 94 | 0 /* launchedFromPid */, 0, null, intent, null, |
| 95 | aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */, |
| 96 | 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */, |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 97 | service.mStackSupervisor, null /* options */, null /* sourceRecord */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 98 | activity.mWindowContainerController = mock(AppWindowContainerController.class); |
| 99 | |
| 100 | if (task != null) { |
| 101 | task.addActivityToTop(activity); |
| 102 | } |
| 103 | |
| 104 | return activity; |
| 105 | } |
| 106 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 107 | protected static TaskRecord createTask(ActivityStackSupervisor supervisor, |
| 108 | ComponentName component, ActivityStack stack) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 109 | final ActivityInfo aInfo = new ActivityInfo(); |
| 110 | aInfo.applicationInfo = new ApplicationInfo(); |
| 111 | aInfo.applicationInfo.packageName = component.getPackageName(); |
| 112 | |
| 113 | Intent intent = new Intent(); |
| 114 | intent.setComponent(component); |
| 115 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 116 | final TaskRecord task = new TaskRecord(supervisor.mService, 0, aInfo, intent /*intent*/, |
Jorim Jaggi | e7d2b85 | 2017-08-28 17:55:15 +0200 | [diff] [blame] | 117 | null /*_taskDescription*/); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 118 | supervisor.setFocusStackUnchecked("test", stack); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 119 | stack.addTask(task, true, "creating test task"); |
| 120 | task.setStack(stack); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 121 | task.setWindowContainerController(mock(TaskWindowContainerController.class)); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 122 | |
| 123 | return task; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * An {@link ActivityManagerService} subclass which provides a test |
| 128 | * {@link ActivityStackSupervisor}. |
| 129 | */ |
| 130 | protected static class TestActivityManagerService extends ActivityManagerService { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 131 | TestActivityManagerService(Context context) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 132 | super(context); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 133 | mSupportsMultiWindow = true; |
| 134 | mSupportsMultiDisplay = true; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 135 | mSupportsSplitScreenMultiWindow = true; |
| 136 | mSupportsFreeformWindowManagement = true; |
| 137 | mSupportsPictureInPicture = true; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 138 | mWindowManager = WindowTestUtils.getWindowManagerService(context); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected ActivityStackSupervisor createStackSupervisor() { |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 143 | return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 144 | } |
Bryce Lee | 29a649d | 2017-08-18 13:52:31 -0700 | [diff] [blame] | 145 | |
| 146 | @Override |
| 147 | void updateUsageStats(ActivityRecord component, boolean resumed) { |
| 148 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 153 | * setup not available in the test environment. Also specifies an injector for |
| 154 | */ |
| 155 | protected static class TestActivityStackSupervisor extends ActivityStackSupervisor { |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 156 | private final ActivityDisplay mDisplay; |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 157 | private boolean mLastResizeable; |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 158 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 159 | public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) { |
| 160 | super(service, looper); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 161 | mDisplayManager = |
| 162 | (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 163 | mWindowManager = prepareMockWindowManager(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 164 | mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 165 | attachDisplay(mDisplay); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 168 | @Override |
| 169 | ActivityDisplay getDefaultDisplay() { |
| 170 | return mDisplay; |
| 171 | } |
| 172 | |
Bryce Lee | 1533b2b | 2017-09-14 17:06:41 -0700 | [diff] [blame] | 173 | // TODO: Use Mockito spy instead. Currently not possible due to TestActivityStackSupervisor |
| 174 | // access to ActivityDisplay |
| 175 | @Override |
| 176 | boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid, |
| 177 | int callingUid, ActivityInfo activityInfo) { |
| 178 | mLastResizeable = resizeable; |
| 179 | return super.canPlaceEntityOnDisplay(displayId, resizeable, callingPid, callingUid, |
| 180 | activityInfo); |
| 181 | } |
| 182 | |
| 183 | // TODO: remove and use Mockito verify once {@link #canPlaceEntityOnDisplay} override is |
| 184 | // removed. |
| 185 | public boolean getLastResizeableFromCanPlaceEntityOnDisplay() { |
| 186 | return mLastResizeable; |
| 187 | } |
| 188 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 189 | // No home stack is set. |
| 190 | @Override |
| 191 | void moveHomeStackToFront(String reason) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Bryce Lee | 3345c4e | 2017-04-25 07:40:41 -0700 | [diff] [blame] | 194 | @Override |
| 195 | boolean moveHomeStackTaskToTop(String reason) { |
| 196 | return true; |
| 197 | } |
| 198 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 199 | // Invoked during {@link ActivityStack} creation. |
| 200 | @Override |
| 201 | void updateUIDsPresentOnDisplay() { |
| 202 | } |
| 203 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 204 | // Just return the current front task. |
| 205 | @Override |
| 206 | ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) { |
| 207 | return mFocusedStack; |
| 208 | } |
| 209 | |
| 210 | // Called when moving activity to pinned stack. |
| 211 | @Override |
| 212 | void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges, |
| 213 | boolean preserveWindows) { |
| 214 | } |
| 215 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 216 | // Always keep things awake |
| 217 | @Override |
| 218 | boolean hasAwakeDisplay() { |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private static class TestActivityDisplay extends ActivityDisplay { |
| 224 | |
| 225 | private final ActivityStackSupervisor mSupervisor; |
| 226 | TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) { |
| 227 | super(supervisor, displayId); |
| 228 | mSupervisor = supervisor; |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 232 | <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType, |
| 233 | int stackId, boolean onTop) { |
| 234 | if (windowingMode == WINDOWING_MODE_PINNED) { |
| 235 | return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) { |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 236 | @Override |
| 237 | Rect getDefaultPictureInPictureBounds(float aspectRatio) { |
| 238 | return new Rect(50, 50, 100, 100); |
| 239 | } |
| 240 | }; |
| 241 | } else { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 242 | return (T) new TestActivityStack( |
| 243 | this, stackId, mSupervisor, windowingMode, activityType, onTop); |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 244 | } |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 245 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 248 | private static WindowManagerService prepareMockWindowManager() { |
| 249 | final WindowManagerService service = mock(WindowManagerService.class); |
| 250 | |
| 251 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 252 | final Runnable runnable = invocationOnMock.<Runnable>getArgument(0); |
| 253 | if (runnable != null) { |
| 254 | runnable.run(); |
| 255 | } |
| 256 | return null; |
| 257 | }).when(service).inSurfaceTransaction(any()); |
| 258 | |
| 259 | return service; |
| 260 | } |
| 261 | |
| 262 | protected interface ActivityStackReporter { |
| 263 | int onActivityRemovedFromStackInvocationCount(); |
| 264 | } |
| 265 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Override of {@link ActivityStack} that tracks test metrics, such as the number of times a |
| 268 | * method is called. Note that its functionality depends on the implementations of the |
| 269 | * construction arguments. |
| 270 | */ |
| 271 | protected static class TestActivityStack<T extends StackWindowController> |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 272 | extends ActivityStack<T> implements ActivityStackReporter { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 273 | private int mOnActivityRemovedFromStackCount = 0; |
| 274 | private T mContainerController; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 275 | |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 276 | TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor, |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame^] | 277 | int windowingMode, int activityType, boolean onTop) { |
| 278 | super(display, stackId, supervisor, windowingMode, activityType, onTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | void onActivityRemovedFromStack(ActivityRecord r) { |
| 283 | mOnActivityRemovedFromStackCount++; |
| 284 | super.onActivityRemovedFromStack(r); |
| 285 | } |
| 286 | |
| 287 | // Returns the number of times {@link #onActivityRemovedFromStack} has been called |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 288 | @Override |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 289 | public int onActivityRemovedFromStackInvocationCount() { |
| 290 | return mOnActivityRemovedFromStackCount; |
| 291 | } |
| 292 | |
| 293 | @Override |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 294 | protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 295 | mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController(); |
| 296 | return mContainerController; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | T getWindowContainerController() { |
| 301 | return mContainerController; |
| 302 | } |
| 303 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 304 | } |