带有Roboelctric的DialogFragment [英] DialogFragment with Roboelctric

查看:117
本文介绍了带有Roboelctric的DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Roboelectric 测试一个片段"对话框是否显示.

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(.... , ....)
}

推荐答案

按正常fragment情况进行处理.因此,要检查是否显示了片段,您需要输入下一个代码:

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);
}

这篇关于带有Roboelctric的DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆