Issue
Here my xml layout:
<TextView
android:id="@+id/forgotPasswordTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:onClick="@{ () -> presenter.doForgotPassword()}"
android:text="@string/forgot_password"
android:textColor="#bbbbbb"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/loginTextView"
app:layout_constraintStart_toStartOf="@+id/loginTextView" />
Here @string/forgot_password
<string name="forgot_password"><u>Forgot password?</u></string>
As result text in underlined. Nice.
But I want to write Espresso test to check is text in TextView is underline?How I can do this?
Solution
If it's an Espresso test then just create a new matcher as the following:
public static Matcher<View> withUnderlinedText() {
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override
protected boolean matchesSafely(TextView textView) {
CharSequence charSequence = textView.getText();
UnderlineSpan[] underlineSpans = ((SpannedString) charSequence).getSpans(0, charSequence.length(), UnderlineSpan.class);
return underlineSpans != null && underlineSpans.length > 0;
}
@Override
public void describeTo(Description description) {
}
};
}
And use it as below:
onView(withId(R.id.forgotPasswordTextView)).check(matches(withUnderlinedText()));
Answered By - Anatolii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.