简体   繁体   English

如何使用Robolectric测试PreferenceFragment

[英]How to test PreferenceFragment with Robolectric

Im trying to test my PreferenceActivity that uses two different PreferenceFragments. 我试图测试我的PreferenceActivity使用两个不同的PreferenceFragments。 I start the Activity with an Intent like: 我用一个Intent启动Activity,如:

public static Intent getActivityIntent(Context context, Mode mode){
    Intent intent = new Intent(context, MyPreferencesActivity.class);
    intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, getMainFragmentName(mode));
    intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, getMainFragmentArguments(mode));
    intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );
    return intent;
}

where getMainFragmentName and getMainFragmentArguments returns different fragments and arguments depending on the mode. 其中getMainFragmentName和getMainFragmentArguments根据模式返回不同的片段和参数。

Now, my problem is that when I start the activity with robolectric like: 现在,我的问题是当我用robolectric开始活动时:

Robolectric.buildActivity(MyPreferencesActivity.class)
            .withIntent(MyPreferencesActivity.getActivityIntent(Robolectric.application,
                    MyPreferencesActivity.Mode.FULL))
            .attach()
            .create()
            .postCreate(null)
            .start()
            .resume()
            .get();

I don't know how to make sure that the correct fragment has been create. 我不知道如何确保创建了正确的片段。 When I debug the code, it looks like no fragment were created. 当我调试代码时,看起来没有创建任何片段。 Shouldn't the code above be enough to create the fragment? 上面的代码不应该足以创建片段吗?

I have tried to use a shadow of the PreferenceActivity and tested : 我试图使用PreferenceActivity的阴影并测试:

assertNotNull(shadowPreferenceActivity.getPreferencesScreen());

but that just fails. 但那只是失败了。

The code works perfectly in my application and the correct fragment is created and added. 代码在我的应用程序中完美运行,并创建并添加了正确的片段。

I don't know about the preferences in particular, but I have done general fragment testing. 我不是特别了解偏好,但我做了一般的片段测试。 I think you need to add a call to ActivityController.visible() when building your activity in order for it to create the fragments. 我认为您需要在构建活动时添加对ActivityController.visible()的调用 ,以便创建片段。 I've also found you need to be careful about the ordering of the call to visible after create. 我还发现你需要注意创建后可见调用的顺序。 In my tests I do create then visible then start, but see what works for you. 在我的测试中,我创建然后可见然后开始,但看看什么对你有用。 eg: 例如:

activity = Robolectric.buildActivity(YourActivity.class).create(bundle).visible().start().resume().get();

Also note that getPreferencesScreen has been deprecated. 另请注意,不推荐使用getPreferencesScreen。 So you should use one of the standard android methods findFragmentBy[Id|Tag] to get hold of the fragment. 所以你应该使用标准的android方法之一findFragmentBy [Id | Tag]来获取片段。 eg: 例如:

Fragment f = activity.getSupportFragmentManager().findFragmentById(R.id.your_id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM