Skip to content

Instantly share code, notes, and snippets.

@elevenetc
Created February 23, 2016 15:32
Show Gist options
  • Save elevenetc/8038fe98bc603b91b288 to your computer and use it in GitHub Desktop.
Save elevenetc/8038fe98bc603b91b288 to your computer and use it in GitHub Desktop.
Espresso view visibility assertions
public class ExtraAssertions {
public static ViewAssertion isVisible() {
return (view, noView) -> assertThat(view, new VisibilityMatcher(View.VISIBLE));
}
public static ViewAssertion isGone() {
return (view, noView) -> assertThat(view, new VisibilityMatcher(View.GONE));
}
public static ViewAssertion isInvisible() {
return (view, noView) -> assertThat(view, new VisibilityMatcher(View.INVISIBLE));
}
private static class VisibilityMatcher extends BaseMatcher<View> {
private int visibility;
public VisibilityMatcher(int visibility) {
this.visibility = visibility;
}
@Override public void describeTo(Description description) {
String visibilityName;
if (visibility == View.GONE) visibilityName = "GONE";
else if (visibility == View.VISIBLE) visibilityName = "VISIBLE";
else visibilityName = "INVISIBLE";
description.appendText("View visibility must has equals " + visibilityName);
}
@Override public boolean matches(Object o) {
if (o == null) {
if (visibility == View.GONE || visibility == View.INVISIBLE) return true;
else if (visibility == View.VISIBLE) return false;
}
if (!(o instanceof View))
throw new IllegalArgumentException("Object must be instance of View. Object is instance of " + o);
return ((View) o).getVisibility() == visibility;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment