1 /* 2 * Copyright (C) 2016 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 android.server.wm; 18 19 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP; 20 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 21 import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP; 22 import static android.server.wm.ComponentNameUtils.getActivityName; 23 import static android.server.wm.ShellCommandHelper.executeShellCommand; 24 import static android.server.wm.WindowManagerState.STATE_INITIALIZING; 25 import static android.server.wm.WindowManagerState.STATE_RESUMED; 26 import static android.server.wm.app.Components.ENTRY_POINT_ALIAS_ACTIVITY; 27 import static android.server.wm.app.Components.LAUNCHING_ACTIVITY; 28 import static android.server.wm.app.Components.SINGLE_TASK_ACTIVITY; 29 import static android.server.wm.app.Components.TEST_ACTIVITY; 30 import static android.view.Display.DEFAULT_DISPLAY; 31 32 import static org.junit.Assert.assertNotNull; 33 import static org.junit.Assert.assertTrue; 34 35 import android.content.ComponentName; 36 import android.platform.test.annotations.Presubmit; 37 38 import org.junit.After; 39 import org.junit.Test; 40 41 /** 42 * Build/Install/Run: 43 * atest CtsWindowManagerDeviceTestCases:AmStartOptionsTests 44 */ 45 @Presubmit 46 public class AmStartOptionsTests extends ActivityManagerTestBase { 47 48 @Test testDashD()49 public void testDashD() { 50 executeShellCommand("am start -n " + getActivityName(TEST_ACTIVITY) + " -D"); 51 52 mWmState.waitForDebuggerWindowVisible(TEST_ACTIVITY); 53 WindowManagerState.Activity activity = mWmState.getActivity(TEST_ACTIVITY); 54 assertNotNull("Must have activity component created", activity); 55 assertTrue(activity.getState().equals(STATE_INITIALIZING) || activity.getState().equals( 56 STATE_RESUMED)); 57 } 58 59 @Test testDashW_Direct()60 public void testDashW_Direct() throws Exception { 61 testDashW(SINGLE_TASK_ACTIVITY, SINGLE_TASK_ACTIVITY); 62 } 63 64 @Test testDashW_Indirect()65 public void testDashW_Indirect() throws Exception { 66 testDashW(ENTRY_POINT_ALIAS_ACTIVITY, SINGLE_TASK_ACTIVITY); 67 } 68 69 @Test testDashW_FinishingTop()70 public void testDashW_FinishingTop() { 71 // Start LaunchingActivity and TestActivity 72 getLaunchActivityBuilder().setLaunchingActivity(LAUNCHING_ACTIVITY) 73 .setTargetActivity(TEST_ACTIVITY).execute(); 74 75 // Return to home 76 launchHomeActivity(); 77 78 // Start LaunchingActivity again and finish TestActivity 79 final int flags = 80 FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP; 81 executeShellCommand("am start -W -f " + flags + " -n " + getActivityName(LAUNCHING_ACTIVITY) 82 + " --display " + DEFAULT_DISPLAY); 83 waitAndAssertTopResumedActivity(LAUNCHING_ACTIVITY, DEFAULT_DISPLAY, 84 "Activity must be launched."); 85 } 86 testDashW(final ComponentName entryActivity, final ComponentName actualActivity)87 private void testDashW(final ComponentName entryActivity, final ComponentName actualActivity) 88 throws Exception { 89 // Test cold start 90 startActivityAndVerifyResult(entryActivity, actualActivity, true); 91 92 // Test warm start 93 launchHomeActivity(); 94 startActivityAndVerifyResult(entryActivity, actualActivity, false); 95 96 // Test "hot" start (app already in front) 97 startActivityAndVerifyResult(entryActivity, actualActivity, false); 98 } 99 startActivityAndVerifyResult(final ComponentName entryActivity, final ComponentName actualActivity, boolean shouldStart)100 private void startActivityAndVerifyResult(final ComponentName entryActivity, 101 final ComponentName actualActivity, boolean shouldStart) { 102 mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY); 103 104 // Pass in different data only when cold starting. This is to make the intent 105 // different in subsequent warm/hot launches, so that the entrypoint alias 106 // activity is always started, but the actual activity is not started again 107 // because of the NEW_TASK and singleTask flags. 108 executeShellCommand("am start -n " + getActivityName(entryActivity) + " -W --display " 109 + DEFAULT_DISPLAY + (shouldStart ? " -d about:blank" : "")); 110 111 waitAndAssertTopResumedActivity(actualActivity, DEFAULT_DISPLAY, 112 "Activity must be launched"); 113 } 114 115 @After tearDown()116 public void tearDown() { 117 // Ensure debug app is cleaned to avoid impacting other tests (b/271998036) 118 executeShellCommand("am clear-debug-app"); 119 } 120 } 121