{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

TestMonotonicFrameClock

@ExperimentalCoroutinesApi
@ExperimentalTestApi
public final class TestMonotonicFrameClock implements MonotonicFrameClock


A MonotonicFrameClock with a time source controlled by a kotlinx-coroutines-test TestCoroutineScheduler. This frame clock may be used to consistently drive time under controlled tests.

Calls to withFrameNanos will schedule an upcoming frame frameDelayNanos nanoseconds in the future by launching into coroutineScope if such a frame has not yet been scheduled. The current frame time for withFrameNanos is provided by delayController. It is strongly suggested that coroutineScope contain the test dispatcher controlled by delayController.

Summary

Public constructors

TestMonotonicFrameClock(
    @NonNull CoroutineScope coroutineScope,
    long frameDelayNanos,
    @NonNull Function1<@NonNull LongUnit> onPerformTraversals
)

Public methods

final @NonNull ContinuationInterceptor

A CoroutineDispatcher that will defer continuation resumptions requested within withFrameNanos calls to until after the frame callbacks have finished running.

final long

The number of nanoseconds to delay between executing frames.

final boolean

Returns whether there are any awaiters on this clock.

@NonNull R
<R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

Schedules onFrame to be ran on the next "fake" frame, and schedules the task to actually perform that frame if it hasn't already been scheduled.

Extension functions

final long

The frame delay time for the TestMonotonicFrameClock in milliseconds.

Inherited methods

From kotlin.coroutines.CoroutineContext
From kotlin.coroutines.CoroutineContext.Element
@NonNull R
<R extends Object> fold(
    @NonNull R initial,
    @NonNull Function2<@NonNull R, @NonNull CoroutineContext.Element, @NonNull R> operation
)
E
@NonNull CoroutineContext
From androidx.compose.runtime.MonotonicFrameClock

Public constructors

TestMonotonicFrameClock

public TestMonotonicFrameClock(
    @NonNull CoroutineScope coroutineScope,
    long frameDelayNanos,
    @NonNull Function1<@NonNull LongUnit> onPerformTraversals
)
Parameters
@NonNull CoroutineScope coroutineScope

The CoroutineScope used to simulate the main thread and schedule frames on. It must contain a TestCoroutineScheduler.

long frameDelayNanos

The number of nanoseconds to delay between executing frames.

@NonNull Function1<@NonNull LongUnit> onPerformTraversals

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 onPerformTraversals will not be dispatched until after onPerformTraversals finishes. If onPerformTraversals throws, all withFrameNanos callers will be cancelled.

Public methods

getContinuationInterceptor

@ExperimentalTestApi
public final @NonNull ContinuationInterceptor getContinuationInterceptor()

A CoroutineDispatcher that will defer continuation resumptions requested within withFrameNanos calls to until after the frame callbacks have finished running. Resumptions will then be dispatched before resuming the continuations from the withFrameNanos calls themselves.

getFrameDelayNanos

public final long getFrameDelayNanos()

The number of nanoseconds to delay between executing frames.

getHasAwaiters

public final boolean getHasAwaiters()

Returns whether there are any awaiters on this clock.

withFrameNanos

public @NonNull R <R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

Schedules onFrame 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 delay function for the test frame time frameDelayMillis (which the underlying test coroutine scheduler will actually complete immediately without waiting), and then run all scheduled tasks.

Extension functions

TestMonotonicFrameClockKt.getFrameDelayMillis

@ExperimentalTestApi
public final long TestMonotonicFrameClockKt.getFrameDelayMillis(
    @NonNull TestMonotonicFrameClock receiver
)

The frame delay time for the TestMonotonicFrameClock in milliseconds.