Reputation: 135
I am new to using Robotium and can't seem to find any answers to this on the forums anywhere. I have a PreferenceActivity that I am trying to test. The problem is that my unit tests fail whenever I need to click on a preference.
More specifically, I have a CheckBoxPreference that I am running some tests on. I want to verify that when the CheckBoxPreference is checked, there are certain preferences on the screen that are enabled (not grayed out), and vice-versa. As of right now, I can't even find the preference by using the searchText()
/waitForText()
methods (it is the first preference on the screen). I have also tried using the clickOnView()
method after getting the view of the preference from getView()
.
It seems like the best I can do right now is to just manipulate the state of the CheckBoxPreference using Android's setChecked()
method. I'm assuming testing in this manner is possible and there is something fundamental that I am looking over, however. Since a PreferenceActivity is also a ListActivity, I have also tried searching for related questions on testing a ListActivity to no avail.
The following couple of links gave me a good start to working with PreferenceActivities and Robotium.
Here is the code that I am working with:
public void testEnabledChecked() throws Exception {
CheckBoxPreference enabled = PrefTestingUtils.getCheckBoxPreference(mSolo, mActivity,
(String) getVal("KEY_ENABLED"));
EditTextPreference recipient = PrefTestingUtils.getEditTextPreference(mSolo, mActivity,
(String) getVal("KEY_RECIPIENT"));
EditTextPreference title = PrefTestingUtils.getEditTextPreference(mSolo, mActivity,
(String) getVal("KEY_TITLE"));
ListPreference interval = PrefTestingUtils.getListPreference(mSolo, mActivity,
(String) getVal("KEY_INTERVAL"));
// ensures that the preference is unchecked
if (!enabled.isChecked()) {
Log.d(TAG, "Enabled preference unchecked, clicking on it");
// mSolo.clickOnView(enabled.getView(null, null)); // Attempt #1
// mSolo.clickOnText("Enabled"); // Attempt #2
// mSolo.clickOnCheckBox(0); // Attempt #3
// mSolo.searchText("Enabled"); // cannot find view
}
assertTrue(enabled.isChecked()); // AssertionFailedError here
assertTrue(recipient.isEnabled());
assertTrue(title.isEnabled());
assertTrue(interval.isEnabled());
}
I have verified that the PrefTestingUtils functions are indeed returning valid references to the preferences. All of the PrefTestingUtils functions are similar to this:
public static CheckBoxPreference getCheckBoxPreference(Solo solo,
PreferenceActivity activity, String key) {
if (solo == null || activity == null || key == null) {
Log.d(TAG, "getCheckBoxPreference::Null parameter");
return null;
}
Preference p = activity.findPreference(key);
if (p instanceof CheckBoxPreference) {
return (CheckBoxPreference) p;
}
return null;
}
Any help anybody can give would be greatly appreciated. Thanks!
Upvotes: 2
Views: 1847
Reputation: 135
So here's what I came up with that seems to be working.
First you need to get a reference to the CheckBoxPreference:
private CheckBoxPreference getCheckBoxPref(String key) {
ArrayList<ListView> currentListViews = mSolo.getCurrentListViews();
// get the lone ListAdapter for the PreferenceActivity
ListAdapter listAdapter = currentListViews.get(0).getAdapter();
int cnt = listAdapter.getCount();
for (int i = 0; i < cnt; i++) {
Object o = listAdapter.getItem(i);
if (o instanceof CheckBoxPreference) {
CheckBoxPreference pref = (CheckBoxPreference) o;
if (pref.getTitle().equals(key)) {
return pref;
}
}
}
return null;
}
Next, using you can click on the preference using the clickOnText() method with the preference's title. Finally, I found that you have to sleep for some time after clicking. I have MINI_SLEEP
defined to 250 ms.
public void testCheckBoxEnabled() {
CheckBoxPreference pref = getCheckBoxPref("Enabled");
assertNotNull(pref);
assertFalse(pref.isChecked());
mSolo.clickOnText("Enabled");
mSolo.sleep(MINI_SLEEP);
assertTrue(pref.isChecked());
}
Hope this helps for anyone who should need it.
Upvotes: 1