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.app.Instrumentation; 19 import android.support.test.uiautomator.By; 20 import android.support.test.uiautomator.BySelector; 21 import android.support.test.uiautomator.Direction; 22 import android.support.test.uiautomator.UiDevice; 23 import android.support.test.uiautomator.UiObject2; 24 import android.support.test.uiautomator.Until; 25 import android.system.helpers.CommandsHelper; 26 27 import junit.framework.Assert; 28 29 public class AutoLauncherStrategy implements IAutoLauncherStrategy { 30 31 private static final String LOG_TAG = AutoLauncherStrategy.class.getSimpleName(); 32 private static final String CAR_LENSPICKER = "com.android.car.carlauncher"; 33 34 private static final long APP_INIT_WAIT = 10000; 35 private static final int OPEN_FACET_RETRY_TIME = 5; 36 37 //todo: Remove x and y axis and use resource ID's. 38 private static final int FACET_APPS = 560; 39 private static final int MAP_FACET = 250; 40 41 private static final BySelector R_ID_LENSPICKER_PAGE_DOWN = 42 By.res(CAR_LENSPICKER, "page_down"); 43 private static final BySelector R_ID_LENSPICKER_LIST = 44 By.res(CAR_LENSPICKER, "list_view"); 45 46 protected UiDevice mDevice; 47 private Instrumentation mInstrumentation; 48 49 @Override 50 public String getSupportedLauncherPackage() { 51 return CAR_LENSPICKER; 52 } 53 54 @Override 55 public void setUiDevice(UiDevice uiDevice) { 56 mDevice = uiDevice; 57 } 58 59 @Override 60 public void setInstrumentation(Instrumentation instrumentation) { 61 mInstrumentation = instrumentation; 62 } 63 64 @Override 65 public void open() { 66 67 } 68 69 @Override 70 public void openDialFacet() { 71 throw new UnsupportedOperationException( 72 "The feature not supported on Auto"); 73 } 74 75 @Override 76 public void openMediaFacet(String appName) { 77 BySelector button = By.clickable(true).hasDescendant(By.text(appName)); 78 for (int tries = 3; tries >= 0; tries--) { 79 // TODO: Switch this to intents. It doesn't appear to work via intents on my system. 80 CommandsHelper.getInstance(mInstrumentation).executeShellCommand( 81 "am start -n com.android.support.car.lenspicker/.LensPickerActivity" 82 + " --esa categories android.intent.category.APP_MUSIC"); 83 mDevice.wait(Until.findObject(button), APP_INIT_WAIT); 84 if (mDevice.hasObject(button)) { 85 break; 86 } 87 } 88 UiObject2 choice = mDevice.findObject(button); 89 Assert.assertNotNull("Unable to find application " + appName, choice); 90 choice.click(); 91 mDevice.wait(Until.gone(button), APP_INIT_WAIT); 92 Assert.assertFalse("Failed to exit media menu.", mDevice.hasObject(button)); 93 mDevice.waitForIdle(APP_INIT_WAIT); 94 } 95 96 @Override 97 public void openSettingsFacet(String appName) { 98 throw new UnsupportedOperationException( 99 "The feature not supported on Auto"); 100 } 101 102 @Override 103 public void openMapsFacet(String appName) { 104 CommandsHelper.getInstance(mInstrumentation).executeShellCommand( 105 "input tap " + MAP_FACET + " " + FACET_APPS); 106 } 107 108 @Override 109 public void openHomeFacet() { 110 UiDevice.getInstance(mInstrumentation).pressHome(); 111 } 112 113 public void openApp(String appName) { 114 do { 115 // TODO: Switch this to intents. It doesn't appear to work via intents on my system. 116 CommandsHelper.getInstance(mInstrumentation).executeShellCommand( 117 "am start -n com.android.car.carlauncher/.AppGridActivity"); 118 } 119 //R_ID_LENSPICKER_LIST to open app if scrollContainer not avilable. 120 while (!mDevice.hasObject(R_ID_LENSPICKER_PAGE_DOWN) 121 && !mDevice.hasObject(R_ID_LENSPICKER_LIST)); 122 123 UiObject2 scrollContainer = mDevice.findObject(R_ID_LENSPICKER_PAGE_DOWN); 124 125 if (scrollContainer != null) { 126 127 if (!mDevice.hasObject(By.text(appName))) { 128 do { 129 scrollContainer.scroll(Direction.DOWN, 1.0f); 130 } 131 while (!mDevice.hasObject(By.text(appName)) && scrollContainer.isEnabled()); 132 } 133 } 134 135 UiObject2 application = mDevice.wait(Until.findObject(By.text(appName)), APP_INIT_WAIT); 136 if (application != null) { 137 application.click(); 138 mDevice.waitForIdle(); 139 } else { 140 Assert.fail("Unable to find application " + appName); 141 } 142 } 143 144 @SuppressWarnings("unused") 145 @Override 146 public UiObject2 openAllApps(boolean reset) { 147 throw new UnsupportedOperationException( 148 "The feature not supported on Auto"); 149 } 150 151 @SuppressWarnings("unused") 152 @Override 153 public BySelector getAllAppsButtonSelector() { 154 throw new UnsupportedOperationException( 155 "The feature not supported on Auto"); 156 } 157 158 @SuppressWarnings("unused") 159 @Override 160 public BySelector getAllAppsSelector() { 161 throw new UnsupportedOperationException( 162 "The feature not supported on Auto"); 163 } 164 165 @SuppressWarnings("unused") 166 @Override 167 public Direction getAllAppsScrollDirection() { 168 throw new UnsupportedOperationException( 169 "The feature not supported on Auto"); 170 } 171 172 @SuppressWarnings("unused") 173 @Override 174 public UiObject2 openAllWidgets(boolean reset) { 175 throw new UnsupportedOperationException( 176 "The feature not supported on Auto"); 177 } 178 179 @SuppressWarnings("unused") 180 @Override 181 public BySelector getAllWidgetsSelector() { 182 throw new UnsupportedOperationException( 183 "The feature not supported on Auto"); 184 } 185 186 @SuppressWarnings("unused") 187 @Override 188 public Direction getAllWidgetsScrollDirection() { 189 throw new UnsupportedOperationException( 190 "The feature not supported on Auto"); 191 } 192 193 @SuppressWarnings("unused") 194 @Override 195 public BySelector getWorkspaceSelector() { 196 throw new UnsupportedOperationException( 197 "The feature not supported on Auto"); 198 } 199 200 @SuppressWarnings("unused") 201 @Override 202 public BySelector getHotSeatSelector() { 203 throw new UnsupportedOperationException( 204 "The feature not supported on Auto"); 205 } 206 207 @SuppressWarnings("unused") 208 @Override 209 public Direction getWorkspaceScrollDirection() { 210 throw new UnsupportedOperationException( 211 "The feature not supported on Auto"); 212 } 213 214 @SuppressWarnings("unused") 215 @Override 216 public long launch(String appName, String packageName) { 217 return 0; 218 } 219 } 220