DialogFragment with Roboelctric

时光毁灭记忆、已成空白 提交于 2019-12-12 11:27:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!