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 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 23 | import static android.view.Display.DEFAULT_DISPLAY; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.doNothing; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.doReturn; |
| 27 | import static org.mockito.Mockito.any; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.anyBoolean; |
| 29 | import static org.mockito.Mockito.anyInt; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.doAnswer; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.spy; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 32 | |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 33 | import com.android.server.wm.DisplayWindowController; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 34 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 35 | |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 36 | import android.app.IApplicationThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 37 | import android.content.ComponentName; |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
| 40 | import android.content.pm.ActivityInfo; |
| 41 | import android.content.pm.ApplicationInfo; |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 42 | import android.content.pm.IPackageManager; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 43 | import android.content.res.Configuration; |
| 44 | import android.graphics.Rect; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 45 | import android.hardware.display.DisplayManager; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 46 | import android.os.HandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 47 | import android.os.Looper; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 48 | import android.service.voice.IVoiceInteractionSession; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 49 | import android.support.test.InstrumentationRegistry; |
| 50 | import com.android.server.AttributeCache; |
| 51 | import com.android.server.wm.AppWindowContainerController; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 52 | import com.android.server.wm.PinnedStackWindowController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 53 | import com.android.server.wm.StackWindowController; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 54 | import com.android.server.wm.TaskWindowContainerController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 55 | import com.android.server.wm.WindowManagerService; |
| 56 | import com.android.server.wm.WindowTestUtils; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 57 | import org.junit.After; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 58 | import org.junit.Before; |
| 59 | import org.mockito.MockitoAnnotations; |
| 60 | |
| 61 | /** |
| 62 | * A base class to handle common operations in activity related unit tests. |
| 63 | */ |
| 64 | public class ActivityTestsBase { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 65 | private static boolean sOneTimeSetupDone = false; |
| 66 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 67 | private final Context mContext = InstrumentationRegistry.getContext(); |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 68 | private HandlerThread mHandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 69 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 70 | @Before |
| 71 | public void setUp() throws Exception { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 72 | if (!sOneTimeSetupDone) { |
| 73 | sOneTimeSetupDone = true; |
| 74 | |
| 75 | // Allows to mock package local classes and methods |
| 76 | System.setProperty("dexmaker.share_classloader", "true"); |
| 77 | MockitoAnnotations.initMocks(this); |
| 78 | } |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 79 | mHandlerThread = new HandlerThread("ActivityTestsBaseThread"); |
| 80 | mHandlerThread.start(); |
| 81 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 82 | |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 83 | @After |
| 84 | public void tearDown() { |
| 85 | mHandlerThread.quitSafely(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | protected ActivityManagerService createActivityManagerService() { |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 89 | final ActivityManagerService service = |
| 90 | setupActivityManagerService(new TestActivityManagerService(mContext)); |
| 91 | AttributeCache.init(mContext); |
| 92 | return service; |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | protected ActivityManagerService setupActivityManagerService(ActivityManagerService service) { |
| 96 | service = spy(service); |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 97 | doReturn(mock(IPackageManager.class)).when(service).getPackageManager(); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 98 | service.mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 99 | return service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Builder for creating new activities. |
| 104 | */ |
| 105 | protected static class ActivityBuilder { |
| 106 | // An id appended to the end of the component name to make it unique |
| 107 | private static int sCurrentActivityId = 0; |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 108 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 109 | // Default package name |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 110 | static final String DEFAULT_PACKAGE = "com.foo"; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 111 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 112 | // Default base activity name |
| 113 | private static final String DEFAULT_BASE_ACTIVITY_NAME = ".BarActivity"; |
| 114 | |
| 115 | private final ActivityManagerService mService; |
| 116 | |
| 117 | private ComponentName mComponent; |
| 118 | private TaskRecord mTaskRecord; |
| 119 | private int mUid; |
| 120 | private boolean mCreateTask; |
| 121 | private ActivityStack mStack; |
| 122 | |
| 123 | ActivityBuilder(ActivityManagerService service) { |
| 124 | mService = service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 127 | ActivityBuilder setComponent(ComponentName component) { |
| 128 | mComponent = component; |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | ActivityBuilder setTask(TaskRecord task) { |
| 133 | mTaskRecord = task; |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | ActivityBuilder setStack(ActivityStack stack) { |
| 138 | mStack = stack; |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | ActivityBuilder setCreateTask(boolean createTask) { |
| 143 | mCreateTask = createTask; |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | ActivityBuilder setUid(int uid) { |
| 148 | mUid = uid; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | ActivityRecord build() { |
| 153 | if (mComponent == null) { |
| 154 | final int id = sCurrentActivityId++; |
| 155 | mComponent = ComponentName.createRelative(DEFAULT_PACKAGE, |
| 156 | DEFAULT_BASE_ACTIVITY_NAME + id); |
| 157 | } |
| 158 | |
| 159 | if (mCreateTask) { |
| 160 | mTaskRecord = new TaskBuilder(mService.mStackSupervisor) |
| 161 | .setComponent(mComponent) |
| 162 | .setStack(mStack).build(); |
| 163 | } |
| 164 | |
| 165 | Intent intent = new Intent(); |
| 166 | intent.setComponent(mComponent); |
| 167 | final ActivityInfo aInfo = new ActivityInfo(); |
| 168 | aInfo.applicationInfo = new ApplicationInfo(); |
| 169 | aInfo.applicationInfo.packageName = mComponent.getPackageName(); |
| 170 | aInfo.applicationInfo.uid = mUid; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 171 | final ActivityRecord activity = new ActivityRecord(mService, null /* caller */, |
| 172 | 0 /* launchedFromPid */, 0, null, intent, null, |
| 173 | aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */, |
| 174 | 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */, |
| 175 | mService.mStackSupervisor, null /* options */, null /* sourceRecord */); |
| 176 | activity.mWindowContainerController = mock(AppWindowContainerController.class); |
| 177 | |
| 178 | if (mTaskRecord != null) { |
| 179 | mTaskRecord.addActivityToTop(activity); |
| 180 | } |
| 181 | |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 182 | activity.setProcess(new ProcessRecord(null, mService.mContext.getApplicationInfo(), |
| 183 | "name", 12345)); |
| 184 | activity.app.thread = mock(IApplicationThread.class); |
| 185 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 186 | return activity; |
| 187 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Builder for creating new tasks. |
| 192 | */ |
| 193 | protected static class TaskBuilder { |
| 194 | private final ActivityStackSupervisor mSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 195 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 196 | private ComponentName mComponent; |
| 197 | private String mPackage; |
| 198 | private int mFlags = 0; |
| 199 | private int mTaskId = 0; |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 200 | private int mUserId = 0; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 201 | private IVoiceInteractionSession mVoiceSession; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 202 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 203 | private ActivityStack mStack; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 204 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 205 | TaskBuilder(ActivityStackSupervisor supervisor) { |
| 206 | mSupervisor = supervisor; |
| 207 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 208 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 209 | TaskBuilder setComponent(ComponentName component) { |
| 210 | mComponent = component; |
| 211 | return this; |
| 212 | } |
| 213 | |
| 214 | TaskBuilder setPackage(String packageName) { |
| 215 | mPackage = packageName; |
| 216 | return this; |
| 217 | } |
| 218 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 219 | TaskBuilder setVoiceSession(IVoiceInteractionSession session) { |
| 220 | mVoiceSession = session; |
| 221 | return this; |
| 222 | } |
| 223 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 224 | TaskBuilder setFlags(int flags) { |
| 225 | mFlags = flags; |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | TaskBuilder setTaskId(int taskId) { |
| 230 | mTaskId = taskId; |
| 231 | return this; |
| 232 | } |
| 233 | |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 234 | TaskBuilder setUserId(int userId) { |
| 235 | mUserId = userId; |
| 236 | return this; |
| 237 | } |
| 238 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 239 | TaskBuilder setStack(ActivityStack stack) { |
| 240 | mStack = stack; |
| 241 | return this; |
| 242 | } |
| 243 | |
| 244 | TaskRecord build() { |
| 245 | if (mStack == null) { |
| 246 | mStack = mSupervisor.getDefaultDisplay().createStack( |
| 247 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 248 | } |
| 249 | |
| 250 | final ActivityInfo aInfo = new ActivityInfo(); |
| 251 | aInfo.applicationInfo = new ApplicationInfo(); |
| 252 | aInfo.applicationInfo.packageName = mPackage; |
| 253 | |
| 254 | Intent intent = new Intent(); |
| 255 | intent.setComponent(mComponent); |
| 256 | intent.setFlags(mFlags); |
| 257 | |
| 258 | final TaskRecord task = new TaskRecord(mSupervisor.mService, mTaskId, aInfo, |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 259 | intent /*intent*/, mVoiceSession, null /*_voiceInteractor*/); |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 260 | task.userId = mUserId; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 261 | mSupervisor.setFocusStackUnchecked("test", mStack); |
| 262 | mStack.addTask(task, true, "creating test task"); |
| 263 | task.setStack(mStack); |
| 264 | task.setWindowContainerController(mock(TaskWindowContainerController.class)); |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 265 | task.touchActiveTime(); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 266 | |
| 267 | return task; |
| 268 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * An {@link ActivityManagerService} subclass which provides a test |
| 273 | * {@link ActivityStackSupervisor}. |
| 274 | */ |
| 275 | protected static class TestActivityManagerService extends ActivityManagerService { |
Bryce Lee | b0f993f | 2018-03-02 15:38:01 -0800 | [diff] [blame^] | 276 | private ClientLifecycleManager mLifecycleManager; |
| 277 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 278 | TestActivityManagerService(Context context) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 279 | super(context); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 280 | mSupportsMultiWindow = true; |
| 281 | mSupportsMultiDisplay = true; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 282 | mSupportsSplitScreenMultiWindow = true; |
| 283 | mSupportsFreeformWindowManagement = true; |
| 284 | mSupportsPictureInPicture = true; |
Bryce Lee | 6a0754a | 2017-10-10 17:53:50 -0700 | [diff] [blame] | 285 | mWindowManager = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Override |
Bryce Lee | b0f993f | 2018-03-02 15:38:01 -0800 | [diff] [blame^] | 289 | public ClientLifecycleManager getLifecycleManager() { |
| 290 | if (mLifecycleManager == null) { |
| 291 | return super.getLifecycleManager(); |
| 292 | } |
| 293 | return mLifecycleManager; |
| 294 | } |
| 295 | |
| 296 | void setLifecycleManager(ClientLifecycleManager manager) { |
| 297 | mLifecycleManager = manager; |
| 298 | } |
| 299 | |
| 300 | @Override |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 301 | final protected ActivityStackSupervisor createStackSupervisor() { |
| 302 | final ActivityStackSupervisor supervisor = spy(createTestSupervisor()); |
| 303 | |
| 304 | // No home stack is set. |
| 305 | doNothing().when(supervisor).moveHomeStackToFront(any()); |
| 306 | doReturn(true).when(supervisor).moveHomeStackTaskToTop(any()); |
| 307 | // Invoked during {@link ActivityStack} creation. |
| 308 | doNothing().when(supervisor).updateUIDsPresentOnDisplay(); |
| 309 | // Always keep things awake. |
| 310 | doReturn(true).when(supervisor).hasAwakeDisplay(); |
| 311 | // Called when moving activity to pinned stack. |
| 312 | doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(), anyBoolean()); |
| 313 | // Do not schedule idle timeouts |
| 314 | doNothing().when(supervisor).scheduleIdleTimeoutLocked(any()); |
| 315 | |
| 316 | supervisor.initialize(); |
| 317 | |
| 318 | return supervisor; |
| 319 | } |
| 320 | |
| 321 | protected ActivityStackSupervisor createTestSupervisor() { |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 322 | return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 323 | } |
Bryce Lee | 29a649d | 2017-08-18 13:52:31 -0700 | [diff] [blame] | 324 | |
| 325 | @Override |
| 326 | void updateUsageStats(ActivityRecord component, boolean resumed) { |
| 327 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 332 | * setup not available in the test environment. Also specifies an injector for |
| 333 | */ |
| 334 | protected static class TestActivityStackSupervisor extends ActivityStackSupervisor { |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 335 | private ActivityDisplay mDisplay; |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 336 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 337 | public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) { |
| 338 | super(service, looper); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 339 | mDisplayManager = |
| 340 | (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 341 | mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void initialize() { |
| 346 | super.initialize(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 347 | mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 348 | attachDisplay(mDisplay); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 351 | @Override |
| 352 | ActivityDisplay getDefaultDisplay() { |
| 353 | return mDisplay; |
| 354 | } |
| 355 | |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 356 | // Just return the current front task. This is called internally so we cannot use spy to mock this out. |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 357 | @Override |
| 358 | ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) { |
| 359 | return mFocusedStack; |
| 360 | } |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 363 | protected static class TestActivityDisplay extends ActivityDisplay { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 364 | |
| 365 | private final ActivityStackSupervisor mSupervisor; |
| 366 | TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) { |
| 367 | super(supervisor, displayId); |
| 368 | mSupervisor = supervisor; |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @Override |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 372 | <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType, |
| 373 | int stackId, boolean onTop) { |
| 374 | if (windowingMode == WINDOWING_MODE_PINNED) { |
| 375 | return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) { |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 376 | @Override |
| 377 | Rect getDefaultPictureInPictureBounds(float aspectRatio) { |
| 378 | return new Rect(50, 50, 100, 100); |
| 379 | } |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 380 | |
| 381 | @Override |
| 382 | PinnedStackWindowController createStackWindowController(int displayId, |
| 383 | boolean onTop, Rect outBounds) { |
| 384 | return mock(PinnedStackWindowController.class); |
| 385 | } |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 386 | }; |
| 387 | } else { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 388 | return (T) new TestActivityStack( |
| 389 | this, stackId, mSupervisor, windowingMode, activityType, onTop); |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 390 | } |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 391 | } |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 392 | |
| 393 | @Override |
| 394 | protected DisplayWindowController createWindowContainerController() { |
| 395 | return mock(DisplayWindowController.class); |
| 396 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 399 | private static WindowManagerService prepareMockWindowManager() { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 400 | final WindowManagerService service = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 401 | |
| 402 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 403 | final Runnable runnable = invocationOnMock.<Runnable>getArgument(0); |
| 404 | if (runnable != null) { |
| 405 | runnable.run(); |
| 406 | } |
| 407 | return null; |
| 408 | }).when(service).inSurfaceTransaction(any()); |
| 409 | |
| 410 | return service; |
| 411 | } |
| 412 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 413 | /** |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 414 | * 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] | 415 | * method is called. Note that its functionality depends on the implementations of the |
| 416 | * construction arguments. |
| 417 | */ |
| 418 | protected static class TestActivityStack<T extends StackWindowController> |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 419 | extends ActivityStack<T> { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 420 | private int mOnActivityRemovedFromStackCount = 0; |
| 421 | private T mContainerController; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 422 | |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 423 | static final int IS_TRANSLUCENT_UNSET = 0; |
| 424 | static final int IS_TRANSLUCENT_FALSE = 1; |
| 425 | static final int IS_TRANSLUCENT_TRUE = 2; |
| 426 | private int mIsTranslucent = IS_TRANSLUCENT_UNSET; |
| 427 | |
Wale Ogunwale | 30e441d | 2017-11-09 08:28:45 -0800 | [diff] [blame] | 428 | static final int SUPPORTS_SPLIT_SCREEN_UNSET = 0; |
| 429 | static final int SUPPORTS_SPLIT_SCREEN_FALSE = 1; |
| 430 | static final int SUPPORTS_SPLIT_SCREEN_TRUE = 2; |
| 431 | private int mSupportsSplitScreen = SUPPORTS_SPLIT_SCREEN_UNSET; |
| 432 | |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 433 | TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor, |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 434 | int windowingMode, int activityType, boolean onTop) { |
| 435 | super(display, stackId, supervisor, windowingMode, activityType, onTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | @Override |
| 439 | void onActivityRemovedFromStack(ActivityRecord r) { |
| 440 | mOnActivityRemovedFromStackCount++; |
| 441 | super.onActivityRemovedFromStack(r); |
| 442 | } |
| 443 | |
| 444 | // Returns the number of times {@link #onActivityRemovedFromStack} has been called |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 445 | int onActivityRemovedFromStackInvocationCount() { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 446 | return mOnActivityRemovedFromStackCount; |
| 447 | } |
| 448 | |
| 449 | @Override |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 450 | protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 451 | mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController(); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 452 | |
| 453 | // Primary pinned stacks require a non-empty out bounds to be set or else all tasks |
| 454 | // will be moved to the full screen stack. |
| 455 | if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) { |
| 456 | outBounds.set(0, 0, 100, 100); |
| 457 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 458 | return mContainerController; |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | T getWindowContainerController() { |
| 463 | return mContainerController; |
| 464 | } |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 465 | |
| 466 | void setIsTranslucent(boolean isTranslucent) { |
| 467 | mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE; |
| 468 | } |
| 469 | |
| 470 | @Override |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 471 | boolean isStackTranslucent(ActivityRecord starting) { |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 472 | switch (mIsTranslucent) { |
| 473 | case IS_TRANSLUCENT_TRUE: |
| 474 | return true; |
| 475 | case IS_TRANSLUCENT_FALSE: |
| 476 | return false; |
| 477 | case IS_TRANSLUCENT_UNSET: |
| 478 | default: |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 479 | return super.isStackTranslucent(starting); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
Wale Ogunwale | 30e441d | 2017-11-09 08:28:45 -0800 | [diff] [blame] | 482 | |
| 483 | void setSupportsSplitScreen(boolean supportsSplitScreen) { |
| 484 | mSupportsSplitScreen = supportsSplitScreen |
| 485 | ? SUPPORTS_SPLIT_SCREEN_TRUE : SUPPORTS_SPLIT_SCREEN_FALSE; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public boolean supportsSplitScreenWindowingMode() { |
| 490 | switch (mSupportsSplitScreen) { |
| 491 | case SUPPORTS_SPLIT_SCREEN_TRUE: |
| 492 | return true; |
| 493 | case SUPPORTS_SPLIT_SCREEN_FALSE: |
| 494 | return false; |
| 495 | case SUPPORTS_SPLIT_SCREEN_UNSET: |
| 496 | default: |
| 497 | return super.supportsSplitScreenWindowingMode(); |
| 498 | } |
| 499 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 500 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 501 | } |