11

The test looks like that (it's 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 test works. But the test fails because activity.runOnUiThread returns immediately. I can insert Thread.sleep and the test turns green but it looks kinda clumsy to me. Do I have to use some thread synchronization or may be poll for some UI element to be ready?

I tried to annotate it with @UiThreadTest but that doesn't work either. The code in list.show() populates a ListView via custom adapter and getView is called on another thread (not the one test runs on - and I have nothing to do with that, I have no threads or asynctasks, no nothing). The test fails again because it returns before UI is ready to be checked.

3 Answers 3

22

Calling waitForIdleSync() is better than sleeping for a fixed time.

2
  • Perfect. Exactly what I was looking for. This should be the accepted answer. Nov 6, 2016 at 9:33
  • 1
    Helps. But i still needed to add getInstrumentation().waitForIdleSync(); later make checking of ui element work.
    – shtolik
    Apr 27, 2019 at 12:30
3

You have to do a Thread.sleep. I don't think there's a way around this. I don't see why that's "clunky"; you're doing a test, so you have to wait for the system to show the UI element you want to test.

It seems to me, though, that you're really trying to test AccountsList or list. There's little reason to test ListView or findViewById unless you're paranoid.

You should focus on testing AccountsList and your custom adapter. You shouldn't have to use the UI to do this.

3
  • 13
    I can tell you why I don't like Thread.sleep. You sleep too much - you waste precious time running your tests. You sleep too little - your test fails. How much sleep is just okay? Well, it depends. On the code you are testing and on the workstation you are testing it on. You move your project to another computer and your tests start to fail. That's Thread.sleep for you. Feb 23, 2012 at 13:52
  • 1
    Regarding what I should test let's just say my example is rather contrived. Let's not argue here about why would someone want to test UI. Feb 23, 2012 at 13:56
  • 3
    I've just re-read the comments I made yesterday and they look like not exactly polite to me now. Whatever the case may be I assure you it's all on account of English being not my native language. I value you answer much and will accept it (if nothing better pop's up :). Feb 24, 2012 at 7:54
0

Following documentation, "One of the key parts of Espresso is its ability to synchronize all test actions. Espresso waits until the UI is idle before it moves to the next operation. Likewise, it waits for AsyncTask background operations to complete. In general, this should address the majority of test synchronizations in your application. If you have written UI tests before, you will appreciate this feature - there's no need to add waits or synchronization points to your app!

However, sometimes it is not possible to rely on automatic synchronisation, for instance when your app does background operations via non-standard means (managing threads directly or using custom Services). If you have run into a situation where you cannot rely on Espresso to automatically handle the synchronization for you, you can use idling resources and still rely on Espresso for synchronization."

You can read a full example at the testing codelab, you can also get the source code of the sample in github.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.