Java Code Examples for android.support.test.uiautomator.Direction

The following are top voted examples for showing how to use android.support.test.uiautomator.Direction. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: android_packages_apps_tv   File: UiDeviceUtils.java   View source code 6 votes vote down vote up
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());
    }
}
 
Example 2
Project: FlickLauncher   File: LauncherInstrumentationTestCase.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 3
Project: SimpleUILauncher   File: LauncherInstrumentationTestCase.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 4
Project: android_packages_apps_tv   File: UiObject2Utils.java   View source code 5 votes vote down vote up
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;
}
 
Example 5
Project: android_packages_apps_tv   File: SidePanelHelper.java   View source code 5 votes vote down vote up
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);
}
 
Example 6
Project: android_packages_apps_tv   File: UiDeviceAsserts.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Project: android-uiautomator-server   File: AutomatorServiceImpl.java   View source code 5 votes vote down vote up
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;
}
 
Example 8
Project: android_packages_apps_tv   File: MenuHelper.java   View source code 3 votes vote down vote up
/**
 * 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);
}
 
Example 9
Project: android_packages_apps_tv   File: MenuHelper.java   View source code 2 votes vote down vote up
/**
 * 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);
}