• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.jetpack.utils;
18 
19 import android.app.Activity;
20 import android.app.ActivityOptions;
21 import android.app.Instrumentation;
22 import android.content.Context;
23 import android.content.Intent;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 
28 public class TestActivityLauncher<T extends Activity> {
29 
30     /** Key for string extra, ID to track an Activity that is launched. */
31     public static final String KEY_ACTIVITY_ID = "ActivityID";
32 
33     /**
34      * Options that will be passed to the instrumentation.
35      * @see TestActivityLauncher#launch(Instrumentation)
36      */
37     private final ActivityOptions mOptions = ActivityOptions.makeBasic();
38 
39     /**
40      * The class for the {@link Activity} that you are launching.
41      */
42     private final Class<T> mActivityClass;
43 
44     /**
45      * The intent that will be used to launch the {@link Activity}.
46      */
47     private final Intent mIntent;
48 
TestActivityLauncher(@onNull Context context, @NonNull Class<T> activityClass)49     public TestActivityLauncher(@NonNull Context context, @NonNull Class<T> activityClass) {
50         mActivityClass = activityClass;
51         mIntent = new Intent(context, activityClass);
52     }
53 
addIntentFlag(int flag)54     public TestActivityLauncher<T> addIntentFlag(int flag) {
55         mIntent.addFlags(flag);
56         return this;
57     }
58 
setActivityId(@ullable String id)59     public TestActivityLauncher<T> setActivityId(@Nullable String id) {
60         mIntent.putExtra(KEY_ACTIVITY_ID, id);
61         return this;
62     }
63 
setWindowingMode(int windowingMode)64     public TestActivityLauncher<T> setWindowingMode(int windowingMode) {
65         mOptions.setLaunchWindowingMode(windowingMode);
66         return this;
67     }
68 
launch(@onNull Instrumentation instrumentation)69     public T launch(@NonNull Instrumentation instrumentation) {
70         return mActivityClass.cast(instrumentation.startActivitySync(mIntent, mOptions.toBundle()));
71     }
72 
73 }
74