Class FakeClock

  • All Implemented Interfaces:
    Clock

    public class FakeClock
    extends Object
    implements Clock
    Fake Clock implementation that allows to advance the time manually to trigger pending timed messages.

    All timed messages sent by a Handler created from this clock are governed by the clock's time. Messages sent through these handlers are not triggered until previous messages on any thread have been handled to ensure deterministic execution. Note that this includes messages sent from the main Robolectric test thread, meaning that these messages are only triggered if the main test thread is idle, which can be explicitly requested by calling ShadowLooper.idleMainLooper().

    The clock also sets the time of the SystemClock to match the clock's time.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  FakeClock.HandlerMessage
      Message data saved to send messages or execute runnables at a later time on a Handler.
    • Field Summary

      • Fields inherited from interface com.google.android.exoplayer2.util.Clock

        DEFAULT
    • Constructor Summary

      Constructors 
      Constructor Description
      FakeClock​(boolean isAutoAdvancing)
      Creates a fake clock that assumes that the system was booted exactly at time 0 (the Unix Epoch) and an initialTimeMs of 0.
      FakeClock​(long initialTimeMs)
      Creates a fake clock that doesn't auto-advance and assumes that the system was booted exactly at time 0 (the Unix Epoch) and initialTimeMs milliseconds have passed since system boot.
      FakeClock​(long initialTimeMs, boolean isAutoAdvancing)
      Creates a fake clock that assumes that the system was booted exactly at time 0 (the Unix Epoch) and initialTimeMs milliseconds have passed since system boot.
      FakeClock​(long bootTimeMs, long initialTimeMs, boolean isAutoAdvancing)
      Creates a fake clock specifying when the system was booted and how much time has passed since then.
    • Constructor Detail

      • FakeClock

        public FakeClock​(long initialTimeMs)
        Creates a fake clock that doesn't auto-advance and assumes that the system was booted exactly at time 0 (the Unix Epoch) and initialTimeMs milliseconds have passed since system boot.
        Parameters:
        initialTimeMs - The initial elapsed time since the boot time, in milliseconds.
      • FakeClock

        public FakeClock​(boolean isAutoAdvancing)
        Creates a fake clock that assumes that the system was booted exactly at time 0 (the Unix Epoch) and an initialTimeMs of 0.
        Parameters:
        isAutoAdvancing - Whether the clock should automatically advance the time to the time of next message that is due to be sent.
      • FakeClock

        public FakeClock​(long initialTimeMs,
                         boolean isAutoAdvancing)
        Creates a fake clock that assumes that the system was booted exactly at time 0 (the Unix Epoch) and initialTimeMs milliseconds have passed since system boot.
        Parameters:
        initialTimeMs - The initial elapsed time since the boot time, in milliseconds.
        isAutoAdvancing - Whether the clock should automatically advance the time to the time of next message that is due to be sent.
      • FakeClock

        public FakeClock​(long bootTimeMs,
                         long initialTimeMs,
                         boolean isAutoAdvancing)
        Creates a fake clock specifying when the system was booted and how much time has passed since then.
        Parameters:
        bootTimeMs - The time the system was booted since the Unix Epoch, in milliseconds.
        initialTimeMs - The initial elapsed time since the boot time, in milliseconds.
        isAutoAdvancing - Whether the clock should automatically advance the time to the time of next message that is due to be sent.