2016-04-04 13 views

回答

50

下面的例子ViewAssertion檢查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)); 
    } 
} 

然後用這個說法

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

我已經開始寫一個庫,它應該使測試更加簡單與咖啡和uiautomator 。這包括RecyclerView操作和斷言的工具。 https://github.com/nenick/espresso-macchiato例如見EspRecyclerView與方法assertItemCountIs(INT)

+0

這個測試總是通過。我認爲它沒有正常工作。 –

+0

adapter.getItemCount()將會變爲null ..請讓我知道原因。 –

+0

@AdamHurwitz許多人已經確認這項工作,但請解釋你的情況,它總是通過。 – nenick

13

要完成nenick答案,並提供多一點靈活的解決方案也測試,如果項目COUT是GREATERTHAN,每種不超過...

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

} 

用法:

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

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

用法:

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

很好。我建議把它變成espresso,因爲真的很奇怪,這樣的核心功能不可用。 – Samuel

3

我用下面的方法來獲取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; 
    } 

用法 -

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

然後執行斷言檢查itemsCount與預期的一樣

1

基於@Sivakumar Kamichetty答案:

  1. 變量 '計數' 從內部類中訪問,需要被聲明爲final。
  2. 不必要的行:COUNT = 0;
  3. COUNT變量轉移到一個元素數組。
  4. 變量result是不必要的。

不是很好,但工程:

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]; 
} 
相關問題