Since AndroidX release we are using Robolectric with Espresso in our unit tests. But we are unable to find a way to test dialogs. Espresso community suggests we do:
onView(withId(android.R.id.button1))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click())
But the test here fails with exception
Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds.
When debugging the code, we found that dialog is found, but somehow decorView.hasWindowFocus()
returns false.
Is it because of some bug in Robolectric that view cannot get window focus? Is there a way to resolve it? At the moment we are resorting back to Robolectric shadows.
We are using Robolectric 4.2
and AndroidX test 1.1.0
versions
Update: Apparently after showing dialog, activity keeps Window focus, so it is most probably Robolectric bug (or my misunderstanding).