エスプレッソとハムクレストを使用して、
RecyclerViewで利用可能なアイテム数をカウントするにはどうすればよいですか?
例:特定のRecyclerViewに5つのアイテムが表示されているかどうかを確認したい(必要に応じてスクロール)。
ここでは、RecyclerViewのアイテム数を確認するためのViewAssertionの例
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));
私は、espressoとuiautomatorでテストをより簡単にするライブラリを書き始めました。これには、RecyclerViewアクションとアサーションのツールが含まれます。 https://github.com/nenick/espresso-macchiato メソッドassertItemCountIs(int)を使用したEspRecyclerViewの例を参照してください
@ 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)));
// ...
ネニックの回答を完了し、アイテムのcoutがより大きいかより小さいかをテストするためのもう少し柔軟なソリューションを提供するには...
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));
// ...
@Sivakumar Kamichettyの回答に基づく:
COUNT = 0;
COUNT
変数を1つの要素配列に転送します。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];
}
以下のメソッドを使用して、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行でアダプターの認識なしでこの問題を解決できます。
onView(withId(R.id.your_recycler_view_id)).check(matches(hasChildCount(2)))
置換your_recycler_view_id
あなたのIDと2
アサートする番号。
カスタムBoundedMatcher
を作成できます:
object RecyclerViewMatchers {
@JvmStatic
fun hasItemCount(itemCount: Int): Matcher<View> {
return object : BoundedMatcher<View, RecyclerView>(
RecyclerView::class.Java) {
override fun describeTo(description: Description) {
description.appendText("has $itemCount items")
}
override fun matchesSafely(view: RecyclerView): Boolean {
return view.adapter.itemCount == itemCount
}
}
}
}
そして、次のように使用します:
onView(withId(R.id.recycler_view)).check(matches((hasItemCount(5))))