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