# RobolectricUtil

Artifact: [androidx.media3:media3-test-utils-robolectric](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/robolectric/RobolectricUtil.java+class:androidx.media3.test.utils.robolectric.RobolectricUtil)

*** ** * ** ***

<br />

```
@UnstableApi
class RobolectricUtil
```

<br />

*** ** * ** ***

Utility methods for Robolectric-based tests.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS() = 1000` The default maximum allowed time difference between two messages on the `https://developer.android.com/reference/android/os/Looper.html` when running `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)` or `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>)`. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_TIMEOUT_MS() = 10000` The default timeout applied when calling `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)`. |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/common/util/ConditionVariable!` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#createRobolectricConditionVariable()()` Creates a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/ConditionVariable` whose `https://developer.android.com/reference/kotlin/androidx/media3/common/util/ConditionVariable#block(long)` method times out according to wallclock time when used in Robolectric tests. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>)(looper: https://developer.android.com/reference/android/os/Looper.html!, condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!)` Runs tasks of the `looper` until the `condition` returns `true`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>,long,androidx.media3.common.util.Clock)( looper: https://developer.android.com/reference/android/os/Looper.html!, condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!, timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock! )` Runs tasks of the `looper` until the `condition` returns `true`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>,long,androidx.media3.common.util.Clock,long)( looper: https://developer.android.com/reference/android/os/Looper.html!, condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!, timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!, maxTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html )` Runs tasks of the `looper` until the `condition` returns `true`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)(condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!)` Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(long,com.google.common.base.Supplier<java.lang.Boolean>)(maxTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!)` Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>,long,androidx.media3.common.util.Clock)( condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!, timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock! )` Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`. |

## Constants

### DEFAULT_MAX_TIME_DIFF_MS

```
const val DEFAULT_MAX_TIME_DIFF_MS = 1000: Long
```

The default maximum allowed time difference between two messages on the `https://developer.android.com/reference/android/os/Looper.html` when running `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)` or `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>)`.

### DEFAULT_TIMEOUT_MS

```
const val DEFAULT_TIMEOUT_MS = 10000: Long
```

The default timeout applied when calling `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)`. This timeout should be sufficient for any condition using a Robolectric test.

## Public functions

### createRobolectricConditionVariable

```
java-static fun createRobolectricConditionVariable(): ConditionVariable!
```

Creates a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/ConditionVariable` whose `https://developer.android.com/reference/kotlin/androidx/media3/common/util/ConditionVariable#block(long)` method times out according to wallclock time when used in Robolectric tests.

### runLooperUntil

```
java-static fun runLooperUntil(looper: Looper!, condition: Supplier<Boolean!>!): Unit
```

Runs tasks of the `looper` until the `condition` returns `true`.

Must be called on the thread corresponding to the `looper`.

Tasks are only run if their target time is within `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/kotlin/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a message on this `Looper`, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `looper: https://developer.android.com/reference/android/os/Looper.html!` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_TIMEOUT_MS()` is exceeded. |

### runLooperUntil

```
java-static fun runLooperUntil(
    looper: Looper!,
    condition: Supplier<Boolean!>!,
    timeoutMs: Long,
    clock: Clock!
): Unit
```

Runs tasks of the `looper` until the `condition` returns `true`.

Must be called on the thread corresponding to the `looper`.

Tasks are only run if their target time is within `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/kotlin/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a message on this `Looper`, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `looper: https://developer.android.com/reference/android/os/Looper.html!` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The timeout in milliseconds. |
| `clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` to measure the timeout. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `timeoutMs timeout` is exceeded. |

### runLooperUntil

```
java-static fun runLooperUntil(
    looper: Looper!,
    condition: Supplier<Boolean!>!,
    timeoutMs: Long,
    clock: Clock!,
    maxTimeDiffMs: Long
): Unit
```

Runs tasks of the `looper` until the `condition` returns `true`.

Must be called on the thread corresponding to the `looper`.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a message on this `Looper`, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `looper: https://developer.android.com/reference/android/os/Looper.html!` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The timeout in milliseconds. |
| `clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` to measure the timeout. |
| `maxTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The maximum allowed time differences between two messages on the `https://developer.android.com/reference/android/os/Looper.html`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `timeoutMs timeout` is exceeded. |

### runMainLooperUntil

```
java-static fun runMainLooperUntil(condition: Supplier<Boolean!>!): Unit
```

Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`.

Must be called on the main test thread.

Tasks are only run if their target time is within `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/kotlin/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a main `https://developer.android.com/reference/android/os/Looper.html` message, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_TIMEOUT_MS()` is exceeded. |

### runMainLooperUntil

```
java-static fun runMainLooperUntil(maxTimeDiffMs: Long, condition: Supplier<Boolean!>!): Unit
```

Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`.

Must be called on the main test thread.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a main `https://developer.android.com/reference/android/os/Looper.html` message, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `maxTimeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The maximum allowed time differences between two messages on the `https://developer.android.com/reference/android/os/Looper.html`. |
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_TIMEOUT_MS()` is exceeded. |

### runMainLooperUntil

```
java-static fun runMainLooperUntil(
    condition: Supplier<Boolean!>!,
    timeoutMs: Long,
    clock: Clock!
): Unit
```

Runs tasks of the main Robolectric `https://developer.android.com/reference/android/os/Looper.html` until the `condition` returns ` true`.

Must be called on the main test thread.

Tasks are only run if their target time is within `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/kotlin/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` users: If the condition changes outside of a main `https://developer.android.com/reference/android/os/Looper.html` message, for example because it's checking a volatile variable or shared synchronized state that is updated on a background thread, or because checking the condition itself may cause it to become true, then the remainder of the test method may be executed in parallel with other background thread messages.

| Parameters |
|---|---|
| `condition: https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html!>!` | The condition. |
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The timeout in milliseconds. |
| `clock: https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` to measure the timeout. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the `timeoutMs timeout` is exceeded. |