在浓缩咖啡中,我如何测试 textview 只显示两行

In espresso, how can I test that textview shows only two lines

我有一个 TextView,其中包含以下文本:"line1.\nline2.\n line3"。 我将 TextView 设置为在末尾省略并限制为最多两行。为了测试它是否显示省略号,我有这个代码

public static final String TEST_BODY = "line1.\nline2.\nline3.";
//a setup method is called so that mail_cell_body_preview_field is set to above TEST_BODY
//...  

public void testMessageSnippetIsVisible() {
    onView(withId(R.id.mail_cell_body_preview_field)).check(matches(isDisplayed()));
    onView(withId(R.id.mail_cell_body_preview_field)).check(matches(hasEllipsizedText()));
    onView(withId(R.id.mail_cell_body_preview_field)).check(matches(withText(TEST_BODY)));
    //Add statement to verify that line3 not visible in UI           
}

如何编写测试用例来验证 UI 中的第三行不可见?

onView(withId(R.id.mail_cell_body_preview_field)).check(matches( ......compare length for this...);

将文本与您的文本长度进行比较。就像如果您有 2 行,那么文本长度会更长。

我写了那个匹配器来检查行数:

public static TypeSafeMatcher<View> isTextInLines(final int lines) {
    return new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View item) {
            return ((TextView) item).getLineCount() == lines;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("isTextInLines");
        }
    };
}

用法:

onView(withId(R.id.activity_main_text_tv))
            .check(matches(CustomMatchers.isTextInLines(1)));