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