lundi 24 octobre 2016

Android Espresso - Testing a ListView row

I'm currently trying to test a ListView on android with Espresso, with a custom ArrayAdapter. Here is the test :

 final AssociationsListActivity listActivity = getActivity();

    final Association association1 = new Association(ASSOCIATION_NAMES[0], ASSOCIATION_DESCRIPTIONS[0]);
    final Association association2 = new Association(ASSOCIATION_NAMES[1], ASSOCIATION_DESCRIPTIONS[1]);
    final Association association3 = new Association(ASSOCIATION_NAMES[2], ASSOCIATION_DESCRIPTIONS[2]);

    listActivity.addAssociation(association1);
    listActivity.addAssociation(association2);
    listActivity.addAssociation(association3);

    assertTrue(listActivity.getAssociationsList().size() == 3);

    onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));
    onData(anything())
            .inAdapterView(withId(R.id.associationsListView))
            .atPosition(0)
            .perform(click());

    assertCurrentActivityIsInstanceOf(AssociationsListActivityTest.this
            , AssociationsPageActivity.class);

The method listActivity.addAssociation(association); simply does associationArrayAdapter.add(association);.

The ArrayAdapteris used to add elements to a ListView(visible by default) with the id R.id.associationsListView

When I run this test, I get the following error :

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"

Which is caused by the following assertion, onView(withId(R.id.associationsListView)).check(matches(isDisplayed()));

Also when commenting this assertion, the next one causes the following exception :

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: ch.epfl.sweng.project:id/associationsListView'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "ListView{id=2131427414, res-name=associationsListView, visibility=VISIBLE, width=996, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=42.0, child-count=0}"

I went through a lot of stack overflow threads covering those exception, I replaced those tests by other equivalents but those errors still show up I don't know what to do! Thanks for any help!

Aucun commentaire:

Enregistrer un commentaire