ClockAutoAdvancingFakeClockpublic class FakeClock extends Object implements Clock
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.
The clock also sets the time of the SystemClock to match the clock's time.
| Constructor | Description |
|---|---|
FakeClock(long initialTimeMs) |
Creates a fake clock assuming the system was booted exactly at time
0 (the Unix Epoch)
and initialTimeMs milliseconds have passed since system boot. |
FakeClock(long bootTimeMs,
long initialTimeMs) |
Creates a fake clock specifying when the system was booted and how much time has passed since
then.
|
| Modifier and Type | Method | Description |
|---|---|---|
protected boolean |
addHandlerMessageAtTime(HandlerWrapper handler,
int message,
long timeMs) |
Adds an empty handler message to list of pending messages.
|
protected boolean |
addHandlerMessageAtTime(HandlerWrapper handler,
Runnable runnable,
long timeMs) |
Adds a handler post to list of pending messages.
|
void |
advanceTime(long timeDiffMs) |
Advance timestamp of
FakeClock by the specified duration. |
HandlerWrapper |
createHandler(Looper looper,
Handler.Callback callback) |
Creates a
HandlerWrapper using a specified looper and a specified callback for handling
messages. |
long |
currentTimeMillis() |
Returns the current time in milliseconds since the Unix Epoch.
|
long |
elapsedRealtime() |
|
void |
sleep(long sleepTimeMs) |
|
long |
uptimeMillis() |
public FakeClock(long initialTimeMs)
0 (the Unix Epoch)
and initialTimeMs milliseconds have passed since system boot.initialTimeMs - The initial elapsed time since the boot time, in milliseconds.public FakeClock(long bootTimeMs,
long initialTimeMs)
bootTimeMs - The time the system was booted since the Unix Epoch, in milliseconds.initialTimeMs - The initial elapsed time since the boot time, in milliseconds.public void advanceTime(long timeDiffMs)
FakeClock by the specified duration.timeDiffMs - The amount of time to add to the timestamp in milliseconds.public long currentTimeMillis()
ClockcurrentTimeMillis in interface ClockSystem.currentTimeMillis()public long elapsedRealtime()
elapsedRealtime in interface ClockSystemClock.elapsedRealtime()public long uptimeMillis()
uptimeMillis in interface ClockSystemClock.uptimeMillis()public void sleep(long sleepTimeMs)
sleep in interface ClockSystemClock.sleep(long)public HandlerWrapper createHandler(Looper looper, @Nullable Handler.Callback callback)
ClockHandlerWrapper using a specified looper and a specified callback for handling
messages.createHandler in interface ClockHandler(Looper, Handler.Callback)protected boolean addHandlerMessageAtTime(HandlerWrapper handler, Runnable runnable, long timeMs)
protected boolean addHandlerMessageAtTime(HandlerWrapper handler, int message, long timeMs)