Skip to content

Instantly share code, notes, and snippets.

@Aracem
Created March 28, 2017 10:03
Show Gist options
  • Save Aracem/a596c40f9750f02f4e606dad170925e7 to your computer and use it in GitHub Desktop.
Save Aracem/a596c40f9750f02f4e606dad170925e7 to your computer and use it in GitHub Desktop.
TextColorMatcher for your Espresso test
package com.homerthebulldog.android.matcher;
import android.content.res.Resources;
import android.support.annotation.ColorRes;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.view.View;
import android.widget.TextView;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static org.hamcrest.Matchers.is;
public class TextColorMatcher extends BoundedMatcher<View, TextView> {
private Matcher<Integer> integerMatcher;
private TextColorMatcher(final Matcher<Integer> integerMatcher) {
super(TextView.class);
this.integerMatcher = integerMatcher;
}
public static Matcher<View> withTextColor(@ColorRes int textColor, Resources resources) {
int color = resources.getColor(textColor);
return new TextColorMatcher(is(color));
}
@Override
public boolean matchesSafely(TextView textView) {
return integerMatcher.matches(textView.getCurrentTextColor());
}
@Override
public void describeMismatch(Object item, Description mismatchDescription) {
mismatchDescription.appendText("TextView with text color: " +
((TextView) item).getCurrentTextColor() + ", expected: ");
integerMatcher.describeMismatch(item, mismatchDescription);
}
@Override
public void describeTo(Description description) {
description.appendText("with text color ");
integerMatcher.describeTo(description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment