# TestMonotonicFrameClock

Artifact: [androidx.compose.ui:ui-test](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/TestMonotonicFrameClock.jvmAndAndroid.kt+class:androidx.compose.ui.test.TestMonotonicFrameClock) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0) Common/AllAndroid/JVM
- android

  ```
  @ExperimentalCoroutinesApi
  @ExperimentalTestApi
  class TestMonotonicFrameClock : MonotonicFrameClock
  ```

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MonotonicFrameClock` with a time source controlled by a `kotlinx-coroutines-test` `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html`. This frame clock may be used to consistently drive time under controlled tests.

Calls to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` will schedule an upcoming frame `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#(kotlinx.coroutines.CoroutineScope,kotlin.Long,kotlin.Function1)` nanoseconds in the future by launching into `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#(kotlinx.coroutines.CoroutineScope,kotlin.Long,kotlin.Function1)` if such a frame has not yet been scheduled. The current frame time for `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` is provided by `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#delayController()`. It is strongly suggested that `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#(kotlinx.coroutines.CoroutineScope,kotlin.Long,kotlin.Function1)` contain the test dispatcher controlled by `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#delayController()`.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#TestMonotonicFrameClock(kotlinx.coroutines.CoroutineScope,kotlin.Long,kotlin.Function1)( coroutineScope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html, frameDelayNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, onPerformTraversals: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` | android |

| ### Public functions |
|---|---|---|
| `open suspend R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)(onFrame: (frameTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) -> R)` Schedules `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` to be ran on the next "fake" frame, and schedules the task to actually perform that frame if it hasn't already been scheduled. | android |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation-interceptor/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/ui/test/ExperimentalTestApi https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#continuationInterceptor()` A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` that will defer continuation resumptions requested within `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` calls to until after the frame callbacks have finished running. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#frameDelayNanos()` The number of nanoseconds to `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html` between executing frames. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#hasAwaiters()` Returns whether there are any awaiters on this clock. | android |

| ### Extension properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/ui/test/ExperimentalTestApi https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock.https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#(androidx.compose.ui.test.TestMonotonicFrameClock).frameDelayMillis()` The frame delay time for the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock` in milliseconds. | android |

### Inherited functions

From [kotlin.coroutines.CoroutineContext](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html) `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/plus.html(context: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html)` android From [kotlin.coroutines.CoroutineContext.Element](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/index.html) `open R` `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/fold.html(initial: R, operation: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/index.html, R) -> R)` android `open operator E?` `<E : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/index.html> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/get.html(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-key/index.html<E>)` android `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-element/minus-key.html(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-key/index.html<*>)` android

### Inherited properties

From [androidx.compose.runtime.MonotonicFrameClock](https://developer.android.com/reference/kotlin/androidx/compose/runtime/MonotonicFrameClock) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/-key/index.html<*>` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MonotonicFrameClock#key()` android

## Public constructors

### TestMonotonicFrameClock

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
TestMonotonicFrameClock(
    coroutineScope: CoroutineScope,
    frameDelayNanos: Long = DefaultFrameDelay,
    onPerformTraversals: (Long) -> Unit = {}
)
```

| Parameters |
|---|---|
| `coroutineScope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` used to simulate the main thread and schedule frames on. It must contain a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` and a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation-interceptor/index.html`. |
| `frameDelayNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html = DefaultFrameDelay` | The number of nanoseconds to `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html` between executing frames. |
| `onPerformTraversals: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html = {}` | Called with the frame time of the frame that was just executed, after running all `withFrameNanos` callbacks, but before resuming their callers' continuations. Any continuations resumed while running frame callbacks or `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#onPerformTraversals()` will not be dispatched until after `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#onPerformTraversals()` finishes. If `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#onPerformTraversals()` throws, all `withFrameNanos` callers will be cancelled. |

## Public functions

### withFrameNanos

android

```
open suspend fun <R : Any?> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R
```

Schedules `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` to be ran on the next "fake" frame, and schedules the task to actually perform that frame if it hasn't already been scheduled.

Instead of waiting for a vsync message to perform the next frame, it simply calls the coroutine `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html` function for the test frame time `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/package-summary#(androidx.compose.ui.test.TestMonotonicFrameClock).frameDelayMillis()` (which the underlying test coroutine scheduler will actually complete immediately without waiting), and then run all scheduled tasks.

## Public properties

### continuationInterceptor

android Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
@ExperimentalTestApi
val continuationInterceptor: ContinuationInterceptor
```

A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` that will defer continuation resumptions requested within `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` calls to until after the frame callbacks have finished running. Resumptions will then be dispatched before resuming the continuations from the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock#withFrameNanos(kotlin.Function1)` calls themselves.

### frameDelayNanos

android Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val frameDelayNanos: Long
```

The number of nanoseconds to `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/index.html` between executing frames.

### hasAwaiters

android Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val hasAwaiters: Boolean
```

Returns whether there are any awaiters on this clock.

## Extension properties

### TestMonotonicFrameClock.frameDelayMillis

android Artifact: [androidx.compose.ui:ui-test](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/TestMonotonicFrameClock.jvmAndAndroid.kt+symbol:frameDelayMillis) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
@ExperimentalTestApi
val TestMonotonicFrameClock.frameDelayMillis: Long
```

The frame delay time for the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/TestMonotonicFrameClock` in milliseconds.