@UnstableApi
class RobolectricUtil


Utility methods for Robolectric-based tests.

Summary

Constants

const Long

The default timeout applied when calling runMainLooperUntil.

Public functions

java-static ConditionVariable!

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

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

Runs tasks of the looper until the condition returns true.

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

Runs tasks of the looper until the condition returns true.

java-static Unit

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

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

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

Constants

DEFAULT_TIMEOUT_MS

const val DEFAULT_TIMEOUT_MS = 10000: Long

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

Public functions

createRobolectricConditionVariable

java-static fun createRobolectricConditionVariable(): ConditionVariable!

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

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.

condition: Supplier<Boolean!>!

The condition.

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.

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.

condition: Supplier<Boolean!>!

The condition.

timeoutMs: Long

The timeout in milliseconds.

clock: Clock!

The Clock to measure the timeout.

Throws
java.util.concurrent.TimeoutException

If the timeoutMs timeout is exceeded.

runMainLooperUntil

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

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
condition: Supplier<Boolean!>!

The condition.

runMainLooperUntil

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

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
condition: Supplier<Boolean!>!

The condition.

timeoutMs: Long

The timeout in milliseconds.

clock: Clock!

The Clock to measure the timeout.

Throws
java.util.concurrent.TimeoutException

If the timeoutMs timeout is exceeded.