# 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
public final class RobolectricUtil
```

<br />

*** ** * ** ***

Utility methods for Robolectric-based tests.

## Summary

| ### Constants |
|---|---|
| `static final long` | `https://developer.android.com/reference/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/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)` or `https://developer.android.com/reference/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>)`. |
| `static final long` | `https://developer.android.com/reference/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_TIMEOUT_MS() = 10000` The default timeout applied when calling `https://developer.android.com/reference/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)`. |

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

```
public static final long 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/androidx/media3/test/utils/robolectric/RobolectricUtil#runMainLooperUntil(com.google.common.base.Supplier<java.lang.Boolean>)` or `https://developer.android.com/reference/androidx/media3/test/utils/robolectric/RobolectricUtil#runLooperUntil(android.os.Looper,com.google.common.base.Supplier<java.lang.Boolean>)`.

### DEFAULT_TIMEOUT_MS

```
public static final long DEFAULT_TIMEOUT_MS = 10000
```

The default timeout applied when calling `https://developer.android.com/reference/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 methods

### createRobolectricConditionVariable

```
public static ConditionVariable createRobolectricConditionVariable()
```

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

### runLooperUntil

```
public static void runLooperUntil(Looper looper, Supplier<Boolean> condition)
```

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/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |

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

### runLooperUntil

```
public static void runLooperUntil(
    Looper looper,
    Supplier<Boolean> condition,
    long timeoutMs,
    Clock clock
)
```

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/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |
| `long timeoutMs` | The timeout in milliseconds. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/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

```
public static void runLooperUntil(
    Looper looper,
    Supplier<Boolean> condition,
    long timeoutMs,
    Clock clock,
    long maxTimeDiffMs
)
```

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/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 |
|---|---|
| `https://developer.android.com/reference/android/os/Looper.html looper` | The `https://developer.android.com/reference/android/os/Looper.html`. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |
| `long timeoutMs` | The timeout in milliseconds. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/androidx/media3/common/util/Clock` to measure the timeout. |
| `long maxTimeDiffMs` | 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

```
public static void runMainLooperUntil(Supplier<Boolean> condition)
```

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/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/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 |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |

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

### runMainLooperUntil

```
public static void runMainLooperUntil(long maxTimeDiffMs, Supplier<Boolean> condition)
```

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/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 |
|---|---|
| `long maxTimeDiffMs` | The maximum allowed time differences between two messages on the `https://developer.android.com/reference/android/os/Looper.html`. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |

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

### runMainLooperUntil

```
public static void runMainLooperUntil(
    Supplier<Boolean> condition,
    long timeoutMs,
    Clock clock
)
```

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/androidx/media3/test/utils/robolectric/RobolectricUtil#DEFAULT_MAX_TIME_DIFF_MS()` of the current `https://developer.android.com/reference/androidx/media3/common/util/SystemClock` time.

Note for `https://developer.android.com/reference/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 |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/base/Supplier.html<https://developer.android.com/reference/java/lang/Boolean.html> condition` | The condition. |
| `long timeoutMs` | The timeout in milliseconds. |
| `https://developer.android.com/reference/androidx/media3/common/util/Clock clock` | The `https://developer.android.com/reference/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. |