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