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(.... , ....)
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With