Search in sources :

Example 56 with ViewAction

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

the class NavigationViewActions method setItemTextColor.

/** Sets item text color on the content of the navigation view. */
public static ViewAction setItemTextColor(final ColorStateList textColor) {
    return new ViewAction() {

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

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

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            NavigationView navigationView = (NavigationView) view;
            navigationView.setItemTextColor(textColor);
            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 57 with ViewAction

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

the class TabLayoutWithViewPagerTest method addItemsToPager.

private static <Q> ViewAction addItemsToPager(final String[] title, final Q[] content) {
    return new ViewAction() {

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

        @Override
        public String getDescription() {
            return "Add items and notify on content change";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            final ViewPager viewPager = (ViewPager) view;
            // no way to avoid this cast
            @SuppressWarnings("unchecked") final BasePagerAdapter<Q> viewPagerAdapter = (BasePagerAdapter<Q>) viewPager.getAdapter();
            int itemCount = title.length;
            for (int i = 0; i < itemCount; i++) {
                viewPagerAdapter.add(title[i], content[i]);
            }
            viewPagerAdapter.notifyDataSetChanged();
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : ViewAction(android.support.test.espresso.ViewAction) UiController(android.support.test.espresso.UiController) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Espresso.onView(android.support.test.espresso.Espresso.onView) ViewPager(android.support.v4.view.ViewPager) TabLayoutActions.setupWithViewPager(android.support.design.testutils.TabLayoutActions.setupWithViewPager)

Example 58 with ViewAction

use of android.support.test.espresso.ViewAction in project iosched by google.

the class MatchersHelper method getNumberOfDescendantsForViewGroupDescendant.

/**
     * This returns the number of descendants of the {@code parentViewWithMultipleChildren} that
     * have an id of {@code idOfDescendantViewToGetTextFor}}. The parent view is expected to be a
     * {@link ViewGroup}. This is used when there is a certain randomness in the elements shown in a
     * collection view, even with mock data, but we want to check the number of elements inside the
     * parent view.
     *
     * @see com.google.samples.apps.iosched.explore.ExploreIOActivityTest
     */
public static int getNumberOfDescendantsForViewGroupDescendant(final Matcher<View> parentViewWithMultipleChildren, final int idOfDescendantViewToGetTextFor) {
    /**
         * We cannot use a int directly as we need to make it final to access it inside the
         * inner method but we cannot reassign a value to a final int.
         */
    final int[] intHolder = { 0 };
    onView(parentViewWithMultipleChildren).perform(new ViewAction() {

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

        @Override
        public String getDescription() {
            return "getting text from a TextView with a known id and that is a descendant of " + "the nth child of a viewgroup";
        }

        @Override
        public void perform(UiController uiController, View view) {
            ViewGroup vg = (ViewGroup) view;
            int descendants = vg.getChildCount();
            for (int i = 0; i < descendants; i++) {
                View ithDescendant = vg.getChildAt(i);
                View matchedView = ithDescendant.findViewById(idOfDescendantViewToGetTextFor);
                if (matchedView != null) {
                    intHolder[0] += 1;
                }
            }
        }
    });
    return intHolder[0];
}
Also used : ViewAction(android.support.test.espresso.ViewAction) BaseMatcher(org.hamcrest.BaseMatcher) Matcher(org.hamcrest.Matcher) ViewGroup(android.view.ViewGroup) UiController(android.support.test.espresso.UiController) TextView(android.widget.TextView) Espresso.onView(android.support.test.espresso.Espresso.onView) View(android.view.View)

Example 59 with ViewAction

use of android.support.test.espresso.ViewAction in project iosched by google.

the class MatchersHelper method getText.

/**
     * This returns the text for the view that has been matched with {@code matcher}. The matched
     * view is expected to be a {@link TextView}. This is different from matching a view by text,
     * this is intended to be used when matching a view by another mean (for example, by id) but
     * when we need to know the text of that view, for use later in a test.
     * <p/>
     * In general, this isn't good practice as tests should be written using mock data (so we always
     * know what the data is) but this enables us to write tests that are written using real data
     * (so we don't always know what the data is but we want to verify something later in a test).
     * This is enables us to write UI tests before refactoring a feature to make it easier to mock
     * the data.
     */
public static String getText(final Matcher<View> matcher) {
    /**
         * We cannot use a String directly as we need to make it final to access it inside the
         * inner method but we cannot reassign a value to a final String.
         */
    final String[] stringHolder = { null };
    onView(matcher).perform(new ViewAction() {

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

        @Override
        public String getDescription() {
            return "getting text from a TextView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            TextView tv = (TextView) view;
            stringHolder[0] = tv.getText().toString();
        }
    });
    return stringHolder[0];
}
Also used : ViewAction(android.support.test.espresso.ViewAction) BaseMatcher(org.hamcrest.BaseMatcher) Matcher(org.hamcrest.Matcher) UiController(android.support.test.espresso.UiController) TextView(android.widget.TextView) TextView(android.widget.TextView) Espresso.onView(android.support.test.espresso.Espresso.onView) View(android.view.View)

Example 60 with ViewAction

use of android.support.test.espresso.ViewAction in project ChipsLayoutManager by BelooS.

the class ColumnTest method smoothScrollToPosition_ScrollItemIsVisible_ScrollItemDockedToStartBorder.

@Test
public synchronized void smoothScrollToPosition_ScrollItemIsVisible_ScrollItemDockedToStartBorder() throws Exception {
    //arrange
    InstrumentalUtil.waitForIdle();
    //act
    ViewAction scrollAction = smoothScrollToPosition(3);
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (scrollAction) {
        recyclerView.perform(scrollAction);
        //wait for completion of SmoothScrollAction
        scrollAction.wait();
    }
    //assert
    int actual = layoutManager.findFirstCompletelyVisibleItemPosition();
    assertEquals(3, actual);
}
Also used : ViewAction(android.support.test.espresso.ViewAction) Test(org.junit.Test)

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