In a Compose UI test, how do you use mainClock to check a confirmation banner midway through its enter animation, and why does advanceTimeBy(32) right after the click show no progress?
answer
- turn off automatic advancing
- frames are 16 ms on Android
- recomposition first, then the start frame
- advanceTimeBy rounds up to whole frames
- draw needs waitForIdle before captureToImage
basics
~20 sSet mainClock.autoAdvance = false, click, then step frames. The first frame recomposes and schedules the animation, the second gives it its start time, so advanceTimeBy(32) leaves it at 0 ms; advance further, then sample the banner.
solid answer
~40 sBy default the rule advances the virtual `mainClock` until Compose is idle, so any assertion sees the finished animation. I set `composeTestRule.mainClock.autoAdvance = false` before the click; then nothing recomposes or animates until I advance the clock. Frames are 16 ms on Android. After the click, the first frame runs recomposition, where the banner's animation is scheduled; only the next frame hands the animation its start time, so `advanceTimeBy(32)` leaves its play time at 0 ms. I advance two frames, then `advanceTimeBy(144)` to sit 144 ms into a 300 ms enter animation, and sample the banner's bounds with `fetchSemanticsNode().boundsInRoot`. `advanceTimeBy` rounds up to whole frames unless `ignoreFrameDuration = true`. Before `captureToImage()` I call `waitForIdle()`, because on Android the clock does not drive the draw pass.
code
kotlin · 18 lines@Test
fun confirmationBannerSlidesIn() {
composeTestRule.setContent { RehearsalScreen(rehearsals = fakeRehearsals) }
composeTestRule.mainClock.autoAdvance = false
composeTestRule.onNodeWithText("Confirm").performClick()
composeTestRule.mainClock.advanceTimeByFrame() // recomposes; the enter animation is created
composeTestRule.mainClock.advanceTimeByFrame() // the animation gets its start frame: t = 0
composeTestRule.mainClock.advanceTimeBy(144) // nine frames: t = 144 ms of a 300 ms tween
val banner = composeTestRule.onNodeWithTag("confirm-banner")
banner.assertIsDisplayed()
val midBounds = banner.fetchSemanticsNode().boundsInRoot
composeTestRule.mainClock.advanceTimeBy(320) // past the end of the animation
val endBounds = banner.fetchSemanticsNode().boundsInRoot
assertNotEquals(endBounds, midBounds)
}go deeper
Recall that Compose tests use a virtual clock and that mainClock.autoAdvance = false stops the test from skipping to the end of animations.
Explain advanceTimeByFrame, advanceTimeBy with frame rounding, and why recomposition and the animation's start frame take two frames after a click.
Write a stable frame-accurate check: control the clock, sample bounds or pixels at a known play time, and add waitForIdle before captures because draw is not clock-driven.
Decide which animations deserve frame-level tests at all; most screens need only start and end states, and frame tests cost upkeep whenever specs change.
## The clock that drives a Compose test Every Compose test has a `MainTestClock`, reachable as `composeTestRule.mainClock`. It drives three things: recompositions, every caller of `withFrameNanos` (which is how Compose animations get frame times), and coroutines launched from `LaunchedEffect`. If this clock does not tick, recomposition does not happen and animations are frozen. With the default `autoAdvance = true`, the rule advances the clock for you whenever it waits for idle, which is before every action and assertion. That is why ordinary tests see animations complete instantly: by the time an assertion runs, the clock has been pushed to the end of every running animation. Good for behaviour tests; useless for checking an intermediate frame. ## Taking control Set `composeTestRule.mainClock.autoAdvance = false`. From then on: - recomposition only happens when the clock produces a frame (the initial composition from `setContent` is the exception); - animations only get frame times when the clock produces a frame; - waiting for idle no longer treats pending recompositions or animations as busy, so finders and assertions do not hang. Then move time yourself: | Call | Effect | |---|---| | `advanceTimeByFrame()` | one frame, 16 ms on Android and Desktop JVMs | | `advanceTimeBy(ms)` | `ms` rounded **up** to a whole number of frames, unless `ignoreFrameDuration = true` | | `advanceTimeUntil(timeoutMillis) { ... }` | frame by frame until the condition holds, timeout in test-clock time | | `scheduler.runCurrent()` | runs due coroutines without moving time | ## Why advanceTimeBy(32) shows no progress The library documents an ordering detail that catches almost everyone. When a frame is produced, the clock **first** sends the new frame time to animations waiting for one, and **then** runs recomposition. So after a click that flips the banner's visibility state: 1. **Frame 1 (16 ms):** recomposition sees the new state; the banner's enter animation is created and requests a frame. It has no start time yet, because animations were served before recomposition. 2. **Frame 2 (32 ms):** the animation receives its first frame time and sets its play time to `t = 0`. 3. **Each later frame:** play time advances by 16 ms. Hence `advanceTimeBy(32)` straight after the click leaves the animation at 0 ms. To land 144 ms into a 300 ms enter animation, advance two frames, then `advanceTimeBy(144)`, which is nine frames. Because animations ask for the next frame inside the current one, `advanceTimeBy(160)` during an animation produces ten 16 ms frames rather than one long frame; measure and layout run after each frame. ## Reading the intermediate state - Use semantics: `onNodeWithTag("confirm-banner").fetchSemanticsNode().boundsInRoot` gives the banner's bounds at that frame; compare it with the bounds after the animation ends. - For pixels, call `composeTestRule.waitForIdle()` before `captureToImage()`. On Android the draw pass is driven by the Choreographer, not by this clock, so after `advanceTimeBy` a draw may still be pending. With `autoAdvance = false`, `waitForIdle()` does not move the clock; it waits for idling resources and pending layout and draw passes. - A value read only during draw, such as one used inside a draw lambda, is likewise not refreshed by `advanceTimeBy` alone; `waitForIdle()` is needed. ## A recipe for a frame-accurate check 1. Call `setContent` and let the first composition settle while `autoAdvance` is still on. 2. Set `mainClock.autoAdvance = false`. 3. Perform the action that starts the animation, such as clicking **Confirm** on the rehearsal screen. 4. Advance two frames: one for recomposition, one for the animation's start frame. 5. Advance by the play time you want to sample, as a multiple of 16 ms to avoid rounding surprises. 6. Read bounds through semantics, or call `waitForIdle()` and then `captureToImage()` for pixels. 7. Advance past the end and assert the final state, so the test proves the animation also finishes. Keep the expected durations in one place shared with the app's animation spec, or the test breaks every time a designer retunes the motion. ## Other clock behaviours worth knowing - Delayed `LaunchedEffect` code resumes at its scheduled time: `repeat(2) { delay(1000) }` completes with one `advanceTimeBy(2000)`. - After changing a state variable, one `advanceTimeByFrame()` commits it and runs exactly one recomposition plus measure and layout if triggered. - `hasPendingWork()` reports, without advancing anything, whether Compose still has work queued, which suits a loop that steps frame by frame until an animation ends. The animation APIs themselves, enter transitions, specs and their durations, belong to the Compose animation material; this question is only about driving them from a test.
- Why does advanceTimeBy(150) end up at 160 ms?`advanceTimeBy` rounds the duration up to the nearest multiple of the frame duration, 16 ms on Android, so every call produces the same number of frames regardless of the clock's current time. Pass `ignoreFrameDuration = true` to stop the rounding, accepting that the last few milliseconds may not be observed, because most work only runs when a frame is produced.
- Your frame-by-frame loop is slow because every node query waits for idle; what can help in 1.12?`runWithoutImplicitWait { }`, added in Compose 1.12, runs read-only queries with the implicit synchronization suppressed. The docs say to call it on the UI thread, for example inside `runOnUiThread`, after a `waitForIdle()` for the current frame, and to keep state changes outside the block.
A film projector you crank by hand: nothing moves until you turn the handle, the first turn only threads the new reel, and the first picture of that reel appears on the second turn. Counting from the click, the banner's frame 0 is the second crank, not the first.
- Two frames after the click, the animation has already run for 32 ms
- With autoAdvance on, an assertion can observe an animation midway
- advanceTimeBy also runs the Android draw pass, so captureToImage can follow at once
- advanceTimeBy(150) advances exactly 150 ms by default
- With autoAdvance off, every finder hangs waiting for the animation
Assumes Compose UI testing 1.12; the clock ordering is long-standing, while hasPendingWork and runWithoutImplicitWait were added in 1.12.