0

我在 FragmentActivity 中有一个按钮,按下时应该打开一个 DialogFragment。

我想测试它在 Eclipse 中使用 JUnit for Android 是否有效。

通过 Activity,我可以使用 ActivityMonitor。单击按钮时,如何测试是否打开了正确的 Fragment?

4

1 回答 1

2

假设您正在打开您的对话框片段,如下所示:

DialogFragment dlg = YourDialogFragment.newInstance(..);
dlg.show(getFragmentManager(), "yourDialogTag");

您可以通过在测试用例中执行此操作来检查它是否已启动并显示:

// Click stuff on the UI-thread
getInstrumentation().waitForIdleSync();
Fragment dialog = getActivity().getFragmentManager().findFragmentByTag("yourDialogTag");
assertTrue(dialog instanceof DialogFragment);
assertTrue(((DialogFragment) dialog).getShowsDialog());
于 2012-08-08T12:39:23.747 回答