Android.Эспрессо: Как проверить проверку записи, если текст подчеркнут в TextView? - PullRequest
0 голосов
/ 12 июня 2018

Здесь мой xml-макет:

<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" />

Здесь @ string / Forgot_password

<string name="forgot_password"><u>Forgot password?</u></string>

В результате текст подчеркнут.Хорошо.

Но я хочу написать эспрессо-тест, чтобы проверить, текст в TextView подчеркнут? Как я могу это сделать?

enter image description here

1 Ответ

0 голосов
/ 12 июня 2018

Если это тест на эспрессо, просто создайте новый сопоставитель следующим образом:

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) {
        }
    };
}

и используйте его, как показано ниже:

onView(withId(R.id.forgotPasswordTextView)).check(matches(withUnderlinedText()));
...