2016-04-04 11 views

risposta

50

Ecco un esempio ViewAssertion per controllare il conteggio voce RecyclerView

public class RecyclerViewItemCountAssertion implements ViewAssertion { 
    private final int expectedCount; 

    public RecyclerViewItemCountAssertion(int expectedCount) { 
    this.expectedCount = expectedCount; 
    } 

    @Override 
    public void check(View view, NoMatchingViewException noViewFoundException) { 
    if (noViewFoundException != null) { 
     throw noViewFoundException; 
    } 

    RecyclerView recyclerView = (RecyclerView) view; 
    RecyclerView.Adapter adapter = recyclerView.getAdapter(); 
    assertThat(adapter.getItemCount(), is(expectedCount)); 
    } 
} 

e quindi utilizzare questa affermazione

onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5)); 

ho iniziato a scrivere una libreria che dovrebbe rendere il test più semplice con caffè espresso e uiautomator . Questo include strumenti per l'azione e le asserzioni di RecyclerView. https://github.com/nenick/espresso-macchiato Si veda ad esempio EspRecyclerView con il metodo assertItemCountIs (int)

+0

Questo test passa sempre. Non penso che funzioni correttamente. –

+0

adapter.getItemCount() sta arrivando a null..Può farmi sapere il motivo. –

+0

@AdamHurwitz molti hanno confermato che questo lavoro, ma per favore spiega la tua situazione dove passa sempre. – nenick

13

Per completare la risposta nenick e forniscono e un po 'soluzione più flessibile per verificare anche se l'oggetto è cout GreaterThan, lessThan ...

public class RecyclerViewItemCountAssertion implements ViewAssertion { 

    private final Matcher<Integer> matcher; 

    public RecyclerViewItemCountAssertion(int expectedCount) { 
     this.matcher = is(expectedCount); 
    } 

    public RecyclerViewItemCountAssertion(Matcher<Integer> matcher) { 
     this.matcher = matcher; 
    } 

    @Override 
    public void check(View view, NoMatchingViewException noViewFoundException) { 
     if (noViewFoundException != null) { 
      throw noViewFoundException; 
     } 

     RecyclerView recyclerView = (RecyclerView) view; 
     RecyclerView.Adapter adapter = recyclerView.getAdapter(); 
     assertThat(adapter.getItemCount(), matcher); 
    } 

} 

Usage :

onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5)); 
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(greaterThan(5)); 
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(lessThan(5)); 
// ... 
12

l'aggiunta di un po 'di zucchero sintassi per la @Stephane's answer.

public class RecyclerViewItemCountAssertion implements ViewAssertion { 
    private final Matcher<Integer> matcher; 

    public static RecyclerViewItemCountAssertion withItemCount(int expectedCount) { 
     return withItemCount(is(expectedCount)); 
    } 

    public static RecyclerViewItemCountAssertion withItemCount(Matcher<Integer> matcher) { 
     return new RecyclerViewItemCountAssertion(matcher); 
    } 

    private RecyclerViewItemCountAssertion(Matcher<Integer> matcher) { 
     this.matcher = matcher; 
    } 

    @Override 
    public void check(View view, NoMatchingViewException noViewFoundException) { 
     if (noViewFoundException != null) { 
      throw noViewFoundException; 
     } 

     RecyclerView recyclerView = (RecyclerView) view; 
     RecyclerView.Adapter adapter = recyclerView.getAdapter(); 
     assertThat(adapter.getItemCount(), matcher); 
    } 
} 

Usage:

import static your.package.RecyclerViewItemCountAssertion.withItemCount; 

onView(withId(R.id.recyclerView)).check(withItemCount(5)); 
onView(withId(R.id.recyclerView)).check(withItemCount(greaterThan(5)); 
onView(withId(R.id.recyclerView)).check(withItemCount(lessThan(5)); 
// ... 
+0

Meraviglioso. Suggerirei di farlo in espresso perché è davvero strano che una caratteristica così fondamentale non sia disponibile. – Samuel

3

ho usato il metodo seguito per ottenere il conteggio di RecyclerView

public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) { 
int COUNT = 0; 
     Matcher matcher = new TypeSafeMatcher<View>() { 
      @Override 
      protected boolean matchesSafely(View item) { 
       COUNT = ((RecyclerView) item).getAdapter().getItemCount(); 
       return true; 
      } 
      @Override 
      public void describeTo(Description description) { 
      } 
     }; 
     onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher)); 
     int result = COUNT; 
      COUNT = 0; 
     return result; 
    } 

di utilizzo -

int itemsCount = getCountFromRecyclerView(R.id.RecyclerViewId); 

Quindi eseguire affermazioni per verificare se il itemsCount è come previsto

1

Sulla base @Sivakumar Kamichetty risposta:

  1. variabile 'count' si accede dall'interno di classe interna, deve essere dichiarato finale.
  2. Riga non necessaria: COUNT = 0;
  3. Trasferimento COUNT variabile su un array di elementi.
  4. La variabile result non è necessaria.

Non bello, ma funziona:

public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) { 
    final int[] COUNT = {0}; 
    Matcher matcher = new TypeSafeMatcher<View>() { 
     @Override 
     protected boolean matchesSafely(View item) { 
      COUNT[0] = ((RecyclerView) item).getAdapter().getItemCount(); 
      return true; 
     } 
     @Override 
     public void describeTo(Description description) {} 
    }; 
    onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher)); 
    return COUNT[0]; 
} 
Problemi correlati