为什么不正确检查“是”?

时间:2019-06-05 08:05:29

标签: kotlin android-espresso

科特林的摘录:

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

您可以看到对象“ 视图”的类型为androidx.appcompat.widget.AppCompatTextView。我们知道AppCompatTextView源自View

但是为什么打印

withBackgroundColorResId_incorrect_type, view = androidx.appcompat.widget.AppCompatTextView

2 个答案:

答案 0 :(得分:3)

在Kotlin的文档import re json_data = {'descriptions': "Foo: bar\n Blah: Lahb\n Account Id: 12345678\n Dummy: data ..."} text = json_data['descriptions'] match = re.findall(r'Account Id: (.\d+)\n',text) result = None if len(match) > 0: result = match[0] print(result) 中指出:

  

检查值是否具有特定类型

所以根据您的代码,您以一种方式询问

  

我的视图实例是X吗?

在此行之后,您将得到:

  1. is吗?答案是肯定的。
  2. view instanceof View吗?答案是否定的。

单个视图主要由View类扩展。 ViewGroup通常用于更复杂的布局中,例如view instanceof ViewGroupLinearLayout等。

结论,您具有:

FrameLayout = true

答案 1 :(得分:0)

问题在于您的if条件,因为视图不是ViewGroup,条件始终为true,因此将打印LOG。

if (view is View) {
     Log.w(TAG, "withBackgroundColorResId_incorrect_type, view = $view")
     return false
}

请尝试此操作并查看结果。