Class RobolectricUtil


  • public final class RobolectricUtil
    extends Object
    Utility methods for Robolectric-based tests.
    • Field Detail

      • DEFAULT_TIMEOUT_MS

        public static final long DEFAULT_TIMEOUT_MS
        The default timeout applied when calling runMainLooperUntil(Supplier). This timeout should be sufficient for any condition using a Robolectric test.
        See Also:
        Constant Field Values
    • Method Detail

      • runMainLooperUntil

        public static void runMainLooperUntil​(Supplier<Boolean> condition,
                                              long timeoutMs,
                                              Clock clock)
                                       throws TimeoutException
        Runs tasks of the main Robolectric Looper until the condition returns true.

        Must be called on the main test thread.

        Parameters:
        condition - The condition.
        timeoutMs - The timeout in milliseconds.
        clock - The Clock to measure the timeout.
        Throws:
        TimeoutException - If the timeoutMs timeout is exceeded.
      • runLooperUntil

        public static void runLooperUntil​(Looper looper,
                                          Supplier<Boolean> condition,
                                          long timeoutMs,
                                          Clock clock)
                                   throws TimeoutException
        Runs tasks of the looper until the condition returns true.

        Must be called on the thread corresponding to the looper.

        Parameters:
        looper - The Looper.
        condition - The condition.
        timeoutMs - The timeout in milliseconds.
        clock - The Clock to measure the timeout.
        Throws:
        TimeoutException - If the timeoutMs timeout is exceeded.