Why android espresso test fails when checking whether textView text ends with expected string (when ellipsized)

前端 未结 3 464
暗喜
暗喜 2021-01-24 08:06

I have an android test checking that external text message is truncated and ends with three dots when applying android:ellipsize=\"end\". I do not know why test fails despite te

3条回答
  •  借酒劲吻你
    2021-01-24 08:48

    To check if the text of a TextView has been ellipsized you can create your custom matcher like this one:

    fun ellipsized() = object : TypeSafeMatcher() {
        override fun describeTo(description: Description) {
            description.appendText("with ellipsized text")
        }
    
        override fun matchesSafely(v: View): Boolean {
            if (!(v is TextView)) {
                return false
            }
            val textView: TextView = v
            val layout: Layout = textView.getLayout()
            val lines = layout.lineCount
            if (lines > 0) {
                val ellipsisCount = layout.getEllipsisCount(lines - 1)
                if (ellipsisCount > 0) {
                    return true
                }
            }
    
            return false
        }
    }
    

    And call it this way:

    onView(withId(R.id.errorMessageTextView))
        .check(matches(ellipsized()))
    

提交回复
热议问题