在espresso中,我如何测试textview只显示两行

时间:2015-07-15 05:02:26

标签: android android-espresso

我有一个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中第三行是不可见的?

1 个答案:

答案 0 :(得分:4)

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

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)));