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 android.contentcaptureservice.cts; 17 18 import static android.contentcaptureservice.cts.Helper.GENERIC_TIMEOUT_MS; 19 import static android.contentcaptureservice.cts.Helper.sContext; 20 21 import android.app.Application; 22 import android.content.Intent; 23 import android.util.Log; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.test.rule.ActivityTestRule; 28 29 import com.android.compatibility.common.util.ActivitiesWatcher; 30 import com.android.compatibility.common.util.ActivitiesWatcher.ActivityWatcher; 31 import com.android.compatibility.common.util.Visitor; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.rules.RuleChain; 36 import org.junit.rules.TestRule; 37 38 public abstract class AbstractContentCaptureIntegrationAutoActivityLaunchTest 39 <A extends AbstractContentCaptureActivity> extends AbstractContentCaptureIntegrationTest { 40 41 protected ActivitiesWatcher mActivitiesWatcher; 42 43 private final Class<A> mActivityClass; 44 AbstractContentCaptureIntegrationAutoActivityLaunchTest( @onNull Class<A> activityClass)45 protected AbstractContentCaptureIntegrationAutoActivityLaunchTest( 46 @NonNull Class<A> activityClass) { 47 mActivityClass = activityClass; 48 } 49 50 @Before registerLifecycleCallback()51 public void registerLifecycleCallback() { 52 Log.v(mTag, "@Before: Registering lifecycle callback"); 53 final Application app = (Application) sContext.getApplicationContext(); 54 mActivitiesWatcher = new ActivitiesWatcher(GENERIC_TIMEOUT_MS); 55 app.registerActivityLifecycleCallbacks(mActivitiesWatcher); 56 } 57 58 @After unregisterLifecycleCallback()59 public void unregisterLifecycleCallback() { 60 Log.d(mTag, "@After: Unregistering lifecycle callback: " + mActivitiesWatcher); 61 if (mActivitiesWatcher != null) { 62 final Application app = (Application) sContext.getApplicationContext(); 63 app.unregisterActivityLifecycleCallbacks(mActivitiesWatcher); 64 } 65 } 66 67 /** 68 * Gets the {@link ActivityTestRule} use to launch this activity. 69 * 70 * <p><b>NOTE: </b>implementation must return a static singleton, otherwise it might be 71 * {@code null} when used it in this class' {@code @Rule} 72 */ getActivityTestRule()73 protected abstract ActivityTestRule<A> getActivityTestRule(); 74 75 /** 76 * {@inheritDoc} 77 * 78 * <p>By default it returns {@link #getActivityTestRule()}, but subclasses with more than one 79 * rule can override it to return a {@link RuleChain}. 80 */ 81 @NonNull 82 @Override getMainTestRule()83 protected TestRule getMainTestRule() { 84 return getActivityTestRule(); 85 } 86 launchActivity()87 protected A launchActivity() { 88 Log.d(mTag, "Launching " + mActivityClass.getSimpleName()); 89 90 return getActivityTestRule().launchActivity(getLaunchIntent()); 91 } 92 launchActivity(@ullable Visitor<Intent> visitor)93 protected A launchActivity(@Nullable Visitor<Intent> visitor) { 94 Log.d(mTag, "Launching " + mActivityClass.getSimpleName()); 95 96 final Intent intent = getLaunchIntent(); 97 if (visitor != null) { 98 visitor.visit(intent); 99 } 100 return getActivityTestRule().launchActivity(intent); 101 } 102 getLaunchIntent()103 private Intent getLaunchIntent() { 104 return new Intent(sContext, mActivityClass); 105 } 106 107 @NonNull startWatcher()108 protected ActivityWatcher startWatcher() { 109 return mActivitiesWatcher.watch(mActivityClass); 110 } 111 } 112