2012-06-28 96 views
2

我是使用Robotium的新手,似乎無法在任何地方的論壇上找到任何答案。我有一個PreferenceActivity,我試圖測試。問題是,當我需要點擊一個首選項時,我的單元測試失敗。使用Robotium測試PreferenceActivity

更具體地說,我有一個CheckBoxPreference,我正在運行一些測試。我想驗證當CheckBoxPreference被選中時,屏幕上的某些首選項已啓用(不會變灰),反之亦然。截至目前,我甚至無法通過使用searchText()/waitForText()方法(它是屏幕上的第一首選項)找到首選項。在從getView()獲得首選項的視圖後,我也嘗試使用clickOnView()方法。

看來我現在能做的最好的事情就是使用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; 
} 

任何人都可以給任何幫助將不勝感激。謝謝!

+0

我已經成功測試過PreferenceActivity。你有什麼嘗試?你應該發佈你當前的測試類代碼,否則很難給你指針。 – dmon

+0

謝謝,dmon。我編輯了我的帖子以包含我的代碼。 – rach5000

回答

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

希望這有助於任何需要它的人。

+1

我投票了,但有一件事要改變 - getCheckBoxPref(String ** key **)應該改爲getCheckBoxPref(String ** title **) –

+1

在Robotium 4+中,'.getCurrentListViews()'成爲' .getCurrentViews(ListView.class)' – Mendhak