Android Espresso - 如何检查字符串是否被缩写
Android Espresso - How to check if string was abbreviated
我想检查我的操作栏的字符串是否太长并且缩写为“...”。
我正在使用此代码来检查我的操作栏的标题是否正确。如果字符串太长并被缩写,它不会失败。
我也尝试过使用 .isCompletelyDisplayed 但这也不起作用。
onView(withId(R.id.action_bar).check(matches(withText(text)));
使用普通的文本视图,您可以编写一个小的 Matcher
来检查 getEllipsisCount
是否大于 0。使用工具栏,除非您有自定义的 TextView
磁贴,这有点棘手。您必须查看标题视图的 Toolbar
视图(使用 getChildAt
和 getChildCount
)并进行相同的检查。像
val matcher = object : BoundedMatcher<View, Toolbar>(Toolbar::class.java) {
override fun describeTo(description: Description?) {
}
override fun matchesSafely(item: Toolbar?): Boolean {
for (i in 0 until (item?.childCount ?: 0)) {
val v = item?.getChildAt(i)
(v as? TextView)?.let {
// check somehow that this textview is the title
val lines = it.layout.lineCount
if (it.layout.getEllipsisCount(lines - 1) > 0) {
return true
}
}
}
return false
}
}
然后像
一样使用它
onView(withId(R.id.action_bar)).check(matches(matcher))
我自己还没有测试过 - 但你明白了。您也可以查看代码 LayoutMatchers
以获取一些灵感
我想检查我的操作栏的字符串是否太长并且缩写为“...”。 我正在使用此代码来检查我的操作栏的标题是否正确。如果字符串太长并被缩写,它不会失败。 我也尝试过使用 .isCompletelyDisplayed 但这也不起作用。
onView(withId(R.id.action_bar).check(matches(withText(text)));
使用普通的文本视图,您可以编写一个小的 Matcher
来检查 getEllipsisCount
是否大于 0。使用工具栏,除非您有自定义的 TextView
磁贴,这有点棘手。您必须查看标题视图的 Toolbar
视图(使用 getChildAt
和 getChildCount
)并进行相同的检查。像
val matcher = object : BoundedMatcher<View, Toolbar>(Toolbar::class.java) {
override fun describeTo(description: Description?) {
}
override fun matchesSafely(item: Toolbar?): Boolean {
for (i in 0 until (item?.childCount ?: 0)) {
val v = item?.getChildAt(i)
(v as? TextView)?.let {
// check somehow that this textview is the title
val lines = it.layout.lineCount
if (it.layout.getEllipsisCount(lines - 1) > 0) {
return true
}
}
}
return false
}
}
然后像
一样使用它 onView(withId(R.id.action_bar)).check(matches(matcher))
我自己还没有测试过 - 但你明白了。您也可以查看代码 LayoutMatchers
以获取一些灵感