# FakeClock

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/FakeClock.java+class:androidx.media3.test.utils.FakeClock)

*** ** * ** ***


```
@UnstableApi
class FakeClock : Clock
```

<br />

*** ** * ** ***

Fake `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` implementation that allows to `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#advanceTime(long)` manually to trigger pending timed messages.

All timed messages sent by a `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#createHandler(android.os.Looper,android.os.Handler.Callback)` 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 `https://developer.android.com/reference/android/os/SystemClock.html` to match the `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#elapsedRealtime()`.

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock.Builder` A builder for `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` instances. |
| `protected inner class https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock.HandlerMessage : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-comparable/index.html<https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock.HandlerMessage!>, https://developer.android.com/reference/kotlin/androidx/media3/common/util/HandlerWrapper.Message` Message data saved to send messages or execute runnables at a later time on a Handler. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#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 |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#FakeClock(long)(initialTimeMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` 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. |
| `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#FakeClock(boolean)(isAutoAdvancing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Creates a fake clock that assumes that the system was booted exactly at time `0` (the Unix Epoch) and an `initialTimeMs` of `0`. |
| `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#FakeClock(long,boolean)(initialTimeMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, isAutoAdvancing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` 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. |
| `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#FakeClock(long,long,boolean)(bootTimeMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, initialTimeMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, isAutoAdvancing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Creates a fake clock specifying when the system was booted and how much time has passed since then. |

| ### Public functions |
|---|---|
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#advanceTime(long)(timeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Advance timestamp of `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` by the specified duration. |
| `https://developer.android.com/reference/kotlin/androidx/media3/common/util/HandlerWrapper!` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#createHandler(android.os.Looper,android.os.Handler.Callback)( looper: https://developer.android.com/reference/android/os/Looper.html!, @https://checkerframework.org/api/org/checkerframework/checker/initialization/qual/UnknownInitialization.html callback: @https://checkerframework.org/api/org/checkerframework/checker/initialization/qual/UnknownInitialization.html https://developer.android.com/reference/android/os/Handler.Callback.html? )` Creates a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/HandlerWrapper` using a specified looper and a specified callback for handling messages. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#currentTimeMillis()()` Returns the current time in milliseconds since the Unix Epoch. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#elapsedRealtime()()` |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#nanoTime()()` See `https://developer.android.com/reference/java/lang/System.html#nanoTime()` |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#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. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#uptimeMillis()()` |

| ### Protected functions |
|---|---|
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock#addPendingHandlerMessage(androidx.media3.test.utils.FakeClock.HandlerMessage)(message: https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock.HandlerMessage!)` Adds a message to the list of pending messages. |

### Inherited Constants

From [androidx.media3.common.util.Clock](https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock) `const https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock!` `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock#DEFAULT()`

Default `https://developer.android.com/reference/kotlin/androidx/media3/common/util/Clock` to use for all non-test cases.

## Constants

### DEFAULT_MAX_AUTO_ADVANCING_TIME_DIFF_MS

```
const val DEFAULT_MAX_AUTO_ADVANCING_TIME_DIFF_MS = 1000: Long
```

The default maximum time difference between two messages that the fake clock will automatically advance.

## 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The initial elapsed time since the boot time, in milliseconds. |
| `isAutoAdvancing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The time the system was booted since the Unix Epoch, in milliseconds. |
| `initialTimeMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The initial elapsed time since the boot time, in milliseconds. |
| `isAutoAdvancing: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | 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 `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FakeClock` by the specified duration.

| Parameters |
|---|---|
| `timeDiffMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The amount of time to add to the timestamp in milliseconds. |

### createHandler

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

Creates a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/HandlerWrapper` using a specified looper and a specified callback for handling messages.

| See also |
|---|---|
| `https://developer.android.com/reference/android/os/Handler.html#Handler(android.os.Looper, android.os.Handler.Callback)` |   |

### currentTimeMillis

```
synchronized fun currentTimeMillis(): Long
```

Returns the current time in milliseconds since the Unix Epoch.

| See also |
|---|---|
| `https://developer.android.com/reference/java/lang/System.html#currentTimeMillis()` |   |

### elapsedRealtime

```
synchronized fun elapsedRealtime(): Long
```

| See also |
|---|---|
| `https://developer.android.com/reference/android/os/SystemClock.html#elapsedRealtime()` |   |

### nanoTime

```
synchronized fun nanoTime(): Long
```

See `https://developer.android.com/reference/java/lang/System.html#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 |
|---|---|
| `https://developer.android.com/reference/android/os/SystemClock.html#uptimeMillis()` |   |

## Protected functions

### addPendingHandlerMessage

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

Adds a message to the list of pending messages.