Espresso List header non-clickable

416 Views Asked by At

In my functional tests while using espresso I want to click on a view inside a header in a ListView.

According to https://code.google.com/p/android-test-kit/wiki/EspressoSamples#Matching_a_view_that_is_a_footer/header_in_a_ListView in order to have access to the header in my test I need to do this:

listView.addHeaderView(headerView, HEADER, true);

And access it like this:

public static Matcher<Object> isHeader() {
    return allOf(is(instanceOf(String.class)), Matchers.<Object>is(TestUtil.HEADER));
}

&

onData(ViewMatchers.isHeader())
        .inAdapterView(allOf(withId(R.id.list_view), isDisplayed()))
        .onChildView(withId(R.id.view_to_click))
        .check(matches(isDisplayed()))
        .perform(click());

However, when I do that, my header will be clickable.

when I use:

listView.addHeaderView(headerView, HEADER, false);

espresso will not be able to access my view anymore.

How can I access the view from my test without making the header view clickable?

1

There are 1 best solutions below

0
On

So far the best workaround I have found was to do the following:

Set the data on the headerView.

listView.addHeaderView(header, TestUtil.HEADER, true);

Ignore click on the headerView.

@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
    // Ignore the headerView.
    if (position == 0) {
        return;
    }
    ...
}

Hide the UI change on header click.

android:listSelector="@android:color/transparent"