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     /** Key for string extra, ID to track an Activity that is launched. */
30     public static final String KEY_ACTIVITY_ID = "ActivityID";
31 
32     /**
33      * Options that will be passed to the instrumentation.
34      * @see TestActivityLauncher#launch(Instrumentation)
35      */
36     private final ActivityOptions mOptions = ActivityOptions.makeBasic();
37 
38     /**
39      * The class for the {@link Activity} that you are launching.
40      */
41     private final Class<T> mActivityClass;
42 
43     /**
44      * The intent that will be used to launch the {@link Activity}.
45      */
46     private final Intent mIntent;
47 
TestActivityLauncher(@onNull Context context, @NonNull Class<T> activityClass)48     public TestActivityLauncher(@NonNull Context context, @NonNull Class<T> activityClass) {
49         mActivityClass = activityClass;
50         mIntent = new Intent(context, activityClass);
51     }
52 
addIntentFlag(int flag)53     public TestActivityLauncher<T> addIntentFlag(int flag) {
54         mIntent.addFlags(flag);
55         return this;
56     }
57 
setActivityId(@ullable String id)58     public TestActivityLauncher<T> setActivityId(@Nullable String id) {
59         mIntent.putExtra(KEY_ACTIVITY_ID, id);
60         return this;
61     }
62 
setWindowingMode(int windowingMode)63     public TestActivityLauncher<T> setWindowingMode(int windowingMode) {
64         mOptions.setLaunchWindowingMode(windowingMode);
65         return this;
66     }
67 
setLaunchDisplayId(int displayId)68     public TestActivityLauncher<T> setLaunchDisplayId(int displayId) {
69         mOptions.setLaunchDisplayId(displayId);
70         return this;
71     }
72 
getIntent()73     public Intent getIntent() {
74         return mIntent;
75     }
76 
launch(@onNull Instrumentation instrumentation)77     public T launch(@NonNull Instrumentation instrumentation) {
78         return mActivityClass.cast(instrumentation.startActivitySync(mIntent, mOptions.toBundle()));
79     }
80 }
81