/** * Returns a matcher that matches {@link TextView}s based on text property value. * * @param stringMatcher {@link Matcher} of {@link String} with text to match */ @NonNull public static Matcher<View> withPasswordText(final Matcher<String> stringMatcher) { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("with error text: "); stringMatcher.describeTo(description); } @Override public boolean matchesSafely(final TextView textView) { return stringMatcher.matches(textView.getText().toString()); } }; }
private static Matcher<Object> withCollapsingToolbarTitle( final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, CollapsingToolbarLayout>(CollapsingToolbarLayout.class) { @Override public void describeTo(Description description) { description.appendText("with collapsing toolbar title: "); textMatcher.describeTo(description); } @Override protected boolean matchesSafely(CollapsingToolbarLayout collapsingToolbarLayout) { return textMatcher.matches(collapsingToolbarLayout.getTitle()); } }; }
public static Matcher<View> atPosition(final int position, final Matcher<View> itemMatcher) { return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ": "); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); if (viewHolder == null) { return false; } return itemMatcher.matches(viewHolder.itemView); } }; }
/** * Returns a matcher that matches {@link TextView}s based on text property value. * * @param stringMatcher {@link Matcher} of {@link String} with text to match */ @NonNull public static Matcher<View> withErrorText(final Matcher<String> stringMatcher) { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("with error text: "); stringMatcher.describeTo(description); } @Override public boolean matchesSafely(final TextView textView) { return stringMatcher.matches(textView.getError().toString()); } }; }
/** * Returns a matcher that matches {@link TextView}s based on text property value. */ @NonNull public static Matcher<View> hasError() { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("Has error case failed."); } @Override public boolean matchesSafely(final TextView textView) { return textView.getError() != null; } }; }
/** * Returns a matcher that matches {@link TextView}s based on text property value. */ @NonNull public static Matcher<View> checkTextLength(final int length) { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("Text length is : " + description); } @Override public boolean matchesSafely(final TextView textView) { return textView.getText().length() == length; } }; }
/** * Returns a matcher that matches {@link TextView}s based on text property value. */ @NonNull public static Matcher<View> hasText() { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("Text length is : " + description); } @Override public boolean matchesSafely(final TextView textView) { return textView.getText().length() > 0; } }; }
public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ":\n"); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); return viewHolder != null && itemMatcher.matches(viewHolder.itemView); } }; }
public static <T extends RecyclerView.ViewHolder> Matcher<View> atPosition(final int position, @NonNull final ViewHolderMatcher<T> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ":\n"); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); return viewHolder != null && itemMatcher.matches(viewHolder); } }; }
/** * Matches fragment with specific tag * * @param tag fragment tag * @return BaseMatcher<Fragment> */ public static Matcher<Fragment> withTag(final String tag) { return new BoundedMatcher<Fragment, Fragment>(Fragment.class) { @Override public void describeTo(Description description) { description.appendText("Matches fragment tag"); } @Override protected boolean matchesSafely(Fragment item) { String fragTag = item.getTag(); return fragTag != null && fragTag.equals(tag); } }; }
public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ": "); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); return viewHolder != null && itemMatcher.matches(viewHolder.itemView); } }; }
/** * Matcher for finding the SwipeView for a SwipeOpenViewHolders in a RecyclerView * @param position the position of the view holder * @param itemMatcher matcher to compare the SwipeView to * @return a Matcher that compares a SwipeOpenViewHolder at a position with a passed in matcher */ public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override protected boolean matchesSafely(RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); if (viewHolder == null || !(viewHolder instanceof SwipeOpenViewHolder)) { // has no item on such position return false; } return itemMatcher.matches(((SwipeOpenViewHolder) viewHolder).getSwipeView()); } @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ": "); itemMatcher.describeTo(description); } }; }
/** * Checks for a positive or negative translationX in a View * @param positive true if positive translation, false if negative * @return matcher for checking positive/negative translationX */ public static Matcher<View> checkTranslationX(final boolean positive) { return new BoundedMatcher<View, View>(View.class) { @Override public void describeTo(Description description) { description.appendText("translationX should be non-zero"); } @Override protected boolean matchesSafely(View item) { if (positive) { return ViewCompat.getTranslationX(item) > 0; } else { return ViewCompat.getTranslationX(item) < 0; } } }; }
public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ": "); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); if (viewHolder == null) { // has no item on such position return false; } return itemMatcher.matches(viewHolder.itemView); } }; }
public static Matcher<View> onMenuItems(final Matcher<Iterable<MenuItem>> matcher) { return new BoundedMatcher<View, NavigationView>(NavigationView.class) { @Override protected boolean matchesSafely(NavigationView item) { try { viewResources = item.getResources(); return matcher.matches(new IterableMenu(item.getMenu())); } finally { viewResources = null; } } @Override public void describeTo(Description description) { description.appendText("from navigation menu items "); description.appendDescriptionOf(matcher); } }; }
public static Matcher<View> withCompoundDrawable(final int resourceId) { return new BoundedMatcher<View, Button>(Button.class) { @Override public void describeTo(Description description) { description.appendText("has compound drawable resource " + resourceId); } @Override public boolean matchesSafely(Button textView) { for (Drawable drawable : textView.getCompoundDrawables()) { if (sameBitmap(textView.getContext(), drawable, resourceId)) { return true; } } return false; } }; }
private Matcher<? super View> withToolbarBackGroundColor () { return new BoundedMatcher<View, View>( View.class ) { @Override public boolean matchesSafely (View view) { final ColorDrawable backgroundColor = ( ColorDrawable ) view.getBackground( ); return ContextCompat .getColor( activityTestRule.getActivity( ), R.color.colorPrimary ) == backgroundColor.getColor( ); } @Override public void describeTo (Description description) { } }; }
public static Matcher<View> hasErrorText(final String expectedError) { return new BoundedMatcher<View, View>(View.class) { @Override public void describeTo(Description description) { description.appendText("with error: " + expectedError); } @Override protected boolean matchesSafely(View view) { if (!(view instanceof EditText)) { return false; } EditText editText = (EditText) view; return expectedError.equals(editText.getError()); } }; }
/** Returns a matcher that matches Views that are not narrower than specified width in pixels. */ public static Matcher<View> isNotNarrowerThan(final int minWidth) { return new BoundedMatcher<View, View>(View.class) { private String failedCheckDescription; @Override public void describeTo(final Description description) { description.appendText(failedCheckDescription); } @Override public boolean matchesSafely(final View view) { final int viewWidth = view.getWidth(); if (viewWidth < minWidth) { failedCheckDescription = "width " + viewWidth + " is less than minimum " + minWidth; return false; } return true; } }; }
/** Returns a matcher that matches Views that are not wider than specified width in pixels. */ public static Matcher<View> isNotWiderThan(final int maxWidth) { return new BoundedMatcher<View, View>(View.class) { private String failedCheckDescription; @Override public void describeTo(final Description description) { description.appendText(failedCheckDescription); } @Override public boolean matchesSafely(final View view) { final int viewWidth = view.getWidth(); if (viewWidth > maxWidth) { failedCheckDescription = "width " + viewWidth + " is more than maximum " + maxWidth; return false; } return true; } }; }
/** Returns a matcher that matches TextViews with the specified text size. */ public static Matcher<View> withTextSize(final float textSize) { return new BoundedMatcher<View, TextView>(TextView.class) { private String failedCheckDescription; @Override public void describeTo(final Description description) { description.appendText(failedCheckDescription); } @Override public boolean matchesSafely(final TextView view) { final float ourTextSize = view.getTextSize(); if (Math.abs(textSize - ourTextSize) > 1.0f) { failedCheckDescription = "text size " + ourTextSize + " is different than expected " + textSize; return false; } return true; } }; }
/** Returns a matcher that matches Views with the specified background fill color. */ public static Matcher<View> withBackgroundFill(final @ColorInt int fillColor) { return new BoundedMatcher<View, View>(View.class) { private String failedCheckDescription; @Override public void describeTo(final Description description) { description.appendText(failedCheckDescription); } @Override public boolean matchesSafely(final View view) { Drawable background = view.getBackground(); try { TestUtils.assertAllPixelsOfColor( "", background, view.getWidth(), view.getHeight(), true, fillColor, 0, true); } catch (Throwable t) { failedCheckDescription = t.getMessage(); return false; } return true; } }; }
@Test public void testWrongCredentialsValidation() { Espresso.onView(withId(R.id.usernameEditText)).perform(typeText("Akshay")); Espresso.onView(withId(R.id.validateButton)).perform(click()); Espresso.onView(withId(R.id.resultTextView)).check(matches(withText(R.string.failure_message))); Espresso.onView(withId(R.id.resultTextView)).check(matches(new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(Description description) { // do nothing } @Override protected boolean matchesSafely(TextView textView) { int color = textView.getTextColors().getDefaultColor(); int colorToMatch = ContextCompat.getColor(textView.getContext(), R.color.colorAccent); return color == colorToMatch; } })); }
@Test public void testValidCredentialsValidation() { Espresso.onView(withId(R.id.usernameEditText)).perform(typeText("vipul")); Espresso.onView(withId(R.id.validateButton)).perform(click()); Espresso.onView(withId(R.id.resultTextView)).check(matches(withText(R.string.success_message))); Espresso.onView(withId(R.id.resultTextView)).check(matches(new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(Description description) { // do nothing } @Override protected boolean matchesSafely(TextView textView) { int color = textView.getTextColors().getDefaultColor(); int colorToMatch = ContextCompat.getColor(textView.getContext(), R.color.colorPrimary); return color == colorToMatch; } })); }
@Test public void testSpinnerBackgroundColor() { Espresso.onView(withId(R.id.colorSpinner)).perform(click()); Espresso.onData(allOf(is(instanceOf(String.class)), is("Color 1"))).perform(click()); Espresso.onView(withId(R.id.container)).check(matches(new BoundedMatcher<View, ViewGroup>(ViewGroup.class) { @Override public void describeTo(Description description) { } @Override protected boolean matchesSafely(ViewGroup viewGroup) { int color = ((ColorDrawable) viewGroup.getBackground()).getColor(); return color == ContextCompat.getColor(viewGroup.getContext(), R.color.color1); } })); }
/** * Returns a matcher that matches {@link TextView}s based on text property value. Note: View's * text property is never null. If you setText(null) it will still be "". Do not use null * matcher. * * @param integerMatcher {@link Matcher} of {@link String} with text to match */ public static Matcher<View> withCurrentTextColor(final Matcher<Integer> integerMatcher) { checkNotNull(integerMatcher); return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(Description description) { description.appendText("with text color: "); integerMatcher.describeTo(description); } @Override public boolean matchesSafely(TextView textView) { return integerMatcher.matches(textView.getCurrentTextColor()); } }; }
public static Matcher<Object> hasInputType(final int inputType) { return new BoundedMatcher<Object, EditText>(EditText.class) { int v; @Override protected boolean matchesSafely(EditText editText) { v = editText.getInputType(); return v == inputType; } @Override public void describeTo(org.hamcrest.Description description) { description.appendText("has inputyType:" + inputType + ", but has " + v); } }; }
private static Matcher<View> breadcrumbsMatcher(final Func func, final Matcher<Integer> expectedCount, final String msgDescription) { return new BoundedMatcher<View, BreadcrumbsView>(BreadcrumbsView.class) { @Override protected boolean matchesSafely(BreadcrumbsView view) { int nDots = 0; for (int i = 0; i < view.getChildCount(); i++) { View viewChild = view.getChildAt(i); if (viewChild.getTag() != null) { if (func.call((ViewGroup) viewChild)) nDots++; } } return expectedCount.matches(nDots); } @Override public void describeTo(Description description) { description.appendText(msgDescription); expectedCount.describeTo(description); } }; }
public BoundedMatcher<View, ImageView> hasDrawableImageView() { return new BoundedMatcher<View, ImageView>(ImageView.class) { @Override public void describeTo(Description description) { description.appendText("has drawable"); } @Override public boolean matchesSafely(ImageView imageView) { return (imageView.getDrawable() != null); } }; }
public static Matcher<View> atPosition( final int position, @NonNull final Matcher<View> itemMatcher) { checkNotNull(itemMatcher); return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) { @Override public void describeTo(Description description) { description.appendText("has item at position " + position + ": "); itemMatcher.describeTo(description); } @Override protected boolean matchesSafely(final RecyclerView view) { RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position); if (viewHolder == null) { return false; } return itemMatcher.matches(viewHolder.itemView); } }; }
public static Matcher<RecyclerView.ViewHolder> withMessageHolder(final String text, final int textViewId) { return new BoundedMatcher<RecyclerView.ViewHolder, MessageHolder>(MessageHolder.class) { private boolean found = false; @Override public void describeTo(Description description) { description.appendText("No ViewHolder found with text: "); } @Override protected boolean matchesSafely(MessageHolder item) { if (found) return false; found = ((TextView) item.itemView.findViewById(textViewId)).getText().toString().matches(text); return found; } }; }
public static Matcher<RecyclerView.ViewHolder> withMessageHeaderHolder(final MessageType messageType) { return new BoundedMatcher<RecyclerView.ViewHolder, MessageHeaderParent.MessageHeaderHolder>(MessageHeaderParent.MessageHeaderHolder.class) { private boolean found = false; @Override public void describeTo(Description description) { description.appendText("No ViewHolder found with text: " + messageType.toString()); } @Override protected boolean matchesSafely(MessageHeaderParent.MessageHeaderHolder item) { if (found) return false; MessageHeaderParent messageHeaderParent = item.onItemClickListener.getMessageHeaderParentAtPosition(item.getLayoutPosition()); found = (messageType == messageHeaderParent.getMessageType()); return found; } }; }
private Matcher<View> emailFilter() { return new BoundedMatcher<View, Button>(Button.class) { @Override public void describeTo(Description description) { description.appendText("ERROR"); } @Override protected boolean matchesSafely(Button item) { if (!matchedBefore && item.getText().toString().contains("@")) { EMAIL_TEST = item.getText().toString(); matchedBefore = true; return true; } return false; } }; }
/** * This matcher checks if a TextView displays its text in * a specific color. * * @param color The color to verify. * @return Corresponding matcher. */ @NonNull public static Matcher<View> withTextColor(@ColorInt final int color) { Checks.checkNotNull(color); return new BoundedMatcher<View, TextView>(TextView.class) { @Override public boolean matchesSafely(TextView warning) { return color == warning.getCurrentTextColor(); } @Override public void describeTo(Description description) { description.appendText("with text color: "); } }; }
/** * This matcher checks if a View displays its background in * a specific color. * * @param color The color to verify. * @return Corresponding matcher. */ @NonNull public static Matcher<View> withBackgroundColor(@ColorInt final int color) { Checks.checkNotNull(color); return new BoundedMatcher<View, View>(View.class) { @Override public boolean matchesSafely(View warning) { return color == ((ColorDrawable) warning.getBackground()).getColor(); } @Override public void describeTo(Description description) { description.appendText("with text color: "); } }; }
/** * Returns a matcher that matches {@link android.widget.TextView}s based on text property value. * * @param stringMatcher {@link Matcher} of {@link String} with text to match */ @NonNull public static Matcher<View> withErrorText(final Matcher<String> stringMatcher) { return new BoundedMatcher<View, TextView>(TextView.class) { @Override public void describeTo(final Description description) { description.appendText("with error text: "); stringMatcher.describeTo(description); } @Override public boolean matchesSafely(final TextView textView) { return stringMatcher.matches(textView.getError().toString()); } }; }