【问题标题】:Android Testing: Dialog. Check it isShowing()Android 测试:对话框。检查它是显示()
【发布时间】:2012-11-01 05:16:51
【问题描述】:

这是我的方法,它工作正常并显示对话框。

public void showDialog(){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.mylayout);
    dialog.show();
}

我有一个测试项目,我想测试对话框是否显示。我想应用 .isShowing() 方法。像这样的...

assertTrue(dialog.isShowing());

但我不知道如何在我的测试中访问对话框变量。

我没有使用 Robotium(这不是我的选择)。 我目前正在使用 ActivityUnitTestCase 进行测试。如果需要更多信息,请随时询问。

编辑

我试图通过公开对话框来使用下面的答案

public Dialog getDiag(){
    return dialog;
}

使用这个答案:我在测试中运行 showDialog() 时遇到一个新问题,它在命中时会中断:dialog.show();

android.view.WindowManager$BadTokenException: * 无法添加窗口 -- token null

【问题讨论】:

    标签: java android unit-testing reflection dialog


    【解决方案1】:

    在 showDialog 函数之外声明 Dialog,然后实现一个返回此 Dialog 实例的方法。

    public Dialog getDiag(){
        return dialog;
    }
    

    然后做这样的事情

    assertTrue(new YourClassName().getDialog().isShowing());
    

    【讨论】:

    • 谢谢我已经改变了返回对话框的方法,然后我至少可以在我的测试中访问对话框。但我无法调用 Dialog.show() 因此我已将其从我的方法中取出,并且在返回 Dialog 后(在活动中)我将调用 show()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多