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