Skip to content

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

TypeScript

test(interaction-harness): hold a finger down through a page freeze (#10569)

Christopher Chedeaud61a84acb394
diff --git a/internal/interaction-harness/src/freeze.ts b/internal/interaction-harness/src/freeze.ts
index 7708cdaee4..a94cce5b45 100644
--- a/internal/interaction-harness/src/freeze.ts
+++ b/internal/interaction-harness/src/freeze.ts
@@ -203,8 +203,10 @@ const FREEZE_SCRIPT = `(() => {
holds += 1;
const held = {id: holds, types, afterMs, resumeAt: null, intended: null, arrivedAt: null, releasedAt: null, stalledMs: null, type: null, outcome: null};
inputHold = held;
+ // It marks the first frame drawn unfrozen even after an input came
+ // early, so resumed() still answers and landed() names the input.
const tick = time => {
- if (inputHold !== held || held.outcome != null) return;
+ if (inputHold !== held || held.resumeAt != null) return;
if (state.frozenAt != null) { nativeFrame(tick); return; }
held.resumeAt = now(time);
held.intended = held.resumeAt + afterMs;
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index e872aef832..55235b0e35 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -85,6 +85,7 @@ import {
holdAnimations,
holdInputUntil,
installInputLog,
+ type HeldInput,
type InputRecord,
type PlayOptions,
installLongTaskLog,
@@ -6535,7 +6536,91 @@ test.describe('page freeze', () => {
expect((error as Error).message).toMatch(
/before its first frame after the thaw/,
);
+ // After the thaw, resumed() still reads the first frame drawn unfrozen,
+ // and landed() still names the early input.
await thawPage(page);
+ const resumeMs = await hold.resumed();
+ const again = await hold.landed().catch((caught: unknown) => caught);
+ expect(again).toBeInstanceOf(LiftPlacementError);
+ expect((again as LiftPlacementError).numbers.resumeMs).toBe(resumeMs);
+ });
+
+ test('a finger held down through a freeze lifts alone after the thaw, and its hold lands mid-travel on a stalled runner', async ({
+ page,
+ }) => {
+ await installPageFreeze(page);
+ await freezeFixture(page);
+ await installTimerGate(page);
+ const driver = await touchDriver(page);
+ // The page's own handlers: the touch-down starts a 400 ms travel, and the
+ // lift records how far it had run.
+ await page.evaluate(() => {
+ const box = document.getElementById('box')!;
+ addEventListener(
+ 'pointerdown',
+ () => {
+ box.animate(
+ [{transform: 'translateX(0px)'}, {transform: 'translateX(400px)'}],
+ {duration: 400, easing: 'linear'},
+ );
+ },
+ {passive: true},
+ );
+ addEventListener(
+ 'pointerup',
+ () => {
+ const w = window as unknown as {liftAt?: number | null};
+ w.liftAt ??= Number(box.getAnimations()[0]?.currentTime ?? null);
+ },
+ {passive: true},
+ );
+ });
+ // The finger goes down while the page is frozen, so the travel it starts
+ // is held at its start. Only the lift is left to send.
+ await freezePage(page);
+ const finger = await driver.press({x: 20, y: 60});
+ const hold = await holdInputUntil(page, {afterResumeMs: 150});
+ await thawPage(page);
+ // The runner stalls 40 ms in every 45 from here, as a loaded one does.
+ const stall = setInterval(() => {
+ const end = Date.now() + 40;
+ while (Date.now() < end) {
+ // the runner is busy
+ }
+ }, 45);
+ let held: HeldInput;
+ try {
+ await hold.resumed();
+ await finger.lift();
+ held = await hold.landed();
+ } finally {
+ clearInterval(stall);
+ }
+ expect(held.arrivedMs).toBeLessThan(held.intendedMs);
+ const liftAt = await page.evaluate(
+ () => (window as unknown as {liftAt?: number | null}).liftAt,
+ );
+ // The page handled the lift mid-travel: after the travel started, and
+ // before it ended.
+ expect(liftAt).toBeGreaterThan(0);
+ expect(liftAt).toBeLessThan(400);
+ // A lifted finger fails by name, and so does a second finger 0, a play or
+ // a tap while a finger is held.
+ await expect(finger.lift()).rejects.toThrow(/finger 0 was lifted/);
+ const again = await driver.press({x: 20, y: 60});
+ await expect(driver.press({x: 30, y: 60})).rejects.toThrow(
+ /finger 0 is already down/,
+ );
+ await expect(
+ driver.play([
+ {t: 0, fingers: [{id: 1, x: 30, y: 60}]},
+ {t: 30, fingers: []},
+ ]),
+ ).rejects.toThrow(/lift the 1 finger press holds down before a play/);
+ await expect(driver.tap({x: 30, y: 60})).rejects.toThrow(
+ /lift the 1 finger press holds down before a play or a tap/,
+ );
+ await again.lift();
});
test('a lift that reaches the page while it is frozen again after the thaw fails by name, and the page still answers', async ({
diff --git a/internal/interaction-harness/src/touch.ts b/internal/interaction-harness/src/touch.ts
index 134b34b9ad..580c145fcd 100644
--- a/internal/interaction-harness/src/touch.ts
+++ b/internal/interaction-harness/src/touch.ts
@@ -217,6 +217,82 @@ export interface TouchDriver {
play(script: TouchScript, options?: PlayOptions): Promise<PlayedEvent[]>;
/** A tap through the engine's tap path (WebKit synthesises click only from this). */
tap(at: Point): Promise<void>;
+ /**
+ * Puts a finger down at `at` and keeps it there: only the touch-down is
+ * sent, as a play's first step. The finger moves and lifts through the
+ * handle, each call one event. Its timing is the caller's, so it is never
+ * judged for pacing. A lift that `holdInputUntil` places then races the
+ * hold alone, after the page is thawed.
+ */
+ press(at: Point, options?: {readonly id?: number}): Promise<HeldTouch>;
+}
+
+/** A finger that `TouchDriver.press` holds down. */
+export interface HeldTouch {
+ readonly id: number;
+ /** Moves the finger to `to`; every other finger held stays where it is. */
+ move(to: Point): Promise<void>;
+ /** Lifts the finger: only its touch-up is sent. */
+ lift(): Promise<void>;
+}
+
+/**
+ * The fingers a driver holds down between calls. `dispatch` sends one event
+ * with the finger that changed and every finger down after it.
+ */
+function heldFingers(
+ dispatch: (
+ type: TouchEventStep['type'],
+ changed: Finger,
+ down: readonly Finger[],
+ ) => Promise<void>,
+) {
+ const down = new Map<number, Finger>();
+ return {
+ get size() {
+ return down.size;
+ },
+ async press(at: Point, {id = 0}: {readonly id?: number} = {}) {
+ if (down.has(id)) {
+ throw new Error(`touchDriver: finger ${id} is already down`);
+ }
+ const finger = {id, x: at.x, y: at.y};
+ down.set(id, finger);
+ await dispatch('touchStart', finger, [...down.values()]);
+ let lifted = false;
+ const live = () => {
+ if (lifted) {
+ throw new Error(`touchDriver: finger ${id} was lifted`);
+ }
+ };
+ const held: HeldTouch = {
+ id,
+ async move(to) {
+ live();
+ const moved = {id, x: to.x, y: to.y};
+ down.set(id, moved);
+ await dispatch('touchMove', moved, [...down.values()]);
+ },
+ async lift() {
+ live();
+ lifted = true;
+ const last = down.get(id) ?? finger;
+ down.delete(id);
+ await dispatch('touchEnd', last, [...down.values()]);
+ },
+ };
+ return held;
+ },
+ };
+}
+
+/** A play or a tap cannot share the screen with fingers `press` holds down. */
+function assertNoneHeld(held: {readonly size: number}): void {
+ if (held.size > 0) {
+ throw new Error(
+ `touchDriver: lift the ${held.size} finger${held.size === 1 ? '' : 's'} press holds down before a play or a tap`,
+ );
+ }
}
/**
@@ -937,12 +1013,28 @@ async function chromiumDriver(page: Page): Promise<TouchDriver> {
radiusY: 1,
force: 1,
}));
+ // A start or move lists every finger down. An end lists the finger that
+ // lifts while others stay down, and none for the last lift, as play does.
+ const held = heldFingers(async (type, changed, down) => {
+ await cdp.send('Input.dispatchTouchEvent', {
+ type,
+ touchPoints:
+ type !== 'touchEnd'
+ ? touchPoints(down)
+ : down.length > 0
+ ? touchPoints([changed])
+ : [],
+ timestamp: Date.now() / 1000,
+ });
+ });
return {
engine: 'chromium',
scriptedTimestamps: true,
drivesNativeGestures: true,
partialLifts: true,
+ press: (at, options) => held.press(at, options),
async play(script, options = {}) {
+ assertNoneHeld(held);
// Stamps follow the wall clock the page's own clock is on: with real-time
// pacing an event's stamp and its dispatch agree to ~0.1 ms.
const base = Date.now();
@@ -970,6 +1062,7 @@ async function chromiumDriver(page: Page): Promise<TouchDriver> {
);
},
async tap(at) {
+ assertNoneHeld(held);
await page.touchscreen.tap(at.x, at.y);
},
};
@@ -1135,12 +1228,20 @@ async function webkitDriver(page: Page): Promise<TouchDriver> {
// (an empty list releases nothing), a move every finger down.
const points = (fingers: readonly Finger[]) =>
fingers.map(f => ({x: Math.round(f.x), y: Math.round(f.y), id: f.id}));
+ const held = heldFingers(async (type, changed, down) => {
+ await session.send('Input.dispatchTouchEvent', {
+ type,
+ touchPoints: points(type === 'touchMove' ? down : [changed]),
+ });
+ });
return {
engine: 'webkit',
scriptedTimestamps: false,
drivesNativeGestures: false,
partialLifts: false,
+ press: (at, options) => held.press(at, options),
async play(script, options = {}) {
+ assertNoneHeld(held);
return playSteps(
script,
options,
@@ -1157,6 +1258,7 @@ async function webkitDriver(page: Page): Promise<TouchDriver> {
).then(played => played.map(p => ({...p, timestamp: null})));
},
async tap(at) {
+ assertNoneHeld(held);
await page.touchscreen.tap(Math.round(at.x), Math.round(at.y));
},
};