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

前端 未结 3 461
暗喜
暗喜 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:49

    You can't test TextView with android:ellipsize by checking whether it ends with "..."

    That is because even if you see it ends with "...", but it actually doesn't end with "...".

    To verify this you can do this test

    @Test
    fun check_textview_text() {
    
        //given
        val errorMessage = """
                Very long error, Very long error, Very long error, Very long error, Very long error,
                Very long error, Very long error, Very long error, Very long error, Very long error,
                Very long error, Very long error, Very long error, Very long error, Very long error,
                Very long error, Very long error, Very long error, Very long error, Very long 
        error,"""
    
        //when
        presentErrorActivityWith(errorMessage)
    
        //then
        onView(withId(R.id.errorMessageTextView)).check(matches(withText(containsString(errorMessage ))));
    }
    
    

    This test must pass if your TextView contains the same string as in errorMessage variable. Although you actually see the elipsize three dots "..." on the screen instead.

    Because actually under the hood android system doesn't set the "..." to end of the truncated string of the text field of the TextView.

    In order to test android:ellipsize the right way:

    @RunWith(AndroidJUnit4.class)
    public class TextViewLongTextWithEllipsizeTest {
        private static final String TAG = "LOG_TAG";
    
        @Rule
        public ActivityTestRule mActivityRule = new ActivityTestRule<>(
                LongTextActivity.class);
    
        /**
         * Testing if a TextView uses "android:ellipsize"
         * at the end of its text as the text is long
         */
        @Test
        public void testTextIsEllipsized() {
    
            TextView textView = mActivityRule.getActivity().findViewById(R.id.errorMessageTextView);
    
            Layout layout = textView.getLayout();
            if (layout != null) {
                int ellipsisCount = layout.getEllipsisCount(layout.getLineCount() - 1);
                assertThat(ellipsisCount, is(greaterThan(0))); // if true then text is ellipsized
            }
        }
    
        /**
         * Testing if a TextView doesn't use "android:ellipsize"
         * at the end of its text as the text is not that long
         */
        @Test
        public void testTextNotEllipsized() {
    
            TextView textView = mActivityRule.getActivity().findViewById(R.id.no_ellipsize_text);
    
            Layout layout = textView.getLayout();
            if (layout != null) {
                int ellipsisCount = layout.getEllipsisCount(layout.getLineCount() - 1);
                assertThat(ellipsisCount, not(greaterThan(0))); // if true then text is not ellipsized
            }
        }
    
    
    }
    

    Hint: I just added the test method, please replicate the test given and when statements before testing.

提交回复
热议问题