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 39 public void setUiDevice(UiDevice uiDevice) { 40 mDevice = uiDevice; 41 } 42 43 /** 44 * {@inheritDoc} 45 */ 46 @Override 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 */ 76 protected void dismissHomeScreenCling() { 77 // empty default implementation 78 } 79 80 /** 81 * {@inheritDoc} 82 */ 83 @Override 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 UiObject2 allAppsButton = 90 mDevice.wait(Until.findObject(getAllAppsButtonSelector()), 2000); 91 Assert.assertNotNull("openAllApps: did not find open all apps button."); 92 allAppsButton.click(); 93 // wait until hotseat disappears, so that we know that we are no longer on home screen 94 mDevice.wait(Until.gone(getHotSeatSelector()), 2000); 95 mDevice.waitForIdle(); 96 } 97 UiObject2 allAppsContainer = mDevice.wait(Until.findObject(getAllAppsSelector()), 2000); 98 Assert.assertNotNull("openAllApps: did not find all apps container", allAppsContainer); 99 if (reset) { 100 CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning( 101 allAppsContainer, Direction.reverse(getAllAppsScrollDirection())); 102 } 103 return allAppsContainer; 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 @Override 110 public Direction getAllAppsScrollDirection() { 111 return Direction.DOWN; 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public UiObject2 openAllWidgets(boolean reset) { 119 if (!mDevice.hasObject(getAllWidgetsSelector())) { 120 open(); 121 // trigger the wallpapers/widgets/settings view 122 mDevice.pressMenu(); 123 mDevice.waitForIdle(); 124 mDevice.findObject(By.res(getSupportedLauncherPackage(), "widget_button")).click(); 125 } 126 UiObject2 allWidgetsContainer = mDevice.wait( 127 Until.findObject(getAllWidgetsSelector()), 2000); 128 Assert.assertNotNull("openAllWidgets: did not find all widgets container", 129 allWidgetsContainer); 130 if (reset) { 131 CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning( 132 allWidgetsContainer, Direction.reverse(getAllWidgetsScrollDirection())); 133 } 134 return allWidgetsContainer; 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 @Override 141 public Direction getAllWidgetsScrollDirection() { 142 return Direction.DOWN; 143 } 144 145 /** 146 * {@inheritDoc} 147 */ 148 @Override 149 public long launch(String appName, String packageName) { 150 BySelector app = By.res( 151 getSupportedLauncherPackage(), "icon").clazz(TextView.class).desc(appName); 152 return CommonLauncherHelper.getInstance(mDevice).launchApp(this, app, packageName); 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public BySelector getAllAppsSelector() { 160 return By.res(getSupportedLauncherPackage(), "apps_list_view"); 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 @Override 167 public BySelector getAllWidgetsSelector() { 168 return By.res(getSupportedLauncherPackage(), "widgets_list_view"); 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override 175 public BySelector getWorkspaceSelector() { 176 return By.res(getSupportedLauncherPackage(), "workspace"); 177 } 178 179 /** 180 * {@inheritDoc} 181 */ 182 @Override 183 public BySelector getHotSeatSelector() { 184 return By.res(getSupportedLauncherPackage(), "hotseat"); 185 } 186 187 /** 188 * {@inheritDoc} 189 */ 190 @Override 191 public Direction getWorkspaceScrollDirection() { 192 return Direction.RIGHT; 193 } 194 } 195