Search in sources :

Example 6 with BoundedMatcher

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

the class TestUtilsMatchers method withStartDrawableFilledWith.

/**
   * Returns a matcher that matches TextViews whose start drawable is filled with the specified fill
   * color.
   */
public static Matcher withStartDrawableFilledWith(@ColorInt final int fillColor, final int allowedComponentVariance) {
    return new BoundedMatcher<View, TextView>(TextView.class) {

        private String failedCheckDescription;

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

        @Override
        public boolean matchesSafely(final TextView view) {
            final Drawable[] compoundDrawables = view.getCompoundDrawables();
            final boolean isRtl = (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL);
            final Drawable startDrawable = isRtl ? compoundDrawables[2] : compoundDrawables[0];
            if (startDrawable == null) {
                failedCheckDescription = "no start drawable";
                return false;
            }
            try {
                final Rect bounds = startDrawable.getBounds();
                TestUtils.assertAllPixelsOfColor("", startDrawable, bounds.width(), bounds.height(), true, fillColor, allowedComponentVariance, true);
            } catch (Throwable t) {
                failedCheckDescription = t.getMessage();
                return false;
            }
            return true;
        }
    };
}
Also used : Rect(android.graphics.Rect) Description(org.hamcrest.Description) Drawable(android.graphics.drawable.Drawable) BoundedMatcher(android.support.test.espresso.matcher.BoundedMatcher) 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