1 /* 2 * Copyright (C) 2019 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 package com.android.compatibility.common.util; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.content.Intent; 21 22 import androidx.annotation.NonNull; 23 import androidx.test.rule.ActivityTestRule; 24 25 import com.android.compatibility.common.util.ActivitiesWatcher.ActivityWatcher; 26 27 /** 28 * Helper used to launch an activity and watch for its lifecycle events. 29 * 30 * @param <A> activity type 31 */ 32 public final class ActivityLauncher<A extends Activity> { 33 34 private final ActivityWatcher mWatcher; 35 private final ActivityTestRule<A> mActivityTestRule; 36 private final Intent mLaunchIntent; 37 ActivityLauncher(@onNull Context context, @NonNull ActivitiesWatcher watcher, @NonNull Class<A> activityClass)38 public ActivityLauncher(@NonNull Context context, @NonNull ActivitiesWatcher watcher, 39 @NonNull Class<A> activityClass) { 40 mWatcher = watcher.watch(activityClass); 41 mActivityTestRule = new ActivityTestRule<>(activityClass); 42 mLaunchIntent = new Intent(context, activityClass); 43 } 44 45 /** 46 * Gets a watcher for the activity lifecycle events. 47 */ 48 @NonNull getWatcher()49 public ActivityWatcher getWatcher() { 50 return mWatcher; 51 } 52 53 /** 54 * Launches the activity. 55 */ 56 @NonNull launchActivity()57 public A launchActivity() { 58 return mActivityTestRule.launchActivity(mLaunchIntent); 59 } 60 } 61