Search in sources :

Example 61 with UiController

use of android.support.test.espresso.UiController 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) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) 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 62 with UiController

use of android.support.test.espresso.UiController 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) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) 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 63 with UiController

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

the class MatchersHelper method moveViewPagerToPage.

/**
 * Moves <code>ViewPager</code> to specific page.
 */
public static ViewAction moveViewPagerToPage(final int page) {
    return new ViewAction() {

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

        @Override
        public String getDescription() {
            return "ViewPager move to a specific page";
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            ViewPager viewPager = (ViewPager) view;
            viewPager.setCurrentItem(page, false);
            uiController.loopMainThreadUntilIdle();
        }
    };
}
Also used : ViewAction(android.support.test.espresso.ViewAction) UiController(android.support.test.espresso.UiController) TextView(android.widget.TextView) Espresso.onView(android.support.test.espresso.Espresso.onView) View(android.view.View) ViewPager(android.support.v4.view.ViewPager)

Example 64 with UiController

use of android.support.test.espresso.UiController in project cameraview by google.

the class CameraViewTest method testAdjustViewBounds.

@Test
public void testAdjustViewBounds() {
    onView(withId(R.id.camera)).check(new ViewAssertion() {

        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            CameraView cameraView = (CameraView) view;
            assertThat(cameraView.getAdjustViewBounds(), is(false));
            cameraView.setAdjustViewBounds(true);
            assertThat(cameraView.getAdjustViewBounds(), is(true));
        }
    }).perform(new AnythingAction("layout") {

        @Override
        public void perform(UiController uiController, View view) {
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            view.setLayoutParams(params);
        }
    }).check(new ViewAssertion() {

        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            CameraView cameraView = (CameraView) view;
            AspectRatio cameraRatio = cameraView.getAspectRatio();
            AspectRatio viewRatio = AspectRatio.of(view.getWidth(), view.getHeight());
            assertThat(cameraRatio, is(closeToOrInverse(viewRatio)));
        }
    });
}
Also used : CameraViewMatchers.hasAspectRatio(com.google.android.cameraview.CameraViewMatchers.hasAspectRatio) CameraViewActions.setAspectRatio(com.google.android.cameraview.CameraViewActions.setAspectRatio) ViewAssertion(android.support.test.espresso.ViewAssertion) ViewGroup(android.view.ViewGroup) UiController(android.support.test.espresso.UiController) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) TextureView(android.view.TextureView) Espresso.onView(android.support.test.espresso.Espresso.onView) FlakyTest(android.support.test.filters.FlakyTest) Test(org.junit.Test)

Aggregations

UiController (android.support.test.espresso.UiController)64 View (android.view.View)64 ViewAction (android.support.test.espresso.ViewAction)63 NavigationView (android.support.design.widget.NavigationView)18 Espresso.onView (android.support.test.espresso.Espresso.onView)14 TextView (android.widget.TextView)14 ViewPager (android.support.v4.view.ViewPager)9 TabLayout (android.support.design.widget.TabLayout)8 TextInputLayout (android.support.design.widget.TextInputLayout)7 FloatingActionButton (android.support.design.widget.FloatingActionButton)6 BottomNavigationView (android.support.design.widget.BottomNavigationView)4 ViewGroup (android.view.ViewGroup)4 Matcher (org.hamcrest.Matcher)4 BaseMatcher (org.hamcrest.BaseMatcher)3 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)3 TabLayoutActions.setupWithViewPager (android.support.design.testutils.TabLayoutActions.setupWithViewPager)2 CollapsingToolbarLayout (android.support.design.widget.CollapsingToolbarLayout)2 DrawerLayout (android.support.v4.widget.DrawerLayout)2 RecyclerView (android.support.v7.widget.RecyclerView)2 HorizontalScrollView (android.widget.HorizontalScrollView)2