Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2018 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.server.wm.app.Components.TEST_ACTIVITY;
     20 import static android.server.wm.app27.Components.SDK_27_LAUNCHING_ACTIVITY;
     21 
     22 import static org.junit.Assert.assertFalse;
     23 
     24 import android.app.Activity;
     25 import android.platform.test.annotations.Presubmit;
     26 
     27 import androidx.test.rule.ActivityTestRule;
     28 import androidx.test.filters.FlakyTest;
     29 
     30 import org.junit.Rule;
     31 import org.junit.Test;
     32 
     33 /**
     34  * Build/Install/Run:
     35  *     atest CtsWindowManagerDeviceTestCases:StartActivityTests
     36  */
     37 @Presubmit
     38 public class StartActivityTests extends ActivityManagerTestBase {
     39 
     40     @Rule
     41     public final ActivityTestRule<TestActivity2> mTestActivity2Rule =
     42             new ActivityTestRule<>(TestActivity2.class);
     43 
     44     /**
     45      * Ensures {@link Activity} can only be launched from an {@link Activity}
     46      * {@link android.content.Context}.
     47      */
     48     @Test
     49     public void testStartActivityContexts() throws Exception {
     50         // Launch Activity from application context.
     51         getLaunchActivityBuilder()
     52                 .setTargetActivity(TEST_ACTIVITY)
     53                 .setUseApplicationContext(true)
     54                 .setSuppressExceptions(true)
     55                 .execute();
     56 
     57         // Launch second Activity from Activity Context to ensure previous Activity has launched.
     58         final Activity testActivity2 = mTestActivity2Rule.launchActivity(null);
     59 
     60         mAmWmState.computeState(testActivity2.getComponentName());
     61 
     62         // Verify Activity was not started.
     63         assertFalse(mAmWmState.getAmState().containsActivity(TEST_ACTIVITY));
     64         mAmWmState.assertResumedActivity(
     65                 "Activity launched from activity context should be present",
     66                 testActivity2.getComponentName());
     67     }
     68 
     69     /**
     70      * Ensures you can start an {@link Activity} from a non {@link Activity}
     71      * {@link android.content.Context} with the {@code FLAG_ACTIVITY_NEW_TASK}.
     72      */
     73     @Test
     74     public void testStartActivityNewTask() throws Exception {
     75         // Launch Activity from application context.
     76         getLaunchActivityBuilder()
     77                 .setTargetActivity(TEST_ACTIVITY)
     78                 .setUseApplicationContext(true)
     79                 .setSuppressExceptions(true)
     80                 .setNewTask(true)
     81                 .execute();
     82 
     83         mAmWmState.computeState(TEST_ACTIVITY);
     84         mAmWmState.assertResumedActivity("Test Activity should be started with new task flag",
     85                 TEST_ACTIVITY);
     86     }
     87 
     88     /**
     89      * Ensures you can start an {@link Activity} from a non {@link Activity}
     90      * {@link android.content.Context} when the target sdk is between N and O Mr1.
     91      * @throws Exception
     92      */
     93     @Test
     94     @FlakyTest(bugId = 131005232)
     95     public void testLegacyStartActivityFromNonActivityContext() throws Exception {
     96         getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY)
     97                 .setLaunchingActivity(SDK_27_LAUNCHING_ACTIVITY)
     98                 .setUseApplicationContext(true)
     99                 .execute();
    100 
    101         mAmWmState.computeState(TEST_ACTIVITY);
    102         mAmWmState.assertResumedActivity("Test Activity should be resumed without older sdk",
    103                 TEST_ACTIVITY);
    104     }
    105 
    106     public static class TestActivity2 extends Activity {
    107     }
    108 }
    109