我有以下例程,我正在尝试使用Robolectric进行测试:
/**
* The add attachment button has been selected, present the picker.
*/
public void onAddAttachment() {
AttachmentPickerDialog.create(this).show(getFragmentManager(), "attachment");
}
到目前为止,我的测试看起来像:
@Test
public void onAddAttachment() {
// Bunch of setup stuff so APD can do it's thing and MessageFragment can do their things
// Create and start the fragment
MessageFragment fragment = new MessageFragment();
fragment.setMessage(new MailMessage(RuntimeEnvironment.application, "http://some.random.com", 32));
startFragment(fragment);
// Call I'm actually trying to test
fragment.onAddAttachment();
// This verifies that my desired fragment is the top fragment
List<Fragment> fragments = fragment.getFragmentManager().getFragments();
AttachmentPickerDialog attachmentPickerDialog = (AttachmentPickerDialog) fragments.get(fragments.size() - 1);
// And that it got initialized with the correct stuff, really an APD test
// not an onAddAttachment test
assertEquals(providers, attachmentPickerDialog.getOptions());
// How to verify that the fragment dialog showing happened correctly?
// This verifies that A dialog was created, but not necessarily that
// it was a result of apd.show
Dialog dialog = ShadowDialog.getLatestDialog();
assertNotNull(dialog);
}
这一切似乎工作正常,我可以验证是否显示了正确的片段并且它已正确设置,但我正在努力尝试验证DialogFragment对话框是否显示。我是否需要麻烦,或者是否足以验证片段堆栈顶部是否有正确的DF?