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;
}
}
};
}
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;
}
};
}
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;
}
}
};
}
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);
}
};
}
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;
}
};
}
Aggregations