Class FakeClock
- java.lang.Object
-
- com.google.android.exoplayer2.testutil.FakeClock
-
- All Implemented Interfaces:
Clock
public class FakeClock extends Object implements Clock
FakeClockimplementation that allows toadvance the timemanually to trigger pending timed messages.All timed messages sent by a
Handlercreated 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 callingShadowLooper.idleMainLooper().The clock also sets the time of the
SystemClockto match theclock's time.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classFakeClock.HandlerMessageMessage data saved to send messages or execute runnables at a later time on a Handler.
-
Constructor Summary
Constructors Constructor Description FakeClock(boolean isAutoAdvancing)Creates a fake clock that assumes that the system was booted exactly at time0(the Unix Epoch) and aninitialTimeMsof0.FakeClock(long initialTimeMs)Creates a fake clock that doesn't auto-advance and assumes that the system was booted exactly at time0(the Unix Epoch) andinitialTimeMsmilliseconds have passed since system boot.FakeClock(long initialTimeMs, boolean isAutoAdvancing)Creates a fake clock that assumes that the system was booted exactly at time0(the Unix Epoch) andinitialTimeMsmilliseconds 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddPendingHandlerMessage(FakeClock.HandlerMessage message)Adds a message to the list of pending messages.voidadvanceTime(long timeDiffMs)Advance timestamp ofFakeClockby the specified duration.HandlerWrappercreateHandler(Looper looper, Handler.Callback callback)Creates aHandlerWrapperusing a specified looper and a specified callback for handling messages.longcurrentTimeMillis()Returns the current time in milliseconds since the Unix Epoch.longelapsedRealtime()voidonThreadBlocked()Notifies the clock that the current thread is about to be blocked and won't return until a condition on another thread becomes true.longuptimeMillis()
-
-
-
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 time0(the Unix Epoch) andinitialTimeMsmilliseconds 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 time0(the Unix Epoch) and aninitialTimeMsof0.- 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 time0(the Unix Epoch) andinitialTimeMsmilliseconds 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.
-
-
Method Detail
-
advanceTime
public void advanceTime(long timeDiffMs)
Advance timestamp ofFakeClockby the specified duration.- Parameters:
timeDiffMs- The amount of time to add to the timestamp in milliseconds.
-
currentTimeMillis
public long currentTimeMillis()
Description copied from interface:ClockReturns the current time in milliseconds since the Unix Epoch.- Specified by:
currentTimeMillisin interfaceClock- See Also:
System.currentTimeMillis()
-
elapsedRealtime
public long elapsedRealtime()
- Specified by:
elapsedRealtimein interfaceClock- See Also:
SystemClock.elapsedRealtime()
-
uptimeMillis
public long uptimeMillis()
- Specified by:
uptimeMillisin interfaceClock- See Also:
SystemClock.uptimeMillis()
-
createHandler
public HandlerWrapper createHandler(Looper looper, @Nullable Handler.Callback callback)
Description copied from interface:ClockCreates aHandlerWrapperusing a specified looper and a specified callback for handling messages.- Specified by:
createHandlerin interfaceClock- See Also:
Handler(Looper, Handler.Callback)
-
onThreadBlocked
public void onThreadBlocked()
Description copied from interface:ClockNotifies the clock that the current thread is about to be blocked and won't return until a condition on another thread becomes true.Should be a no-op for all non-test cases.
- Specified by:
onThreadBlockedin interfaceClock
-
addPendingHandlerMessage
protected void addPendingHandlerMessage(FakeClock.HandlerMessage message)
Adds a message to the list of pending messages.
-
-