class CountingIdlingResource : IdlingResource


An implementation of IdlingResource that determines idleness by maintaining an internal counter. When the counter is 0 - it is considered to be idle, when it is non-zero it is not idle. This is very similar to the way a java.util.concurrent.Semaphore behaves.

The counter may be incremented or decremented from any thread. If it reaches an illogical state (like counter less than zero) it will throw an IllegalStateException.

This class can then be used to wrap up operations that while in progress should block tests from accessing the UI.

public interface FooServer {
  public Foo newFoo();
  public void updateFoo(Foo foo);
}

public DecoratedFooServer implements FooServer {
  private final FooServer realFooServer;
  private final CountingIdlingResource fooServerIdlingResource;

  public DecoratedFooServer(FooServer realFooServer,
      CountingIdlingResource fooServerIdlingResource) {
    this.realFooServer = checkNotNull(realFooServer);
    this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource);
  }

  public Foo newFoo() {
    fooServerIdlingResource.increment();
    try {
      return realFooServer.newFoo();
    } finally {
      fooServerIdlingResource.decrement();
    }
  }

  public void updateFoo(Foo foo) {
    fooServerIdlingResource.increment();
    try {
      realFooServer.updateFoo(foo);
    } finally {
      fooServerIdlingResource.decrement();
    }
  }
}
Then in your test setup:
public void setUp() throws Exception {
  super.setUp();
  FooServer realServer = FooApplication.getFooServer();
  CountingIdlingResource countingResource = new CountingIdlingResource("FooServerCalls");
  FooApplication.setFooServer(new DecoratedFooServer(realServer, countingResource));
  Espresso.registerIdlingResource(countingResource);
}

Summary

Public constructors

Creates a CountingIdlingResource without debug tracing.

CountingIdlingResource(resourceName: String!, debugCounting: Boolean)

Creates a CountingIdlingResource.

Public functions

Unit

Decrements the count of in-flight transactions to the resource being monitored.

Unit

Prints the current state of this resource to the logcat at info level.

String!

Returns the name of the resources (used for logging and idempotency of registration).

Unit

Increments the count of in-flight transactions to the resource being monitored.

Boolean

Returns true if resource is currently idle.

Unit

Registers the given ResourceCallback with the resource.

Public constructors

CountingIdlingResource

CountingIdlingResource(resourceName: String!)

Creates a CountingIdlingResource without debug tracing.

Parameters
resourceName: String!

the resource name this resource should report to Espresso.

CountingIdlingResource

CountingIdlingResource(resourceName: String!, debugCounting: Boolean)

Creates a CountingIdlingResource.

Parameters
resourceName: String!

the resource name this resource should report to Espresso.

debugCounting: Boolean

if true increment &decrement calls will print trace information to logs.

Public functions

decrement

fun decrement(): Unit

Decrements the count of in-flight transactions to the resource being monitored.

If this operation results in the counter falling below 0 - an exception is raised.

Throws
java.lang.IllegalStateException

if the counter is below 0.

dumpStateToLogs

fun dumpStateToLogs(): Unit

Prints the current state of this resource to the logcat at info level.

getName

fun getName(): String!

Returns the name of the resources (used for logging and idempotency of registration).

increment

fun increment(): Unit

Increments the count of in-flight transactions to the resource being monitored.

This method can be called from any thread.

isIdleNow

fun isIdleNow(): Boolean

Returns true if resource is currently idle. Espresso will always call this method from the main thread, therefore it should be non-blocking and return immediately.

registerIdleTransitionCallback

fun registerIdleTransitionCallback(
    resourceCallback: IdlingResource.ResourceCallback!
): Unit

Registers the given ResourceCallback with the resource. Espresso will call this method:

  • with its implementation of ResourceCallback so it can be notified asynchronously that your resource is idle
  • from the main thread, but you are free to execute the callback's onTransitionToIdle from any thread
  • once (when it is initially given a reference to your IdlingResource)

You only need to call this upon transition from busy to idle - if the resource is already idle when the method is called invoking the call back is optional and has no significant impact.