diff --git a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts index fba9741b82b24..936e0d95a8cf4 100644 --- a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts +++ b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts @@ -38,7 +38,7 @@ import {expect, test, type Page} from '@playwright/test'; import {build} from 'esbuild'; import type {Animator, MotionHandle} from '../animator'; import type * as MotionBundle from './motionBundle'; -import {controlClock} from './controlClock'; +import {CYCLE, controlClock} from './controlClock'; let script = ''; @@ -133,6 +133,13 @@ async function blockedRun( // Before the animation, so it is on the compositor first; in the top // strip, clear of the box (top 40 px) and its marker. await startBusyControl(page); + // The control's start, epoch ms: its position then gives each frame's + // time without the frame's stamp. + const controlStart = await page.evaluate(() => { + const [animation] = + document.querySelector('[data-harness-control]')?.getAnimations() ?? []; + return performance.timeOrigin + Number(animation?.startTime ?? Number.NaN); + }); const cast = await startScreencast(page); // The curve starts when the browser commits it (handle.started). const start = await page.evaluate( @@ -152,14 +159,17 @@ async function blockedRun( const frames = await cast.stop(); const tracked = await trackMarker(page, frames); const control = await trackMarker(page, frames, {rgb: CONTROL_RGB}); - return {start, busy, tracked, control}; + return {start, busy, tracked, control, controlStart}; } test('a spring keeps advancing every frame through a 1 s main-thread block, on its curve', async ({ page, browserName, }) => { - const {start, busy, tracked, control} = await blockedRun(page, SLOW); + const {start, busy, tracked, control, controlStart} = await blockedRun( + page, + SLOW, + ); // Judged against what the compositor presented in the same window, not a // display-rate bound: a loaded runner's compositor skips frames for every // animation alike, and the harness reads a slow tail inside the block at @@ -186,8 +196,8 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i // screencast's timestamps say (a loaded runner stamps some frames late). // Placing the box's position on the analytic curve (inverting it), the // spring must have advanced the same time over each such run of frames. - // Chromium also stamps frames on the page's clock, so there the median - // offset from the curve is near zero too. + // In Chromium the spring must also sit on its curve from its start + // (`fromStart` below). const inside = tracked.filter( f => f.box != null && f.t > busy.from + 20 && f.t < busy.to - 20, ); @@ -240,6 +250,19 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i const offsets = inside.map((f, i) => f.t - start - curve[i]); const sorted = [...offsets].sort((x, y) => x - y); const median = sorted[Math.floor(sorted.length / 2)]; + // The same offset at each frame's time as the control gives it: its time + // since its start, in the cycle the frame's stamp falls in. A loaded + // runner stamps frames late, which moves the stamped median (recorded) and + // not this one. Only Chromium stamps frames on the page's clock, so only + // there does a stamp tell the cycle. + const fromControl = clock + .map(({i, ms}) => { + const sinceStart = inside[i].t - controlStart; + const controlMs = ms + CYCLE * Math.round((sinceStart - ms) / CYCLE); + return controlStart + controlMs - start - curve[i]; + }) + .sort((x, y) => x - y); + const fromStart = fromControl[Math.floor(fromControl.length / 2)]; // The offsets' spread reads the screencast's timestamps, which a loaded // runner stamps late on some frames: recorded, judged by the drift instead. const spread = @@ -249,6 +272,7 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i frames: clock.length, drift: drift(curve), faster: drift(faster), + fromStart, median, spread, }; @@ -262,7 +286,7 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i expect(read.drift, JSON.stringify(read)).toBeLessThan(12); expect(read.faster, JSON.stringify(read)).toBeGreaterThan(24); if (browserName === 'chromium') { - expect(Math.abs(median)).toBeLessThan(20); + expect(Math.abs(read.fromStart), JSON.stringify(read)).toBeLessThan(20); } }); diff --git a/packages/core/src/motion/__tests__/controlClock.ts b/packages/core/src/motion/__tests__/controlClock.ts index 641dd7e479cb9..d7cc2a69228fd 100644 --- a/packages/core/src/motion/__tests__/controlClock.ts +++ b/packages/core/src/motion/__tests__/controlClock.ts @@ -4,7 +4,8 @@ * @file controlClock.ts * @input Screencast frames: each one's timestamp and the harness's control * box's x on it (240 px out in 400 ms, back in 400 ms, repeating) - * @output Exports controlClock: the control's own time at each usable frame + * @output Exports controlClock: the control's own time at each usable frame; + * and CYCLE, one out-and-back, ms * @position Test support for animator.interaction.browser.spec.ts, whose * busy-window proof reads the spring against the control's clock */ @@ -12,7 +13,7 @@ /** The control's speed, px per ms: 240 px in 400 ms. */ const SPEED = 0.6; /** One out-and-back, ms. */ -const CYCLE = 800; +export const CYCLE = 800; /** * Frames this near a turn are left out: there the two times a position * gives are under 80 ms apart.