AndroidX测试对话框

时间:2019-02-22 13:03:10

标签: android unit-testing robolectric androidx androidx-test

自AndroidX发行以来,我们在单元测试中将Robolectric与Espresso结合使用。但是我们找不到测试对话框的方法。 意式浓缩咖啡社区建议我们这样做:

onView(withId(android.R.id.button1))
                .inRoot(isDialog())
                .check(matches(isDisplayed()))
                .perform(click())

但是这里的测试由于异常而失败

Waited for the root of the view hierarchy to have window focus and not request layout for 10 seconds.

调试代码时,我们发现找到了对话框,但是以某种方式decorView.hasWindowFocus()返回false。

是否由于Robolectric中的一些错误而导致视图无法获得窗口焦点?有办法解决吗?此刻,我们正在重新使用Robolectric阴影。

我们正在使用Robolectric 4.2AndroidX test 1.1.0版本

更新: 显然,在显示对话框之后,活动使Window保持焦点,因此很可能是Robolectric错误(或我的误解)。

1 个答案:

答案 0 :(得分:0)

Robolectric有一种方法,它是

ShadowAlertDialog.getLatestDialog()

要在“确定”按钮上执行点击操作,您可以像这样使用它:

((AlertDialog) ShadowAlertDialog.getLatestDialog())
        .getButton(AlertDialog.BUTTON_POSITIVE).performClick();
相关问题