Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. Espresso: How write test check is text is underlined on TextView?

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?

enter image description here

like image 433
Alexei Avatar asked Sep 20 '25 11:09

Alexei


1 Answers

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()));
like image 80
Anatolii Avatar answered Sep 23 '25 20:09

Anatolii