diff options
8 files changed, 144 insertions, 213 deletions
diff --git a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/NetworkIOTest.java b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/NetworkIOTest.java index 279a92bc..6e51ffed 100644 --- a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/NetworkIOTest.java +++ b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/NetworkIOTest.java @@ -21,6 +21,7 @@ import com.android.devtools.systemimage.uitest.common.Res; import com.android.devtools.systemimage.uitest.framework.SystemImageTestFramework; import com.android.devtools.systemimage.uitest.utils.AppLauncher; import com.android.devtools.systemimage.uitest.utils.NetworkUtil; +import com.android.devtools.systemimage.uitest.utils.SettingsUtil; import com.android.devtools.systemimage.uitest.utils.UiAutomatorPlus; import com.android.devtools.systemimage.uitest.utils.Wait; @@ -37,7 +38,7 @@ import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; -import android.support.test.uiautomator.UiScrollable; +import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.telephony.TelephonyManager; @@ -105,6 +106,19 @@ public class NetworkIOTest { } } + private UiObject2 navigateToDataSwitch(Instrumentation instrumentation) throws UiObjectNotFoundException { + String containerRes = (testFramework.getApi() >= 24) ? Res.NETWORK_SWITCHES_RECYCLER_VIEW_RES : + Res.NETWORK_SWITCHES_CONTAINER_RES; + + SettingsUtil.openItem(instrumentation, "Data usage"); + + return UiAutomatorPlus.findObjectByRelative( + instrumentation, + By.clazz("android.widget.Switch"), + By.text("Cellular data"), + By.res(containerRes)); + } + /** * Verifies cellular data can be toggled off. * <p> @@ -130,26 +144,19 @@ public class NetworkIOTest { final Instrumentation instrumentation = testFramework.getInstrumentation(); UiDevice device = UiDevice.getInstance(instrumentation); // TODO: Add a fixture method in AppLauncher class to launch a specified path. - if (testFramework.getApi() >= 23) { - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollToBeginning(100); - device.findObject(new UiSelector().textContains("Data usage")).click(); - - UiObject2 dataSwitch = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.Switch"), - By.text("Cellular data"), - By.res(Res.NETWORK_SWITCHES_CONTAINER_RES)); + int api = testFramework.getApi(); + if (api >= 23) { + UiObject2 dataSwitch = navigateToDataSwitch(instrumentation); + // Test requires "Cellular data" switch widget to start in the on state. if (!dataSwitch.isChecked()) { dataSwitch.click(); } // Disable "Cellular data" option. dataSwitch.click(); - device.findObject(new UiSelector().text("OK")).click(); + if (api < 24) { + device.findObject(new UiSelector().text("OK")).click(); + } // Wait for data connection to turn off. new Wait().until(new Wait.ExpectedCondition() { @Override @@ -161,9 +168,9 @@ public class NetworkIOTest { assertFalse("Cellular data is enabled.", NetworkUtil.hasCellularNetworkConnection(instrumentation)); - if (device.findObject( - new UiSelector().textContains("Set cellular data limit")).exists()) { - assertTrue("Set cellular data limit text not visible.", false); + if (api < 24) { + assertFalse("Set cellular data limit text is visible.", device.findObject( + new UiSelector().textContains("Set cellular data limit")).exists()); } // Enable Cellular data. dataSwitch.click(); @@ -195,22 +202,16 @@ public class NetworkIOTest { final Instrumentation instrumentation = testFramework.getInstrumentation(); UiDevice device = UiDevice.getInstance(instrumentation); // TODO: Add a fixture method in AppLauncher class to launch a specified path. - if (testFramework.getApi() >= 23) { - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollToBeginning(100); - device.findObject(new UiSelector().textContains("Data usage")).click(); - - UiObject2 dataSwitch = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.Switch"), - By.text("Cellular data"), - By.res(Res.NETWORK_SWITCHES_CONTAINER_RES)); + int api = testFramework.getApi(); + if (api >= 23) { + UiObject2 dataSwitch = navigateToDataSwitch(instrumentation); + // Test requires "Cellular data" switch widget to start in the off state. if (dataSwitch.isChecked()) { dataSwitch.click(); + if (api < 24) { + device.findObject(new UiSelector().text("OK")).click(); + } } // Enable Cellular data. dataSwitch.click(); @@ -225,9 +226,9 @@ public class NetworkIOTest { assertTrue("Cellular data is disabled.", NetworkUtil.hasCellularNetworkConnection(instrumentation)); - if (!device.findObject( - new UiSelector().textContains("Set cellular data limit")).exists()) { - assertTrue("Set cellular data limit text is visible.", false); + if (api < 24) { + assertTrue("Set cellular data limit text is not visible.", device.findObject( + new UiSelector().textContains("Set cellular data limit")).exists()); } } } diff --git a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/SettingsTest.java b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/SettingsTest.java index be689c96..9d0c2d80 100644 --- a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/SettingsTest.java +++ b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/SettingsTest.java @@ -19,8 +19,8 @@ package com.android.devtools.systemimage.uitest.smoke; import com.android.devtools.systemimage.uitest.annotations.TestInfo; import com.android.devtools.systemimage.uitest.common.Res; import com.android.devtools.systemimage.uitest.framework.SystemImageTestFramework; -import com.android.devtools.systemimage.uitest.utils.AppLauncher; import com.android.devtools.systemimage.uitest.utils.DeveloperOptionsManager; +import com.android.devtools.systemimage.uitest.utils.SettingsUtil; import com.android.devtools.systemimage.uitest.utils.UiAutomatorPlus; import com.android.devtools.systemimage.uitest.utils.Wait; @@ -33,17 +33,11 @@ import static org.junit.Assert.*; import android.app.Instrumentation; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.By; -import android.support.test.uiautomator.BySelector; import android.support.test.uiautomator.UiDevice; -import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiScrollable; import android.support.test.uiautomator.UiSelector; -import android.support.test.uiautomator.Until; - -import java.util.List; -import java.util.concurrent.TimeUnit; /** @@ -84,12 +78,7 @@ public class SettingsTest { return; } - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollIntoView(new UiSelector().textContains("Google")); - device.findObject(new UiSelector().textContains("Google")).clickAndWaitForNewWindow(); + SettingsUtil.openItem(instrumentation, "Google"); device.findObject(new UiSelector().textContains("Location")).clickAndWaitForNewWindow(); assertTrue("Failed to find Location title.", new Wait().until(new Wait.ExpectedCondition() { @@ -125,15 +114,7 @@ public class SettingsTest { UiDevice device = UiDevice.getInstance(instrumentation); if (testFramework.getApi() >= 23) { - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable settingsList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES) - ); - settingsList.setAsVerticalList(); - UiObject appsObject = settingsList.getChildByText( - new UiSelector().className("android.widget.TextView"), "Apps"); - appsObject.clickAndWaitForNewWindow(); + SettingsUtil.openItem(instrumentation, "Apps"); device.findObject(new UiSelector().resourceId(Res.SETTINGS_ADVANCED_OPTION_RES)) .clickAndWaitForNewWindow(); device.findObject(new UiSelector().textContains("App permissions")) @@ -149,6 +130,33 @@ public class SettingsTest { } /** + * Common code for finding a checkbox/switch in the Date & time settings. + */ + private UiObject2 navigateToDateTimeSwitch(String text) throws UiObjectNotFoundException { + final Instrumentation instrumentation = testFramework.getInstrumentation(); + SettingsUtil.openItem(instrumentation, "Date & time"); + + UiObject2 widget; + try { + final String listViewClass = ( + testFramework.getApi() >= 24) ? "android.support.v7.widget.RecyclerView" : + "android.widget.ListView"; + widget = UiAutomatorPlus.findObjectByRelative( + instrumentation, + By.clazz("android.widget.Switch"), + By.text(text), + By.clazz(listViewClass)); + } catch (UiObjectNotFoundException e) { + widget = UiAutomatorPlus.findObjectByRelative( + instrumentation, + By.clazz("android.widget.CheckBox"), + By.text(text), + By.clazz("android.widget.ListView")); + } + return widget; + } + + /** * Verifies set date and set time fields are editable. * <p> * This is run to qualify releases. Please involve the test team in substantial changes. @@ -170,33 +178,9 @@ public class SettingsTest { @TestInfo(id = "14581295") public void enableSetDateAndSetTime() throws Exception { int api = testFramework.getApi(); - Instrumentation instrumentation = testFramework.getInstrumentation(); final UiDevice device = testFramework.getDevice(); - AppLauncher.launch(instrumentation, "Settings"); - String scrollableRes = (api >= 24) ? Res.SETTINGS_RECYCLER_VIEW_RES : - Res.SETTINGS_LIST_CONTAINER_RES; - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(scrollableRes)); - itemList.setAsVerticalList(); - itemList.scrollIntoView(new UiSelector().textContains("Date & time")); - device.findObject(new UiSelector().text("Date & time")).click(); + final UiObject2 widget = navigateToDateTimeSwitch("Automatic date & time"); - UiObject2 widget; - try { - String listViewClass = (api >= 24) ? "android.support.v7.widget.RecyclerView" : - "android.widget.ListView"; - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.Switch"), - By.text("Automatic date & time"), - By.clazz(listViewClass)); - } catch (UiObjectNotFoundException e) { - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.CheckBox"), - By.text("Automatic date & time"), - By.clazz("android.widget.ListView")); - } // Test requires "Automatic date & time" widget to start in the enabled state. if (!widget.isChecked()) { widget.click(); @@ -305,12 +289,7 @@ public class SettingsTest { return; } - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollIntoView(new UiSelector().textContains("Google")); - device.findObject(new UiSelector().text("Google")).click(); + SettingsUtil.openItem(instrumentation, "Google"); device.findObject(new UiSelector().text("Search & Now")).click(); device.findObject(new UiSelector().text("Now cards")).click(); @@ -353,30 +332,9 @@ public class SettingsTest { @Test @TestInfo(id = "14581409") public void enableTimeZone() throws Exception { - Instrumentation instrumentation = testFramework.getInstrumentation(); final UiDevice device = testFramework.getDevice(); - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollIntoView(new UiSelector().textContains("Date & time")); - device.findObject(new UiSelector().text("Date & time")).click(); + final UiObject2 widget = navigateToDateTimeSwitch("Automatic time zone"); - UiObject2 widget; - try { - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.Switch"), - By.text("Automatic time zone"), - By.clazz("android.widget.ListView")); - } catch (UiObjectNotFoundException e) { - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.CheckBox"), - By.text("Automatic time zone"), - By.clazz("android.widget.ListView")); - } // Initialize automatic time zone option to enabled state. if (!widget.isChecked()) { widget.click(); @@ -432,30 +390,9 @@ public class SettingsTest { @Test @TestInfo(id = "14581410") public void enableTwentyFourHourFormat() throws Exception { - Instrumentation instrumentation = testFramework.getInstrumentation(); final UiDevice device = testFramework.getDevice(); - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - itemList.scrollIntoView(new UiSelector().textContains("Date & time")); - device.findObject(new UiSelector().text("Date & time")).click(); + final UiObject2 widget = navigateToDateTimeSwitch("Use 24-hour format"); - UiObject2 widget; - try { - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.Switch"), - By.text("Use 24-hour format"), - By.clazz("android.widget.ListView")); - } catch (UiObjectNotFoundException e) { - widget = UiAutomatorPlus.findObjectByRelative( - instrumentation, - By.clazz("android.widget.CheckBox"), - By.text("Use 24-hour format"), - By.clazz("android.widget.ListView")); - } // Initialize 24-hour format option to disabled state. if (widget.isChecked()) { widget.click(); diff --git a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/ShellUtilTest.java b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/ShellUtilTest.java index 7637156a..5f3b4798 100644 --- a/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/ShellUtilTest.java +++ b/system_image_uitests/app/src/androidTest/java/com/android/devtools/systemimage/uitest/smoke/ShellUtilTest.java @@ -17,16 +17,14 @@ package com.android.devtools.systemimage.uitest.smoke; import com.android.devtools.systemimage.uitest.annotations.TestInfo; -import com.android.devtools.systemimage.uitest.common.Res; import com.android.devtools.systemimage.uitest.framework.SystemImageTestFramework; +import com.android.devtools.systemimage.uitest.utils.SettingsUtil; import com.android.devtools.systemimage.uitest.utils.ShellUtil; -import com.android.devtools.systemimage.uitest.utils.AppLauncher; import com.android.devtools.systemimage.uitest.utils.DeveloperOptionsManager; import com.android.devtools.systemimage.uitest.utils.Wait; import org.hamcrest.Matchers; -import org.junit.After; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -37,7 +35,6 @@ import android.app.Instrumentation; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; -import android.support.test.uiautomator.UiScrollable; import android.support.test.uiautomator.UiSelector; import android.util.Log; @@ -121,6 +118,7 @@ public class ShellUtilTest { public void createBugReport() throws Exception { Instrumentation instrumentation = testFramework.getInstrumentation(); final UiDevice device = UiDevice.getInstance(instrumentation); + if (testFramework.getApi() >= 21) { deleteBugReportFiles(); @@ -128,20 +126,15 @@ public class ShellUtilTest { DeveloperOptionsManager.enableDeveloperOptions(testFramework.getInstrumentation()); } - AppLauncher.launch(instrumentation, "Settings"); - UiScrollable itemList = new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES)); - itemList.setAsVerticalList(); - UiObject item = itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), "Developer options"); - item.click(); + SettingsUtil.openItem(instrumentation, "Developer options"); // Remove bug report files even if the test fails. try { device.findObject( new UiSelector().text("Take bug report")).clickAndWaitForNewWindow(); - if (device.findObject(new UiSelector().text("Report")).exists()) { - device.findObject(new UiSelector().text("Report")).click(); + UiObject reportButton = device.findObject(new UiSelector().text("Report")); + if (reportButton.exists()) { + reportButton.click(); } boolean gotPngAndZip = new Wait( TimeUnit.MILLISECONDS.convert(30L, TimeUnit.SECONDS)).until( diff --git a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/common/Res.java b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/common/Res.java index 11762ec8..4bdc05d3 100644 --- a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/common/Res.java +++ b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/common/Res.java @@ -50,10 +50,12 @@ public class Res { public static final String APPS_LIST_CONTAINER_RES = "com.android.settings:id/list_container"; public static final String APPS_TAB_CONTAINER_RES = "com.android.settings:id/pager"; public static final String NETWORK_SWITCHES_CONTAINER_RES = - "com.android.settings:id/network_switches"; + "com.android.settings:id/network_switches"; // Removed in API 24 + public static final String NETWORK_SWITCHES_RECYCLER_VIEW_RES = + "com.android.settings:id/list"; // Added in API 24 public static final String SETTINGS_ADVANCED_OPTION_RES = "com.android.settings:id/advanced"; public static final String SETTINGS_LIST_CONTAINER_RES = - "(com.android.settings|android):id/(dashboard|list)"; + "(com.android.settings|android):id/(dashboard|list)"; // Still present in API 24 but at a different point in the hierarchy. public static final String SETTINGS_RECYCLER_VIEW_RES = "com.android.settings:id/dashboard_container"; // Added in API 24 diff --git a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AccountManager.java b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AccountManager.java index bfb3de9c..410dd289 100644 --- a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AccountManager.java +++ b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AccountManager.java @@ -24,7 +24,6 @@ import android.app.Instrumentation; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObjectNotFoundException; -import android.support.test.uiautomator.UiScrollable; import android.support.test.uiautomator.UiSelector; import java.io.BufferedReader; @@ -170,20 +169,6 @@ public class AccountManager { private static void openAccountList(Instrumentation instrumentation) throws UiObjectNotFoundException { - // Open Settings - AppLauncher.launch(instrumentation, "Settings"); - - // Find and click "Accounts" in Settings - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES) - ); - itemList.setAsVerticalList(); - UiObject item = - itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), - "Accounts" - ); - item.clickAndWaitForNewWindow(); + SettingsUtil.openItem(instrumentation, "Accounts"); } } diff --git a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AppManager.java b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AppManager.java index eeb7df61..7ed95892 100644 --- a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AppManager.java +++ b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/AppManager.java @@ -29,7 +29,6 @@ import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiScrollable; import android.support.test.uiautomator.UiSelector; -import android.util.Log; import java.io.File; import java.io.FileOutputStream; @@ -192,21 +191,7 @@ public class AppManager { */ public static void openAppList(Instrumentation instrumentation) throws UiObjectNotFoundException { - // Open settings - AppLauncher.launch(instrumentation, "Settings"); - - // Find and click "Apps" in Settings - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES) - ); - itemList.setAsVerticalList(); - UiObject item = - itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), - "Apps" - ); - item.clickAndWaitForNewWindow(); + SettingsUtil.openItem(instrumentation, "Apps"); } /** diff --git a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/DeveloperOptionsManager.java b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/DeveloperOptionsManager.java index 8d8a6286..4bc61968 100644 --- a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/DeveloperOptionsManager.java +++ b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/DeveloperOptionsManager.java @@ -42,33 +42,19 @@ public class DeveloperOptionsManager { */ public static void enableDeveloperOptions(Instrumentation instrumentation) throws UiObjectNotFoundException { - AppLauncher.launch(instrumentation, "Settings"); - - // Click "About phone". - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES) - ); - itemList.setAsVerticalList(); - UiObject item; try { - item = itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), - "About phone"); + SettingsUtil.openItem(instrumentation, "About phone"); } catch (UiObjectNotFoundException e) { - item = itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), - "About emulated device"); + SettingsUtil.openItem(instrumentation, "About emulated device"); } - item.clickAndWaitForNewWindow(); // Click "Build number" - itemList = + UiScrollable itemList = new UiScrollable( new UiSelector().resourceIdMatches(Res.ABOUT_PHONE_LIST_CONTAINER_RES) ); itemList.setAsVerticalList(); - item = + UiObject item = itemList.getChildByText( new UiSelector().className("android.widget.TextView"), "Build number" @@ -91,22 +77,6 @@ public class DeveloperOptionsManager { */ public static boolean isDeveloperOptionsEnabled(Instrumentation instrumentation) throws UiObjectNotFoundException { - AppLauncher.launch(instrumentation, "Settings"); - - // Look for "Developer options". - UiScrollable itemList = - new UiScrollable( - new UiSelector().resourceIdMatches(Res.SETTINGS_LIST_CONTAINER_RES) - ); - itemList.setAsVerticalList(); - try { - itemList.getChildByText( - new UiSelector().className("android.widget.TextView"), - "Developer options" - ); - return true; - } catch (UiObjectNotFoundException e) { - return false; - } + return SettingsUtil.scrollToItem(instrumentation, "Developer options"); } } diff --git a/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/SettingsUtil.java b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/SettingsUtil.java new file mode 100644 index 00000000..b9f2c333 --- /dev/null +++ b/system_image_uitests/app/src/main/java/com/android/devtools/systemimage/uitest/utils/SettingsUtil.java @@ -0,0 +1,58 @@ +package com.android.devtools.systemimage.uitest.utils; + +import android.app.Instrumentation; +import android.support.test.uiautomator.UiObject; +import android.support.test.uiautomator.UiObjectNotFoundException; +import android.support.test.uiautomator.UiScrollable; +import android.support.test.uiautomator.UiSelector; + +import com.android.devtools.systemimage.uitest.common.Res; + +public class SettingsUtil { + public static final String TAG = SettingsUtil.class.getName(); + + private SettingsUtil() { + throw new AssertionError(); + } + + /** + * Launches Settings and get the item list as a @{code UiScrollable}, ready to search for + * clickable items. + */ + public static UiScrollable launchAndGetItemList(Instrumentation instrumentation) throws UiObjectNotFoundException { + AppLauncher.launch(instrumentation, "Settings"); + + UiScrollable itemList = new UiScrollable(new UiSelector().resourceIdMatches( + Res.SETTINGS_LIST_CONTAINER_RES)); + if (!itemList.exists()) { + itemList = new UiScrollable(new UiSelector().resourceIdMatches( + Res.SETTINGS_RECYCLER_VIEW_RES)); + } + return itemList.setAsVerticalList(); + } + + /** + * Launches Settings and scroll to the item whose name contains the given text. Returns + * @{code true} iff the item is there. + */ + public static boolean scrollToItem(Instrumentation instrumentation, String text) throws UiObjectNotFoundException { + UiScrollable itemList = launchAndGetItemList(instrumentation); + return itemList.scrollIntoView(new UiSelector().textContains(text)); + } + + /** + * Launches Settings and find the item with the given name. Returns the item. + */ + public static UiObject findItem(Instrumentation instrumentation, String name) throws UiObjectNotFoundException { + UiScrollable itemList = launchAndGetItemList(instrumentation); + itemList.scrollIntoView(new UiSelector().textContains(name)); + return itemList.getChild(new UiSelector().text(name)); + } + + /** + * Launches Settings and launch the item with the given name. Returns the result of the click call. + */ + public static boolean openItem(Instrumentation instrumentation, String name) throws UiObjectNotFoundException { + return findItem(instrumentation, name).clickAndWaitForNewWindow(); + } +}
\ No newline at end of file |