• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.system.helpers;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.RemoteException;
22 import android.support.test.InstrumentationRegistry;
23 import android.support.test.uiautomator.By;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiScrollable;
26 import android.support.test.uiautomator.UiSelector;
27 import android.support.test.uiautomator.Until;
28 import android.util.Log;
29 import android.view.KeyEvent;
30 
31 import junit.framework.Assert;
32 
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 
36 /**
37  * Implement common helper methods for activities.
38  */
39 public class ActivityHelper {
40     private static final String TAG = ActivityHelper.class.getSimpleName();
41 
42     public static final String SYSTEMUI_PACKAGE = "com.android.systemui";
43     public static final int FULLSCREEN = 1;
44     public static final int SPLITSCREEN = 3;
45     public static final int TIMEOUT = 1000;
46     public static final int INVALID_TASK_ID = -1;
47 
48     private static ActivityHelper sInstance = null;
49     private Context mContext = null;
50     private UiDevice mDevice = null;
51 
ActivityHelper()52     public ActivityHelper() {
53         mContext = InstrumentationRegistry.getTargetContext();
54         mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
55     }
56 
getInstance()57     public static ActivityHelper getInstance() {
58         if (sInstance == null) {
59             sInstance = new ActivityHelper();
60         }
61         return sInstance;
62     }
63 
64     /**
65      * Gets task id for an activity
66      *
67      * @param pkgName
68      * @param activityName
69      * @return taskId or -1 when no activity is found
70      */
getTaskIdForActivity(String pkgName, String activityName)71     public int getTaskIdForActivity(String pkgName, String activityName) {
72         int taskId = INVALID_TASK_ID;
73         // Find task id for given package and activity
74         final Pattern TASK_REGEX = Pattern.compile(
75                 String.format("taskId=(\\d+): %s/%s", pkgName, activityName));
76         Matcher matcher = TASK_REGEX.matcher(CommandsHelper.execute("am stack list"));
77         if (matcher.find()) {
78             taskId = Integer.parseInt(matcher.group(1));
79             Log.i(TAG, String.format("TaskId found: %d for %s/%s",
80                     taskId, pkgName, activityName));
81         }
82         Assert.assertTrue("Taskid hasn't been found", taskId != -1);
83         return taskId;
84     }
85 
86     /**
87      * Helper to change window mode between fullscreen and splitscreen for a given task
88      *
89      * @param taskId
90      * @param mode
91      * @throws InterruptedException
92      */
changeWindowMode(int taskId, int mode)93     public void changeWindowMode(int taskId, int mode) throws InterruptedException {
94         CommandsHelper.execute(
95                 String.format("am stack move-task %d %d true", taskId, mode));
96         Thread.sleep(TIMEOUT);
97     }
98 
99     /**
100      * Clears apps in overview/recents
101      *
102      * @throws InterruptedException
103      * @throws RemoteException
104      */
clearRecents()105     public void clearRecents() throws InterruptedException, RemoteException {
106         // Launch recents if it's not already
107         int retry = 5;
108         while (!mDevice.wait(Until.hasObject(By.res(SYSTEMUI_PACKAGE, "recents_view")),
109                 TIMEOUT * 5) && --retry > 0) {
110             mDevice.pressRecentApps();
111             Thread.sleep(TIMEOUT);
112         }
113         // Return if there is no apps in recents
114         if (mDevice.wait(Until.hasObject(By.text("No recent items")), TIMEOUT * 5)) {
115             return;
116         } else {
117             Assert.assertTrue("Device expects recent items", mDevice.wait(Until.hasObject(
118                     By.res(SYSTEMUI_PACKAGE, "recents_view")), TIMEOUT * 5));
119         }
120         // Get recents items
121         int recents = mDevice.wait(Until.findObjects(
122                 By.res(SYSTEMUI_PACKAGE, "task_view_thumbnail")), TIMEOUT * 5).size();
123         // Clear recents
124         for (int i = 0; i < recents; ++i) {
125             mDevice.pressKeyCode(KeyEvent.KEYCODE_APP_SWITCH);
126             Thread.sleep(TIMEOUT);
127             mDevice.pressKeyCode(KeyEvent.KEYCODE_DEL);
128             Thread.sleep(TIMEOUT);
129         }
130     }
131 
132     /**
133      * Clear recent apps by click 'CLEAR ALL' button in the recents view.
134      *
135      * @throws Exception
136      */
clearRecentsByClearAll()137     public void clearRecentsByClearAll() throws Exception {
138         int retry = 5;
139         while (!mDevice.wait(Until.hasObject(By.res(SYSTEMUI_PACKAGE, "recents_view")),
140                 TIMEOUT * 5) && --retry > 0) {
141             mDevice.pressRecentApps();
142             Thread.sleep(TIMEOUT);
143         }
144         int maxTries = 20;
145         while (!mDevice.hasObject(By.text("No recent items")) && maxTries-- >= 0) {
146             UiScrollable thumbnailScrollable = new UiScrollable(
147                     new UiSelector().className("android.widget.ScrollView"));
148             thumbnailScrollable.scrollToBeginning(100);
149             if (!mDevice.wait(Until.hasObject(By.text("CLEAR ALL")), TIMEOUT * 2)) {
150                 continue;
151             } else {
152                 int tries = 3;
153                 while (mDevice.hasObject(By.text("CLEAR ALL")) && tries-- > 0) {
154                     mDevice.findObject(By.text("CLEAR ALL")).click();
155                     Thread.sleep(TIMEOUT * 2);
156                 }
157                 break;
158             }
159         }
160     }
161 
162     /**
163      * Enable/disable bmgr service
164      *
165      * @param enable true to enable, false to disable
166      */
enableBmgr(boolean enable)167     public void enableBmgr(boolean enable) {
168         String output = CommandsHelper.execute("bmgr enable " + Boolean.toString(enable));
169         if (enable) {
170             Assert.assertTrue("Bmgr not enabled",
171                     output.indexOf("Backup Manager now enabled") >= 0);
172         } else {
173             Assert.assertTrue("Bmgr not disabled",
174                     output.indexOf("Backup Manager now disabled") >= 0);
175         }
176     }
177 
178     /**
179      * Launch an intent when intent is of string type
180      *
181      * @param intentName
182      * @throws InterruptedException
183      */
launchIntent(String intentName)184     public void launchIntent(String intentName) throws InterruptedException {
185         mDevice.pressHome();
186         Intent intent = new Intent(intentName);
187         launchIntent(intent);
188     }
189 
190     /**
191      * Find intent of a package and launch
192      *
193      * @param pkgName
194      * @throws InterruptedException
195      */
launchPackage(String pkgName)196     public void launchPackage(String pkgName) throws InterruptedException {
197         Intent pkgIntent = mContext.getPackageManager()
198                 .getLaunchIntentForPackage(pkgName);
199         launchIntent(pkgIntent);
200     }
201 
202     /**
203      * launch an intent when intent is of Intent type
204      *
205      * @param intent
206      * @throws InterruptedException
207      */
launchIntent(Intent intent)208     public void launchIntent(Intent intent) throws InterruptedException {
209         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
210         mContext.startActivity(intent);
211         Thread.sleep(TIMEOUT * 5);
212     }
213 }
214