Espresso check everything listed

Is it possible in espresso to check all the views in a listview , not to get one of them, but to check all for a certain condition.

It seems that onData() serves to return interaction with only one list item from a list. And this is not for my problem.

Edit: Actually, I found one solution that solves my problem, but rather looks like a spike instead of a good one. I run the StealCount action to get the number of items from a listview , because we cannot do this from the test (because we are inside the Inst thread). After that, I run validation data for any items from the list using DataInteraction . It looks 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(0) .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 to me. Are there any other abilities?

+6
source share
1 answer

I would do it differently.

To check the list, 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, before, to get the numItems variable you just need to access your list adapter through an 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(); 

Despite the fact that I understand your code, I think it is easier to understand. The only confusing part is the way to access the adapter. But you can create other intermediate variables to make it more readable or create a function to get this number if you want.

+1
source

All Articles