Skip to content

nsgotest: no step runs a display frame stamped with a time other than the clock's now (RunFrameAt) #2256

Done

Description

@houko

Context

Since #1048, Scope.RequestAnimationFrame hands its callback the display's frame time, which "may lie a frame ahead of Scope.Now where it is when the frame will show" (core/frame.go:92). Hosts deliver it through Scope.RunFrameAt / Tree.RunFrameAt (core/frame.go:126-150), and docs/architecture/animation.md:21-31 lists which targets stamp a frame with when it will show (macOS and iOS targetTimestamp, HarmonyOS) and which with when it began.

A headless nsgotest.App only ever runs frames stamped with the clock's own now: RequestFrame just sets frameWanted (nsgotest/nsgotest.go:761-766), and runFrame calls a.root.RunFrame() (nsgotest/nsgotest.go:1122-1131), which Advance, FastForward and the steps' takeFrames all go through (nsgotest/nsgotest.go:1023-1033, 1077-1117). docs/architecture/animation.md:33 and docs/public/en/testing.md:143 describe this, and #1048 deliberately kept Advance's ats deterministic, which is right as a default. But there is no step that delivers a frame whose time differs from app.Clock.Now(), so the case the frame contract singles out cannot be tested through App.

Why it matters

Code that combines frame times with Scope.Now has to handle a stamp a frame ahead of now, a stamp earlier than a time it took from Scope.Now (an Android vsync or a web frame that began before a resume), and display times that advance by the display's frame length while the graph clock does not. A fixed-timestep loop in a game layer built on NSGo (nsgo-dev/game) is one example: it seeds its accumulator from Scope.Now when it starts or resumes and then adds the difference between frame times, so its first frame on macOS or iOS behaves differently from every headless frame.

Today the only way to test this is to capture the root view's *core.Scope from the view function and post cx.RunFrameAt(at) from the test:

app.Post(func() { cx.RunFrameAt(at) })
app.Settle()

That works headless, but it relies on the test smuggling a scope out of the view, it goes around App's own frame bookkeeping (frameWanted, which runFrame clears), it is not a documented pattern, and it does nothing meaningful under -target.

Proposal

Add func (a *App) FrameAt(at time.Time): run the display frame the app waits for, stamped at (on the graph clock's base), without moving a.Clock, then settle, as runFrame does for Advance but calling RunFrameAt(at) and clearing frameWanted. A zero at behaves as today's frame at the clock's now. Under -target it skips the test, as other headless-only steps do.

Optionally, an Option that makes every frame Advance runs carry a fixed lead (for example FrameLead(d), stamping each frame Clock.Now().Add(d)) would let a whole test run as a display that stamps frames when they show, but FrameAt alone covers the targeted cases.

Acceptance

  • A headless test can run a waiting frame stamped a frame ahead of, or before, app.Clock.Now() through App, without capturing a scope, and the clock does not move.
  • FrameAt with no frame waiting runs nothing, as runFrame does.
  • docs/public/en/testing.md (and its translations) and the headless note in docs/architecture/animation.md mention it.

Activity

  1. self-assigned this
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions