Espresso testing with never ending AsyncTask

Viewed 785

I am trying to run an Espresso test under real conditions in my app. Problem is that tests never run because Espresso cannot detect an idle state in order to inject it's event to queue. Therefore it hangs and ends with known ASYNC_TASKS_HAVE_IDLED error.

Why this happens?

The application is using SignalR library and it is initialized on app start. "Library GitHub url". This library creates multiple long polling connections in order to send and receive all kind of messages and events to/from the server.

These connections are implemented with AsyncTask (class url) and since they end rarely (and when they do, they are restarted instantly), Espresso has very little chance to detect idle state and continue the execution.

So, is there some workaround for this problem? Am I really forced to switch to UIAutomator because of this?

Notes:

  • Disabling the SignalR is not an option (tried, tests work but then I'm missing the point of the app)
  • Mocking the SignalR is also not an option. I want to test the real data.

Thank you for your time.

1 Answers

I also have many Async. tasks on my app.. So what I do is :

  1. Registered idling resource.
  2. Made it false when I need to stop/wait.
  3. Made it true when I want to proceed.
  4. But when there is a lot of calls then this won't work. Else it works fine in my simple cases.

@Before
    public void registerIdlingResource() {
        Utils.idle = true;
        mIdlingResource = mActivityRule.getActivity().getIdlingResource();
        Espresso.registerIdlingResources(mIdlingResource);

}

-----------------------------

 @After
    public void unregisterIdlingResource() {
        if (mIdlingResource != null) {
            Espresso.unregisterIdlingResources(mIdlingResource);
        }
    }


-------------------
signIn();
Utils.idle = false;
onView(withId(R.id.ll_reports)).perform(click());
Utils.idle = false;

After sign in method i wait until the mail page is loaded. Then I click on my layout with id ll_reports. Then again I wait for next task that I want to perform.

Related