Espresso idling resources (original) (raw)

An idling resource represents an asynchronous operation whose results affect subsequent operations in a UI test. By registering idling resources with Espresso, you can validate these asynchronous operations more reliably when testing your app.

Identify when idling resources are needed

Espresso provides a sophisticated set ofsynchronization capabilities. This characteristic of the framework, however, applies only to operations that post messages on the [MessageQueue](/reference/android/os/MessageQueue), such as a subclass of[View](/reference/android/view/View) that's drawing its contents on the screen.

Because Espresso isn't aware of any other asynchronous operations, including those running on a background thread, Espresso can't provide its synchronization guarantees in those situations. In order to make Espresso aware of your app's long-running operations, you must register each one as an idling resource.

If you don't use idling resources when testing the results of your app's asynchronous work, you might find yourself having to use one of the following bad workarounds to improve your tests' reliability:

Espresso allows you to remove these unreliable workarounds from your tests and instead register your app's asynchronous work as idling resources.

Common use cases

When performing operations similar to the following examples in your tests, consider using an idling resource:

It's especially important to register idling resources when these operations update a UI that your tests then validate.

Example idling resource implementations

The following list describes several example implementations of idling resources that you can integrate into your app:

CountingIdlingResource

Maintains a counter of active tasks. When the counter is zero, the associated resource is considered idle. This functionality closely resembles that of a[Semaphore](/reference/java/util/concurrent/Semaphore). In most cases, this implementation is sufficient for managing your app's asynchronous work during testing.

UriIdlingResource

Similar toCountingIdlingResource, but the counter needs to be zero for a specific period of time before the resource is considered idle. This additional waiting period takes consecutive network requests into account, where an app in your thread might make a new request immediately after receiving a response to a previous request.

IdlingThreadPoolExecutor

A custom implementation of [ThreadPoolExecutor](/reference/java/util/concurrent/ThreadPoolExecutor)that keeps track of the total number of running tasks within the created thread pools. This class uses aCountingIdlingResource to maintain the counter of active tasks.

IdlingScheduledThreadPoolExecutor

A custom implementation of[ScheduledThreadPoolExecutor](/reference/java/util/concurrent/ScheduledThreadPoolExecutor). It provides the same functionality and capabilities as theIdlingThreadPoolExecutorclass, but it can also keep track of tasks that are scheduled for the future or are scheduled to execute periodically.

Create your own idling resource

As you use idling resources in your app's tests, you might need to provide custom resource management or logging. In those cases, the implementations listed in the previous section might not suffice. If that's the case, you can extend one of these idling resource implementations or create your own.

If you implement your own idling resource functionality, keep the following best practices in mind, particularly the first one:

Invoke transitions to the idle state outside idle checks.

After your app becomes idle, callonTransitionToIdle()outside any implementations ofisIdleNow(). That way, Espresso doesn't make a second, unnecessary check to determine whether a given idling resource is idle.

The following code snippet illustrates this recommendation:

Kotlin

fun isIdle() { // DON'T call callback.onTransitionToIdle() here! }

fun backgroundWorkDone() { // Background work finished. callback.onTransitionToIdle() // Good. Tells Espresso that the app is idle.

// Don't do any post-processing work beyond this point. Espresso now
// considers your app to be idle and moves on to the next test action.

}

Java

public void isIdle() { // DON'T call callback.onTransitionToIdle() here! }

public void backgroundWorkDone() { // Background work finished. callback.onTransitionToIdle() // Good. Tells Espresso that the app is idle.

// Don't do any post-processing work beyond this point. Espresso now
// considers your app to be idle and moves on to the next test action.

}

Register idling resources before you need them.

The synchronization benefits associated with idling resources only take effect following Espresso's first invocation of that resource'sisIdleNow() method.

The following list shows several examples of this property:

Unregister idling resources after you're done using them.

To conserve system resources, you should unregister idling resources as soon as you don't need them anymore. For example, if you register an idling resource in a method annotated with @Before, it's best to unregister this resource in a corresponding method that's annotated with @After.

Use an idling registry to register and unregister idling resources.

By using this container for your app's idling resources, you can register and unregister idling resources repeatedly as needed and still observe consistent behavior.

Maintain only simple app state within idling resources.

For example, the idling resources that you implement and register shouldn't contain references to [View](/reference/android/view/View) objects.

Register idling resources

Espresso provides a container class into which you can place your app's idling resources. This class, calledIdlingRegistry, is a self-contained artifact that introduces minimal overhead to your app. The class also allows you to take the following steps toward improving your app's maintainability:

Integrate idling resources into your app

Although you can add idling resources to an app in several different ways, one approach in particular maintains encapsulation for your app while still allowing you to specify a particular operation that a given idling resource represents.

When adding idling resources into your app, we highly recommend placing the idling resource logic in the app itself and performing only the registration and unregistration operations in your tests.

Although you create the unusual situation of using a test-only interface in production code by following this approach, you can wrap idling resources around code that you already have, maintaining your app's APK size and method count.

Alternative approaches

If you'd prefer to not have idling resources logic in your app's production code, there are several other viable integration strategies:

Additional resources

For more information about using Espresso in Android tests, consult the following resources.

Samples