Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 1 | /* |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 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 |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 14 | * limitations under the License |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 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; |
Evan Rosky | 72f084d | 2019-09-11 17:05:16 -0700 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 23 | import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE; |
| 24 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 25 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 26 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 27 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 28 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
| 29 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyBoolean; |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 30 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 31 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 32 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
Wale Ogunwale | b73f396 | 2018-11-20 07:58:22 -0800 | [diff] [blame] | 33 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 34 | |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 35 | import android.app.ActivityOptions; |
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; |
| 42 | import android.content.res.Configuration; |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 43 | import android.os.Build; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 44 | import android.os.UserHandle; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 45 | import android.service.voice.IVoiceInteractionSession; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 46 | import android.view.DisplayInfo; |
Adrian Roos | 3150dbf | 2018-03-28 18:06:52 +0200 | [diff] [blame] | 47 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 48 | import com.android.server.AttributeCache; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 49 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 50 | import org.junit.Before; |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 51 | import org.junit.BeforeClass; |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 52 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 53 | /** |
| 54 | * A base class to handle common operations in activity related unit tests. |
| 55 | */ |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 56 | class ActivityTestsBase extends SystemServiceTestsBase { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 57 | final Context mContext = getInstrumentation().getTargetContext(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 58 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 59 | ActivityTaskManagerService mService; |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 60 | RootActivityContainer mRootActivityContainer; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 61 | ActivityStackSupervisor mSupervisor; |
| 62 | |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 63 | // Default package name |
| 64 | static final String DEFAULT_COMPONENT_PACKAGE_NAME = "com.foo"; |
| 65 | |
| 66 | // Default base activity name |
| 67 | private static final String DEFAULT_COMPONENT_CLASS_NAME = ".BarActivity"; |
| 68 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 69 | @BeforeClass |
| 70 | public static void setUpOnceBase() { |
| 71 | AttributeCache.init(getInstrumentation().getTargetContext()); |
| 72 | } |
| 73 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 74 | @Before |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 75 | public void setUpBase() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 76 | mService = mSystemServicesTestRule.getActivityTaskManagerService(); |
Wale Ogunwale | 976ca47 | 2019-01-17 09:55:38 -0800 | [diff] [blame] | 77 | mSupervisor = mService.mStackSupervisor; |
| 78 | mRootActivityContainer = mService.mRootActivityContainer; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 79 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 80 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 81 | /** Creates a {@link TestActivityDisplay}. */ |
| 82 | TestActivityDisplay createNewActivityDisplay() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 83 | return TestActivityDisplay.create(mSupervisor); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Garfield Tan | 891146c | 2018-10-09 12:14:00 -0700 | [diff] [blame] | 86 | TestActivityDisplay createNewActivityDisplay(DisplayInfo info) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 87 | return TestActivityDisplay.create(mSupervisor, info); |
Garfield Tan | 891146c | 2018-10-09 12:14:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 90 | /** Creates and adds a {@link TestActivityDisplay} to supervisor at the given position. */ |
| 91 | TestActivityDisplay addNewActivityDisplayAt(int position) { |
| 92 | final TestActivityDisplay display = createNewActivityDisplay(); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 93 | mRootActivityContainer.addChild(display, position); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 94 | return display; |
| 95 | } |
| 96 | |
Evan Rosky | 730f6e8 | 2018-12-03 17:40:11 -0800 | [diff] [blame] | 97 | /** Creates and adds a {@link TestActivityDisplay} to supervisor at the given position. */ |
| 98 | TestActivityDisplay addNewActivityDisplayAt(DisplayInfo info, int position) { |
| 99 | final TestActivityDisplay display = createNewActivityDisplay(info); |
| 100 | mRootActivityContainer.addChild(display, position); |
| 101 | return display; |
| 102 | } |
| 103 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 104 | /** Sets the default minimum task size to 1 so that tests can use small task sizes */ |
| 105 | public void removeGlobalMinSizeRestriction() { |
| 106 | mService.mRootActivityContainer.mDefaultMinSizeOfResizeableTaskDp = 1; |
Riddle Hsu | 609a8e2 | 2019-06-27 16:46:29 -0600 | [diff] [blame] | 107 | } |
| 108 | |
Riddle Hsu | ff9e828 | 2019-04-24 23:55:11 +0800 | [diff] [blame] | 109 | /** |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 110 | * Builder for creating new activities. |
| 111 | */ |
| 112 | protected static class ActivityBuilder { |
| 113 | // An id appended to the end of the component name to make it unique |
| 114 | private static int sCurrentActivityId = 0; |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 115 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 116 | private final ActivityTaskManagerService mService; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 117 | |
| 118 | private ComponentName mComponent; |
Louis Chang | 23df1a6 | 2019-01-09 15:10:49 +0800 | [diff] [blame] | 119 | private String mTargetActivity; |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 120 | private Task mTask; |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 121 | private String mProcessName = "name"; |
| 122 | private int mUid = 12345; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 123 | private boolean mCreateTask; |
| 124 | private ActivityStack mStack; |
Bryce Lee | c961e0a | 2018-04-13 17:58:02 -0700 | [diff] [blame] | 125 | private int mActivityFlags; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 126 | private int mLaunchMode; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 127 | private int mResizeMode = RESIZE_MODE_RESIZEABLE; |
| 128 | private float mMaxAspectRatio; |
| 129 | private int mScreenOrientation = SCREEN_ORIENTATION_UNSPECIFIED; |
| 130 | private boolean mLaunchTaskBehind; |
| 131 | private int mConfigChanges; |
lumark | f65e02d | 2019-09-14 19:25:21 +0800 | [diff] [blame] | 132 | private int mLaunchedFromPid; |
| 133 | private int mLaunchedFromUid; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 134 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 135 | ActivityBuilder(ActivityTaskManagerService service) { |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 136 | mService = service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 139 | ActivityBuilder setComponent(ComponentName component) { |
| 140 | mComponent = component; |
| 141 | return this; |
| 142 | } |
| 143 | |
Louis Chang | 23df1a6 | 2019-01-09 15:10:49 +0800 | [diff] [blame] | 144 | ActivityBuilder setTargetActivity(String targetActivity) { |
| 145 | mTargetActivity = targetActivity; |
| 146 | return this; |
| 147 | } |
| 148 | |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 149 | static ComponentName getDefaultComponent() { |
| 150 | return ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 151 | DEFAULT_COMPONENT_PACKAGE_NAME); |
| 152 | } |
| 153 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 154 | ActivityBuilder setTask(Task task) { |
| 155 | mTask = task; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 156 | return this; |
| 157 | } |
| 158 | |
Bryce Lee | c961e0a | 2018-04-13 17:58:02 -0700 | [diff] [blame] | 159 | ActivityBuilder setActivityFlags(int flags) { |
| 160 | mActivityFlags = flags; |
| 161 | return this; |
| 162 | } |
| 163 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 164 | ActivityBuilder setLaunchMode(int launchMode) { |
| 165 | mLaunchMode = launchMode; |
| 166 | return this; |
| 167 | } |
| 168 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 169 | ActivityBuilder setStack(ActivityStack stack) { |
| 170 | mStack = stack; |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | ActivityBuilder setCreateTask(boolean createTask) { |
| 175 | mCreateTask = createTask; |
| 176 | return this; |
| 177 | } |
| 178 | |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 179 | ActivityBuilder setProcessName(String name) { |
| 180 | mProcessName = name; |
| 181 | return this; |
| 182 | } |
| 183 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 184 | ActivityBuilder setUid(int uid) { |
| 185 | mUid = uid; |
| 186 | return this; |
| 187 | } |
| 188 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 189 | ActivityBuilder setResizeMode(int resizeMode) { |
| 190 | mResizeMode = resizeMode; |
| 191 | return this; |
| 192 | } |
| 193 | |
| 194 | ActivityBuilder setMaxAspectRatio(float maxAspectRatio) { |
| 195 | mMaxAspectRatio = maxAspectRatio; |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | ActivityBuilder setScreenOrientation(int screenOrientation) { |
| 200 | mScreenOrientation = screenOrientation; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | ActivityBuilder setLaunchTaskBehind(boolean launchTaskBehind) { |
| 205 | mLaunchTaskBehind = launchTaskBehind; |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | ActivityBuilder setConfigChanges(int configChanges) { |
| 210 | mConfigChanges = configChanges; |
| 211 | return this; |
| 212 | } |
| 213 | |
lumark | f65e02d | 2019-09-14 19:25:21 +0800 | [diff] [blame] | 214 | ActivityBuilder setLaunchedFromPid(int pid) { |
| 215 | mLaunchedFromPid = pid; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | ActivityBuilder setLaunchedFromUid(int uid) { |
| 220 | mLaunchedFromUid = uid; |
| 221 | return this; |
| 222 | } |
| 223 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 224 | ActivityRecord build() { |
| 225 | if (mComponent == null) { |
| 226 | final int id = sCurrentActivityId++; |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 227 | mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 228 | DEFAULT_COMPONENT_CLASS_NAME + id); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | if (mCreateTask) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 232 | mTask = new TaskBuilder(mService.mStackSupervisor) |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 233 | .setComponent(mComponent) |
| 234 | .setStack(mStack).build(); |
| 235 | } |
| 236 | |
| 237 | Intent intent = new Intent(); |
| 238 | intent.setComponent(mComponent); |
| 239 | final ActivityInfo aInfo = new ActivityInfo(); |
| 240 | aInfo.applicationInfo = new ApplicationInfo(); |
Wale Ogunwale | 3198da4 | 2019-10-10 14:45:03 +0200 | [diff] [blame] | 241 | aInfo.applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 242 | aInfo.applicationInfo.packageName = mComponent.getPackageName(); |
| 243 | aInfo.applicationInfo.uid = mUid; |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 244 | aInfo.processName = mProcessName; |
Louis Chang | 23df1a6 | 2019-01-09 15:10:49 +0800 | [diff] [blame] | 245 | aInfo.packageName = mComponent.getPackageName(); |
| 246 | if (mTargetActivity != null) { |
| 247 | aInfo.targetActivity = mTargetActivity; |
| 248 | } |
Bryce Lee | c961e0a | 2018-04-13 17:58:02 -0700 | [diff] [blame] | 249 | aInfo.flags |= mActivityFlags; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 250 | aInfo.launchMode = mLaunchMode; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 251 | aInfo.resizeMode = mResizeMode; |
| 252 | aInfo.maxAspectRatio = mMaxAspectRatio; |
| 253 | aInfo.screenOrientation = mScreenOrientation; |
| 254 | aInfo.configChanges |= mConfigChanges; |
| 255 | |
| 256 | ActivityOptions options = null; |
| 257 | if (mLaunchTaskBehind) { |
| 258 | options = ActivityOptions.makeTaskLaunchBehind(); |
| 259 | } |
Bryce Lee | c961e0a | 2018-04-13 17:58:02 -0700 | [diff] [blame] | 260 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 261 | final ActivityRecord activity = new ActivityRecord(mService, null /* caller */, |
lumark | f65e02d | 2019-09-14 19:25:21 +0800 | [diff] [blame] | 262 | mLaunchedFromPid /* launchedFromPid */, mLaunchedFromUid /* launchedFromUid */, |
| 263 | null, intent, null, aInfo /*aInfo*/, new Configuration(), null /* resultTo */, |
| 264 | null /* resultWho */, 0 /* reqCode */, false /*componentSpecified*/, |
| 265 | false /* rootVoiceInteraction */, mService.mStackSupervisor, options, |
| 266 | null /* sourceRecord */); |
Yunfan Chen | d4ef301 | 2018-11-28 21:14:32 -0800 | [diff] [blame] | 267 | spyOn(activity); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 268 | if (mTask != null) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 269 | // fullscreen value is normally read from resources in ctor, so for testing we need |
| 270 | // to set it somewhere else since we can't mock resources. |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 271 | doReturn(true).when(activity).occludesParent(); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 272 | mTask.addChild(activity); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 273 | // Make visible by default... |
Issei Suzuki | f2f6c91 | 2019-11-08 11:24:18 +0100 | [diff] [blame] | 274 | activity.setVisible(true); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Andrii Kulian | beadacc | 2019-05-20 12:18:01 +0000 | [diff] [blame] | 277 | final WindowProcessController wpc = new WindowProcessController(mService, |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 278 | mService.mContext.getApplicationInfo(), mProcessName, mUid, |
Andrii Kulian | beadacc | 2019-05-20 12:18:01 +0000 | [diff] [blame] | 279 | UserHandle.getUserId(12345), mock(Object.class), |
| 280 | mock(WindowProcessListener.class)); |
| 281 | wpc.setThread(mock(IApplicationThread.class)); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 282 | activity.setProcess(wpc); |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 283 | doReturn(wpc).when(mService).getProcessController( |
| 284 | activity.processName, activity.info.applicationInfo.uid); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 285 | |
| 286 | // Resume top activities to make sure all other signals in the system are connected. |
| 287 | mService.mRootActivityContainer.resumeFocusedStacksTopActivities(); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 288 | return activity; |
| 289 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Builder for creating new tasks. |
| 294 | */ |
| 295 | protected static class TaskBuilder { |
| 296 | private final ActivityStackSupervisor mSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 297 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 298 | private ComponentName mComponent; |
| 299 | private String mPackage; |
| 300 | private int mFlags = 0; |
Kazuki Takise | c2e17bf | 2018-07-17 17:46:38 +0900 | [diff] [blame] | 301 | // Task id 0 is reserved in ARC for the home app. |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 302 | private int mTaskId = SystemServicesTestRule.sNextTaskId++; |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 303 | private int mUserId = 0; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 304 | private IVoiceInteractionSession mVoiceSession; |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 305 | private boolean mCreateStack = true; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 306 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 307 | private ActivityStack mStack; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 308 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 309 | TaskBuilder(ActivityStackSupervisor supervisor) { |
| 310 | mSupervisor = supervisor; |
| 311 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 312 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 313 | TaskBuilder setComponent(ComponentName component) { |
| 314 | mComponent = component; |
| 315 | return this; |
| 316 | } |
| 317 | |
| 318 | TaskBuilder setPackage(String packageName) { |
| 319 | mPackage = packageName; |
| 320 | return this; |
| 321 | } |
| 322 | |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Set to {@code true} by default, set to {@code false} to prevent the task from |
| 325 | * automatically creating a parent stack. |
| 326 | */ |
| 327 | TaskBuilder setCreateStack(boolean createStack) { |
| 328 | mCreateStack = createStack; |
| 329 | return this; |
| 330 | } |
| 331 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 332 | TaskBuilder setVoiceSession(IVoiceInteractionSession session) { |
| 333 | mVoiceSession = session; |
| 334 | return this; |
| 335 | } |
| 336 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 337 | TaskBuilder setFlags(int flags) { |
| 338 | mFlags = flags; |
| 339 | return this; |
| 340 | } |
| 341 | |
| 342 | TaskBuilder setTaskId(int taskId) { |
| 343 | mTaskId = taskId; |
| 344 | return this; |
| 345 | } |
| 346 | |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 347 | TaskBuilder setUserId(int userId) { |
| 348 | mUserId = userId; |
| 349 | return this; |
| 350 | } |
| 351 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 352 | TaskBuilder setStack(ActivityStack stack) { |
| 353 | mStack = stack; |
| 354 | return this; |
| 355 | } |
| 356 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 357 | Task build() { |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 358 | if (mStack == null && mCreateStack) { |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 359 | mStack = mSupervisor.mRootActivityContainer.getDefaultDisplay().createStack( |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 360 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 361 | spyOn(mStack); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | final ActivityInfo aInfo = new ActivityInfo(); |
| 365 | aInfo.applicationInfo = new ApplicationInfo(); |
| 366 | aInfo.applicationInfo.packageName = mPackage; |
| 367 | |
| 368 | Intent intent = new Intent(); |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 369 | if (mComponent == null) { |
| 370 | mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 371 | DEFAULT_COMPONENT_CLASS_NAME); |
| 372 | } |
| 373 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 374 | intent.setComponent(mComponent); |
| 375 | intent.setFlags(mFlags); |
| 376 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 377 | final Task task = new Task(mSupervisor.mService, mTaskId, aInfo, |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 378 | intent /*intent*/, mVoiceSession, null /*_voiceInteractor*/, |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 379 | null /*taskDescription*/, mStack); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 380 | spyOn(task); |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 381 | task.mUserId = mUserId; |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 382 | |
| 383 | if (mStack != null) { |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 384 | mStack.moveToFront("test"); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 385 | mStack.addChild(task, true, true); |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 388 | return task; |
| 389 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 390 | } |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 391 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 392 | static class StackBuilder { |
| 393 | private final RootActivityContainer mRootActivityContainer; |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 394 | private ActivityDisplay mDisplay; |
| 395 | private int mStackId = -1; |
Evan Rosky | 72f084d | 2019-09-11 17:05:16 -0700 | [diff] [blame] | 396 | private int mWindowingMode = WINDOWING_MODE_UNDEFINED; |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 397 | private int mActivityType = ACTIVITY_TYPE_STANDARD; |
| 398 | private boolean mOnTop = true; |
| 399 | private boolean mCreateActivity = true; |
| 400 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 401 | StackBuilder(RootActivityContainer root) { |
| 402 | mRootActivityContainer = root; |
| 403 | mDisplay = mRootActivityContainer.getDefaultDisplay(); |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | StackBuilder setWindowingMode(int windowingMode) { |
| 407 | mWindowingMode = windowingMode; |
| 408 | return this; |
| 409 | } |
| 410 | |
| 411 | StackBuilder setActivityType(int activityType) { |
| 412 | mActivityType = activityType; |
| 413 | return this; |
| 414 | } |
| 415 | |
| 416 | StackBuilder setStackId(int stackId) { |
| 417 | mStackId = stackId; |
| 418 | return this; |
| 419 | } |
| 420 | |
| 421 | StackBuilder setDisplay(ActivityDisplay display) { |
| 422 | mDisplay = display; |
| 423 | return this; |
| 424 | } |
| 425 | |
| 426 | StackBuilder setOnTop(boolean onTop) { |
| 427 | mOnTop = onTop; |
| 428 | return this; |
| 429 | } |
| 430 | |
| 431 | StackBuilder setCreateActivity(boolean createActivity) { |
| 432 | mCreateActivity = createActivity; |
| 433 | return this; |
| 434 | } |
| 435 | |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 436 | ActivityStack build() { |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 437 | final int stackId = mStackId >= 0 ? mStackId : mDisplay.getNextStackId(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 438 | final ActivityStack stack; |
| 439 | final ActivityStackSupervisor supervisor = mRootActivityContainer.mStackSupervisor; |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 440 | if (mWindowingMode == WINDOWING_MODE_PINNED) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 441 | stack = new ActivityStack(mDisplay, stackId, supervisor, |
Hongwei Wang | 43a752b | 2019-09-17 20:20:30 +0000 | [diff] [blame] | 442 | mWindowingMode, ACTIVITY_TYPE_STANDARD, mOnTop); |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 443 | } else { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 444 | stack = new ActivityStack(mDisplay, stackId, supervisor, |
| 445 | mWindowingMode, mActivityType, mOnTop); |
| 446 | |
| 447 | if (mCreateActivity) { |
| 448 | new ActivityBuilder(supervisor.mService) |
| 449 | .setCreateTask(true) |
| 450 | .setStack(stack) |
| 451 | .build(); |
| 452 | if (mOnTop) { |
| 453 | // We move the task to front again in order to regain focus after activity |
| 454 | // added to the stack. |
| 455 | // Or {@link ActivityDisplay#mPreferredTopFocusableStack} could be other |
| 456 | // stacks (e.g. home stack). |
| 457 | stack.moveToFront("createActivityStack"); |
| 458 | } else { |
| 459 | stack.moveToBack("createActivityStack", null); |
| 460 | } |
| 461 | } |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 462 | } |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 463 | |
| 464 | spyOn(stack); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 465 | doNothing().when(stack).startActivityLocked( |
| 466 | any(), any(), anyBoolean(), anyBoolean(), any()); |
| 467 | |
| 468 | return stack; |
Louis Chang | f2835df | 2018-10-17 15:14:45 +0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 472 | } |