Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment with Roboelctric

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(.... , ....)
}
like image 586
Pranav Kotecha Avatar asked May 03 '16 22:05

Pranav Kotecha


1 Answers

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);
}
like image 80
Eugen Martynov Avatar answered Oct 08 '22 22:10

Eugen Martynov