Android JUnit to test settings

Pretty common scenario: an Android application has settings activity and selecting an option from the ListPreference trigger code to modify this ListPreference summary text. i.e.: Green from the color of the ListPreference would change the summary text of the ListPreference to Green by using the onPreferenceChange .

I would like to use Android JUnit testing to confirm that these resulting changes are being performed correctly. However, there seems to be very little information on how to do this.

I tried the options for using setValue() in ListPreference, both in the test thread and through runOnUiThread() , without success - this does not cause the onPreferenceChange() call. I also tried getInstrumentation().waitForIdleSync() after calling setValue() , but also without success.

So my question is: how is this done?

Thank!

+4
android unit-testing junit android-preferences
Aug 03 2018-11-11T00:
source share
1 answer

A few hours of work produced this working solution, but I'm curious if anyone has a better solution. This code was inspired by this solution to a similar question, but this case differs in two ways:

  • It is designed to use Android JUnit, which means it needs to call the ListPreference UI clicks through runOnUiThread() .
  • He expects preference categories to be used that make it difficult to find a position (relative to the entire list of preferences) for a click. The above solution only works in cases without preference categories.

This method will accept the key for a specific ListPreference element, as well as the position of the element in the list to be clicked. Then he will perform this click on the list item and the other code will perform the check I'm looking for.

Note that this requires setActivityInitialTouchMode(true); before calling getActivity() in the setUp() method.

 private void clickListPreference(String _listPreferenceKey, int _listItemPos){ final String listPreferenceKey = _listPreferenceKey; final int listItemPos = _listItemPos; mActivity.runOnUiThread( new Runnable() { public void run() { // get a handle to the particular ListPreference ListPreference listPreference= (ListPreference) mActivity.findPreference(listPreferenceKey); // bring up the dialog box mActivity.getPreferenceScreen().onItemClick( null, null, getPreferencePosition(), 0 ); // click the requested item AlertDialog listDialog = (AlertDialog) listPreference.getDialog(); ListView listView = listDialog.getListView(); listView.performItemClick(listView, listItemPos, 0); } /*** * Finding a ListPreference is difficult when Preference Categories are involved, * as the category header itself counts as a position in the preferences screen * list. * * This method iterates over the preference items inside preference categories * to find the ListPreference that is wanted. * * @return The position of the ListPreference relative to the entire preferences screen list */ private int getPreferencePosition(){ int counter = 0; PreferenceScreen screen = mActivity.getPreferenceScreen(); // loop over categories for (int i = 0; i < screen.getPreferenceCount(); i++){ PreferenceCategory cat = (PreferenceCategory) screen.getPreference(i); counter++; // loop over category items for (int j = 0; j < cat.getPreferenceCount(); j++){ if (cat.getPreference(j).getKey().contentEquals(listPreferenceKey)){ return counter; } counter++; } } return 0; // did not match } } ); getInstrumentation().waitForIdleSync(); } 
+4
Aug 03 2018-11-11T00:
source share



All Articles