The test looks like this (it ActivityInstrumentationTestCase2):
public void testItShowsThreeRows() { activity = getActivity(); activity.runOnUiThread(new Runnable() { public void run() { AccountsList accountsList = new AccountsList(activity, accounts); list.show(); } }); ListView listView = (ListView)activity.findViewById(R.id.list); assertEquals(3, listView.getChildCount()); }
The code I'm trying to verify works. But the test fails because activity.runOnUiThread returns immediately. I can insert Thread.sleep and the test turns green, but for me it looks pretty awkward. Should I use some thread synchronization or maybe there is a poll for some user interface element to be ready?
I tried to annotate it with @UiThreadTest , but that won't work either. The code in list.show() populates the ListView through the user adapter, and getView is called in another thread (none of them are executed), and I have nothing to do with it, I have neither threads, nor async, nor anything), The test failed again as it returns before the user interface is ready for verification.
android testing
Art shayderov
source share