Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -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 | 4e4a3ec | 2017-09-27 08:25:03 -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 | 4e4a3ec | 2017-09-27 08:25:03 -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 | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 18 | |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 19 | import static android.app.ActivityManager.PROCESS_STATE_TOP; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 20 | import static android.app.ActivityManager.START_ABORTED; |
| 21 | import static android.app.ActivityManager.START_CLASS_NOT_FOUND; |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 22 | import static android.app.ActivityManager.START_DELIVERED_TO_TOP; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 23 | import static android.app.ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT; |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 24 | import static android.app.ActivityManager.START_INTENT_NOT_RESOLVED; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 25 | import static android.app.ActivityManager.START_NOT_VOICE_COMPATIBLE; |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 26 | import static android.app.ActivityManager.START_PERMISSION_DENIED; |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 27 | import static android.app.ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 28 | import static android.app.ActivityManager.START_SUCCESS; |
| 29 | import static android.app.ActivityManager.START_SWITCHES_CANCELED; |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 30 | import static android.app.ActivityManager.START_TASK_TO_FRONT; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 31 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 32 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 33 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 34 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
| 35 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 36 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 37 | import static android.content.Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 38 | import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 39 | |
Tadashi G. Takaoka | a7a6695 | 2018-11-16 15:04:21 +0900 | [diff] [blame] | 40 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.clearInvocations; |
| 41 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; |
| 42 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
| 43 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 44 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 45 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.never; |
| 46 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy; |
| 47 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.times; |
| 48 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 49 | import static com.android.server.wm.ActivityDisplay.POSITION_BOTTOM; |
| 50 | import static com.android.server.wm.ActivityDisplay.POSITION_TOP; |
| 51 | import static com.android.server.wm.ActivityTaskManagerService.ANIMATE; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 52 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 53 | import static com.google.common.truth.Truth.assertThat; |
| 54 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 55 | import static org.junit.Assert.assertEquals; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 56 | import static org.mockito.ArgumentMatchers.any; |
| 57 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 58 | import static org.mockito.ArgumentMatchers.anyInt; |
| 59 | import static org.mockito.ArgumentMatchers.anyObject; |
| 60 | import static org.mockito.ArgumentMatchers.eq; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 61 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 62 | import android.app.ActivityOptions; |
| 63 | import android.app.IApplicationThread; |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 64 | import android.content.ComponentName; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 65 | import android.content.Intent; |
| 66 | import android.content.pm.ActivityInfo; |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 67 | import android.content.pm.ActivityInfo.WindowLayout; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 68 | import android.content.pm.ApplicationInfo; |
| 69 | import android.content.pm.IPackageManager; |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 70 | import android.content.pm.PackageManagerInternal; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 71 | import android.graphics.Rect; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 72 | import android.os.IBinder; |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 73 | import android.os.Process; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 74 | import android.os.RemoteException; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 75 | import android.platform.test.annotations.Presubmit; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 76 | import android.service.voice.IVoiceInteractionSession; |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 77 | import android.view.Gravity; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 78 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 79 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 80 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 81 | import com.android.server.wm.LaunchParamsController.LaunchParamsModifier; |
| 82 | import com.android.server.wm.TaskRecord.TaskRecordFactory; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 83 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 84 | import org.junit.Before; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 85 | import org.junit.Test; |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 86 | |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 87 | /** |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 88 | * Tests for the {@link ActivityStarter} class. |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 89 | * |
| 90 | * Build/Install/Run: |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 91 | * atest WmTests:ActivityStarterTests |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 92 | */ |
| 93 | @SmallTest |
| 94 | @Presubmit |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 95 | public class ActivityStarterTests extends ActivityTestsBase { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 96 | private ActivityStarter mStarter; |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 97 | private ActivityStartController mController; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 98 | private ActivityMetricsLogger mActivityMetricsLogger; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 99 | |
| 100 | private static final int PRECONDITION_NO_CALLER_APP = 1; |
| 101 | private static final int PRECONDITION_NO_INTENT_COMPONENT = 1 << 1; |
| 102 | private static final int PRECONDITION_NO_ACTIVITY_INFO = 1 << 2; |
| 103 | private static final int PRECONDITION_SOURCE_PRESENT = 1 << 3; |
| 104 | private static final int PRECONDITION_REQUEST_CODE = 1 << 4; |
| 105 | private static final int PRECONDITION_SOURCE_VOICE_SESSION = 1 << 5; |
| 106 | private static final int PRECONDITION_NO_VOICE_SESSION_SUPPORT = 1 << 6; |
| 107 | private static final int PRECONDITION_DIFFERENT_UID = 1 << 7; |
| 108 | private static final int PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION = 1 << 8; |
| 109 | private static final int PRECONDITION_CANNOT_START_ANY_ACTIVITY = 1 << 9; |
| 110 | private static final int PRECONDITION_DISALLOW_APP_SWITCHING = 1 << 10; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 111 | |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 112 | private static final int FAKE_CALLING_UID = 666; |
| 113 | private static final int FAKE_REAL_CALLING_UID = 667; |
| 114 | private static final String FAKE_CALLING_PACKAGE = "com.whatever.dude"; |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 115 | private static final int UNIMPORTANT_UID = 12345; |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 116 | private static final int UNIMPORTANT_UID2 = 12346; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 117 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 118 | @Before |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 119 | public void setUp() throws Exception { |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 120 | setupActivityTaskManagerService(); |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 121 | mController = mock(ActivityStartController.class); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 122 | mActivityMetricsLogger = mock(ActivityMetricsLogger.class); |
| 123 | clearInvocations(mActivityMetricsLogger); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 124 | mStarter = new ActivityStarter(mController, mService, mService.mStackSupervisor, |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 125 | mock(ActivityStartInterceptor.class)); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 129 | public void testUpdateLaunchBounds() { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 130 | // When in a non-resizeable stack, the task bounds should be updated. |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 131 | final TaskRecord task = new TaskBuilder(mService.mStackSupervisor) |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 132 | .setStack(mService.mRootActivityContainer.getDefaultDisplay().createStack( |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 133 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */)) |
| 134 | .build(); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 135 | final Rect bounds = new Rect(10, 10, 100, 100); |
| 136 | |
| 137 | mStarter.updateBounds(task, bounds); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 138 | assertEquals(bounds, task.getOverrideBounds()); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 139 | assertEquals(new Rect(), task.getStack().getOverrideBounds()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 140 | |
| 141 | // When in a resizeable stack, the stack bounds should be updated as well. |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 142 | final TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor) |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 143 | .setStack(mService.mRootActivityContainer.getDefaultDisplay().createStack( |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 144 | WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */)) |
| 145 | .build(); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 146 | assertThat((Object) task2.getStack()).isInstanceOf(PinnedActivityStack.class); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 147 | mStarter.updateBounds(task2, bounds); |
| 148 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 149 | verify(mService, times(1)).resizeStack(eq(task2.getStack().mStackId), |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 150 | eq(bounds), anyBoolean(), anyBoolean(), anyBoolean(), anyInt()); |
| 151 | |
| 152 | // In the case of no animation, the stack and task bounds should be set immediately. |
| 153 | if (!ANIMATE) { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 154 | assertEquals(bounds, task2.getStack().getOverrideBounds()); |
| 155 | assertEquals(bounds, task2.getOverrideBounds()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 156 | } else { |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 157 | assertEquals(new Rect(), task2.getOverrideBounds()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 160 | |
| 161 | @Test |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 162 | public void testStartActivityPreconditions() { |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 163 | verifyStartActivityPreconditions(PRECONDITION_NO_CALLER_APP, START_PERMISSION_DENIED); |
| 164 | verifyStartActivityPreconditions(PRECONDITION_NO_INTENT_COMPONENT, |
| 165 | START_INTENT_NOT_RESOLVED); |
| 166 | verifyStartActivityPreconditions(PRECONDITION_NO_ACTIVITY_INFO, START_CLASS_NOT_FOUND); |
| 167 | verifyStartActivityPreconditions(PRECONDITION_SOURCE_PRESENT | PRECONDITION_REQUEST_CODE, |
| 168 | Intent.FLAG_ACTIVITY_FORWARD_RESULT, START_FORWARD_AND_REQUEST_CONFLICT); |
| 169 | verifyStartActivityPreconditions( |
| 170 | PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT |
| 171 | | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID, |
| 172 | START_NOT_VOICE_COMPATIBLE); |
| 173 | verifyStartActivityPreconditions( |
| 174 | PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT |
| 175 | | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID |
| 176 | | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION, |
| 177 | START_NOT_VOICE_COMPATIBLE); |
| 178 | verifyStartActivityPreconditions(PRECONDITION_CANNOT_START_ANY_ACTIVITY, START_ABORTED); |
| 179 | verifyStartActivityPreconditions(PRECONDITION_DISALLOW_APP_SWITCHING, |
| 180 | START_SWITCHES_CANCELED); |
| 181 | } |
| 182 | |
| 183 | private static boolean containsConditions(int preconditions, int mask) { |
| 184 | return (preconditions & mask) == mask; |
| 185 | } |
| 186 | |
| 187 | private void verifyStartActivityPreconditions(int preconditions, int expectedResult) { |
| 188 | verifyStartActivityPreconditions(preconditions, 0 /*launchFlags*/, expectedResult); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Excercises how the {@link ActivityStarter} reacts to various preconditions. The caller |
| 193 | * provides a bitmask of all the set conditions (such as {@link #PRECONDITION_NO_CALLER_APP}) |
| 194 | * and the launch flags specified in the intent. The method constructs a call to |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 195 | * {@link ActivityStarter#execute} based on these preconditions and ensures the result matches |
| 196 | * the expected. It is important to note that the method also checks side effects of the start, |
| 197 | * such as ensuring {@link ActivityOptions#abort()} is called in the relevant scenarios. |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 198 | * @param preconditions A bitmask representing the preconditions for the launch |
| 199 | * @param launchFlags The launch flags to be provided by the launch {@link Intent}. |
| 200 | * @param expectedResult The expected result from the launch. |
| 201 | */ |
| 202 | private void verifyStartActivityPreconditions(int preconditions, int launchFlags, |
| 203 | int expectedResult) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 204 | final ActivityTaskManagerService service = mService; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 205 | final IPackageManager packageManager = mock(IPackageManager.class); |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 206 | final ActivityStartController controller = mock(ActivityStartController.class); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 207 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 208 | final ActivityStarter starter = new ActivityStarter(controller, service, |
Bryce Lee | d3624e1 | 2017-11-30 08:51:45 -0800 | [diff] [blame] | 209 | service.mStackSupervisor, mock(ActivityStartInterceptor.class)); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 210 | prepareStarter(launchFlags); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 211 | final IApplicationThread caller = mock(IApplicationThread.class); |
| 212 | |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 213 | final WindowProcessController wpc = |
| 214 | containsConditions(preconditions, PRECONDITION_NO_CALLER_APP) |
| 215 | ? null : new WindowProcessController( |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 216 | service, mock(ApplicationInfo.class), null, 0, -1, null, null); |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 217 | doReturn(wpc).when(service).getProcessController(anyObject()); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 218 | |
| 219 | final Intent intent = new Intent(); |
| 220 | intent.setFlags(launchFlags); |
| 221 | |
| 222 | final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO) |
| 223 | ? null : new ActivityInfo(); |
| 224 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 225 | IVoiceInteractionSession voiceSession = |
| 226 | containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION) |
| 227 | ? mock(IVoiceInteractionSession.class) : null; |
| 228 | |
| 229 | // Create source token |
| 230 | final ActivityBuilder builder = new ActivityBuilder(service).setTask( |
| 231 | new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build()); |
| 232 | |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 233 | if (aInfo != null) { |
| 234 | aInfo.applicationInfo = new ApplicationInfo(); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 235 | aInfo.applicationInfo.packageName = |
| 236 | ActivityBuilder.getDefaultComponent().getPackageName(); |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 239 | // Offset uid by one from {@link ActivityInfo} to simulate different uids. |
| 240 | if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) { |
| 241 | builder.setUid(aInfo.applicationInfo.uid + 1); |
| 242 | } |
| 243 | |
| 244 | final ActivityRecord source = builder.build(); |
| 245 | |
| 246 | if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) { |
| 247 | intent.setComponent(source.realActivity); |
| 248 | } |
| 249 | |
| 250 | if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 251 | doReturn(false).when(service).checkAppSwitchAllowedLocked( |
Wale Ogunwale | a6191b4 | 2018-05-09 07:41:32 -0700 | [diff] [blame] | 252 | anyInt(), anyInt(), anyInt(), anyInt(), any()); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 255 | if (containsConditions(preconditions, PRECONDITION_CANNOT_START_ANY_ACTIVITY)) { |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 256 | doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission( |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 257 | any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), |
Winson Chung | c9804e7 | 2018-05-15 11:01:44 -0700 | [diff] [blame] | 258 | anyBoolean(), anyBoolean(), any(), any(), any()); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | try { |
| 262 | if (containsConditions(preconditions, |
| 263 | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) { |
| 264 | doAnswer((inv) -> { |
| 265 | throw new RemoteException(); |
| 266 | }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent), |
| 267 | any()); |
| 268 | } else { |
| 269 | doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT)) |
| 270 | .when(packageManager).activitySupportsIntent(eq(source.realActivity), |
| 271 | eq(intent), any()); |
| 272 | } |
| 273 | } catch (RemoteException e) { |
| 274 | } |
| 275 | |
| 276 | final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT) |
| 277 | || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION) |
| 278 | ? source.appToken : null; |
| 279 | |
| 280 | final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE) |
| 281 | ? 1 : 0; |
| 282 | |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 283 | final int result = starter.setCaller(caller) |
| 284 | .setIntent(intent) |
| 285 | .setActivityInfo(aInfo) |
| 286 | .setResultTo(resultTo) |
| 287 | .setRequestCode(requestCode) |
| 288 | .setReason("testLaunchActivityPermissionDenied") |
| 289 | .execute(); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 290 | |
| 291 | // In some cases the expected result internally is different than the published result. We |
| 292 | // must use ActivityStarter#getExternalResult to translate. |
| 293 | assertEquals(ActivityStarter.getExternalResult(expectedResult), result); |
| 294 | |
| 295 | // Ensure that {@link ActivityOptions} are aborted with unsuccessful result. |
| 296 | if (expectedResult != START_SUCCESS) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 297 | final ActivityStarter optionStarter = new ActivityStarter(mController, mService, |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 298 | mService.mStackSupervisor, mock(ActivityStartInterceptor.class)); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 299 | final ActivityOptions options = spy(ActivityOptions.makeBasic()); |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 300 | |
| 301 | final int optionResult = optionStarter.setCaller(caller) |
| 302 | .setIntent(intent) |
| 303 | .setActivityInfo(aInfo) |
| 304 | .setResultTo(resultTo) |
| 305 | .setRequestCode(requestCode) |
| 306 | .setReason("testLaunchActivityPermissionDenied") |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 307 | .setActivityOptions(new SafeActivityOptions(options)) |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 308 | .execute(); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 309 | verify(options, times(1)).abort(); |
| 310 | } |
| 311 | } |
Bryce Lee | b802ea1 | 2017-11-15 21:25:03 -0800 | [diff] [blame] | 312 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 313 | private ActivityStarter prepareStarter(@Intent.Flags int launchFlags) { |
| 314 | return prepareStarter(launchFlags, true /* mockGetLaunchStack */); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Creates a {@link ActivityStarter} with default parameters and necessary mocks. |
| 319 | * |
| 320 | * @param launchFlags The intent flags to launch activity. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 321 | * @param mockGetLaunchStack Whether to mock {@link RootActivityContainer#getLaunchStack} for |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 322 | * always launching to the testing stack. Set to false when allowing |
| 323 | * the activity can be launched to any stack that is decided by real |
| 324 | * implementation. |
| 325 | * @return A {@link ActivityStarter} with default setup. |
| 326 | */ |
| 327 | private ActivityStarter prepareStarter(@Intent.Flags int launchFlags, |
| 328 | boolean mockGetLaunchStack) { |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 329 | // always allow test to start activity. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 330 | doReturn(true).when(mSupervisor).checkStartAnyActivityPermission( |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 331 | any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), |
Winson Chung | c9804e7 | 2018-05-15 11:01:44 -0700 | [diff] [blame] | 332 | anyBoolean(), anyBoolean(), any(), any(), any()); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 333 | |
| 334 | // instrument the stack and task used. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 335 | final ActivityStack stack = mRootActivityContainer.getDefaultDisplay().createStack( |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 336 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 337 | final TaskRecord task = new TaskBuilder(mSupervisor) |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 338 | .setCreateStack(false) |
| 339 | .build(); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 340 | |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 341 | // use factory that only returns spy task. |
| 342 | final TaskRecordFactory factory = mock(TaskRecordFactory.class); |
| 343 | TaskRecord.setTaskRecordFactory(factory); |
| 344 | |
| 345 | // return task when created. |
| 346 | doReturn(task).when(factory).create(any(), anyInt(), any(), any(), any(), any()); |
| 347 | |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 348 | if (mockGetLaunchStack) { |
| 349 | // Direct starter to use spy stack. |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 350 | doReturn(stack).when(mRootActivityContainer) |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 351 | .getLaunchStack(any(), any(), any(), anyBoolean()); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 352 | doReturn(stack).when(mRootActivityContainer) |
Garfield Tan | 20d9e2f | 2018-11-16 15:42:29 -0800 | [diff] [blame] | 353 | .getLaunchStack(any(), any(), any(), anyBoolean(), any()); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 354 | } |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 355 | |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 356 | // Set up mock package manager internal and make sure no unmocked methods are called |
| 357 | PackageManagerInternal mockPackageManager = mock(PackageManagerInternal.class, |
| 358 | invocation -> { |
| 359 | throw new RuntimeException("Not stubbed"); |
| 360 | }); |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 361 | doReturn(mockPackageManager).when(mService).getPackageManagerInternalLocked(); |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 362 | |
| 363 | // Never review permissions |
| 364 | doReturn(false).when(mockPackageManager).isPermissionsReviewRequired(any(), anyInt()); |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 365 | doNothing().when(mockPackageManager).grantEphemeralAccess( |
| 366 | anyInt(), any(), anyInt(), anyInt()); |
Philip P. Moltmann | 6c644e6 | 2018-07-18 15:41:24 -0700 | [diff] [blame] | 367 | |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 368 | final Intent intent = new Intent(); |
| 369 | intent.addFlags(launchFlags); |
| 370 | intent.setComponent(ActivityBuilder.getDefaultComponent()); |
| 371 | |
| 372 | final ActivityInfo info = new ActivityInfo(); |
| 373 | |
| 374 | info.applicationInfo = new ApplicationInfo(); |
| 375 | info.applicationInfo.packageName = ActivityBuilder.getDefaultComponent().getPackageName(); |
| 376 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 377 | return new ActivityStarter(mController, mService, |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 378 | mService.mStackSupervisor, mock(ActivityStartInterceptor.class)) |
| 379 | .setIntent(intent) |
| 380 | .setActivityInfo(info); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Ensures that values specified at launch time are passed to {@link LaunchParamsModifier} |
| 385 | * when we are laying out a new task. |
| 386 | */ |
| 387 | @Test |
| 388 | public void testCreateTaskLayout() { |
| 389 | // modifier for validating passed values. |
| 390 | final LaunchParamsModifier modifier = mock(LaunchParamsModifier.class); |
| 391 | mService.mStackSupervisor.getLaunchParamsController().registerModifier(modifier); |
| 392 | |
| 393 | // add custom values to activity info to make unique. |
| 394 | final ActivityInfo info = new ActivityInfo(); |
| 395 | final Rect launchBounds = new Rect(0, 0, 20, 30); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 396 | |
| 397 | final WindowLayout windowLayout = |
| 398 | new WindowLayout(10, .5f, 20, 1.0f, Gravity.NO_GRAVITY, 1, 1); |
| 399 | |
| 400 | info.windowLayout = windowLayout; |
| 401 | info.applicationInfo = new ApplicationInfo(); |
| 402 | info.applicationInfo.packageName = ActivityBuilder.getDefaultComponent().getPackageName(); |
| 403 | |
| 404 | // create starter. |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 405 | final ActivityStarter optionStarter = prepareStarter(0 /* launchFlags */); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 406 | |
| 407 | final ActivityOptions options = ActivityOptions.makeBasic(); |
| 408 | options.setLaunchBounds(launchBounds); |
| 409 | |
| 410 | // run starter. |
| 411 | optionStarter |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 412 | .setReason("testCreateTaskLayout") |
| 413 | .setActivityInfo(info) |
| 414 | .setActivityOptions(new SafeActivityOptions(options)) |
| 415 | .execute(); |
| 416 | |
Garfield Tan | 706dbcb | 2018-10-15 11:33:02 -0700 | [diff] [blame] | 417 | // verify that values are passed to the modifier. Values are passed twice -- once for |
| 418 | // setting initial state, another when task is created. |
| 419 | verify(modifier, times(2)).onCalculate(any(), eq(windowLayout), any(), any(), eq(options), |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 420 | any(), any()); |
| 421 | } |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 422 | |
| 423 | /** |
| 424 | * This test ensures that if the intent is being delivered to a |
| 425 | */ |
| 426 | @Test |
| 427 | public void testSplitScreenDeliverToTop() { |
| 428 | final ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 429 | |
| 430 | final ActivityRecord focusActivity = new ActivityBuilder(mService) |
| 431 | .setCreateTask(true) |
| 432 | .build(); |
| 433 | |
| 434 | focusActivity.getStack().setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY); |
| 435 | |
| 436 | final ActivityRecord reusableActivity = new ActivityBuilder(mService) |
| 437 | .setCreateTask(true) |
| 438 | .build(); |
| 439 | |
| 440 | // Create reusable activity after entering split-screen so that it is the top secondary |
| 441 | // stack. |
| 442 | reusableActivity.getStack().setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY); |
| 443 | |
| 444 | // Set focus back to primary. |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 445 | final ActivityStack focusStack = focusActivity.getStack(); |
| 446 | focusStack.moveToFront("testSplitScreenDeliverToTop"); |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 447 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 448 | doReturn(reusableActivity).when(mRootActivityContainer).findTask(any(), anyInt()); |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 449 | |
| 450 | final int result = starter.setReason("testSplitScreenDeliverToTop").execute(); |
| 451 | |
| 452 | // Ensure result is delivering intent to top. |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 453 | assertEquals(START_DELIVERED_TO_TOP, result); |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
| 457 | * This test ensures that if the intent is being delivered to a split-screen unfocused task |
| 458 | * reports it is brought to front instead of delivering to top. |
| 459 | */ |
| 460 | @Test |
| 461 | public void testSplitScreenTaskToFront() { |
| 462 | final ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 463 | |
| 464 | // Create reusable activity here first. Setting the windowing mode of the primary stack |
| 465 | // will move the existing standard full screen stack to secondary, putting this one on the |
| 466 | // bottom. |
| 467 | final ActivityRecord reusableActivity = new ActivityBuilder(mService) |
| 468 | .setCreateTask(true) |
| 469 | .build(); |
| 470 | |
| 471 | reusableActivity.getStack().setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY); |
| 472 | |
| 473 | final ActivityRecord focusActivity = new ActivityBuilder(mService) |
| 474 | .setCreateTask(true) |
| 475 | .build(); |
| 476 | |
| 477 | // Enter split-screen. Primary stack should have focus. |
| 478 | focusActivity.getStack().setWindowingMode(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY); |
| 479 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 480 | doReturn(reusableActivity).when(mRootActivityContainer).findTask(any(), anyInt()); |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 481 | |
| 482 | final int result = starter.setReason("testSplitScreenMoveToFront").execute(); |
| 483 | |
| 484 | // Ensure result is moving task to front. |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 485 | assertEquals(START_TASK_TO_FRONT, result); |
Bryce Lee | 32e09ef | 2018-03-19 15:29:49 -0700 | [diff] [blame] | 486 | } |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Tests activity is cleaned up properly in a task mode violation. |
| 490 | */ |
| 491 | @Test |
| 492 | public void testTaskModeViolation() { |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 493 | final ActivityDisplay display = mService.mRootActivityContainer.getDefaultDisplay(); |
Andrii Kulian | 6a6c4f1 | 2018-07-16 21:23:33 -0700 | [diff] [blame] | 494 | ((TestActivityDisplay) display).removeAllTasks(); |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 495 | assertNoTasks(display); |
| 496 | |
| 497 | final ActivityStarter starter = prepareStarter(0); |
| 498 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 499 | final LockTaskController lockTaskController = mService.getLockTaskController(); |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 500 | doReturn(true).when(lockTaskController).isLockTaskModeViolation(any()); |
| 501 | |
| 502 | final int result = starter.setReason("testTaskModeViolation").execute(); |
| 503 | |
| 504 | assertEquals(START_RETURN_LOCK_TASK_MODE_VIOLATION, result); |
| 505 | assertNoTasks(display); |
| 506 | } |
| 507 | |
| 508 | private void assertNoTasks(ActivityDisplay display) { |
| 509 | for (int i = display.getChildCount() - 1; i >= 0; --i) { |
| 510 | final ActivityStack stack = display.getChildAt(i); |
Tadashi G. Takaoka | 74ccec2 | 2018-10-23 11:07:13 +0900 | [diff] [blame] | 511 | assertThat(stack.getAllTasks()).isEmpty(); |
Bryce Lee | 2b8e037 | 2018-04-05 17:01:37 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * This test ensures that activity starts are not being logged when the logging is disabled. |
| 517 | */ |
| 518 | @Test |
| 519 | public void testActivityStartsLogging_noLoggingWhenDisabled() { |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 520 | doReturn(false).when(mService).isActivityStartsLoggingEnabled(); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 521 | doReturn(mActivityMetricsLogger).when(mService.mStackSupervisor).getActivityMetricsLogger(); |
| 522 | |
| 523 | ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK); |
| 524 | starter.setReason("testActivityStartsLogging_noLoggingWhenDisabled").execute(); |
| 525 | |
| 526 | // verify logging wasn't done |
| 527 | verify(mActivityMetricsLogger, never()).logActivityStart(any(), any(), any(), anyInt(), |
| 528 | any(), anyInt(), anyBoolean(), anyInt(), anyInt(), anyBoolean(), anyInt(), any(), |
| 529 | anyInt(), anyBoolean(), any(), anyBoolean()); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * This test ensures that activity starts are being logged when the logging is enabled. |
| 534 | */ |
| 535 | @Test |
| 536 | public void testActivityStartsLogging_logsWhenEnabled() { |
| 537 | // note: conveniently this package doesn't have any activity visible |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 538 | doReturn(true).when(mService).isActivityStartsLoggingEnabled(); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 539 | doReturn(mActivityMetricsLogger).when(mService.mStackSupervisor).getActivityMetricsLogger(); |
| 540 | |
| 541 | ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK) |
| 542 | .setCallingUid(FAKE_CALLING_UID) |
| 543 | .setRealCallingUid(FAKE_REAL_CALLING_UID) |
| 544 | .setCallingPackage(FAKE_CALLING_PACKAGE) |
| 545 | .setOriginatingPendingIntent(null); |
| 546 | |
| 547 | starter.setReason("testActivityStartsLogging_logsWhenEnabled").execute(); |
| 548 | |
| 549 | // verify the above activity start was logged |
| 550 | verify(mActivityMetricsLogger, times(1)).logActivityStart(any(), any(), any(), |
| 551 | eq(FAKE_CALLING_UID), eq(FAKE_CALLING_PACKAGE), anyInt(), anyBoolean(), |
| 552 | eq(FAKE_REAL_CALLING_UID), anyInt(), anyBoolean(), anyInt(), |
| 553 | eq(ActivityBuilder.getDefaultComponent().getPackageName()), anyInt(), anyBoolean(), |
| 554 | any(), eq(false)); |
| 555 | } |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 556 | |
| 557 | /** |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 558 | * This test ensures that unsupported usecases aren't aborted when background starts are |
| 559 | * allowed. |
| 560 | */ |
| 561 | @Test |
| 562 | public void testBackgroundActivityStartsAllowed_noStartsAborted() { |
| 563 | doReturn(true).when(mService).isBackgroundActivityStartsEnabled(); |
| 564 | |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 565 | runAndVerifyBackgroundActivityStartsSubtest("allowed_noStartsAborted", false, |
| 566 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 567 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 568 | false, false); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * This test ensures that unsupported usecases are aborted when background starts are |
| 573 | * disallowed. |
| 574 | */ |
| 575 | @Test |
| 576 | public void testBackgroundActivityStartsDisallowed_unsupportedStartsAborted() { |
| 577 | doReturn(false).when(mService).isBackgroundActivityStartsEnabled(); |
| 578 | |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 579 | runAndVerifyBackgroundActivityStartsSubtest( |
| 580 | "disallowed_unsupportedUsecase_aborted", true, |
| 581 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 582 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 583 | false, false); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * This test ensures that supported usecases aren't aborted when background starts are |
| 588 | * disallowed. |
| 589 | * The scenarios each have only one condidion that makes them supported. |
| 590 | */ |
| 591 | @Test |
| 592 | public void testBackgroundActivityStartsDisallowed_supportedStartsNotAborted() { |
| 593 | doReturn(false).when(mService).isBackgroundActivityStartsEnabled(); |
| 594 | |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 595 | runAndVerifyBackgroundActivityStartsSubtest("disallowed_rootUid_notAborted", false, |
| 596 | Process.ROOT_UID, false, PROCESS_STATE_TOP + 1, |
| 597 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 598 | false, false); |
| 599 | runAndVerifyBackgroundActivityStartsSubtest("disallowed_systemUid_notAborted", false, |
| 600 | Process.SYSTEM_UID, false, PROCESS_STATE_TOP + 1, |
| 601 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 602 | false, false); |
| 603 | runAndVerifyBackgroundActivityStartsSubtest( |
| 604 | "disallowed_callingUidHasVisibleWindow_notAborted", false, |
| 605 | UNIMPORTANT_UID, true, PROCESS_STATE_TOP + 1, |
| 606 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 607 | false, false); |
| 608 | runAndVerifyBackgroundActivityStartsSubtest( |
| 609 | "disallowed_callingUidProcessStateTop_notAborted", false, |
| 610 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP, |
| 611 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 612 | false, false); |
| 613 | runAndVerifyBackgroundActivityStartsSubtest( |
| 614 | "disallowed_realCallingUidHasVisibleWindow_notAborted", false, |
| 615 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 616 | UNIMPORTANT_UID2, true, PROCESS_STATE_TOP + 1, |
| 617 | false, false); |
| 618 | runAndVerifyBackgroundActivityStartsSubtest( |
| 619 | "disallowed_realCallingUidProcessStateTop_notAborted", false, |
| 620 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 621 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP, |
| 622 | false, false); |
| 623 | runAndVerifyBackgroundActivityStartsSubtest( |
| 624 | "disallowed_hasForegroundActivities_notAborted", false, |
| 625 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 626 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 627 | true, false); |
| 628 | runAndVerifyBackgroundActivityStartsSubtest( |
| 629 | "disallowed_callerIsRecents_notAborted", false, |
| 630 | UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, |
| 631 | UNIMPORTANT_UID2, false, PROCESS_STATE_TOP + 1, |
| 632 | false, true); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | private void runAndVerifyBackgroundActivityStartsSubtest(String name, boolean shouldHaveAborted, |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 636 | int callingUid, boolean callingUidHasVisibleWindow, int callingUidProcState, |
| 637 | int realCallingUid, boolean realCallingUidHasVisibleWindow, int realCallingUidProcState, |
Michal Karpinski | 82bb590 | 2018-11-28 15:52:52 +0000 | [diff] [blame] | 638 | boolean hasForegroundActivities, boolean callerIsRecents) { |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 639 | // window visibility |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 640 | doReturn(callingUidHasVisibleWindow).when(mService.mWindowManager).isAnyWindowVisibleForUid( |
| 641 | callingUid); |
| 642 | doReturn(realCallingUidHasVisibleWindow).when(mService.mWindowManager) |
| 643 | .isAnyWindowVisibleForUid(realCallingUid); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 644 | // process importance |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 645 | doReturn(callingUidProcState).when(mService).getUidStateLocked(callingUid); |
| 646 | doReturn(realCallingUidProcState).when(mService).getUidStateLocked(realCallingUid); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 647 | // foreground activities |
| 648 | final IApplicationThread caller = mock(IApplicationThread.class); |
| 649 | final ApplicationInfo ai = new ApplicationInfo(); |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 650 | ai.uid = callingUid; |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 651 | final WindowProcessController callerApp = |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 652 | new WindowProcessController(mService, ai, null, callingUid, -1, null, null); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 653 | callerApp.setHasForegroundActivities(hasForegroundActivities); |
| 654 | doReturn(callerApp).when(mService).getProcessController(caller); |
Michal Karpinski | 82bb590 | 2018-11-28 15:52:52 +0000 | [diff] [blame] | 655 | // caller is recents |
| 656 | RecentTasks recentTasks = mock(RecentTasks.class); |
| 657 | mService.mStackSupervisor.setRecentTasks(recentTasks); |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 658 | doReturn(callerIsRecents).when(recentTasks).isCallerRecents(callingUid); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 659 | |
| 660 | final ActivityOptions options = spy(ActivityOptions.makeBasic()); |
Michal Karpinski | 0ad4e2f | 2018-11-29 16:22:34 +0000 | [diff] [blame^] | 661 | ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK) |
| 662 | .setCaller(caller) |
| 663 | .setCallingUid(callingUid) |
| 664 | .setRealCallingUid(realCallingUid) |
| 665 | .setActivityOptions(new SafeActivityOptions(options)); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 666 | |
| 667 | final int result = starter.setReason("testBackgroundActivityStarts_" + name).execute(); |
| 668 | |
| 669 | assertEquals(ActivityStarter.getExternalResult( |
| 670 | shouldHaveAborted ? START_ABORTED : START_SUCCESS), result); |
| 671 | verify(options, times(shouldHaveAborted ? 1 : 0)).abort(); |
| 672 | } |
| 673 | |
| 674 | /** |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 675 | * This test ensures that when starting an existing single task activity on secondary display |
| 676 | * which is not the top focused display, it should deliver new intent to the activity and not |
| 677 | * create a new stack. |
| 678 | */ |
| 679 | @Test |
| 680 | public void testDeliverIntentToTopActivityOfNonTopDisplay() { |
| 681 | final ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK, |
| 682 | false /* mockGetLaunchStack */); |
| 683 | |
| 684 | // Create a secondary display at bottom. |
Riddle Hsu | fd4a050 | 2018-10-16 01:05:16 +0800 | [diff] [blame] | 685 | final TestActivityDisplay secondaryDisplay = spy(createNewActivityDisplay()); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 686 | mRootActivityContainer.addChild(secondaryDisplay, POSITION_BOTTOM); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 687 | final ActivityStack stack = secondaryDisplay.createStack(WINDOWING_MODE_FULLSCREEN, |
| 688 | ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 689 | |
| 690 | // Create an activity record on the top of secondary display. |
Riddle Hsu | fd4a050 | 2018-10-16 01:05:16 +0800 | [diff] [blame] | 691 | final ActivityRecord topActivityOnSecondaryDisplay = createSingleTaskActivityOn(stack); |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 692 | |
| 693 | // Put an activity on default display as the top focused activity. |
| 694 | new ActivityBuilder(mService).setCreateTask(true).build(); |
| 695 | |
| 696 | // Start activity with the same intent as {@code topActivityOnSecondaryDisplay} |
| 697 | // on secondary display. |
| 698 | final ActivityOptions options = ActivityOptions.makeBasic() |
| 699 | .setLaunchDisplayId(secondaryDisplay.mDisplayId); |
| 700 | final int result = starter.setReason("testDeliverIntentToTopActivityOfNonTopDisplay") |
| 701 | .setIntent(topActivityOnSecondaryDisplay.intent) |
| 702 | .setActivityOptions(options.toBundle()) |
| 703 | .execute(); |
| 704 | |
| 705 | // Ensure result is delivering intent to top. |
| 706 | assertEquals(START_DELIVERED_TO_TOP, result); |
| 707 | |
| 708 | // Ensure secondary display only creates one stack. |
| 709 | verify(secondaryDisplay, times(1)).createStack(anyInt(), anyInt(), anyBoolean()); |
| 710 | } |
| 711 | |
| 712 | /** |
Riddle Hsu | fd4a050 | 2018-10-16 01:05:16 +0800 | [diff] [blame] | 713 | * This test ensures that when starting an existing non-top single task activity on secondary |
| 714 | * display which is the top focused display, it should bring the task to front without creating |
| 715 | * unused stack. |
| 716 | */ |
| 717 | @Test |
| 718 | public void testBringTaskToFrontOnSecondaryDisplay() { |
| 719 | final ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK, |
| 720 | false /* mockGetLaunchStack */); |
| 721 | |
| 722 | // Create a secondary display with an activity. |
| 723 | final TestActivityDisplay secondaryDisplay = spy(createNewActivityDisplay()); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 724 | mRootActivityContainer.addChild(secondaryDisplay, POSITION_TOP); |
Riddle Hsu | fd4a050 | 2018-10-16 01:05:16 +0800 | [diff] [blame] | 725 | final ActivityRecord singleTaskActivity = createSingleTaskActivityOn( |
| 726 | secondaryDisplay.createStack(WINDOWING_MODE_FULLSCREEN, |
| 727 | ACTIVITY_TYPE_STANDARD, false /* onTop */)); |
| 728 | |
| 729 | // Create another activity on top of the secondary display. |
| 730 | final ActivityStack topStack = secondaryDisplay.createStack(WINDOWING_MODE_FULLSCREEN, |
| 731 | ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 732 | final TaskRecord topTask = new TaskBuilder(mSupervisor).setStack(topStack).build(); |
| 733 | new ActivityBuilder(mService).setTask(topTask).build(); |
| 734 | |
| 735 | // Start activity with the same intent as {@code singleTaskActivity} on secondary display. |
| 736 | final ActivityOptions options = ActivityOptions.makeBasic() |
| 737 | .setLaunchDisplayId(secondaryDisplay.mDisplayId); |
| 738 | final int result = starter.setReason("testBringTaskToFrontOnSecondaryDisplay") |
| 739 | .setIntent(singleTaskActivity.intent) |
| 740 | .setActivityOptions(options.toBundle()) |
| 741 | .execute(); |
| 742 | |
| 743 | // Ensure result is moving existing task to front. |
| 744 | assertEquals(START_TASK_TO_FRONT, result); |
| 745 | |
| 746 | // Ensure secondary display only creates two stacks. |
| 747 | verify(secondaryDisplay, times(2)).createStack(anyInt(), anyInt(), anyBoolean()); |
| 748 | } |
| 749 | |
| 750 | private ActivityRecord createSingleTaskActivityOn(ActivityStack stack) { |
| 751 | final ComponentName componentName = ComponentName.createRelative( |
| 752 | DEFAULT_COMPONENT_PACKAGE_NAME, |
| 753 | DEFAULT_COMPONENT_PACKAGE_NAME + ".SingleTaskActivity"); |
| 754 | final TaskRecord taskRecord = new TaskBuilder(mSupervisor) |
| 755 | .setComponent(componentName) |
| 756 | .setStack(stack) |
| 757 | .build(); |
| 758 | return new ActivityBuilder(mService) |
| 759 | .setComponent(componentName) |
| 760 | .setLaunchMode(LAUNCH_SINGLE_TASK) |
| 761 | .setTask(taskRecord) |
| 762 | .build(); |
| 763 | } |
| 764 | |
| 765 | /** |
Riddle Hsu | b70b36d | 2018-09-11 21:20:02 +0800 | [diff] [blame] | 766 | * This test ensures that a reused top activity in the top focused stack is able to be |
| 767 | * reparented to another display. |
| 768 | */ |
| 769 | @Test |
| 770 | public void testReparentTopFocusedActivityToSecondaryDisplay() { |
| 771 | final ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK, |
| 772 | false /* mockGetLaunchStack */); |
| 773 | |
| 774 | // Create a secondary display at bottom. |
| 775 | final TestActivityDisplay secondaryDisplay = addNewActivityDisplayAt(POSITION_BOTTOM); |
| 776 | secondaryDisplay.createStack(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, |
| 777 | true /* onTop */); |
| 778 | |
| 779 | // Put an activity on default display as the top focused activity. |
| 780 | final ActivityRecord topActivity = new ActivityBuilder(mService) |
| 781 | .setCreateTask(true) |
| 782 | .setLaunchMode(LAUNCH_SINGLE_TASK) |
| 783 | .build(); |
| 784 | |
| 785 | // Start activity with the same intent as {@code topActivity} on secondary display. |
| 786 | final ActivityOptions options = ActivityOptions.makeBasic() |
| 787 | .setLaunchDisplayId(secondaryDisplay.mDisplayId); |
| 788 | starter.setReason("testReparentTopFocusedActivityToSecondaryDisplay") |
| 789 | .setIntent(topActivity.intent) |
| 790 | .setActivityOptions(options.toBundle()) |
| 791 | .execute(); |
| 792 | |
| 793 | // Ensure the activity is moved to secondary display. |
| 794 | assertEquals(secondaryDisplay, topActivity.getDisplay()); |
| 795 | } |
Wale Ogunwale | 44f036f | 2017-09-29 05:09:09 -0700 | [diff] [blame] | 796 | } |