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