Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(interaction-harness): a traced Paint names the node it painted (#10486)

Christopher Chedeau07cc0fd5da2c
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index 5a5bc62d60..046916629c 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -365,7 +365,9 @@ long, and the error says that instead.
**No Layout / Paint.** `traceRendering` records a Chromium trace around the
interaction; `countBetween` counts the steady middle of an animation (its
first and last frames may commit). A transform/opacity animation does no
-Layout or Paint there; a layout-property animation does one per frame. The
+Layout or Paint there; a layout-property animation does one per frame. Each
+counted `Paint` names the node it painted (`node`), and each `UpdateLayoutTree`
+counts the elements it restyled (`elements`). The
harness's own animation recorder stands down while the trace runs, since its
frame loop would make every frame a main-thread one, restyling each running
animation's target and laying out an anchored one: it reads only the element a
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index bebd426d15..ad373e1125 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -2031,6 +2031,55 @@ test.describe('frame evidence', () => {
expect(steady(busy)).toBeGreaterThan(10);
});
+ test('a traced Paint names the node it painted, and a style recalculation counts the elements it restyled', async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'chromium',
+ 'rendering traces are Chromium-only (README: engine limits)',
+ );
+ await page.setContent(
+ `<style>body { margin: 0; }</style><div id="list"><span>a</span><span>b</span><span>c</span><span>d</span><span>e</span></div><div id="patch" style="width: 50px; height: 50px; background: red; will-change: transform"></div>`,
+ );
+ await frames(page, 3);
+ // One frame restyles the five spans; a later one repaints the patch,
+ // which has its own layer.
+ const trace = await traceRendering(page, async () => {
+ await page.evaluate(async () => {
+ const twoFrames = () =>
+ new Promise(resolve =>
+ requestAnimationFrame(() => requestAnimationFrame(resolve)),
+ );
+ for (const span of document.querySelectorAll('#list span')) {
+ (span as HTMLElement).style.color = 'blue';
+ }
+ await twoFrames();
+ (document.getElementById('patch') as HTMLElement).style.background =
+ 'blue';
+ await twoFrames();
+ });
+ });
+ const restyled = trace.events
+ .filter(e => e.name === 'UpdateLayoutTree')
+ .map(e => e.elements);
+ expect(restyled).toContain(5);
+ expect(restyled).toContain(1);
+ const painted = trace.events
+ .filter(e => e.name === 'Paint')
+ .map(e => e.node);
+ expect(painted).toContain("DIV id='patch'");
+ // Only those two events carry the fields.
+ expect(
+ trace.events.filter(
+ e =>
+ e.name !== 'Paint' &&
+ e.name !== 'UpdateLayoutTree' &&
+ (e.node != null || e.elements != null),
+ ),
+ ).toEqual([]);
+ });
+
test("the trace counts no Layout from the harness's own animation recorder, which stands down while it runs", async ({
page,
browserName,
diff --git a/internal/interaction-harness/src/trace.ts b/internal/interaction-harness/src/trace.ts
index d24b95d57b..5ca17350f9 100644
--- a/internal/interaction-harness/src/trace.ts
+++ b/internal/interaction-harness/src/trace.ts
@@ -66,6 +66,16 @@ export interface TracedTask {
readonly threadMs: number;
}
+/** A counted rendering event, epoch ms. */
+export interface RenderingEvent {
+ readonly name: string;
+ readonly t: number;
+ /** A `Paint`'s node, as the trace names it. */
+ readonly node?: string;
+ /** How many elements an `UpdateLayoutTree` restyled. */
+ readonly elements?: number;
+}
+
export interface RenderingTrace {
/** Count per trace event name inside the window, on the page's main thread. */
readonly counts: Readonly<Record<string, number>>;
@@ -90,8 +100,12 @@ export interface RenderingTrace {
* keyframes, or `blockMainThread(page, ms)` without `during`.
*/
readonly devtoolsTasks: readonly TracedTask[];
- /** Every counted rendering event with its epoch-ms time, for sub-window counts. */
- readonly events: readonly {readonly name: string; readonly t: number}[];
+ /**
+ * Every counted rendering event with its epoch-ms time, for sub-window
+ * counts. A `Paint` names the node it painted, and an `UpdateLayoutTree`
+ * counts the elements it restyled.
+ */
+ readonly events: readonly RenderingEvent[];
/** The window, epoch ms on the page's clock. */
readonly window: {readonly from: number; readonly to: number};
}
@@ -217,14 +231,21 @@ export async function traceRendering(
// Trace time (µs, monotonic) -> epoch ms, anchored on the start mark.
const toEpochMs = (ts: number) => fromEpoch + (ts - start.ts) / 1000;
const counts: Record<string, number> = {};
- const counted: {name: string; t: number}[] = [];
+ const counted: RenderingEvent[] = [];
for (const e of events) {
if (!onMain(e) || e.ph === 'E') {
continue;
}
if ((RENDERING_EVENTS as readonly string[]).includes(e.name)) {
counts[e.name] = (counts[e.name] ?? 0) + 1;
- counted.push({name: e.name, t: toEpochMs(e.ts)});
+ const node = (e.args?.data as {nodeName?: unknown} | undefined)?.nodeName;
+ const elements = e.args?.elementCount;
+ counted.push({
+ name: e.name,
+ t: toEpochMs(e.ts),
+ ...(typeof node === 'string' ? {node} : {}),
+ ...(typeof elements === 'number' ? {elements} : {}),
+ });
}
}
const {longTasks, devtoolsTasks} = longTasksOf(