5

나는이 문제에 조금 어려움을 겪었으며 Robolectric에 대해 근본적인 문제가 없다고 생각합니다. 일반적으로 일부 Google 검색은 이러한 유형의 문제를 해결하는 데 도움이 될 수 있지만 그 사이에 샘플 코드를 보는 동안 사용 방법을 찾지 못합니다.Robolectric, 목록 항목을 클릭 할 때의 문제

목록보기 항목을 클릭하여 에뮬레이트하려고 시도하고 클릭 후 활동이 시작되는지 확인하려고합니다. 나는 현재 테스트하고있는 활동이 결과 활동이라는 것을 계속해서 알게된다. 모든 목록 항목을 클릭하여 코드를 제거하고 결과 활동을 확인해 보았습니다. 그리고 이것은 제가 테스트중인 InstallationListActivity로 돌아 왔습니다. 그래서 목록보기 항목이 클릭되지 않고 결론을 내 렸습니다. 그 이유는 확실하지 않습니다. 아래 테스트 코드에서 설정 한 시스템 로그는 예상되는 값입니다. 목록은 13 개 항목이며 getChildAt (0)는 헤더를 반환합니다. 첫 번째 항목 (getChildAt (1))을 가져 와서 performClick을 호출하거나 자식 텍스트보기를 호출하면 예상되는 활동이 시작될 것이라고 생각하지만 그럴 것 같지 않습니다. 여기

@Before 
    public void setUp() { 
     mAppLaunch = new ApplicationLaunchActivity(); 
     mAppLaunch.onCreate(null); 
     mActivity = new InstallationListActivity(); 
     mActivity.onCreate(null); 
    } 

    @Test 
    public void shouldHaveNonEmptyInstallationList() throws Exception { 
     assert(mActivity.installationListCount() > 0); 
    } 

    @Test 
    public void shouldHaveSameNumberOfElements() throws Exception { 
     ListView installationListView = (ListView) mActivity.getListView(); 

     ShadowListView shadowListView = shadowOf(installationListView); 

     assert(shadowListView.getChildCount() == mActivity.installationListCount()); 
    } 

    @Test 
    public void pressingTheFirstItemInTheListShouldLaunchVenueListActivity() { 
     ListView installationListView = (ListView) mActivity.findViewById(android.R.id.list); 

     System.out.println("qty: " + installationListView.getChildCount()); 
     System.out.println("class: " + installationListView.getChildAt(0).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(1).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(2).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(3).getClass()); 
     System.out.println("class: " + installationListView.getChildAt(4).getClass()); 

     LinearLayout firstItemLayout = (LinearLayout) installationListView.getChildAt(1); 
     TextView firstItem = (TextView) firstItemLayout.getChildAt(0); 

     ShadowTextView shadowFirstItem = shadowOf(firstItem); 
     ShadowLinearLayout shadowLayout = (ShadowLinearLayout) shadowOf(firstItemLayout); 

     shadowLayout.performClick(); 
     shadowFirstItem.performClick(); 

     clickOn(firstItem); 
     clickOn(firstItemLayout); 

     System.out.println("class: " + firstItemLayout.getChildAt(0).getClass()); 
     System.out.println("Layout shadow" + shadowOf(firstItemLayout).getClass()); 
     System.out.println("First Item Text: " + shadowFirstItem.getText()); 

     ShadowActivity shadowActivity = shadowOf(mActivity); 
     Intent startedIntent = shadowActivity.getNextStartedActivity(); 
     assertNotNull(startedIntent); 
     ShadowIntent shadowIntent = shadowOf(startedIntent); 

     assertThat(shadowIntent.getComponent().getClassName(), equalTo(VenueListActivity.class.getName())); 
    } 
} 

레이아웃의 내가 목록보기 구축하기 위해 사용하고 있습니다 : 여기

This is list.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
    <ListView android:id="@android:id/list" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:cacheColorHint="#00000000" 
       android:background="@drawable/background"/> 
</LinearLayout> 

list_item.xml 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/list_item" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent"> 
    <TextView android:id="@+id/list_item_text" 
       style="@style/tw_list_font" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:gravity="center_vertical" 
       android:paddingLeft="6dip" 
       android:minHeight="?android:attr/listPreferredItemHeight" /> 
</LinearLayout> 

과 목록을 초기화하는 코드입니다 어쨌든, 여기에 내가 사용하고있는 robolectric/테스트 코드는 다음과 같습니다

InstallationListActivity.java 

setContentView(R.layout.list); 
     final ListView installationListView = getListView(); 
     LayoutInflater inflater = getLayoutInflater(); 
     ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, installationListView, false); 
     TextView headerText = (TextView) header.findViewById(R.id.header_text); 
     headerText.setText("Installations On Live"); // TODO: This should not be hardcoded 
     installationListView.addHeaderView(header, null, false); 

     setListAdapter(new ArrayAdapter<Installation>(this, R.layout.list_item, R.id.list_item_text, mInstallations)); 

     // Click event 
     installationListView.setClickable(true); 
     installationListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg1) { 
       Installation installationClicked = (Installation) installationListView.getItemAtPosition(position); 
       if (LOCAL_LOG) { 
        Log.d(LOG_TAG, "Installation: " + installationClicked.toString() + " clicked."); 
        Log.d(LOG_TAG, installationClicked.toString() + " has installationDirectory: " + 
          installationClicked.rootDir); 
       } 
       AppState.installation = installationClicked; 
       AppState.serverInstallationName = installationClicked.rootDir; 
       Intent venueListIntent = new Intent(InstallationListActivity.this, VenueListActivity.class); 
       startActivity(venueListIntent); 
      } 
     }); 

도움이 되었습니까? 엄청 고마워!

답변

12

시도 도우미 방법 performItemClick :

Robolectric.shadowOf(listView).performItemClick(position); 
관련 문제