2011-08-03 4 views
4

매우 일반적인 시나리오 : Android 애플리케이션에 환경 설정 활동이 있으며 ListPreference에서 옵션을 선택하면 해당 ListPreference의 요약 텍스트가 변경됩니다. 예 : 색상 ListPreference에서 "Green"을 선택하면 onPreferenceChange 콜백을 통해 ListPreference의 요약 텍스트가 "Green"으로 변경됩니다.Android JUnit 환경 설정

Android JUnit 테스트를 사용하여 이러한 요약 변경 사항이 모두 올바르게 수행되고 있는지 확인하고 싶습니다. 그러나이를 수행하는 방법에 대한 정보는 거의 없다.

내가 성공하지, 테스트 스레드와 runOnUiThread()을 통해 모두 ListPreference에 setValue()를 사용하여 변화를 시도했습니다 -이 onPreferenceChange()에 전화를 트리거하지 않습니다. 또한 setValue()을 호출 한 후에 getInstrumentation().waitForIdleSync()을 시도했지만 성공하지 못했습니다.

제 질문은 다음과 같습니다. 어떻게 수행합니까?

감사합니다.

답변

4

이 작업 솔루션을 제작 한 지 몇 시간이 더 걸렸지 만 다른 사람이 더 나은 솔루션을 가지고 있는지 궁금합니다. 이 코드는 비슷한 질문에 this solution에서 영감을하지만,이 경우는 두 가지 방법으로 다른했다 :

  1. 그것은은 (는) ListPreference UI가 runOnUiThread()를 통해 클릭 호출해야 의미 안드로이드 JUnit을, 사용하기위한 것입니다.
  2. 사용중인 기본 설정 범주가있을 것으로 예상되므로 클릭 (전체 기본 설정 목록과 관련이 있음) 위치를 찾는 것이 복잡해집니다. 위에서 언급 한 해결책은 선호 카테고리가없는 경우에만 작동합니다.

이 메서드는 특정 ListPreference 항목의 키와 함께 클릭 할 목록의 항목 위치를 허용합니다. 그런 다음 목록 항목 클릭을 수행하고 다른 코드는 내가 찾고있는 확인 작업을 수행합니다.

메서드에서 getActivity() 호출 전에 setActivityInitialTouchMode(true);을 설정해야합니다.

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

이 코드는 findPreference() 메서드가 손상되어 현대적인 조각 기반의 기본 설정 작업에서는 작동하지 않습니다. 코드를 새로운 기본 설정 아키텍처로 마이그레이션하려고 시도 했습니까? – saurav