@UnstableApi
class FakeClock : 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

Message data saved to send messages or execute runnables at a later time on a Handler.

Public constructors

FakeClock(initialTimeMs: Long)

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(isAutoAdvancing: Boolean)

Creates a fake clock that assumes that the system was booted exactly at time 0 (the Unix Epoch) and an initialTimeMs of 0.

FakeClock(initialTimeMs: Long, isAutoAdvancing: Boolean)

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(bootTimeMs: Long, initialTimeMs: Long, isAutoAdvancing: Boolean)

Creates a fake clock specifying when the system was booted and how much time has passed since then.

Public functions

synchronized Unit
advanceTime(timeDiffMs: Long)

Advance timestamp of FakeClock by the specified duration.

HandlerWrapper!
createHandler(looper: Looper!, callback: Handler.Callback?)

Creates a HandlerWrapper using a specified looper and a specified callback for handling messages.

synchronized Long

Returns the current time in milliseconds since the Unix Epoch.

synchronized Long
synchronized Long

See nanoTime

synchronized Unit

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 functions

synchronized Unit

Adds a message to the list of pending messages.

Inherited Constants

From androidx.media3.common.util.Clock
const Clock!

Default Clock to use for all non-test cases.

Public constructors

FakeClock

FakeClock(initialTimeMs: Long)

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: Long

The initial elapsed time since the boot time, in milliseconds.

FakeClock

FakeClock(isAutoAdvancing: Boolean)

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: Boolean

Whether the clock should automatically advance the time to the time of next message that is due to be sent.

FakeClock

FakeClock(initialTimeMs: Long, isAutoAdvancing: Boolean)

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: Long

The initial elapsed time since the boot time, in milliseconds.

isAutoAdvancing: Boolean

Whether the clock should automatically advance the time to the time of next message that is due to be sent.

FakeClock

FakeClock(bootTimeMs: Long, initialTimeMs: Long, isAutoAdvancing: Boolean)

Creates a fake clock specifying when the system was booted and how much time has passed since then.

Parameters
bootTimeMs: Long

The time the system was booted since the Unix Epoch, in milliseconds.

initialTimeMs: Long

The initial elapsed time since the boot time, in milliseconds.

isAutoAdvancing: Boolean

Whether the clock should automatically advance the time to the time of next message that is due to be sent.

Public functions

advanceTime

synchronized fun advanceTime(timeDiffMs: Long): Unit

Advance timestamp of FakeClock by the specified duration.

Parameters
timeDiffMs: Long

The amount of time to add to the timestamp in milliseconds.

createHandler

fun createHandler(looper: Looper!, callback: Handler.Callback?): HandlerWrapper!

Creates a HandlerWrapper using a specified looper and a specified callback for handling messages.

See also
Handler

currentTimeMillis

synchronized fun currentTimeMillis(): Long

Returns the current time in milliseconds since the Unix Epoch.

elapsedRealtime

synchronized fun elapsedRealtime(): Long
See also
elapsedRealtime

nanoTime

synchronized fun nanoTime(): Long

See nanoTime

onThreadBlocked

synchronized fun onThreadBlocked(): Unit

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.

uptimeMillis

fun uptimeMillis(): Long
See also
uptimeMillis

Protected functions

addPendingHandlerMessage

synchronized protected fun addPendingHandlerMessage(message: FakeClock.HandlerMessage!): Unit

Adds a message to the list of pending messages.