如何在Espresso测试中测试单词样式“ITALIC”

时间:2017-08-22 03:24:14

标签: java android junit4 android-espresso italic

我在测试ITALIC风格的显示字时遇到问题。有人可以提供任何示例代码来显示单词样式吗?我在android studio中使用Espresso和JUnit 4。我非常感谢你的合作。谢谢

2 个答案:

答案 0 :(得分:0)

这应该会同时使您的TextView 粗体带下划线斜体

<强>的strings.xml

 <resources>
    <string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>

要将此String设置为TextView,请在 main.xml

中执行此操作
  <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/register" />

Result

答案 1 :(得分:0)

请尝试以下解决方案。它可能适合你。 核心思想是考虑为您的案例使用自定义ViewMatcher。

public static Matcher<View> withItalicStyle(final int resourceId) {
    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("has Italic Text with resource" );
        }

        @Override
        public boolean matchesSafely(View view) {
            TextView textView = (TextView) view.findViewById(resourceId);
            return (textView.getTypeface().getStyle() == Typeface.ITALIC);
        }
    };
}

在你的测试用例中,你可以

    onView(CustomMatchers.withItalicStyle(R.id.yourResourceId)).check(isDisplayed());

有关教程,请查看https://github.com/googlesamples/android-testing/blob/master/ui/espresso/IdlingResourceSample/app/src/main/java/com/example/android/testing/espresso/IdlingResourceSample/MainActivity.java

中的goole样本