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.
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:
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()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment