Is there any ability in espresso
to check all views in listview
, not for get one from this, but for check all for some condition.
It seems that onData()
serves to return interaction with only one list item from list. And it is not for my issue.
Edit:
Actually I have found one solution, that solves my issue, but it rather looks like spike instead of good one.
I start StealCount action to get items count from ListView
cuz we can not do it strait from test (cuz we are inside Inst thread). Afterwards I start check data for any items from list view using DataInteraction
. It seems like this:
public static void assertAllItems(
final Matcher<View> adapterViewMatcher,
final Matcher<View> itemsMatcher
) {
StealCountAction stealCountAction = new StealCountAction();
onView(adapterViewMatcher).perform(stealCountAction);
DataInteraction dataInteraction = onData(anything())
.inAdapterView(adapterViewMatcher);
for (int i = 0 ; i < stealCountAction.count; i++) {
dataInteraction.atPosition(i)
.onChildView(itemsMatcher)
.check(ViewAssertions.matches(isDisplayed()));
}
}
static class StealCountAction implements ViewAction {
public int count;
@Override
public Matcher<View> getConstraints() {
return instanceOf(AdapterView.class);
}
@Override
public String getDescription() {
return "Steal count action";
}
@Override
public void perform(UiController uiController, View view) {
count = ((AdapterView) view).getCount();
}
}
But anyway it seems ugly for me. Is there any other abilities?
A matcher that allows for a quick creation of a matcher that applies to a given type but only processes items of a specific subtype of that matcher. BoundedMatcher. Some matcher sugar that lets you create a matcher for a given type but only process items of a specific subtype of that matcher.
To interact with RecyclerViews using Espresso, you can use the espresso-contrib package, which has a collection of RecyclerViewActions that can be used to scroll to positions or to perform actions on items: scrollTo() - Scrolls to the matched View, if it exists.
I would have done it in a different way.
To check a listview you only need to do something like:
for (int i = 0; i < numItems; i++) {
onData(anything())
.inAdapterView(withId(R.id.listViewId)).atPosition(i)
.check(matches(isDisplayed()));
}
And, previously, to get the numItems variable you need just to access to your list adapter through your ActivityTestRule:
int numItems = ((ListView) mActivityRule.getActivity().findViewById(R.id.listViewId)).getAdapter().getCount();
Or, if its inside a fragment:
int numItems = ((ListView) mActivityRule.getActivity().getSupportFragmentManager().findFragmentById(R.id.fragmentId).getView().findViewById(R.id.listViewId)).getAdapter().getCount();
Even though I understand your code I think this one is easier to be understood. The only confusing part is the way to access to the adapter. But you can create other intermediate variables to make it more readable or create a function to get this number if you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With