Issue
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 ArrayAdapter
is 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!
Solution
I found what was wrong!
My method to add elements to the ArrayAdapter
used runOnUIThread
but it seems that the test was not waiting for this method, thus the associationsListView
was considered as not displayed to the user ! So I fixed my method listActivity.addAssociation(Association association)
like this :
public void addAssociation(final Association association) {
final CountDownLatch latch = new CountDownLatch(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!associationList.contains(association)) {
associationArrayAdapter.add(association);
}
latch.countDown();
}
});
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!associationList.contains(association)) {
associationList.add(association);
}
}
All parts of the test now pass :)
Answered By - n0xew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.