public final class

CountingIdlingResource

extends Object
implements IdlingResource
java.lang.Object
   ↳ android.support.test.espresso.contrib.CountingIdlingResource

This class is deprecated.
Use ERROR(/android.support.test.espresso.idling.CountingIdlingResource) instead. This class has been moved to a different module to avoid a dependency on espresso-contrib in your production code. This class will be removed in a future release.

Class Overview

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 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
CountingIdlingResource(String resourceName)
Creates a CountingIdlingResource without debug tracing.
CountingIdlingResource(String resourceName, boolean debugCounting)
Creates a CountingIdlingResource.
Public Methods
void decrement()
Decrements the count of in-flight transactions to the resource being monitored.
void dumpStateToLogs()
Prints the current state of this resource to the logcat at info level.
String getName()
void increment()
Increments the count of in-flight transactions to the resource being monitored.
boolean isIdleNow()
void registerIdleTransitionCallback(IdlingResource.ResourceCallback resourceCallback)
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.support.test.espresso.IdlingResource

Public Constructors

public CountingIdlingResource (String resourceName)

Creates a CountingIdlingResource without debug tracing.

Parameters
resourceName the resource name this resource should report to Espresso.

public CountingIdlingResource (String resourceName, boolean debugCounting)

Creates a CountingIdlingResource.

Parameters
resourceName the resource name this resource should report to Espresso.
debugCounting if true increment & decrement calls will print trace information to logs.

Public Methods

public void decrement ()

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
IllegalStateException if the counter is below 0.

public void dumpStateToLogs ()

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

public String getName ()

public void increment ()

Increments the count of in-flight transactions to the resource being monitored. This method can be called from any thread.

public boolean isIdleNow ()

public void registerIdleTransitionCallback (IdlingResource.ResourceCallback resourceCallback)