public static void pressDpad(UiDevice uiDevice, Direction direction) { switch (direction) { case UP: uiDevice.pressDPadUp(); break; case DOWN: uiDevice.pressDPadDown(); break; case LEFT: uiDevice.pressDPadLeft(); break; case RIGHT: uiDevice.pressDPadRight(); break; default: throw new IllegalArgumentException(direction.toString()); } }
/** * Scrolls the {@param container} until it finds an object matching {@param condition}. * @return the matching object. */ protected UiObject2 scrollAndFind(UiObject2 container, BySelector condition) { do { UiObject2 widget = container.findObject(condition); if (widget != null) { return widget; } } while (container.scroll(Direction.DOWN, 1f)); return container.findObject(condition); }
public static boolean hasSiblingInDirection(UiObject2 theUiObject, Direction direction) { Point myCenter = theUiObject.getVisibleCenter(); for (UiObject2 sibling : theUiObject.getParent().getChildren()) { Point siblingCenter = sibling.getVisibleCenter(); switch (direction) { case UP: if (myCenter.y > siblingCenter.y) { return true; } break; case DOWN: if (myCenter.y < siblingCenter.y) { return true; } break; case LEFT: if (myCenter.x > siblingCenter.x) { return true; } break; case RIGHT: if (myCenter.x < siblingCenter.x) { return true; } break; default: throw new IllegalArgumentException(direction.toString()); } } return false; }
public UiObject2 assertNavigateToItem(String title) { BySelector sidePanelSelector = ByResource.id(mTargetResources, R.id.side_panel_list); UiObject2 sidePanelList = mUiDevice.findObject(sidePanelSelector); Assert.assertNotNull(sidePanelSelector + " not found", sidePanelList); return UiDeviceAsserts .assertNavigateTo(mUiDevice, sidePanelList, By.hasDescendant(By.text(title)), Direction.DOWN); }
/** * Navigates through the focus items in a container returning the container child that has a * descendant matching the {@code selector}. * <p> * The navigation starts in the {@code direction} specified and * {@link Direction#reverse(Direction) reverses} once if needed. Fails if there is not a * focused * descendant, or if after completing both directions no focused child has a descendant * matching * {@code selector}. * <p> * Fails if the menu item can not be navigated to. * * @param uiDevice the device under test. * @param container contains children to navigate over. * @param selector the selector for the object to navigate to. * @param direction the direction to start navigating. * @return the object navigated to. */ public static UiObject2 assertNavigateTo(UiDevice uiDevice, UiObject2 container, BySelector selector, Direction direction) { int count = 0; while (count < 2) { BySelector hasFocusedDescendant = By.hasDescendant(FOCUSED_VIEW); UiObject2 focusedChild = null; SearchCondition<Boolean> untilHasFocusedDescendant = Until .hasObject(hasFocusedDescendant); boolean result = container.wait(untilHasFocusedDescendant, UiObject2Asserts.getAdjustedTimeout(Constants.MAX_SHOW_DELAY_MILLIS)); if (!result) { // HACK: Try direction anyways because play control does not always have a // focused item. UiDeviceUtils.pressDpad(uiDevice, direction); UiObject2Asserts.assertWaitForCondition(container, untilHasFocusedDescendant); } for (UiObject2 c : container.getChildren()) { if (c.isFocused() || c.hasObject(hasFocusedDescendant)) { focusedChild = c; break; } } if (focusedChild == null) { Assert.fail("No focused item found in container " + container); } if (focusedChild.hasObject(selector)) { return focusedChild; } if (!UiObject2Utils.hasSiblingInDirection(focusedChild, direction)) { direction = Direction.reverse(direction); count++; } UiDeviceUtils.pressDpad(uiDevice, direction); } Assert.fail("Could not find item with " + selector); return null; }
private boolean swipe(UiObject2 item, String dir,float percent, int steps) throws UiObjectNotFoundException { dir = dir.toLowerCase(); if ("u".equals(dir) || "up".equals(dir)) item.swipe(Direction.UP,percent,steps); else if ("d".equals(dir) || "down".equals(dir)) item.swipe(Direction.DOWN,percent,steps); else if ("l".equals(dir) || "left".equals(dir)) item.swipe(Direction.LEFT,percent,steps); else if ("r".equals(dir) || "right".equals(dir)) item.swipe(Direction.RIGHT,percent,steps); return true; }
/** * Navigate to the menu row with the text title {@code rowTitleResId}. * <p> * Fails if the menu row can not be navigated to. * We can't navigate to the Play controls row with this method, because the row doesn't have the * title when it is selected. Use {@link #assertNavigateToPlayControlsRow} for the row instead. * * @param rowTitleResId the resource id of the string in the desired row title. * @return the row navigated to. */ public UiObject2 assertNavigateToRow(int rowTitleResId) { UiDeviceAsserts.assertHas(mUiDevice, MENU, true); UiObject2 menu = mUiDevice.findObject(MENU); // TODO: handle play controls. They have a different dom structure and navigation sometimes // can get stuck on that row. return UiDeviceAsserts.assertNavigateTo(mUiDevice, menu, By.hasDescendant(ByResource.text(mTargetResources, rowTitleResId)), Direction.DOWN); }
/** * Navigate to the menu item in the given {@code row} with the text {@code itemTextResId} . * <p> * Fails if the menu item can not be navigated to. * * @param row the container to look for menu items in. * @param itemTextResId the resource id of the string in the desired item. * @return the item navigated to. */ public UiObject2 assertNavigateToRowItem(UiObject2 row, int itemTextResId) { return UiDeviceAsserts.assertNavigateTo(mUiDevice, row, By.hasDescendant(ByResource.text(mTargetResources, itemTextResId)), Direction.RIGHT); }