Question

I have settings screen in my android app which is designed using preference headers as shown below.

<header
    android:id="@+id/preference_header_account"
    android:fragment="testPackage.TestPreferenceFragment"
    android:key="pref_key_username"
    android:summary="dynamic"
    android:title="@string/username_title" />

Now in my test i am able to get the view of this header and also i am doing tap on that view to get the TestPreferenceFragment screen as below.

ArrayList<Header> headers = new ArrayList<Header>();
ListAdapter listAdapter = getActivity().getListAdapter();

for (int i = 0; i < listAdapter.getCount(); i++) {
    headers.add((Header) listAdapter.getItem(i));
}

for (int i = 0; i < listAdapter.getCount(); i++) {
    if (headers.get(i).id == R.Id.userName) {
        position = i;
    }
}

View userName = getActivity().getListView().getChildAt(position);
TouchUtils.tapView(this, userName);

Now I want to test the preferences/views in the opened screen which is a preference fragment.Following is my xml which am loading in the TestPreferenceFragment.

<testpackage.CustomPreference1
    android:inputType="textCapWords"
    android:key="pref_key_username"
    android:maxLines="1"
    android:persistent="false"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:title="@string/test_username" />

One i can think of to test the preference fragment is, if i get the preference fragment in my testcase from which i can get the preferences using findPreference(key). But i am not able to get it.

can anyone help me in this?? Thanks in advance :)

Was it helpful?

Solution

I have tried and finally got one solution to get the preference fragment in to my test class just by overriding onAttachFragment() method of Settings activity.

fragmentsList = new ArrayList<Fragment>();

@Override
public void onAttachFragment(Fragment fragment)
{
    fragmentsList.add(fragment);
}

public ArrayList<Fragment> getFragmentsAttachedToActivity()
{
    return fragmentsList;
}

Now using getFragmentsAttachedToActivity() you can access all the fragments of settings activity if they are added to to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top