Search in sources :

Example 1 with BoundedMatcher

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

the class TestUtilsMatchers method withFabContentAreaOnMargins.

/** Returns a matcher that matches FloatingActionButtons with the specified gravity. */
public static Matcher withFabContentAreaOnMargins(final int gravity) {
    return new BoundedMatcher<View, View>(View.class) {

        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final View view) {
            if (!(view instanceof FloatingActionButton)) {
                return false;
            }
            final FloatingActionButton fab = (FloatingActionButton) view;
            final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) fab.getLayoutParams();
            final ViewGroup parent = (ViewGroup) view.getParent();
            final Rect area = new Rect();
            fab.getContentRect(area);
            final int absGravity = GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(view));
            try {
                switch(absGravity & Gravity.VERTICAL_GRAVITY_MASK) {
                    case Gravity.TOP:
                        assertEquals(lp.topMargin, fab.getTop() + area.top);
                        break;
                    case Gravity.BOTTOM:
                        assertEquals(parent.getHeight() - lp.bottomMargin, fab.getTop() + area.bottom);
                        break;
                }
                switch(absGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                    case Gravity.LEFT:
                        assertEquals(lp.leftMargin, fab.getLeft() + area.left);
                        break;
                    case Gravity.RIGHT:
                        assertEquals(parent.getWidth() - lp.rightMargin, fab.getLeft() + area.right);
                        break;
                }
                return true;
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
        }
    };
}
Also used : Rect(android.graphics.Rect) Description(org.hamcrest.Description) ViewGroup(android.view.ViewGroup) FloatingActionButton(android.support.design.widget.FloatingActionButton) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 2 with BoundedMatcher

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

the class TestUtilsMatchers method withFabBackgroundFill.

/**
   * Returns a matcher that matches FloatingActionButtons with the specified background fill color.
   */
public static Matcher withFabBackgroundFill(@ColorInt final int fillColor) {
    return new BoundedMatcher<View, View>(View.class) {

        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final View view) {
            if (!(view instanceof FloatingActionButton)) {
                return false;
            }
            final FloatingActionButton fab = (FloatingActionButton) view;
            // Since the FAB background is round, and may contain the shadow, we'll look at
            // just the center half rect of the content area
            final Rect area = new Rect();
            fab.getContentRect(area);
            final int rectHeightQuarter = area.height() / 4;
            final int rectWidthQuarter = area.width() / 4;
            area.left += rectWidthQuarter;
            area.top += rectHeightQuarter;
            area.right -= rectWidthQuarter;
            area.bottom -= rectHeightQuarter;
            try {
                TestUtils.assertAllPixelsOfColor("", fab.getBackground(), view.getWidth(), view.getHeight(), false, fillColor, area, 0, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}
Also used : Rect(android.graphics.Rect) Description(org.hamcrest.Description) FloatingActionButton(android.support.design.widget.FloatingActionButton) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 3 with BoundedMatcher

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

the class TestUtilsMatchers method drawable.

/**
   * Returns a matcher that matches <code>ImageView</code>s which have drawable flat-filled with the
   * specific color.
   */
public static Matcher drawable(@ColorInt final int color, final int allowedComponentVariance) {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {

        private String mFailedComparisonDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText("with drawable of color: ");
            description.appendText(mFailedComparisonDescription);
        }

        @Override
        public boolean matchesSafely(final ImageView view) {
            Drawable drawable = view.getDrawable();
            if (drawable == null) {
                return false;
            }
            // all pixels in a Drawable are of the same specified color.
            try {
                TestUtils.assertAllPixelsOfColor("", drawable, view.getWidth(), view.getHeight(), true, color, allowedComponentVariance, true);
                // If we are here, the color comparison has passed.
                mFailedComparisonDescription = null;
                return true;
            } catch (Throwable t) {
                // If we are here, the color comparison has failed.
                mFailedComparisonDescription = t.getMessage();
                return false;
            }
        }
    };
}
Also used : Description(org.hamcrest.Description) Drawable(android.graphics.drawable.Drawable) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) ImageView(android.widget.ImageView)

Example 4 with BoundedMatcher

use of android.support.test.espresso.matcher.BoundedMatcher in project Rosie by Karumi.

the class AncestorMatcher method withAncestor.

public static Matcher<Object> withAncestor(final Matcher<View> viewMatcher) {
    return new BoundedMatcher<Object, View>(View.class) {

        @Override
        public boolean matchesSafely(View view) {
            boolean found = false;
            ViewParent parent = view.getParent();
            while (!found && parent != null) {
                found = viewMatcher.matches(parent);
                parent = parent.getParent();
            }
            return found;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with view ancestor: ");
            viewMatcher.describeTo(description);
        }
    };
}
Also used : Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) View(android.view.View)

Example 5 with BoundedMatcher

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

the class TestUtilsMatchers method withFabContentHeight.

/** Returns a matcher that matches FloatingActionButtons with the specified content height */
public static Matcher withFabContentHeight(final int size) {
    return new BoundedMatcher<View, View>(View.class) {

        private String failedCheckDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText(failedCheckDescription);
        }

        @Override
        public boolean matchesSafely(final View view) {
            if (!(view instanceof FloatingActionButton)) {
                return false;
            }
            final FloatingActionButton fab = (FloatingActionButton) view;
            final Rect area = new Rect();
            fab.getContentRect(area);
            return area.height() == size;
        }
    };
}
Also used : Rect(android.graphics.Rect) Description(org.hamcrest.Description) FloatingActionButton(android.support.design.widget.FloatingActionButton) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Aggregations

BoundedMatcher (android.support.test.espresso.matcher.BoundedMatcher)6 Description (org.hamcrest.Description)6 Rect (android.graphics.Rect)4 View (android.view.View)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 FloatingActionButton (android.support.design.widget.FloatingActionButton)3 Drawable (android.graphics.drawable.Drawable)2 ViewGroup (android.view.ViewGroup)1 ViewParent (android.view.ViewParent)1