Skip to content

Instantly share code, notes, and snippets.

@hoombar
Created August 19, 2016 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoombar/2c7276243dfd7f288cc386d008377278 to your computer and use it in GitHub Desktop.
Save hoombar/2c7276243dfd7f288cc386d008377278 to your computer and use it in GitHub Desktop.
Espresso extra view matchers
public class ExtraViewMatchers {
public static Matcher<? super View> hasSelectedTab(final String tabName) {
return new BoundedMatcher<View, TabLayout>(TabLayout.class) {
public void describeTo(Description description) {
description.appendText("has selected tab " + tabName);
}
public boolean matchesSafely(TabLayout tabLayout) {
TabLayout.Tab tab = tabLayout.getTabAt(tabLayout.getSelectedTabPosition());
return tabName.equals(tab.getText().toString());
}
};
}
public static Matcher<View> hasTabAtIndex(final int index, final String tabName) {
return new BoundedMatcher<View, TabLayout>(TabLayout.class) {
public void describeTo(Description description) {
description.appendText("has tab " + tabName + " at index " + index);
}
public boolean matchesSafely(TabLayout tabLayout) {
TabLayout.Tab tab = tabLayout.getTabAt(index);
return tabName.equals(tab.getText().toString());
}
};
}
public static Matcher<View> withImage(@DrawableRes final int imageResource) {
return new BoundedMatcher<View, ImageView>(ImageView.class) {
public void describeTo(Description description) {
description.appendText("with image: " + imageResource);
}
public boolean matchesSafely(ImageView imageView) {
Bitmap expectedBitmap = BitmapFactory
.decodeResource(InstrumentationRegistry.getContext().getResources(), imageResource);
if (expectedBitmap == null) {
expectedBitmap = BitmapFactory
.decodeResource(InstrumentationRegistry.getTargetContext().getResources(), imageResource);
}
Bitmap actualBitmap = ((BitmapDrawable) imageView.getDrawable().getCurrent()).getBitmap();
//essentially compare the middle pixel
int expectedPixel = expectedBitmap.getPixel(expectedBitmap.getWidth() / 2, expectedBitmap.getHeight() / 2);
int actualPixel = actualBitmap.getPixel(actualBitmap.getWidth() / 2, actualBitmap.getHeight() / 2);
return expectedPixel == actualPixel;
}
};
}
public static Matcher<View> hasScrollbar() {
return new BoundedMatcher<View, AbsListView>(AbsListView.class) {
public void describeTo(Description description) {
description.appendText("with scrollbar");
}
public boolean matchesSafely(AbsListView listView) {
return listView.getScrollBarSize() > 0;
}
};
}
public static Matcher<View> isEllipsized() {
return new BoundedMatcher<View, TextView>(TextView.class) {
public void describeTo(Description description) {
description.appendText("with ellipsize");
}
public boolean matchesSafely(TextView textView) {
Layout l = textView.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0) {
if (l.getEllipsisCount(lines - 1) > 0) {
return true;
}
}
}
return false;
}
};
}
/***
* The snackbar will try and wrap lines to make them look pretty, for example, if you have the text: "I like Monkeys. I also like cats."
* and it works out the text is too long to fit on a single line, it will produce: "I like Monkeys.\nI also like cats." which means
* when using withText() as a matcher, it won't actually match
*
* @param message the text without linebreaks you want to match on a snackbar message
*/
public static Matcher<View> hasSnackbarMessage(final String message) {
return new BoundedMatcher<View, AppCompatTextView>(AppCompatTextView.class) {
@Override
public void describeTo(final Description description) {
description.appendText("with snackbar");
}
@Override
protected boolean matchesSafely(final AppCompatTextView item) {
String value = item.getText().toString();
value = value.replaceAll("\n", " ");
return value.equals(message);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment