简体   繁体   English

Android Espresso:检查 TextView 是否显示来自资源的格式化字符串

[英]Android Espresso: checking if a TextView is showing a formatted string from resource

Suppose you have a formatted string like this:假设你有一个这样的格式化字符串:

<string name="saving">You saved %1$s with %2$s</string>

now if you want to check if your TextView is showing it correctly, so naturally, something like this would work:现在,如果您想检查您的 TextView 是否正确显示它,那么很自然地,这样的事情会起作用:

 Espresso.onView(withId(R.id.tv))
            .check(matches(withText(R.string.saving)))

but it doesn't and I cannot check the text with hardcoded strings because it might be on another language.但它没有,我不能用硬编码字符串检查文本,因为它可能是另一种语言。 so is there even a way for that?那么有没有办法做到这一点?

Maybe something like:也许是这样的:

Espresso.onView(withId(R.id.tv))
        .perform(object :ViewAction{
            override fun getDescription(): String {
                return "Normalizing the string"
            }

            override fun getConstraints(): Matcher<View> {
                return isAssignableFrom(TextView::class.java)
            }

            override fun perform(uiController: UiController?, view: View?) {
                val tv = view as TextView
                if (tv.text.matches(Regex("You saved (.)*? with (.)*"))) {
                    tv.text = "You saved %1\$s with %2\$s"
                }
            }
        }).check(matches(withText(R.string.saving)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM