我最近尝试过MotionLayout
,但Espresso
由于将我的转换ConstraintLayout
为MotionLayout
.
这是一个重现的示例:
我的浓缩咖啡测试:
@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()))
}
}
我的布局文件:
<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>
我的动态场景是:
<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>
并且测试失败并出现以下错误:
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: <package>:id/appIcon
注意:测试ConstraintLayout
在activity_splash
文件中时通过了。