【发布时间】:2020-05-26 00:57:30
【问题描述】:
我似乎已经跌到谷底,试图找到一个解决方案,说明如何在我的 Espresso 测试中测试我的 ImageButton 是否具有特定的背景可绘制对象。但我不断收到我粘贴在下面的错误。我尽可能多地提供了我认为相关的代码。
图像按钮
<ImageButton
android:id="@+id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_background_selector"
android:clickable="true"
android:src="@drawable/background" />
选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_background_error" android:state_activated="true" />
<item android:drawable="@drawable/ic_background_okay" android:state_activated="false" />
</selector>
自定义匹配器:
fun matchColor(expectedId: Int): Matcher<View> {
return object : BoundedMatcher<View, ImageButton>(ImageButton::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("with text color: ")
description?.appendValue(expectedId)
}
override fun matchesSafely(item: ImageButton?): Boolean {
return item?.context?.resources?.getDrawable(expectedId)?.constantState == item?.drawable?.constantState
}
}
}
测试:
onView(withId(R.id.image_button)).check(matches(matchColor(R.drawable.ic_background_okay)))
我收到以下错误:
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text
color: <2131165331>' doesn't match the selected view. Expected: with text color:
<2131165331>Got: "AppCompatImageButton{id=2131296363, ...}
【问题讨论】:
-
我打算为此写一个解决方案,但是您是否也遇到了图像视图问题?或者你所有的问题都与ImageButton有关
标签: android android-espresso imagebutton