您可以创建一个自定义的ViewAction
以通过其InventoryOverlay
功能关闭remove:
fun hideIntroductoryOverlay(): ViewAction = object : ViewAction {
override fun getDescription(): String = "hide introductory overlay"
override fun getConstraints(): Matcher<View> = isRoot()
override fun perform(uiController: UiController, view: View) {
val matcher = any(IntroductoryOverlay::class.java)
val matches = TreeIterables.breadthFirstViewTraversal(view).filter(matcher::matches)
val overlay = matches[0] as IntroductoryOverlay
overlay.remove()
}
}
如果过滤器返回空数组,则表示未找到IntroductoryOverlay
。如果数组大小大于1,则表示找到多个IntroductoryOverlay
。否则将其关闭:
onView(isRoot()).perform(hideIntroductoryOverlay())
您还可以通过自定义InventoryOverlay
检查ViewMatcher
是否处于活动状态:
fun hasIntroductoryOverlay(): Matcher<View> = object : BoundedMatcher<View, ViewGroup>(ViewGroup::class.java) {
override fun describeTo(description: Description) {
description.appendText("has introductory overlay")
}
override fun matchesSafely(item: ViewGroup): Boolean {
val matcher = any(IntroductoryOverlay::class.java)
return TreeIterables.breadthFirstViewTraversal(item).any(matcher::matches)
}
}
并在根视图上执行检查:
onView(isRoot()).check(matches(hasIntroductoryOverlay()))