@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.

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.

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.

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.

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.