@UnstableApi
public final class RobolectricUtil


Utility methods for Robolectric-based tests.

Summary

Constants

static final long

The default timeout applied when calling runMainLooperUntil.

Public methods

static ConditionVariable

Creates a ConditionVariable whose block method times out according to wallclock time when used in Robolectric tests.

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

Runs tasks of the looper until the condition returns true.

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

Runs tasks of the looper until the condition returns true.

static void

Runs tasks of the main Robolectric Looper until the condition returns true.

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

Runs tasks of the main Robolectric Looper until the condition returns true.

Constants

DEFAULT_TIMEOUT_MS

public static final long DEFAULT_TIMEOUT_MS = 10000

The default timeout applied when calling runMainLooperUntil. This timeout should be sufficient for any condition using a Robolectric test.

Public methods

createRobolectricConditionVariable

public static ConditionVariable createRobolectricConditionVariable()

Creates a ConditionVariable whose block 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.

Note for 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 looper

The Looper.

Supplier<Boolean> condition

The condition.

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.

Note for 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 looper

The Looper.

Supplier<Boolean> condition

The condition.

long timeoutMs

The timeout in milliseconds.

Clock clock

The Clock to measure the timeout.

Throws
java.util.concurrent.TimeoutException

If the timeoutMs timeout is exceeded.

runMainLooperUntil

public static void runMainLooperUntil(Supplier<Boolean> condition)

Runs tasks of the main Robolectric Looper until the condition returns true.

Must be called on the main test thread.

Note for androidx.media3.test.utils.FakeClock users: If the condition changes outside of a main Looper 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
Supplier<Boolean> condition

The condition.

runMainLooperUntil

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

Runs tasks of the main Robolectric Looper until the condition returns true.

Must be called on the main test thread.

Note for androidx.media3.test.utils.FakeClock users: If the condition changes outside of a main Looper 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
Supplier<Boolean> condition

The condition.

long timeoutMs

The timeout in milliseconds.

Clock clock

The Clock to measure the timeout.

Throws
java.util.concurrent.TimeoutException

If the timeoutMs timeout is exceeded.