Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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           
}

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

like image 296
kdas Avatar asked Mar 16 '23 09:03

kdas


1 Answers

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

Usage:

onView(withId(R.id.activity_main_text_tv))
            .check(matches(CustomMatchers.isTextInLines(1)));
like image 193
Wojtek Avatar answered Mar 24 '23 15:03

Wojtek