Created
January 25, 2016 14:35
-
-
Save Ben834/612cb28e8e5095a0b6f5 to your computer and use it in GitHub Desktop.
Espresso matchers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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