为什么不正确检查 "is"?
Why not correct check "is"?
这里是 Kotlin 的代码片段:
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
?
问题出在您的 if 条件上,因为视图不是 ViewGroup,条件始终为真,因此将打印日志。
if (view is View) {
Log.w(TAG, "withBackgroundColorResId_incorrect_type, view = $view")
return false
}
请尝试这个,看看结果。
在 Kotlin 的文档中 is
指出:
checks that a value has a certain type
所以根据你的代码,你以某种方式提问
is my view instance of X?
跟随这一行你得到:
- 是
view instanceof View
吗?答案是肯定的。
- 是
view instanceof ViewGroup
吗?答案是否定的。
单一视图主要通过视图扩展class。 ViewGroups 主要用于更复杂的布局,如 LinearLayout
、FrameLayout
等
总结结果,您有:
true||false
= 真
这里是 Kotlin 的代码片段:
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
?
问题出在您的 if 条件上,因为视图不是 ViewGroup,条件始终为真,因此将打印日志。
if (view is View) {
Log.w(TAG, "withBackgroundColorResId_incorrect_type, view = $view")
return false
}
请尝试这个,看看结果。
在 Kotlin 的文档中 is
指出:
checks that a value has a certain type
所以根据你的代码,你以某种方式提问
is my view instance of X?
跟随这一行你得到:
- 是
view instanceof View
吗?答案是肯定的。 - 是
view instanceof ViewGroup
吗?答案是否定的。
单一视图主要通过视图扩展class。 ViewGroups 主要用于更复杂的布局,如 LinearLayout
、FrameLayout
等
总结结果,您有:
true||false
= 真