Skip to content

Instantly share code, notes, and snippets.

@dongdaqing
Forked from cpeppas/gist:b5ffe6bd29b67d96416a
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save dongdaqing/a400da0337e7636b5811 to your computer and use it in GitHub Desktop.

Select an option

Save dongdaqing/a400da0337e7636b5811 to your computer and use it in GitHub Desktop.

Revisions

  1. @cpeppas cpeppas revised this gist Oct 17, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,11 @@ public boolean matchesSafely(View view) {


    Usage:

    import static com.octo.android.sample.espresso.test.CustomMatchers.withResourceName;

    ...//Your TestEspressoClass

    public void testActionBarTitleForScreenOneActivity() {
    onView(allOf(isDescendantOfA(withResourceName("android:id/action_bar_container")), withText("My Activity")))
    .check(matches(isDisplayed()));
  2. @cpeppas cpeppas created this gist Oct 17, 2014.
    40 changes: 40 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    import android.view.View;
    import org.hamcrest.Description;
    import org.hamcrest.Matcher;
    import org.hamcrest.TypeSafeMatcher;

    import static org.hamcrest.Matchers.is;

    public class CustomMatchers {

    public static Matcher<View> withResourceName(String resourceName) {
    return withResourceName(is(resourceName));
    }

    public static Matcher<View> withResourceName(final Matcher<String> resourceNameMatcher) {
    return new TypeSafeMatcher<View>() {
    @Override
    public void describeTo(Description description) {
    description.appendText("with resource name: ");
    resourceNameMatcher.describeTo(description);
    }

    @Override
    public boolean matchesSafely(View view) {
    int id = view.getId();
    return id != View.NO_ID && id != 0 && view.getResources() != null
    && resourceNameMatcher.matches(view.getResources().getResourceName(id));
    }
    };
    }
    }


    Usage:

    public void testActionBarTitleForScreenOneActivity() {
    onView(allOf(isDescendantOfA(withResourceName("android:id/action_bar_container")), withText("My Activity")))
    .check(matches(isDisplayed()));
    }