1

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)));
Julie
  • 65
  • 4
  • if ( myString.endsWith("...")) something like that? – Stultuske Jun 08 '20 at 07:07
  • I don't know how to get the actually displayed string. Getting the string from the action_bar always gets the full string even if only a part is displayed – Julie Jun 08 '20 at 08:01

1 Answers1

0

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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305