Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a TextView's text is completely displayed with Android Espresso

In Espresso it's possible to check if the view is displayed:

onView(withText("To create a test configuration in Android Studio, complete the following steps")).check(matches(isDisplayed()));

But what I need to check is if the text of my TextView is completely displayed (not the TextView itself). Here is my XML layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="256dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="#fca"
        android:textSize="48sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

If the text of the TextView is "To create a test configuration in Android Studio, complete the following steps," it's not completely displayed:

enter image description here

If the text is shorter, I see it all:

enter image description here

Putting the TextView in a ScrollView is not an option. Autosizing TextViews is not an option, either.

Sure, I could change the text and make sure it's completely displayed using the Preview view of the XML layout editor in Android Studio. But what if I have 6 locales, 9 emulators and 5 screens to check. 6 * 9 * 5 = 270 screens! It's time-consuming to test this thing manually.

like image 279
Maksim Dmitriev Avatar asked Oct 30 '22 00:10

Maksim Dmitriev


2 Answers

Although I'm not directly answering your questions, here are suggestions:

  • Use pseudo-localization to manually test only once

Pseudo-localization is a testing method available in Android that will replace your production-texts with test-texts that usually cause UI/translation problems. Basically if your app is still readable with a pseudo-locale, you're good to go.

doc on how to use it: https://developer.android.com/guide/topics/resources/pseudolocales.html

  • Starting the version 26.0 from the Support Library you can use auto sizing on your TextView to make sure your tests are always displayed

TextView is now capable of automatically shrinking the text font size so that the whole text fits within its boundaries.

doc: https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

like image 80
Gauthier Avatar answered Nov 13 '22 03:11

Gauthier


You can also use LayoutAsserions.noEllipsizedText() that checks for cut-off and ellipsized text combined with something like Screengrab that lets you change the locale of the device during the test execution.

I am not sure how to solve the device fragmentation without launching multiple emulators(devices), but I think you can generalize them by their pixel density and do device rotation if needed during the test run as well.

like image 24
Be_Negative Avatar answered Nov 13 '22 05:11

Be_Negative