Before each Espresso action or assertion, what does Espresso wait for on its own, what work can it not see, and why turn animations off?
answer
- the app at rest
- three idle conditions
- the main looper, not your threads
- registered resources count too
- three Developer options scales
basics
~20 sEspresso waits until the main message queue has nothing due, no AsyncTask runs and all registered idling resources are idle. Your own threads, executors and coroutine dispatchers are invisible to it; animations go off because they cause flakiness.
solid answer
~50 sBefore every `perform` or `check`, Espresso loops the main thread until three conditions hold: the `MessageQueue` has no message it must process now, no `AsyncTask` is executing, and every idling resource registered with `IdlingRegistry` reports idle. That is why a plain click-then-assert test needs no sleeps. It cannot see anything else: a coroutine on `Dispatchers.IO`, an `Executor` you created, or the thread pool of the app's HTTP client. If such work updates the UI later, the assertion can run first, so the test passes on a fast machine and fails on a slow emulator. For that you register an idling resource. Separately, the setup guide recommends turning off **Window animation scale**, **Transition animation scale** and **Animator duration scale** on test devices, because a view that is still animating makes actions and assertions flaky; a `PerformException` even says so when animations are on.
go deeper
Recall that Espresso waits for the app to be idle before each step, so a click followed by a check needs no sleep, and that test devices should have animations off.
Name the three idle conditions, explain why work on Dispatchers.IO or your own executor is invisible, and describe the race that results when such work updates the UI.
Diagnose a test that is green locally and red on CI from the race between background work and the next assertion, and read AppNotIdleException and IdlingResourceTimeoutException for what they reveal.
Decide where synchronisation lives for the team: device settings in the CI image, a shared idling approach for background work, and a rule that timeouts are investigated rather than raised.
## Why Espresso needs no sleeps An Espresso test runs on the **instrumentation thread**, while the app draws on the **main thread**. A naive UI test would click, then sleep, then assert, hoping the app had finished. Espresso replaces that guess with **synchronisation**: every time a test calls `onView(...).perform(...)` or `check(...)`, Espresso first runs the main thread's loop until the app is at rest, then performs the action or assertion on the main thread itself. The test reads as a straight sequence of steps, and each step sees a settled UI. ## The three conditions Espresso checks The documentation lists exactly three conditions, all of which must hold before an action or assertion runs: 1. **The main thread's `MessageQueue` has no messages that Espresso needs to process immediately.** Layout passes, click handlers, `Handler.post` callbacks and results that a background thread has already posted back to the main thread all travel through this queue. 2. **No `AsyncTask` is currently executing a task.** This dates from the time `AsyncTask` was the common background tool; modern apps rarely use it, but Espresso still watches it. 3. **Every registered idling resource is idle.** These are objects the app or the test registers with `IdlingRegistry` to describe work Espresso would otherwise miss. `Espresso.onIdle()` runs the same wait without touching a view; its documentation reserves it for tests and utilities that do not interact with the UI, since `onView` and `onData` already synchronise. ## What Espresso cannot see Anything outside those three conditions is invisible: - a coroutine running on `Dispatchers.IO` or `Dispatchers.Default`; - a `Thread` or `ExecutorService` the app creates itself; - the thread pool of an HTTP client or an image loader; - work in another process, such as a bound service elsewhere. The failure mode is a **race**. The test clicks "Sign in", the click handler starts a network call on a background thread and returns, the main queue empties, and Espresso runs the next assertion before the response arrives. On a fast laptop the call may finish first and the test passes; on a slow CI emulator it does not. The documentation's list of bad workarounds is worth knowing: `Thread.sleep()` (slow and still flaky), retry loops (burn CPU), and `CountDownLatch` (needs timeouts and adds complexity). The supported fix is to make the work visible through an idling resource such as `CountingIdlingResource`. Note where the gap actually is. A result that a background thread has already posted back to the main thread is visible, because it now sits in the queue Espresso watches. The risk is the time before it is posted, while the work is still running elsewhere. ## Timeouts when the app never goes idle Espresso does not wait forever. `IdlingPolicies` holds the defaults in Espresso 3.7.0: | Policy | Default | What happens | |---|---|---| | Master policy (main thread never idle) | 60 seconds | throws `AppNotIdleException` | | Idling resources still busy, warning | 5 seconds | logs a warning naming the busy resources | | Idling resources still busy, error | 26 seconds | throws `IdlingResourceTimeoutException` | `IdlingPolicies.setMasterPolicyTimeout` and `IdlingPolicies.setIdlingResourceTimeout` change them. Raising a timeout is rarely the fix: an app that is never idle usually has something posting messages in a loop, or a resource that never returns to idle. ## Why animations are turned off The Espresso setup guide says, to avoid flakiness, turn off three settings under **Settings > Developer options** on every test device or emulator: - **Window animation scale** - **Transition animation scale** - **Animator duration scale** With animations running, a view can still be moving, fading in or partly off screen when Espresso acts on it, so the same test can pass or fail depending on timing. Espresso checks these settings itself: when an action fails with a `PerformException` and any of the three scales is not zero, the message begins with "Animations or transitions are enabled on the target device." Teams usually bake the three settings into the CI emulator image or set them in the script that boots the device, so no developer has to remember them. The check in the error message is a hint, not a fix: Espresso reports the settings but does not change them. ## Putting it together - Steps that only touch the main thread need nothing extra. - Background work that changes what the next step checks needs an idling resource, or a test setup that runs it where Espresso can see it. - Animations off is part of the environment, not of the test code. - A timeout exception is a symptom to investigate, not a number to raise.
- What does Espresso do by default when the main thread never becomes idle?It keeps looping the main thread until the master idling policy runs out, 60 seconds by default in Espresso 3.7.0, then throws `AppNotIdleException`. `IdlingPolicies.setMasterPolicyTimeout` changes the limit, but the usual cause is something posting to the main thread in a loop, which a longer timeout only hides.
- A view model launches a coroutine that switches to Dispatchers.IO for a repository call; why can the next Espresso assertion run too early?The work on `Dispatchers.IO` runs on a shared thread pool that is neither the main `MessageQueue` nor `AsyncTask`, and no idling resource describes it. Once the click handler returns, the queue is empty, Espresso declares the app idle and runs the assertion. The result only becomes visible to Espresso when it is posted back to the main thread, which may be after the check.
- When is Espresso.onIdle() the right call?In test code or helpers that need Espresso's main-thread synchronisation without touching a view, for example before reading a value the UI thread sets. Its own documentation says `onView` and `onData` already synchronise, so it adds nothing in an ordinary UI test, and it throws if called on the main thread.
- Espresso waits for every background thread in the app to finish
- Espresso automatically waits for any network call the app makes
- Thread.sleep after a click is the normal way to wait for a load
- Turning animations off only speeds tests up; it does not affect reliability
- Raising the idling timeout is the standard fix for AppNotIdleException
Assumes Espresso 3.7.0, whose IdlingPolicies defaults are 60 seconds for the main thread and 26 seconds for busy idling resources.