RobolectricUtil
@UnstableApi
public final class RobolectricUtil
Utility methods for Robolectric-based tests.
Summary
Constants |
|
|---|---|
static final long |
DEFAULT_MAX_TIME_DIFF_MS = 1000The default maximum allowed time difference between two messages on the |
static final long |
DEFAULT_TIMEOUT_MS = 10000The default timeout applied when calling |
Public methods |
|
|---|---|
static ConditionVariable |
Creates a |
static void |
runLooperUntil(Looper looper, Supplier<Boolean> condition)Runs tasks of the |
static void |
runLooperUntil(Runs tasks of the |
static void |
runLooperUntil(Runs tasks of the |
static void |
runMainLooperUntil(Supplier<Boolean> condition)Runs tasks of the main Robolectric |
static void |
runMainLooperUntil(long maxTimeDiffMs, Supplier<Boolean> condition)Runs tasks of the main Robolectric |
static void |
runMainLooperUntil(Runs tasks of the main Robolectric |
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 Looper when running runMainLooperUntil or runLooperUntil.
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.
Tasks are only run if their target time is within DEFAULT_MAX_TIME_DIFF_MS of the current SystemClock time.
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.
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |
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 DEFAULT_MAX_TIME_DIFF_MS of the current SystemClock time.
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 |
Supplier<Boolean> condition |
The condition. |
long timeoutMs |
The timeout in milliseconds. |
Clock clock |
The |
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |
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 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 |
Supplier<Boolean> condition |
The condition. |
long timeoutMs |
The timeout in milliseconds. |
Clock clock |
The |
long maxTimeDiffMs |
The maximum allowed time differences between two messages on the |
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |
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.
Tasks are only run if their target time is within DEFAULT_MAX_TIME_DIFF_MS of the current SystemClock time.
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.
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |
runMainLooperUntil
public static void runMainLooperUntil(long maxTimeDiffMs, 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 | |
|---|---|
long maxTimeDiffMs |
The maximum allowed time differences between two messages on the |
Supplier<Boolean> condition |
The condition. |
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |
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.
Tasks are only run if their target time is within DEFAULT_MAX_TIME_DIFF_MS of the current SystemClock time.
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 |
| Throws | |
|---|---|
java.util.concurrent.TimeoutException |
If the |