0

I have recently tried MotionLayout but found all my Espresso failed because of converting my ConstraintLayout to MotionLayout.

Here is a sample to repro:

My Espresso Test:

@RunWith(AndroidJUnit4::class)
@LargeTest
class SplashActivityTest {

    @get:Rule
    val rule = ActivityScenarioRule(SplashActivity::class.java)
    private val splashScreenWaitingTime = 2000L

    @Test
    fun imageViewHasAnImagePassedByClientThroughMetaData() {
        onView(withId(R.id.appIcon)).check(matches(isDisplayed()))
    }

    @Test
    fun textViewHasAppNamePassedByClientThroughMetaData() {
        onView(withId(R.id.appName)).check(matches(isDisplayed()))
    }
}

My Layout File:

<androidx.constraintlayout.motion.widget.MotionLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/splashLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/splash_scene">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/appIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/app_icon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/appName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:text="@string/app_name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appIcon"/>

</androidx.constraintlayout.motion.widget.MotionLayout>

And my Motion Scene is:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="2000"
        motion:autoTransition="animateToEnd">
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/appIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"/>
        <Constraint
            android:id="@+id/appName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/appIcon">
            <CustomAttribute
                motion:attributeName="textSize"
                motion:customFloatValue="24" />
        </Constraint>
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/appIcon"
            android:layout_width="300dp"
            android:layout_height="300dp"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintBottom_toBottomOf="parent"/>
        <Constraint
            android:id="@+id/appName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/appIcon">
            <CustomAttribute
                motion:attributeName="textSize"
                motion:customFloatValue="43" />
        </Constraint>
    </ConstraintSet>
</MotionScene>

And the test fails with below error:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: <package>:id/appIcon

NOTE: The tests were passing when ConstraintLayout was there in activity_splash file.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125

0 Answers0