2

我是使用 Robotium 的新手,似乎在任何地方的论坛上都找不到任何答案。我有一个要测试的 PreferenceActivity。问题是每当我需要单击首选项时,我的单元测试都会失败。

更具体地说,我有一个 CheckBoxPreference 我正在运行一些测试。我想验证当 CheckBoxPreference 被选中时,屏幕上有某些首选项已启用(未变灰),反之亦然。截至目前,我什至无法使用searchText()/waitForText()方法找到首选项(这是屏幕上的第一个首选项)。在clickOnView()getView().

看来我现在能做的最好的事情就是使用 Android 的setChecked()方法来操纵 CheckBoxPreference 的状态。我假设以这种方式进行测试可能的,但是我正在研究一些基本的东西。由于 PreferenceActivity 也是 ListActivity,因此我也尝试搜索有关测试 ListActivity 的相关问题,但无济于事。

以下几个链接为我使用 PreferenceActivities 和 Robotium 提供了一个良好的开端。

这是我正在使用的代码:

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());
}

我已经验证 PrefTestingUtils 函数确实返回了对首选项的有效引用。所有 PrefTestingUtils 函数都与此类似:

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;
}

任何人都可以提供的任何帮助将不胜感激。谢谢!

4

1 回答 1

1

所以这就是我想出的似乎有效的方法。

首先,您需要获得对 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;
}

接下来,您可以使用带有首选项标题的 clickOnText() 方法单击首选项。最后,我发现你点击后要睡一段时间。我已MINI_SLEEP定义为 250 毫秒。

public void testCheckBoxEnabled() {
    CheckBoxPreference pref = getCheckBoxPref("Enabled");
    assertNotNull(pref);
    assertFalse(pref.isChecked());
    mSolo.clickOnText("Enabled");
    mSolo.sleep(MINI_SLEEP);
    assertTrue(pref.isChecked());
}

希望这对任何需要它的人有所帮助。

于 2012-07-05T16:52:57.450 回答