在浓缩咖啡中,如何测试 textview 只显示两行 [英] In espresso, how can I test that textview shows only two lines

查看:29
本文介绍了在浓缩咖啡中,如何测试 textview 只显示两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a TextView which has the following text: "line1.\nline2.\n line3". I set the TextView to be ellipsized at the end and limit to maximum two lines. To test that it shows ellipses, I have this code

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 中,第三行不可见?

How can I write a test case to verify that in UI, the third line is not visible?

推荐答案

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

I wrote that matcher to check lines count:

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

这篇关于在浓缩咖啡中,如何测试 textview 只显示两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆