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