如何在 Espresso 测试中测试文字样式“ITALIC”

问题描述 投票:0回答:2

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

java android junit4 android-espresso italic
2个回答
3
投票

请尝试以下解决方案。它可能对你有用。 核心思想是考虑为您的案例使用自定义 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/ 中的 goole 示例浓缩咖啡/IdlingResourceSample/MainActivity.java


0
投票

基于 Wae 的解决方案,但使用

BoundedMatcher
(和 Kotlin):

fun hasTextStyle(textStyle: Int): Matcher<View> {
    return object : BoundedMatcher<View,TextView>(TextView::class.java) {
        override fun describeTo(description: Description) {
            description.appendText("has specified text style")
        }

        override fun matchesSafely(item: TextView): Boolean {
            return item.typeface.style == textStyle
        }
    }
}

onView(withId(R.id.example)).check(matches(hasTextStyle(Typeface.ITALIC)))

一样使用它
© www.soinside.com 2019 - 2024. All rights reserved.