Здесь мой пользовательский сопоставитель Espresso, а не цвет фона проверки представления:
fun withBackgroundColorResId(@IdRes expectedId: Int): Matcher<View> {
return object : BoundedMatcher<View, ViewGroup>(ViewGroup::class.java) {
override fun matchesSafely(view: ViewGroup): Boolean {
Log.d(TAG, "withBackgroundColorResId_matchesSafely = $view")
val currenColor = (view.background.current as ColorDrawable).color
val expectedColor = ContextCompat.getColor(view.context, expectedId)
return currenColor == expectedColor
}
override fun describeTo(description: Description) {
Log.d(TAG, "withBackgroundColorResId_describeTo_test_here")
description.appendText("with background color resId: ")
description.appendValue(context.getResources().getString(expectedId))
}
}
}
Вот как я его использую:
onView(withRecyclerView(tradersRecyclerView).atPositionOnView(traderCheckPos, resumeTextView))
.check(matches(withBackgroundColorResId(trade_not_running_color)))
В logcat у меня есть следующая ошибка:
06-04 15:49:46.562 I/ViewInteraction(17086): Performing 'actionOnItemAtPosition performing ViewAction: fast swipe on item at position: 0' action on view with id: com.myptoject.android.debug:id/tradersRecyclerView
06-04 15:49:46.824 D/com.myptoject.android.custom.matcher.CustomMatchers(17086): withBackgroundColorResId_describeTo_test_here
06-04 15:49:46.824 I/ViewInteraction(17086): Checking 'MatchesViewAssertion{viewMatcher=with background color resId: "#ffbde6ff"}' assertion on view RecyclerView with id: com.myptoject.android.debug:id/tradersRecyclerView at position: 0
06-04 15:49:46.824 D/com.myptoject.android.custom.matcher.CustomMatchers(17086): withBackgroundColorResId_describeTo_test_here
06-04 15:49:46.825 D/com.myptoject.android.custom.matcher.CustomMatchers(17086): withBackgroundColorResId_describeTo_test_here
06-04 15:49:46.831 D/com.myptoject.android.activity.TradersActivityTest(17086): afterEach
06-04 15:49:46.831 I/MockWebServer(17086): MockWebServer[8081] done accepting connections: Socket closed
06-04 15:49:46.859 D/LifecycleMonitor(17086): Lifecycle status change: com.myptoject.android.ui.activity.TradersActivity@968c687 in: PAUSED
06-04 15:49:46.859 D/LifecycleMonitor(17086): running callback: androidx.test.rule.ActivityTestRule$LifecycleCallback@e7fe13f
06-04 15:49:46.859 D/LifecycleMonitor(17086): callback completes: androidx.test.rule.ActivityTestRule$LifecycleCallback@e7fe13f
06-04 15:49:46.878 E/TestRunner(17086): failed: traderOne_pauseItem_click_resumeItem_bg_color(com.myptoject.android.activity.TradersActivityTest)
06-04 15:49:46.878 E/TestRunner(17086): ----- begin exception -----
06-04 15:49:46.879 E/TestRunner(17086): androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with background color resId: "#ffbde6ff"' doesn't match the selected view.
06-04 15:49:46.879 E/TestRunner(17086): Expected: with background color resId: "#ffbde6ff"
06-04 15:49:46.879 E/TestRunner(17086): Got: "AppCompatTextView{id=2131230930, res-name=resumeTextView, visibility=VISIBLE, width=288, height=288, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@f453299, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Resume, input-type=0, ime-target=false, has-links=false}"
06-04 15:49:46.879 E/TestRunner(17086):
06-04 15:49:46.879 E/TestRunner(17086): at dalvik.system.VMStack.getThreadStackTrace(Native Method)
06-04 15:49:46.879 E/TestRunner(17086): at java.lang.Thread.getStackTrace(Thread.java:580)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:318)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
06-04 15:49:46.879 E/TestRunner(17086): at com.myptoject.android.activity.TradersActivityTest.traderOne_pauseItem_click_resumeItem_bg_color(TradersActivityTest.kt:662)
06-04 15:49:46.879 E/TestRunner(17086): at java.lang.reflect.Method.invoke(Native Method)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.rules.RunRules.evaluate(RunRules.java:20)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.Suite.runChild(Suite.java:128)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.Suite.runChild(Suite.java:27)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
06-04 15:49:46.879 E/TestRunner(17086): at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
06-04 15:49:46.879 E/TestRunner(17086): at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
06-04 15:49:46.879 E/TestRunner(17086): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
06-04 15:49:46.879 E/TestRunner(17086): Caused by: junit.framework.AssertionFailedError: 'with background color resId: "#ffbde6ff"' doesn't match the selected view.
06-04 15:49:46.879 E/TestRunner(17086): Expected: with background color resId: "#ffbde6ff"
06-04 15:49:46.879 E/TestRunner(17086): Got: "AppCompatTextView{id=2131230930, res-name=resumeTextView, vis
06-04 15:49:46.879 E/TestRunner(17086): ----- end exception -----
Как видите, журнал withBackgroundColorResId_describeTo_test_here
является печатью, но журнал
withBackgroundColorResId_matchesSafely
НЕ печатается.
Почему метод matchesSafely
не вызывается?