• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.launcher3.util.rule;
17 
18 import android.app.Activity;
19 import android.app.Application;
20 import android.app.Application.ActivityLifecycleCallbacks;
21 import android.os.Bundle;
22 
23 import androidx.test.InstrumentationRegistry;
24 
25 import org.junit.rules.TestRule;
26 import org.junit.runner.Description;
27 import org.junit.runners.model.Statement;
28 
29 /**
30  * Test rule to get the current activity.
31  */
32 public class SimpleActivityRule<T extends Activity> implements TestRule {
33 
34     private final Class<T> mClass;
35     private T mActivity;
36 
SimpleActivityRule(Class<T> clazz)37     public SimpleActivityRule(Class<T> clazz) {
38         mClass = clazz;
39     }
40 
41     @Override
apply(Statement base, Description description)42     public Statement apply(Statement base, Description description) {
43         return new MyStatement(base);
44     }
45 
getActivity()46     public T getActivity() {
47         return mActivity;
48     }
49 
50     protected class MyStatement extends Statement implements ActivityLifecycleCallbacks {
51 
52         private final Statement mBase;
53 
MyStatement(Statement base)54         public MyStatement(Statement base) {
55             mBase = base;
56         }
57 
58         @Override
evaluate()59         public void evaluate() throws Throwable {
60             Application app = (Application)
61                     InstrumentationRegistry.getTargetContext().getApplicationContext();
62             app.registerActivityLifecycleCallbacks(this);
63             try {
64                 mBase.evaluate();
65             } finally {
66                 app.unregisterActivityLifecycleCallbacks(this);
67                 mActivity = null;
68             }
69         }
70 
71         @Override
onActivityCreated(Activity activity, Bundle bundle)72         public void onActivityCreated(Activity activity, Bundle bundle) {
73             if (activity != null && mClass.isInstance(activity)) {
74                 mActivity = (T) activity;
75             }
76         }
77 
78         @Override
onActivityStarted(Activity activity)79         public void onActivityStarted(Activity activity) {
80         }
81 
82         @Override
onActivityResumed(Activity activity)83         public void onActivityResumed(Activity activity) {
84         }
85 
86         @Override
onActivityPaused(Activity activity)87         public void onActivityPaused(Activity activity) {
88         }
89 
90         @Override
onActivityStopped(Activity activity)91         public void onActivityStopped(Activity activity) {
92         }
93 
94         @Override
onActivitySaveInstanceState(Activity activity, Bundle bundle)95         public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
96         }
97 
98         @Override
onActivityDestroyed(Activity activity)99         public void onActivityDestroyed(Activity activity) {
100             if (activity == mActivity) {
101                 mActivity = null;
102             }
103         }
104     }
105 }
106