Android testing preferences fragment by Expresso

放肆的年华 提交于 2021-02-10 05:19:45

问题


I have got a problem with testing my code by Expresso. I wrote this code:

public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}


public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preferences_xml">
<PreferenceCategory
  android:title="@string/application_settings"
  android:id="@+id/application_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_processing_private_data_preference"
    android:title="@string/title_processing_private_data_preference"
    android:summary="@string/summary_processing_private_data_preference"
    android:defaultValue="true"
    android:dependency="@string/key_recording_audio_preference"
    android:id="@+id/private_data_check_box_preference"/>
</PreferenceCategory>

 <PreferenceCategory
  android:title="@string/device_settings"
  android:id="@+id/device_settings_preferences">

<CheckBoxPreference
    android:key="@string/key_recording_audio_preference"
    android:title="@string/title_recording_audio_preference"
    android:summary="@string/summary_recording_audio_preference"
    android:defaultValue="true"
    android:id="@+id/recording_audio_check_box_preference"/>

  </PreferenceCategory>
</PreferenceScreen>

My test rule:

@Rule
public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
        SettingsActivity.class);

I try test first checkbox:

onData(anything())
            .inAdapterView(allOf(
                    isDescendantOfA(withId(R.id.preferences_xml)),
                    withId(R.id.application_settings_preferences)))
            .atPosition(1)
            .check(matches(isDisplayed()));

Test always fail with NoMatchingViewException: No views in hierarchy found matching:

http://prntscr.com/bv8xlb

And also I try:

String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference);
   onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 

Test always fail with NullPointerException:

http://prntscr.com/bv8w04.

onView(withId(R.id.working_in_background_check_box_preference)).check(matches(isDisplayed()));

Also fail with NoMatchingViewException.

Would someone show an example with correct test case?


回答1:


(Posted on behalf of the OP).

Solved:

onData(allOf(
            is(instanceOf(Preference.class)),
            withKey(key),
            withSummary(R.string.summary_working_in_background_preference),
            withTitle(R.string.title_working_in_background_preference)))
            .onChildView(withText(title))
            .check(matches(isCompletelyDisplayed()));


来源:https://stackoverflow.com/questions/38478692/android-testing-preferences-fragment-by-expresso

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