Skip to content

Instantly share code, notes, and snippets.

@Ben834
Created January 25, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ben834/612cb28e8e5095a0b6f5 to your computer and use it in GitHub Desktop.
Save Ben834/612cb28e8e5095a0b6f5 to your computer and use it in GitHub Desktop.
Espresso matchers
package com.djit.apps.karaokey.matcher;
import android.content.res.Resources;
import android.support.annotation.StringRes;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.view.View;
import android.widget.EditText;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static org.hamcrest.Matchers.is;
/**
* A custom {@link org.hamcrest.Matcher} for Espresso that checks if an {@link android.widget.EditText}
* displayds the right error message.
*/
public class EditTextHasError {
private EditTextHasError() {
//Non-instantiable
}
public static BoundedMatcher<View, EditText> withError(final CharSequence substring) {
return withError(is(substring));
}
/**
* Returns a matcher that matches {@link EditText}s based on text property value.
*
* @param stringMatcher : The {@link Matcher} of {@link String} with text to match.
* @return : The newly created {@link Matcher}
*/
private static BoundedMatcher<View, EditText> withError(final Matcher<CharSequence> stringMatcher) {
return new BoundedMatcher<View, EditText>(EditText.class) {
@Override
protected boolean matchesSafely(EditText editText) {
return stringMatcher.matches(editText.getError().toString());
}
@Override
public void describeTo(Description description) {
description.appendText("with error");
}
};
}
/**
* Returns a matcher that matches {@link EditText} based on the error string id.
*
* @param stringId : The ID of the string displayed as an error.
* @return The newly created {@link Matcher}
*/
public static BoundedMatcher<View, EditText> withError(@StringRes final int stringId) {
return new BoundedMatcher<View, EditText>(EditText.class) {
private String resourceName = null;
private String expectedText = null;
@Override
protected boolean matchesSafely(EditText editText) {
if (expectedText == null) {
try {
expectedText = editText.getResources().getString(stringId);
resourceName = editText.getResources().getResourceEntryName(stringId);
} catch (Resources.NotFoundException ignored) {
// view could be from a context unaware of the resource id
}
}
return null != expectedText && expectedText.equals(editText.getError());
}
@Override
public void describeTo(Description description) {
description.appendText("with error string id");
description.appendValue(stringId);
if (null != resourceName) {
description.appendText("[");
description.appendText(resourceName);
description.appendText("]");
}
if (null != expectedText) {
description.appendText(" value: ");
description.appendText(expectedText);
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment