# CountingIdlingResource

Summary:[Ctors](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#pubctors)\|[Methods](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#)  

# CountingIdlingResource

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public

final

class
CountingIdlingResource
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`


``


implements

`[IdlingResource](https://developer.android.com/reference/android/support/test/espresso/IdlingResource)`


`

|---|-------------------------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||
| ↳ | android.support.test.espresso.idling.CountingIdlingResource |

<br />

*** ** * ** ***

An implementation of[IdlingResource](https://developer.android.com/reference/android/support/test/espresso/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](https://developer.android.com/reference/java/util/concurrent/Semaphore.html)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);
     }
     
<br />

## Summary

|                                                                                                                                                          ### Public constructors                                                                                                                                                          ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[CountingIdlingResource](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#CountingIdlingResource(java.lang.String))`(`[String](https://developer.android.com/reference/java/lang/String.html)` resourceName) ` Creates a CountingIdlingResource without debug tracing.           |
| ` `[CountingIdlingResource](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#CountingIdlingResource(java.lang.String, boolean))`(`[String](https://developer.android.com/reference/java/lang/String.html)` resourceName, boolean debugCounting) ` Creates a CountingIdlingResource. |

|                                                                                                                                                                                                                                                                                                                        ### Public methods                                                                                                                                                                                                                                                                                                                        ||
|----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                    | ` `[decrement](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#decrement())`() ` Decrements the count of in-flight transactions to the resource being monitored.                                                                                                                                                                                                                                                                                                                                                                 |
| ` void`                                                                    | ` `[dumpStateToLogs](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#dumpStateToLogs())`() ` Prints the current state of this resource to the logcat at info level.                                                                                                                                                                                                                                                                                                                                                              |
| ` `[String](https://developer.android.com/reference/java/lang/String.html) | ` `[getName](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#getName())`() ` Returns the name of the resources (used for logging and idempotency of registration).                                                                                                                                                                                                                                                                                                                                                               |
| ` void`                                                                    | ` `[increment](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#increment())`() ` Increments the count of in-flight transactions to the resource being monitored.                                                                                                                                                                                                                                                                                                                                                                 |
| ` boolean`                                                                 | ` `[isIdleNow](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#isIdleNow())`() ` Returns`true`if resource is currently idle.                                                                                                                                                                                                                                                                                                                                                                                                     |
| ` void`                                                                    | ` `[registerIdleTransitionCallback](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#registerIdleTransitionCallback(android.support.test.espresso.IdlingResource.ResourceCallback))`(`[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)` resourceCallback) ` Registers the given[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)with the resource. |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#)From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)` ` |-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object.html)          | ` clone() `                                                                                | | ` boolean`                                                                          | ` equals(`[Object](https://developer.android.com/reference/java/lang/Object.html)` arg0) ` | | ` void`                                                                             | ` finalize() `                                                                             | | ` final `[Class](https://developer.android.com/reference/java/lang/Class.html)`<?>` | ` getClass() `                                                                             | | ` int`                                                                              | ` hashCode() `                                                                             | | ` final void`                                                                       | ` notify() `                                                                               | | ` final void`                                                                       | ` notifyAll() `                                                                            | | ` `[String](https://developer.android.com/reference/java/lang/String.html)          | ` toString() `                                                                             | | ` final void`                                                                       | ` wait(long arg0, int arg1) `                                                              | | ` final void`                                                                       | ` wait(long arg0) `                                                                        | | ` final void`                                                                       | ` wait() `                                                                                 | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource#)From interface` `[android.support.test.espresso.IdlingResource](https://developer.android.com/reference/android/support/test/espresso/IdlingResource)` ` |-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract `[String](https://developer.android.com/reference/java/lang/String.html) | ` `[getName](https://developer.android.com/reference/android/support/test/espresso/IdlingResource#getName())`() ` Returns the name of the resources (used for logging and idempotency of registration).                                                                                                                                                                                                                                                                                                                                                       | | ` abstract boolean`                                                                 | ` `[isIdleNow](https://developer.android.com/reference/android/support/test/espresso/IdlingResource#isIdleNow())`() ` Returns`true`if resource is currently idle.                                                                                                                                                                                                                                                                                                                                                                                             | | ` abstract void`                                                                    | ` `[registerIdleTransitionCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource#registerIdleTransitionCallback(android.support.test.espresso.IdlingResource.ResourceCallback))`(`[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)` callback) ` Registers the given[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)with the resource. | ||

## Public constructors

### CountingIdlingResource

```
CountingIdlingResource (String resourceName)
```

Creates a CountingIdlingResource without debug tracing.

<br />

|                                         Parameters                                          ||
|----------------|-----------------------------------------------------------------------------|
| `resourceName` | `String`: the resource name this resource should report to Espresso. <br /> |

### CountingIdlingResource

```
CountingIdlingResource (String resourceName, 
                boolean debugCounting)
```

Creates a CountingIdlingResource.

<br />

|                                                  Parameters                                                   ||
|-----------------|----------------------------------------------------------------------------------------------|
| `resourceName`  | `String`: the resource name this resource should report to Espresso. <br />                  |
| `debugCounting` | `boolean`: if true increment \& decrement calls will print trace information to logs. <br /> |

## Public methods

### decrement

```
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.

<br />

|                                                              Throws                                                               ||
|-------------------------------------------------------------------------------------------------------|----------------------------|
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException.html) | if the counter is below 0. |

### dumpStateToLogs

```
void dumpStateToLogs ()
```

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

<br />

### getName

```
String getName ()
```

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

<br />

|                                     Returns                                     ||
|-------------------------------------------------------------------------|--------|
| [String](https://developer.android.com/reference/java/lang/String.html) | <br /> |

### increment

```
void increment ()
```

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

This method can be called from any thread.

<br />

### isIdleNow

```
boolean isIdleNow ()
```

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.

<br />

|      Returns      ||
|-----------|--------|
| `boolean` | <br /> |

### registerIdleTransitionCallback

```
void registerIdleTransitionCallback (IdlingResource.ResourceCallback resourceCallback)
```

Registers the given[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)with the resource. Espresso will call this method:

- with its implementation of[IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.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.

<br />

|                          Parameters                          ||
|--------------------|------------------------------------------|
| `resourceCallback` | `IdlingResource.ResourceCallback` <br /> |

-

  ## Classes

  - [CountingIdlingResource](https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource)