0

I have a test that checks to see if a dialog is present or not.

@Test
fun dismissedWhenClicked() {
    //dimiss dialog
    onView(withText(R.string.simple)).inRoot(isDialog()).perform(click())
    //check dialog
    onView(isRoot()).inRoot(isDialog()).check(matches(not(isDisplayed())))
}

above is my best guess, but fails because Matcher 'is dialog' did not match any of the following roots

i have found 3 questions on here that address it but none seem to solve it.

Espresso check if no dialog is displayed - the comment works but it also passes when there is a dialog

Check the dialog is visible - Espresso - this doesn't check, instead it will just fail gracefully, i think.

espresso: Assert a Dialog is not shown - seems to have no answer.

glend
  • 1,592
  • 1
  • 17
  • 36
  • see also: https://stackoverflow.com/a/73894325/191761 a bit of a horrible solution I used, but couldn't find any alternative – Adam Burley Sep 29 '22 at 22:52

1 Answers1

0

I have solved this with a custom matcher modified slightly from here

@Test
    fun dismissedWhenClicked() {
        onView(withText(R.string.simple)).inRoot(isDialog()).perform(click())
        onView(withId(R.id.fragment_layout)).inRoot(Utils.ActivityMatcher()).check(matches(isDisplayed()))
    }
class ActivityMatcher : TypeSafeMatcher<Root>() {
        override fun describeTo(description: Description) {
            description.appendText("is activity")
        }

        public override fun matchesSafely(root: Root): Boolean {
            val type: Int = root.windowLayoutParams.get().type
            if (type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION) {
                val windowToken: IBinder = root.decorView.windowToken
                val appToken: IBinder = root.decorView.applicationWindowToken
                if (windowToken === appToken) {
                    //means this window isn't contained by any other windows.
                    return true
                }
            }
            return false
        }
    }
glend
  • 1,592
  • 1
  • 17
  • 36