Skip to content

Instantly share code, notes, and snippets.

@elevenetc
Created February 23, 2016 15:32
Show Gist options
  • Select an option

  • Save elevenetc/8038fe98bc603b91b288 to your computer and use it in GitHub Desktop.

Select an option

Save elevenetc/8038fe98bc603b91b288 to your computer and use it in GitHub Desktop.

Revisions

  1. elevenetc created this gist Feb 23, 2016.
    42 changes: 42 additions & 0 deletions ExtraAssertions.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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;
    }
    }
    }