问题
I wanted to test if a dialog Fragment is shown or not, with Roboelectric.
public class SomeDialogActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
DialogFragment someDialogFragment = new SomeDialogFragment();
someDialogFragment.show(getSupportFragmentManager(), "some_dialog");
}
}
Now I wanted to test if this dialog is shown, something like this:
@Test
public void dialogFragmentIsShownToTheUser() {
DialogFragment dialog = new SomeDialogFragment();
DialogFragment someDialogFragment = new SomeDialogFragment();
startFragment(someDialogFragment);
SomeDialogActivity activity = Robolectric.setupActivity(SomeDialogActivity.class);
Dialog dialog = ShadowDialog.getLatestDialog();
assertNotNull(dialog);
assertEquals(.... , ....)
}
回答1:
Treat it as normal fragment
situation. So to check that fragment is shown you need to have next code:
@Test
public void dialogFragmentIsShownToTheUser() {
SomeDialogActivity activity = Robolectric.setupActivity(SomeDialogActivity.class);
DialogFragment dialogFragment = (DialogFragment) activity.getSupporFragmetManager()
.findFragmentByTag("some_dialog");
assertNotNull(dialogFragment);
}
来源:https://stackoverflow.com/questions/37015686/dialogfragment-with-roboelctric