Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Bryce Lee | 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; |
| 21 | import static android.app.ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT; |
| 22 | import static android.app.ActivityManager.START_NOT_VOICE_COMPATIBLE; |
| 23 | import static android.app.ActivityManager.START_SUCCESS; |
| 24 | import static android.app.ActivityManager.START_SWITCHES_CANCELED; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 25 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 26 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 27 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
| 28 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 29 | import android.app.ActivityOptions; |
| 30 | import android.app.IApplicationThread; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 31 | import android.content.Intent; |
| 32 | import android.content.pm.ActivityInfo; |
| 33 | import android.content.pm.ApplicationInfo; |
| 34 | import android.content.pm.IPackageManager; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 35 | import android.graphics.Rect; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 36 | import android.os.IBinder; |
| 37 | import android.os.RemoteException; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 38 | import android.platform.test.annotations.Presubmit; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 39 | import android.service.voice.IVoiceInteractionSession; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 40 | import android.support.test.filters.SmallTest; |
| 41 | import android.support.test.runner.AndroidJUnit4; |
| 42 | |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.junit.Test; |
| 45 | |
| 46 | import static com.android.server.am.ActivityManagerService.ANIMATE; |
| 47 | |
| 48 | import static org.junit.Assert.assertEquals; |
| 49 | import static org.junit.Assert.assertTrue; |
| 50 | import static org.mockito.Mockito.any; |
| 51 | import static org.mockito.Mockito.anyBoolean; |
| 52 | import static org.mockito.Mockito.anyInt; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 53 | import static org.mockito.Mockito.anyObject; |
| 54 | import static org.mockito.Mockito.doAnswer; |
| 55 | import static org.mockito.Mockito.doReturn; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 56 | import static org.mockito.Mockito.eq; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 57 | import static org.mockito.Mockito.mock; |
| 58 | import static org.mockito.Mockito.spy; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 59 | import static org.mockito.Mockito.verify; |
| 60 | import static org.mockito.Mockito.times; |
| 61 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 62 | import static android.app.ActivityManager.START_PERMISSION_DENIED; |
| 63 | import static android.app.ActivityManager.START_INTENT_NOT_RESOLVED; |
| 64 | |
| 65 | import com.android.internal.os.BatteryStatsImpl; |
| 66 | |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Tests for the {@link ActivityStack} class. |
| 69 | * |
| 70 | * Build/Install/Run: |
| 71 | * bit FrameworksServicesTests:com.android.server.am.ActivityStarterTests |
| 72 | */ |
| 73 | @SmallTest |
| 74 | @Presubmit |
| 75 | @RunWith(AndroidJUnit4.class) |
| 76 | public class ActivityStarterTests extends ActivityTestsBase { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 77 | private ActivityManagerService mService; |
| 78 | private ActivityStarter mStarter; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 79 | private IPackageManager mPackageManager; |
| 80 | |
| 81 | private static final int PRECONDITION_NO_CALLER_APP = 1; |
| 82 | private static final int PRECONDITION_NO_INTENT_COMPONENT = 1 << 1; |
| 83 | private static final int PRECONDITION_NO_ACTIVITY_INFO = 1 << 2; |
| 84 | private static final int PRECONDITION_SOURCE_PRESENT = 1 << 3; |
| 85 | private static final int PRECONDITION_REQUEST_CODE = 1 << 4; |
| 86 | private static final int PRECONDITION_SOURCE_VOICE_SESSION = 1 << 5; |
| 87 | private static final int PRECONDITION_NO_VOICE_SESSION_SUPPORT = 1 << 6; |
| 88 | private static final int PRECONDITION_DIFFERENT_UID = 1 << 7; |
| 89 | private static final int PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION = 1 << 8; |
| 90 | private static final int PRECONDITION_CANNOT_START_ANY_ACTIVITY = 1 << 9; |
| 91 | private static final int PRECONDITION_DISALLOW_APP_SWITCHING = 1 << 10; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 92 | |
| 93 | @Override |
| 94 | public void setUp() throws Exception { |
| 95 | super.setUp(); |
| 96 | mService = createActivityManagerService(); |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 97 | mStarter = new ActivityStarter(mService); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testUpdateLaunchBounds() throws Exception { |
| 102 | // When in a non-resizeable stack, the task bounds should be updated. |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 103 | final TaskRecord task = new TaskBuilder(mService.mStackSupervisor) |
| 104 | .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack( |
| 105 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */)) |
| 106 | .build(); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 107 | final Rect bounds = new Rect(10, 10, 100, 100); |
| 108 | |
| 109 | mStarter.updateBounds(task, bounds); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 110 | assertEquals(task.getOverrideBounds(), bounds); |
| 111 | assertEquals(new Rect(), task.getStack().getOverrideBounds()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 112 | |
| 113 | // 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] | 114 | final TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor) |
| 115 | .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack( |
| 116 | WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */)) |
| 117 | .build(); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 118 | assertTrue(task2.getStack() instanceof PinnedActivityStack); |
| 119 | mStarter.updateBounds(task2, bounds); |
| 120 | |
Wale Ogunwale | 44f036f | 2017-09-29 05:09:09 -0700 | [diff] [blame] | 121 | verify(mService, times(1)).resizeStack(eq(task2.getStack().mStackId), |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 122 | eq(bounds), anyBoolean(), anyBoolean(), anyBoolean(), anyInt()); |
| 123 | |
| 124 | // In the case of no animation, the stack and task bounds should be set immediately. |
| 125 | if (!ANIMATE) { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 126 | assertEquals(task2.getStack().getOverrideBounds(), bounds); |
| 127 | assertEquals(task2.getOverrideBounds(), bounds); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 128 | } else { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 129 | assertEquals(task2.getOverrideBounds(), new Rect()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 132 | |
| 133 | @Test |
| 134 | public void testStartActivityPreconditions() throws Exception { |
| 135 | verifyStartActivityPreconditions(PRECONDITION_NO_CALLER_APP, START_PERMISSION_DENIED); |
| 136 | verifyStartActivityPreconditions(PRECONDITION_NO_INTENT_COMPONENT, |
| 137 | START_INTENT_NOT_RESOLVED); |
| 138 | verifyStartActivityPreconditions(PRECONDITION_NO_ACTIVITY_INFO, START_CLASS_NOT_FOUND); |
| 139 | verifyStartActivityPreconditions(PRECONDITION_SOURCE_PRESENT | PRECONDITION_REQUEST_CODE, |
| 140 | Intent.FLAG_ACTIVITY_FORWARD_RESULT, START_FORWARD_AND_REQUEST_CONFLICT); |
| 141 | verifyStartActivityPreconditions( |
| 142 | PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT |
| 143 | | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID, |
| 144 | START_NOT_VOICE_COMPATIBLE); |
| 145 | verifyStartActivityPreconditions( |
| 146 | PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT |
| 147 | | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID |
| 148 | | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION, |
| 149 | START_NOT_VOICE_COMPATIBLE); |
| 150 | verifyStartActivityPreconditions(PRECONDITION_CANNOT_START_ANY_ACTIVITY, START_ABORTED); |
| 151 | verifyStartActivityPreconditions(PRECONDITION_DISALLOW_APP_SWITCHING, |
| 152 | START_SWITCHES_CANCELED); |
| 153 | } |
| 154 | |
| 155 | private static boolean containsConditions(int preconditions, int mask) { |
| 156 | return (preconditions & mask) == mask; |
| 157 | } |
| 158 | |
| 159 | private void verifyStartActivityPreconditions(int preconditions, int expectedResult) { |
| 160 | verifyStartActivityPreconditions(preconditions, 0 /*launchFlags*/, expectedResult); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Excercises how the {@link ActivityStarter} reacts to various preconditions. The caller |
| 165 | * provides a bitmask of all the set conditions (such as {@link #PRECONDITION_NO_CALLER_APP}) |
| 166 | * and the launch flags specified in the intent. The method constructs a call to |
| 167 | * {@link ActivityStarter#startActivityLocked} based on these preconditions and ensures the |
| 168 | * result matches the expected. It is important to note that the method also checks side effects |
| 169 | * of the start, such as ensuring {@link ActivityOptions#abort()} is called in the relevant |
| 170 | * scenarios. |
| 171 | * @param preconditions A bitmask representing the preconditions for the launch |
| 172 | * @param launchFlags The launch flags to be provided by the launch {@link Intent}. |
| 173 | * @param expectedResult The expected result from the launch. |
| 174 | */ |
| 175 | private void verifyStartActivityPreconditions(int preconditions, int launchFlags, |
| 176 | int expectedResult) { |
| 177 | final ActivityManagerService service = createActivityManagerService(); |
| 178 | final IPackageManager packageManager = mock(IPackageManager.class); |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 179 | final ActivityStarter starter = new ActivityStarter(service); |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 180 | |
| 181 | final IApplicationThread caller = mock(IApplicationThread.class); |
| 182 | |
| 183 | // If no caller app, return {@code null} {@link ProcessRecord}. |
| 184 | final ProcessRecord record = containsConditions(preconditions, PRECONDITION_NO_CALLER_APP) |
| 185 | ? null : new ProcessRecord(mock(BatteryStatsImpl.class), |
| 186 | mock(ApplicationInfo.class), null, 0); |
| 187 | |
| 188 | doReturn(record).when(service).getRecordForAppLocked(anyObject()); |
| 189 | |
| 190 | final Intent intent = new Intent(); |
| 191 | intent.setFlags(launchFlags); |
| 192 | |
| 193 | final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO) |
| 194 | ? null : new ActivityInfo(); |
| 195 | |
| 196 | if (aInfo != null) { |
| 197 | aInfo.applicationInfo = new ApplicationInfo(); |
| 198 | aInfo.applicationInfo.packageName = ActivityBuilder.DEFAULT_PACKAGE; |
| 199 | } |
| 200 | |
| 201 | IVoiceInteractionSession voiceSession = |
| 202 | containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION) |
| 203 | ? mock(IVoiceInteractionSession.class) : null; |
| 204 | |
| 205 | // Create source token |
| 206 | final ActivityBuilder builder = new ActivityBuilder(service).setTask( |
| 207 | new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build()); |
| 208 | |
| 209 | // Offset uid by one from {@link ActivityInfo} to simulate different uids. |
| 210 | if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) { |
| 211 | builder.setUid(aInfo.applicationInfo.uid + 1); |
| 212 | } |
| 213 | |
| 214 | final ActivityRecord source = builder.build(); |
| 215 | |
| 216 | if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) { |
| 217 | intent.setComponent(source.realActivity); |
| 218 | } |
| 219 | |
| 220 | if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) { |
| 221 | doReturn(false).when(service).checkAppSwitchAllowedLocked(anyInt(), anyInt(), anyInt(), |
| 222 | anyInt(), any()); |
| 223 | } |
| 224 | |
| 225 | if (containsConditions(preconditions,PRECONDITION_CANNOT_START_ANY_ACTIVITY)) { |
| 226 | doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission( |
| 227 | any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), anyBoolean(), |
| 228 | any(), any(), any(), any()); |
| 229 | } |
| 230 | |
| 231 | try { |
| 232 | if (containsConditions(preconditions, |
| 233 | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) { |
| 234 | doAnswer((inv) -> { |
| 235 | throw new RemoteException(); |
| 236 | }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent), |
| 237 | any()); |
| 238 | } else { |
| 239 | doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT)) |
| 240 | .when(packageManager).activitySupportsIntent(eq(source.realActivity), |
| 241 | eq(intent), any()); |
| 242 | } |
| 243 | } catch (RemoteException e) { |
| 244 | } |
| 245 | |
| 246 | final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT) |
| 247 | || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION) |
| 248 | ? source.appToken : null; |
| 249 | |
| 250 | final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE) |
| 251 | ? 1 : 0; |
| 252 | |
| 253 | final int result = starter.startActivityLocked(caller, intent, |
| 254 | null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/, |
| 255 | null /*voiceSession*/, null /*voiceInteractor*/, resultTo, |
| 256 | null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/, |
| 257 | null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/, |
| 258 | 0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/, |
| 259 | false /*componentSpecified*/, null /*outActivity*/, |
| 260 | null /*inTask*/, "testLaunchActivityPermissionDenied"); |
| 261 | |
| 262 | // In some cases the expected result internally is different than the published result. We |
| 263 | // must use ActivityStarter#getExternalResult to translate. |
| 264 | assertEquals(ActivityStarter.getExternalResult(expectedResult), result); |
| 265 | |
| 266 | // Ensure that {@link ActivityOptions} are aborted with unsuccessful result. |
| 267 | if (expectedResult != START_SUCCESS) { |
| 268 | final ActivityOptions options = spy(ActivityOptions.makeBasic()); |
| 269 | final int optionResult = starter.startActivityLocked(caller, intent, |
| 270 | null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/, |
| 271 | null /*voiceSession*/, null /*voiceInteractor*/, resultTo, |
| 272 | null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/, |
| 273 | null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/, |
| 274 | 0 /*startFlags*/, options /*options*/, false /*ignoreTargetSecurity*/, |
| 275 | false /*componentSpecified*/, null /*outActivity*/, |
| 276 | null /*inTask*/, "testLaunchActivityPermissionDenied"); |
| 277 | verify(options, times(1)).abort(); |
| 278 | } |
| 279 | } |
Bryce Lee | b802ea1 | 2017-11-15 21:25:03 -0800 | [diff] [blame^] | 280 | |
| 281 | // TODO(b/69270257): Add test to verify task layout is passed additional data such as activity and |
| 282 | // source. |
| 283 | // @Test |
| 284 | // public void testCreateTaskLayout() { |
| 285 | // } |
Wale Ogunwale | 44f036f | 2017-09-29 05:09:09 -0700 | [diff] [blame] | 286 | } |