Search in sources :

Example 31 with ViewAction

use of android.support.test.espresso.ViewAction in project todo-mvp-rxjava by albertizzy.

the class NavigationViewActions method navigateTo.

/**
 * Returns a {@link ViewAction} that navigates to a menu item in {@link NavigationView} using a
 * menu item resource id.
 *
 * <p>
 * View constraints:
 * <ul>
 * <li>View must be a child of a {@link DrawerLayout}
 * <li>View must be of type {@link NavigationView}
 * <li>View must be visible on screen
 * <li>View must be displayed on screen
 * <ul>
 *
 * @param menuItemId the resource id of the menu item
 * @return a {@link ViewAction} that navigates on a menu item
 */
public static ViewAction navigateTo(final int menuItemId) {
    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NavigationView navigationView = (NavigationView) view;
            Menu menu = navigationView.getMenu();
            if (null == menu.findItem(menuItemId)) {
                throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new RuntimeException(getErrorMessage(menu, view))).build();
            }
            menu.performIdentifierAction(menuItemId, 0);
            uiController.loopMainThreadUntilIdle();
        }

        private String getErrorMessage(Menu menu, View view) {
            String NEW_LINE = System.getProperty("line.separator");
            StringBuilder errorMessage = new StringBuilder("Menu item was not found, " + "available menu items:").append(NEW_LINE);
            for (int position = 0; position < menu.size(); position++) {
                errorMessage.append("[MenuItem] position=").append(position);
                MenuItem menuItem = menu.getItem(position);
                if (menuItem != null) {
                    CharSequence itemTitle = menuItem.getTitle();
                    if (itemTitle != null) {
                        errorMessage.append(", title=").append(itemTitle);
                    }
                    if (view.getResources() != null) {
                        int itemId = menuItem.getItemId();
                        try {
                            errorMessage.append(", id=");
                            String menuItemResourceName = view.getResources().getResourceName(itemId);
                            errorMessage.append(menuItemResourceName);
                        } catch (NotFoundException nfe) {
                            errorMessage.append("not found");
                        }
                    }
                    errorMessage.append(NEW_LINE);
                }
            }
            return errorMessage.toString();
        }

        @Override
        public String getDescription() {
            return "click on menu item with id";
        }

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(NavigationView.class), withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), isDisplayingAtLeast(90));
        }
    };
}
Also used : NavigationView(android.support.design.widget.NavigationView) UiController(android.support.test.espresso.UiController) NotFoundException(android.content.res.Resources.NotFoundException) MenuItem(android.view.MenuItem) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) ViewAction(android.support.test.espresso.ViewAction) Menu(android.view.Menu)

Example 32 with ViewAction

use of android.support.test.espresso.ViewAction in project material-components-android by material-components.

the class BottomNavigationViewActions method setIconForMenuItem.

/** Sets icon for the menu item of the navigation view. */
public static ViewAction setIconForMenuItem(@IdRes final int menuItemId, final Drawable iconDrawable) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isDisplayed();
        }

        @Override
        public String getDescription() {
            return "Set menu item icon";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            BottomNavigationView navigationView = (BottomNavigationView) view;
            navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable);
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : ViewAction(android.support.test.espresso.ViewAction) BottomNavigationView(android.support.design.widget.BottomNavigationView) UiController(android.support.test.espresso.UiController) BottomNavigationView(android.support.design.widget.BottomNavigationView) View(android.view.View)

Example 33 with ViewAction

use of android.support.test.espresso.ViewAction in project material-components-android by material-components.

the class BaseDynamicCoordinatorLayoutTest method inflateViewStub.

/** Inflates the <code>ViewStub</code> with the passed layout resource. */
protected ViewAction inflateViewStub(@LayoutRes final int layoutResId) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return allOf(isAssignableFrom(ViewStub.class), hasParent());
        }

        @Override
        public String getDescription() {
            return "Inflates view stub";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            ViewStub viewStub = (ViewStub) view;
            viewStub.setLayoutResource(layoutResId);
            viewStub.inflate();
            mCoordinatorLayout = (CoordinatorLayout) activityTestRule.getActivity().findViewById(viewStub.getInflatedId());
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : ViewStub(android.view.ViewStub) ViewAction(android.support.test.espresso.ViewAction) UiController(android.support.test.espresso.UiController) View(android.view.View)

Example 34 with ViewAction

use of android.support.test.espresso.ViewAction in project material-components-android by material-components.

the class NavigationViewActions method setItemBackgroundResource.

/** Sets item background on the content of the navigation view. */
public static ViewAction setItemBackgroundResource(@DrawableRes final int resId) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isDisplayed();
        }

        @Override
        public String getDescription() {
            return "Set item background";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            NavigationView navigationView = (NavigationView) view;
            navigationView.setItemBackgroundResource(resId);
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : NavigationView(android.support.design.widget.NavigationView) ViewAction(android.support.test.espresso.ViewAction) UiController(android.support.test.espresso.UiController) NavigationView(android.support.design.widget.NavigationView) View(android.view.View)

Example 35 with ViewAction

use of android.support.test.espresso.ViewAction in project material-components-android by material-components.

the class NavigationViewActions method removeMenuItem.

/**
   * Removes the specified menu item from the navigation view.
   *
   * @param menuItemId The ID of the menu item to be removed.
   */
public static ViewAction removeMenuItem(@IdRes final int menuItemId) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(NavigationView.class);
        }

        @Override
        public String getDescription() {
            return "Remove menu item " + menuItemId;
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            NavigationView navigationView = (NavigationView) view;
            navigationView.getMenu().removeItem(menuItemId);
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : NavigationView(android.support.design.widget.NavigationView) ViewAction(android.support.test.espresso.ViewAction) UiController(android.support.test.espresso.UiController) NavigationView(android.support.design.widget.NavigationView) View(android.view.View)

Aggregations

ViewAction (android.support.test.espresso.ViewAction)65 View (android.view.View)61 UiController (android.support.test.espresso.UiController)56 NavigationView (android.support.design.widget.NavigationView)18 Espresso.onView (android.support.test.espresso.Espresso.onView)11 TextView (android.widget.TextView)11 Test (org.junit.Test)9 ViewPager (android.support.v4.view.ViewPager)8 TabLayout (android.support.design.widget.TabLayout)7 TextInputLayout (android.support.design.widget.TextInputLayout)7 FloatingActionButton (android.support.design.widget.FloatingActionButton)6 RecyclerView (android.support.v7.widget.RecyclerView)6 ViewGroup (android.view.ViewGroup)4 Matcher (org.hamcrest.Matcher)4 BaseMatcher (org.hamcrest.BaseMatcher)3 TabLayoutActions.setupWithViewPager (android.support.design.testutils.TabLayoutActions.setupWithViewPager)2 BottomNavigationView (android.support.design.widget.BottomNavigationView)2 CollapsingToolbarLayout (android.support.design.widget.CollapsingToolbarLayout)2 ViewInteraction (android.support.test.espresso.ViewInteraction)2 DrawerLayout (android.support.v4.widget.DrawerLayout)2