Espresso Android测试-检查IntroductoryOverlay是否处于活动状态

问题描述 投票:1回答:1

可以使用Android Espresso进行检查IntroductoryOverlay是否处于活动状态?

如果是,如何在测试中消除IntroductoryOverlay?

enter image description here

android android-espresso
1个回答
0
投票

您可以创建一个自定义的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()))
© www.soinside.com 2019 - 2024. All rights reserved.