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.InstrumentationRegistry; 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 28 import com.android.launcher3.tapl.LauncherInstrumentation; 29 30 import junit.framework.Assert; 31 32 import java.io.IOException; 33 34 /** 35 * Implementation of {@link ILauncherStrategy} to support Nexus launcher 36 */ 37 public class NexusLauncherStrategy extends BaseLauncher3Strategy { 38 39 private static final String LAUNCHER_PKG = "com.google.android.apps.nexuslauncher"; 40 private LauncherInstrumentation mLauncher; 41 42 @Override setUiDevice(UiDevice uiDevice)43 public void setUiDevice(UiDevice uiDevice) { 44 super.setUiDevice(uiDevice); 45 try { 46 uiDevice.executeShellCommand( 47 "settings put secure swipe_up_to_switch_apps_enabled " 48 + (isPixel2OrAbove() ? 1 : 0)); 49 } catch (IOException e) { 50 Assert.fail("Failed to set swipe_up_to_switch_apps_enabled, caused by: " + e); 51 } 52 try { 53 mLauncher = new LauncherInstrumentation(InstrumentationRegistry.getInstrumentation()); 54 55 } catch (IllegalStateException e) { 56 mLauncher = 57 new LauncherInstrumentation( 58 androidx.test.InstrumentationRegistry.getInstrumentation()); 59 } 60 } 61 62 @Override getSupportedLauncherPackage()63 public String getSupportedLauncherPackage() { 64 return LAUNCHER_PKG; 65 } 66 67 /** 68 * {@inheritDoc} 69 */ 70 @Override getAllAppsSelector()71 public BySelector getAllAppsSelector() { 72 return By.res(getSupportedLauncherPackage(), "apps_view"); 73 } 74 75 /** 76 * {@inheritDoc} 77 */ 78 @Override getAllAppsButtonSelector()79 public BySelector getAllAppsButtonSelector() { 80 throw new UnsupportedOperationException("UI element no longer exists."); 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 @Override getHotSeatSelector()87 public BySelector getHotSeatSelector() { 88 return By.res(getSupportedLauncherPackage(), "hotseat"); 89 } 90 getLauncherOverviewSelector()91 private BySelector getLauncherOverviewSelector() { 92 return By.res(LAUNCHER_PKG, "overview_panel"); 93 } 94 95 @Override open()96 public void open() { 97 mDevice.pressHome(); 98 } 99 100 /** 101 * {@inheritDoc} 102 */ 103 @Override openAllApps(boolean reset)104 public UiObject2 openAllApps(boolean reset) { 105 // If not on all apps or to reset, then go to launcher and re-open all apps. 106 if (!mDevice.hasObject(getAllAppsSelector()) || mDevice.hasObject( 107 getLauncherOverviewSelector()) || reset) { 108 // Restart from the launcher home screen. 109 open(); 110 mDevice.waitForIdle(); 111 Assert.assertTrue("openAllApps: can't go to home screen", 112 !mDevice.hasObject(getAllAppsSelector()) && !mDevice.hasObject( 113 getLauncherOverviewSelector())); 114 if (isPixel2OrAbove()) { 115 int midX = mDevice.getDisplayWidth() / 2; 116 int height = mDevice.getDisplayHeight(); 117 // Swipe from 6/7ths down the screen to 1/7th down the screen. 118 mDevice.swipe( 119 midX, 120 height * 6 / 7, 121 midX, 122 height / 7, 123 (height * 2 / 3) / 100); // 100 px/step 124 } else { 125 // Swipe from the hotseat to near the top, e.g. 10% of the screen. 126 UiObject2 hotseat = mDevice.wait(Until.findObject(getHotSeatSelector()), 2500); 127 Point start = hotseat.getVisibleCenter(); 128 int endY = (int) (mDevice.getDisplayHeight() * 0.1f); 129 mDevice.swipe( 130 start.x, 131 start.y, 132 start.x, 133 endY, 134 (start.y - endY) / 100); // 100 px/step 135 } 136 } 137 UiObject2 allAppsContainer = mDevice.wait(Until.findObject(getAllAppsSelector()), 2500); 138 Assert.assertNotNull("openAllApps: did not find all apps container", allAppsContainer); 139 return allAppsContainer; 140 } 141 isPixel2OrAbove()142 private boolean isPixel2OrAbove() { 143 return Build.VERSION.FIRST_SDK_INT >= Build.VERSION_CODES.O; 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 @Override openAllWidgets(boolean reset)150 public UiObject2 openAllWidgets(boolean reset) { 151 if (!mDevice.hasObject(getAllWidgetsSelector())) { 152 open(); 153 // trigger the wallpapers/widgets/settings view 154 mDevice.pressMenu(); 155 mDevice.waitForIdle(); 156 UiObject2 optionsPopup = mDevice.findObject( 157 By.res(getSupportedLauncherPackage(), "deep_shortcuts_container")); 158 optionsPopup.findObject(By.text("Widgets")).click(); 159 } 160 UiObject2 allWidgetsContainer = mDevice.wait( 161 Until.findObject(getAllWidgetsSelector()), 2000); 162 Assert.assertNotNull("openAllWidgets: did not find all widgets container", 163 allWidgetsContainer); 164 if (reset) { 165 CommonLauncherHelper.getInstance(mDevice).scrollBackToBeginning( 166 allWidgetsContainer, Direction.reverse(getAllWidgetsScrollDirection())); 167 } 168 return allWidgetsContainer; 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override launch(String appName, String packageName)175 public long launch(String appName, String packageName) { 176 return CommonLauncherHelper.getInstance(mDevice).launchApp(mLauncher, appName, packageName); 177 } 178 } 179