Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso check all in list

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?

like image 494
busylee Avatar asked May 24 '16 11:05

busylee


People also ask

What is matcher in espresso?

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.

How do you test Recyclerview with espresso?

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.


1 Answers

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.

like image 150
jeprubio Avatar answered Oct 31 '22 05:10

jeprubio