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 package android.support.test.launcherhelper;
17 
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 
21 import android.support.test.uiautomator.By;
22 import android.support.test.uiautomator.BySelector;
23 import android.support.test.uiautomator.Direction;
24 import android.support.test.uiautomator.UiDevice;
25 import android.support.test.uiautomator.UiObject2;
26 import android.support.test.uiautomator.Until;
27 import android.util.Log;
28 import android.widget.TextView;
29 import junit.framework.Assert;
30 
31 public abstract class BaseLauncher3Strategy implements ILauncherStrategy {
32     private static final String LOG_TAG = BaseLauncher3Strategy.class.getSimpleName();
33     protected UiDevice mDevice;
34 
35     /**
36      * {@inheritDoc}
37      */
38     @Override
setUiDevice(UiDevice uiDevice)39     public void setUiDevice(UiDevice uiDevice) {
40         mDevice = uiDevice;
41     }
42 
43     /**
44      * {@inheritDoc}
45      */
46     @Override
open()47     public void open() {
48         // if we see hotseat, assume at home screen already
49         if (!mDevice.hasObject(getHotSeatSelector())) {
50             mDevice.pressHome();
51             // ensure launcher is shown
52             if (!mDevice.wait(Until.hasObject(getHotSeatSelector()), 5000)) {
53                 // HACK: dump hierarchy to logcat
54                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
55                 try {
56                     mDevice.dumpWindowHierarchy(baos);
57                     baos.flush();
58                     baos.close();
59                     String[] lines = baos.toString().split("\\r?\\n");
60                     for (String line : lines) {
61                         Log.d(LOG_TAG, line.trim());
62                     }
63                 } catch (IOException ioe) {
64                     Log.e(LOG_TAG, "error dumping XML to logcat", ioe);
65                 }
66                 Assert.fail("Failed to open launcher");
67             }
68             mDevice.waitForIdle();
69         }
70         dismissHomeScreenCling();
71     }
72 
73     /**
74      * Checks and dismisses home screen cling
75      */
dismissHomeScreenCling()76     protected void dismissHomeScreenCling() {
77         // empty default implementation
78     }
79 
80     /**
81      * {@inheritDoc}
82      */
83     @Override
openAllApps(boolean reset)84     public UiObject2 openAllApps(boolean reset) {
85         // if we see all apps container, skip the opening step
86         if (!mDevice.hasObject(getAllAppsSelector())) {
87             open();
88             // taps on the "apps" button at the bottom of the screen
89             mDevice.findObject(By.desc("Apps")).click();
90             // wait until hotseat disappears, so that we know that we are no longer on home screen
91             mDevice.wait(Until.gone(getHotSeatSelector()), 2000);
92             mDevice.waitForIdle();
93         }
94         UiObject2 allAppsContainer = mDevice.wait(Until.findObject(getAllAppsSelector()), 2000);
95         Assert.assertNotNull("openAllApps: did not find all apps container", allAppsContainer);
96         if (reset) {
97             CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning(
98                     allAppsContainer, Direction.reverse(getAllAppsScrollDirection()));
99         }
100         return allAppsContainer;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
getAllAppsScrollDirection()107     public Direction getAllAppsScrollDirection() {
108         return Direction.DOWN;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
openAllWidgets(boolean reset)115     public UiObject2 openAllWidgets(boolean reset) {
116         if (!mDevice.hasObject(getAllWidgetsSelector())) {
117             open();
118             // trigger the wallpapers/widgets/settings view
119             mDevice.pressMenu();
120             mDevice.waitForIdle();
121             mDevice.findObject(By.res(getSupportedLauncherPackage(), "widget_button")).click();
122         }
123         UiObject2 allWidgetsContainer = mDevice.wait(
124                 Until.findObject(getAllWidgetsSelector()), 2000);
125         Assert.assertNotNull("openAllWidgets: did not find all widgets container",
126                 allWidgetsContainer);
127         if (reset) {
128             CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning(
129                     allWidgetsContainer, Direction.reverse(getAllWidgetsScrollDirection()));
130         }
131         return allWidgetsContainer;
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
getAllWidgetsScrollDirection()138     public Direction getAllWidgetsScrollDirection() {
139         return Direction.DOWN;
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
launch(String appName, String packageName)146     public long launch(String appName, String packageName) {
147         BySelector app = By.res(
148                 getSupportedLauncherPackage(), "icon").clazz(TextView.class).desc(appName);
149         return CommonLauncherHelper.getInstance(mDevice).launchApp(this, app, packageName);
150     }
151 
152     /**
153      * {@inheritDoc}
154      */
155     @Override
getAllAppsSelector()156     public BySelector getAllAppsSelector() {
157         return By.res(getSupportedLauncherPackage(), "apps_list_view");
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
getAllWidgetsSelector()164     public BySelector getAllWidgetsSelector() {
165         return By.res(getSupportedLauncherPackage(), "widgets_list_view");
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
getWorkspaceSelector()172     public BySelector getWorkspaceSelector() {
173         return By.res(getSupportedLauncherPackage(), "workspace");
174     }
175 
176     /**
177      * {@inheritDoc}
178      */
179     @Override
getHotSeatSelector()180     public BySelector getHotSeatSelector() {
181         return By.res(getSupportedLauncherPackage(), "hotseat");
182     }
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
getWorkspaceScrollDirection()188     public Direction getWorkspaceScrollDirection() {
189         return Direction.RIGHT;
190     }
191 }
192