• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.content.Intent;
22 import android.os.Bundle;
23 import android.support.test.InstrumentationRegistry;
24 
25 import com.android.launcher3.Launcher;
26 import com.android.launcher3.Workspace.ItemOperator;
27 
28 import org.junit.rules.TestRule;
29 import org.junit.runner.Description;
30 import org.junit.runners.model.Statement;
31 
32 import java.util.concurrent.Callable;
33 
34 /**
35  * Test rule to get the current Launcher activity.
36  */
37 public class LauncherActivityRule implements TestRule {
38 
39     private Launcher mActivity;
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 Launcher getActivity() {
47         return mActivity;
48     }
49 
itemExists(final ItemOperator op)50     public Callable<Boolean> itemExists(final ItemOperator op) {
51         return new Callable<Boolean>() {
52 
53             @Override
54             public Boolean call() throws Exception {
55                 Launcher launcher = getActivity();
56                 if (launcher == null) {
57                     return false;
58                 }
59                 return launcher.getWorkspace().getFirstMatch(op) != null;
60             }
61         };
62     }
63 
64     /**
65      * Starts the launcher activity in the target package.
66      */
67     public void startLauncher() {
68         InstrumentationRegistry.getInstrumentation().startActivitySync(getHomeIntent());
69     }
70 
71     public void returnToHome() {
72         InstrumentationRegistry.getTargetContext().startActivity(getHomeIntent());
73         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
74     }
75 
76     public static Intent getHomeIntent() {
77         return new Intent(Intent.ACTION_MAIN)
78                 .addCategory(Intent.CATEGORY_HOME)
79                 .setPackage(InstrumentationRegistry.getTargetContext().getPackageName())
80                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
81     }
82 
83     private class MyStatement extends Statement implements ActivityLifecycleCallbacks {
84 
85         private final Statement mBase;
86 
87         public MyStatement(Statement base) {
88             mBase = base;
89         }
90 
91         @Override
92         public void evaluate() throws Throwable {
93             Application app = (Application)
94                     InstrumentationRegistry.getTargetContext().getApplicationContext();
95             app.registerActivityLifecycleCallbacks(this);
96             try {
97                 mBase.evaluate();
98             } finally {
99                 app.unregisterActivityLifecycleCallbacks(this);
100             }
101         }
102 
103         @Override
104         public void onActivityCreated(Activity activity, Bundle bundle) {
105             if (activity instanceof Launcher) {
106                 mActivity = (Launcher) activity;
107             }
108         }
109 
110         @Override
111         public void onActivityStarted(Activity activity) { }
112 
113         @Override
114         public void onActivityResumed(Activity activity) { }
115 
116         @Override
117         public void onActivityPaused(Activity activity) { }
118 
119         @Override
120         public void onActivityStopped(Activity activity) { }
121 
122         @Override
123         public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { }
124 
125         @Override
126         public void onActivityDestroyed(Activity activity) {
127             if (activity == mActivity) {
128                 mActivity = null;
129             }
130         }
131     }
132 }
133