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