Android。 Espresso:如何编写测试检查文本是否在 TextView 上加下划线?

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

这是我的 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>

作为带下划线的结果文本。不错

但我想编写 Espresso 测试来检查 TextView 中的文本是否有下划线?我该怎么做?

如果是 Espresso 测试,则只需创建一个新的匹配器,如下所示:

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()));