Issue
I want to check if the string of my action bar is not too long and gets abbreviated with "...". I'm using this code to check if the title of my action bar is correct. It does not fail if the string is too long and gets abbreviated. I also tried using .isCompletelyDisplayed but that doesn't work either.
onView(withId(R.id.action_bar).check(matches(withText(text)));
Solution
with a normal textview you could write a small Matcher
that checks if getEllipsisCount
is greater than 0. With the toolbar, unless you have a custom TextView
for the tile, it is slightly more tricky. You will have to look in the Toolbar
views for the title view (using getChildAt
and getChildCount
) and doing the same check. Something like
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
}
}
and then use it like
onView(withId(R.id.action_bar)).check(matches(matcher))
haven't test it myself - but you get the idea. Also you can check the code LayoutMatchers
to take some inspirations
Answered By - Blackbelt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.