1 /* 2 * Copyright (C) 2015 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.support.test.uiautomator.By; 19 import android.support.test.uiautomator.BySelector; 20 import android.support.test.uiautomator.Direction; 21 import android.support.test.uiautomator.UiDevice; 22 import android.support.test.uiautomator.UiObject2; 23 import android.support.test.uiautomator.UiObjectNotFoundException; 24 import android.support.test.uiautomator.Until; 25 import android.util.Log; 26 import android.widget.TextView; 27 28 import junit.framework.Assert; 29 30 import java.io.ByteArrayOutputStream; 31 import java.io.IOException; 32 33 /** 34 * Implementation of {@link ILauncherStrategy} to support Google experience launcher 35 */ 36 public class GoogleExperienceLauncherStrategy implements ILauncherStrategy { 37 38 private static final String LOG_TAG = GoogleExperienceLauncherStrategy.class.getSimpleName(); 39 private static final String LAUNCHER_PKG = "com.google.android.googlequicksearchbox"; 40 private static final BySelector APPS_CONTAINER = By.res(LAUNCHER_PKG, "all_apps_container"); 41 private static final BySelector WIDGETS_CONTAINER = By.res(LAUNCHER_PKG, "widgets_list_view"); 42 private static final BySelector WORKSPACE = By.res(LAUNCHER_PKG, "workspace"); 43 private static final BySelector HOTSEAT = By.res(LAUNCHER_PKG, "hotseat"); 44 private UiDevice mDevice; 45 46 /** 47 * {@inheritDoc} 48 */ 49 @Override 50 public void setUiDevice(UiDevice uiDevice) { 51 mDevice = uiDevice; 52 } 53 54 /** 55 * {@inheritDoc} 56 */ 57 @Override 58 public void open() throws UiObjectNotFoundException { 59 // if we see hotseat, assume at home screen already 60 if (!mDevice.hasObject(HOTSEAT)) { 61 mDevice.pressHome(); 62 // ensure launcher is shown 63 if (!mDevice.wait(Until.hasObject(By.res(LAUNCHER_PKG, "hotseat")), 5000)) { 64 // HACK: dump hierarchy to logcat 65 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 66 try { 67 mDevice.dumpWindowHierarchy(baos); 68 Log.d(LOG_TAG, baos.toString()); 69 baos.flush(); 70 baos.close(); 71 } catch (IOException ioe) { 72 Log.e(LOG_TAG, "error dumping XML to logcat", ioe); 73 } 74 Assert.fail("Failed to open launcher"); 75 } 76 mDevice.waitForIdle(); 77 } 78 } 79 80 /** 81 * {@inheritDoc} 82 */ 83 @Override 84 public UiObject2 openAllApps(boolean reset) throws UiObjectNotFoundException { 85 // if we see all apps container, skip the opening step 86 if (!mDevice.hasObject(APPS_CONTAINER)) { 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(HOTSEAT), 2000); 92 mDevice.waitForIdle(); 93 } 94 UiObject2 allAppsContainer = mDevice.wait(Until.findObject(APPS_CONTAINER), 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 107 public Direction getAllAppsScrollDirection() { 108 return Direction.DOWN; 109 } 110 111 /** 112 * {@inheritDoc} 113 */ 114 @Override 115 public UiObject2 openAllWidgets(boolean reset) throws UiObjectNotFoundException { 116 if (!mDevice.hasObject(WIDGETS_CONTAINER)) { 117 open(); 118 // trigger the wallpapers/widgets/settings view 119 mDevice.pressMenu(); 120 mDevice.waitForIdle(); 121 mDevice.findObject(By.res(LAUNCHER_PKG, "widget_button")).click(); 122 } 123 UiObject2 allWidgetsContainer = mDevice.wait(Until.findObject(WIDGETS_CONTAINER), 2000); 124 Assert.assertNotNull("openAllWidgets: did not find all widgets container", 125 allWidgetsContainer); 126 if (reset) { 127 CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning( 128 allWidgetsContainer, Direction.reverse(getAllWidgetsScrollDirection())); 129 } 130 return allWidgetsContainer; 131 } 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override 137 public Direction getAllWidgetsScrollDirection() { 138 return Direction.DOWN; 139 } 140 141 /** 142 * {@inheritDoc} 143 */ 144 @Override 145 public boolean launch(String appName, String packageName) throws UiObjectNotFoundException { 146 BySelector app = By.res(LAUNCHER_PKG, "icon").clazz(TextView.class).desc(appName); 147 return CommonLauncherHelper.getInstance(mDevice).launchApp(this, app, packageName); 148 } 149 150 /** 151 * {@inheritDoc} 152 */ 153 @Override 154 public String getSupportedLauncherPackage() { 155 return LAUNCHER_PKG; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override 162 public BySelector getAllAppsSelector() { 163 return APPS_CONTAINER; 164 } 165 166 /** 167 * {@inheritDoc} 168 */ 169 @Override 170 public BySelector getAllWidgetsSelector() { 171 return WIDGETS_CONTAINER; 172 } 173 174 /** 175 * {@inheritDoc} 176 */ 177 @Override 178 public BySelector getWorkspaceSelector() { 179 return WORKSPACE; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public Direction getWorkspaceScrollDirection() { 187 return Direction.RIGHT; 188 } 189 } 190