Вот фрагмент Котлина:
fun withBackgroundColorResId(@IdRes expectedId: Int): Matcher<Any> {
return object : BoundedMatcher<Any, Any>(Any::class.java) {
override fun matchesSafely(view: Any): Boolean {
if (view !is View || view !is ViewGroup) {
Log.w(TAG, "withBackgroundColorResId_incorrect_type, view = $view")
return false
}
val currenColor = (view.background.current as ColorDrawable).color
val expectedColor = ContextCompat.getColor(view.context, expectedId)
return currenColor == expectedColor
}
override fun describeTo(description: Description) {
description.appendText("textView with background color resId: ")
description.appendValue(context.getResources().getString(expectedId))
}
}
}
и использование:
onView(withRecyclerView(tradersRecyclerView).atPositionOnView(traderCheckPos, pauseTextView)).check(matches(withBackgroundColorResId(trade_not_running_color)))
и здесь logcat:
06-05 10:25:12.787 I/ViewInteraction(22053): Checking 'MatchesViewAssertion{viewMatcher=textView with background color resId: "#ffbde6ff"}' assertion on view RecyclerView with id: com.myproject.debug:id/tradersRecyclerView at position: 0
06-05 10:25:12.787 W/com.myproject.custom.matcher.CustomMatchers(22053): withBackgroundColorResId_incorrect_type, view = androidx.appcompat.widget.AppCompatTextView{a412c35 V.ED..C.. ........ 0,0-288,288 #7f0800c9 app:id/pauseTextView}
06-05 10:25:12.794 D/com.myproject.activity.TradersActivityTest(22053): afterEach
06-05 10:25:12.794 I/MockWebServer(22053): MockWebServer[8081] done accepting connections: Socket closed
06-05 10:25:12.825 D/LifecycleMonitor(22053): Lifecycle status change: com.myproject.ui.activity.TradersActivity@2964795 in: PAUSED
06-05 10:25:12.825 D/LifecycleMonitor(22053): running callback: androidx.test.rule.ActivityTestRule$LifecycleCallback@9705558
06-05 10:25:12.825 D/LifecycleMonitor(22053): callback completes: androidx.test.rule.ActivityTestRule$LifecycleCallback@9705558
как вы можете видеть, объект " view " имеет тип androidx.appcompat.widget.AppCompatTextView
. Как известно AppCompatTextView
простирается от View
а зачем печатать
withBackgroundColorResId_incorrect_type, view = androidx.appcompat.widget.AppCompatTextView