5

He estado luchando un poco con este problema, y ​​creo que no estoy obteniendo algo fundamental sobre Robolectric. Por lo general, algunas búsquedas en Google pueden ayudarme a llegar al fondo de este tipo de problema, pero entre eso y mirando el código de muestra no encuentro nada de uso.Robolectric, Problemas con los elementos de la lista de clics

estoy tratando de emular un clic en un elemento de vista de lista y comprobar que una actividad se puso en marcha después del clic. Sigo volviendo que la actividad actual que estoy probando es la actividad resultante. Intenté eliminar todo el elemento de la lista haciendo clic en el código y comprobando la actividad resultante, y esto volvió como la InstallationListActivity que estoy probando. Así que llegué a la conclusión de que no se hace clic en el elemento de vista de lista, y no estoy seguro de por qué. Los registros del sistema que he configurado en el código de prueba a continuación son los valores que espero que sean. La lista es de 13 elementos, y getChildAt (0) devuelve el encabezado. Creo que obtener el primer elemento (getChildAt (1)) y llamar a performClick en él o su vista de texto secundario iniciaría mi actividad esperada, pero ese no parece ser el caso. De todos modos, aquí está el código robolectric/pruebas de que estoy utilizando:

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

Este es el diagrama de que estoy usando para construir la vista de lista:

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> 

Y aquí está el código que inicializa la lista:

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

¡Toda ayuda es muy apreciada! ¡Gracias una tonelada!

Respuesta

12

Prueba el método de ayuda performItemClick:

Robolectric.shadowOf(listView).performItemClick(position); 
Cuestiones relacionadas