FakeClock
@UnstableApi
public class FakeClock 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.
Summary
Nested types |
|---|
public final class FakeClock.BuilderA builder for |
protected final inner class FakeClock.HandlerMessage implements Comparable, HandlerWrapper.MessageMessage data saved to send messages or execute runnables at a later time on a Handler. |
Constants |
|
|---|---|
static final long |
The default maximum time difference between two messages that the fake clock will automatically advance. |
Public constructors |
|---|
FakeClock(long initialTimeMs)Creates a fake clock that doesn't auto-advance and assumes that the system was booted exactly at time |
FakeClock(boolean isAutoAdvancing)Creates a fake clock that assumes that the system was booted exactly at time |
FakeClock(long initialTimeMs, boolean isAutoAdvancing)Creates a fake clock that assumes that the system was booted exactly at time |
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. |
Public methods |
|
|---|---|
synchronized void |
advanceTime(long timeDiffMs)Advance timestamp of |
HandlerWrapper |
createHandler(Creates a |
synchronized long |
Returns the current time in milliseconds since the Unix Epoch. |
synchronized long |
|
synchronized long |
nanoTime()See |
synchronized void |
Notifies the clock that the current thread is about to be blocked and won't return until a condition on another thread becomes true. |
long |
Protected methods |
|
|---|---|
synchronized void |
Adds a message to the list of pending messages. |
Constants
DEFAULT_MAX_AUTO_ADVANCING_TIME_DIFF_MS
public static final long DEFAULT_MAX_AUTO_ADVANCING_TIME_DIFF_MS = 1000
The default maximum time difference between two messages that the fake clock will automatically advance.
Public constructors
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 | |
|---|---|
long 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 | |
|---|---|
boolean 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 | |
|---|---|
long initialTimeMs |
The initial elapsed time since the boot time, in milliseconds. |
boolean 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 | |
|---|---|
long bootTimeMs |
The time the system was booted since the Unix Epoch, in milliseconds. |
long initialTimeMs |
The initial elapsed time since the boot time, in milliseconds. |
boolean isAutoAdvancing |
Whether the clock should automatically advance the time to the time of next message that is due to be sent. |
Public methods
advanceTime
synchronized public void advanceTime(long timeDiffMs)
Advance timestamp of FakeClock by the specified duration.
| Parameters | |
|---|---|
long timeDiffMs |
The amount of time to add to the timestamp in milliseconds. |
createHandler
public HandlerWrapper createHandler(
Looper looper,
@UnknownInitialization @Nullable @UnknownInitialization Handler.Callback callback
)
Creates a HandlerWrapper using a specified looper and a specified callback for handling messages.
| See also | |
|---|---|
Handler |
currentTimeMillis
synchronized public long currentTimeMillis()
Returns the current time in milliseconds since the Unix Epoch.
| See also | |
|---|---|
currentTimeMillis |
onThreadBlocked
synchronized public void onThreadBlocked()
Notifies 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.
Protected methods
addPendingHandlerMessage
synchronized protected void addPendingHandlerMessage(FakeClock.HandlerMessage message)
Adds a message to the list of pending messages.