# UriIdlingResource

# UriIdlingResource

Artifact:[androidx.test.espresso.idling:idling-net](https://developer.android.com/jetpack/androidx/releases/test-espresso-idling)  
[View Source](https://cs.android.com/search?q=file:androidx/test/espresso/idling/net/UriIdlingResource.java+class:androidx.test.espresso.idling.net.UriIdlingResource)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/test/espresso/idling/net/UriIdlingResource "View this page in Java")

<br />

```
class UriIdlingResource : IdlingResource
```

<br />

*** ** * ** ***

An implementation of[IdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource)useful for monitoring idleness of network traffic.

This is similar to androidx.test.espresso.contrib.CountingIdlingResource, with the additional idleness constraint that the counter must be 0 for a set period of time before the resource becomes idle.

A network timeout is required to be reasonably sure that the webview has finished loading. Imagine the case where each response that comes back causes another request to be made until loading is complete. The counter will go from 0-\>1-\>0-\>1-\>0-\>1..., but we don't want to report the webview as idle each time this happens.

## Summary

|                                                                                  ### Nested types                                                                                   |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `interface `[UriIdlingResource.HandlerIntf](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource.HandlerIntf) Wraps a Handler object. |

|                                                                                                                                                                      ### Public constructors                                                                                                                                                                       |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [UriIdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#UriIdlingResource(java.lang.String,long))`(resourceName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!, timeoutMs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` |

|                                 ### Public functions                                 |
|--------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)        | [beginLoad](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#beginLoad(java.lang.String))`(uri: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Called when a request is made.                                                                                                                                                                                                                                                                                            |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)        | [endLoad](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#endLoad(java.lang.String))`(uri: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Called when a request is completed (ie the response is returned).                                                                                                                                                                                                                                                             |
| [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | [getName](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#getName())`()` Returns the name of the resources (used for logging and idempotency of registration).                                                                                                                                                                                                                                                                                                                                                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)        | [ignoreUri](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#ignoreUri(java.util.regex.Pattern))`(pattern: `[Pattern](https://developer.android.com/reference/java/util/regex/Pattern.html)`!)` Add a regex pattern to the ignore list.                                                                                                                                                                                                                                                                          |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)  | [isIdleNow](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#isIdleNow())`()` Returns`true`if resource is currently idle.                                                                                                                                                                                                                                                                                                                                                                                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)        | [registerIdleTransitionCallback](https://developer.android.com/reference/kotlin/androidx/test/espresso/idling/net/UriIdlingResource#registerIdleTransitionCallback(androidx.test.espresso.IdlingResource.ResourceCallback))`(` ` resourceCallback: `[IdlingResource.ResourceCallback](https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource.ResourceCallback)`!` `)` Registers the given[ResourceCallback](https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource.ResourceCallback)with the resource. |

## Public constructors

### UriIdlingResource

```
UriIdlingResource(resourceName: String!, timeoutMs: Long)
```  

## Public functions

### beginLoad

```
fun beginLoad(uri: String!): Unit
```

Called when a request is made.

If the URI is not blacklisted the idle counter is incremented.  

### endLoad

```
fun endLoad(uri: String!): Unit
```

Called when a request is completed (ie the response is returned).

If the URI is not blacklisted the idle counter is decremented. Once the idle counter reaches 0, the idle update thread will set the resource as idle after the appropriate timeout.  

### getName

```
fun getName(): String!
```

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

### ignoreUri

```
fun ignoreUri(pattern: Pattern!): Unit
```

Add a regex pattern to the ignore list.

All request URIs are checked against all patterns, and matches are ignored for the purposes of detecting when the webview is idle.

Ignored patterns can only be added when the webview is idle.  

### 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](https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource.ResourceCallback)with the resource. Espresso will call this method:

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