Skip to content

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

TypeScript

test(interaction-harness): every WebKit touch play judges the release its page read (#10383)

Christopher Chedeauca88398ad355
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index ce7876dea0..a14e0e7f89 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -503,7 +503,7 @@ sent with `Input.dispatchTouchEvent`. WebKit has no `isInputPending`.
| | Chromium 149 | WebKit 26.5 (Linux build) |
| ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Trusted touch + pointer events, multi-touch | yes | yes (a `touchstart` lists only the fingers that landed in it; pointer events exact) |
-| Page sees the scripted timestamps | yes, exactly | no: real dispatch time; pacing keeps velocities within ~1 ms on an idle runner; after a stall each step waits its gap, less 2 ms, after WebKit answered the one before (unless a `'schedule'` play has `onEvent`); `pacing: 'spacing'` fails a play whose page read a resting finger where the script moves, a throw where the script rests, or fewer than two moves, or, given `releaseTolerance`, a release straying further from the script's (each read as Core's velocity tracker reads it): `TouchPageStallError` when the page's own touch or pointer listeners held an event back, `TouchPacingError` otherwise; the lift's `release` names what the page read |
+| Page sees the scripted timestamps | yes, exactly | no: real dispatch time; pacing keeps velocities within ~1 ms on an idle runner; after a stall each step waits its gap, less 2 ms, after WebKit answered the one before, even with `onEvent`, so no gap can shrink; a play fails when its page read a resting finger where the script moves, a throw where the script rests, or fewer than two moves, or, given `releaseTolerance`, a release straying further from the script's (each read as Core's tracker reads it): `TouchLiftStallError` when `onEvent` held the lift back, `TouchPageStallError` when the page's listeners held an event, else `TouchPacingError`; the lift's `release` names what the page read |
| Sub-pixel touch positions | yes | no: integer CSS px |
| Native scroll, fling, pinch-zoom from injected touches | yes | no |
| Lifting one finger while others stay down | yes | pointer events right, but that `touchend` lists no remaining `touches`: skip with `PARTIAL_LIFT_SKIP` |
@@ -523,6 +523,19 @@ anything iOS-only — needs Mobile Safari itself, on a device or the iOS
simulator. Skip such a proof on the driver's flag, citing the reason:
`test.skip(!driver.partialLifts, PARTIAL_LIFT_SKIP)`.
+A touch play waits for its `onEvent` before it sends the next event, so page
+work there delays that event by a round trip. WebKit stamps a touch when it
+arrives. So a lift that reaches the page past the 40 ms stillness interval
+after its finger's last move reads as a resting finger. The page releases it
+at 0. Every WebKit play reads its release from the page's own stamps, however
+it is paced, and fails by name when the page's release is not the script's.
+The error is `TouchLiftStallError` when `onEvent` ran for the lift gap's
+excess. Record what the page did in a listener of its own, or let the script
+rest before the lift. A runner too loaded to dispatch in time stretches a
+gesture the same way, and the play fails with `TouchPacingError`. A play whose
+proof reads no release from the lift, such as a pinch's end, passes
+`releaseUnread`.
+
A wheel gesture is a run of steps, and `wheel` waits for each step's round
trip, so on a loaded runner awaited steps land as far apart as a round trip
takes: 49 to 141 ms in WebKit, past the 100 ms a proof may allow between the
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index b7ff32bc56..ebc1de162f 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -111,10 +111,12 @@ import {
thawPage,
thawTimers,
timeline,
+ toTouchEvents,
touchDriver,
trackBrightness,
TouchPageStallError,
touchStoryboard,
+ TouchLiftStallError,
traceRendering,
trackControl,
trackMarker,
@@ -394,6 +396,156 @@ test.describe('touch input', () => {
);
});
+ /** A flick up a page that takes every touch, its lift right after its last move. */
+ async function liftStallPage(page: Page) {
+ await page.route('http://lift-stall.test/', route =>
+ route.fulfill({
+ contentType: 'text/html',
+ body: '<!doctype html><body style="margin:0;height:3000px;touch-action:none"><script>window.touches=[];for(const type of ["touchmove","touchend"])addEventListener(type,e=>touches.push([type,e.timeStamp]),{passive:true})</script>',
+ }),
+ );
+ await page.goto('http://lift-stall.test/');
+ const script = flick(
+ {x: 200, y: 400},
+ {velocity: {x: 0, y: -1500}, durationMs: 80, hz: 120},
+ );
+ const lastMove = toTouchEvents(script).length - 2;
+ // Page work at the last move, as a busy runner's round trip.
+ const slowRead = async (_: unknown, index: number) => {
+ if (index === lastMove) {
+ await page.evaluate(
+ () => new Promise(resolve => setTimeout(resolve, 60)),
+ );
+ }
+ };
+ // The page's own gap from the last move to the lift, ms.
+ const pageLiftGap = () =>
+ page.evaluate(() => {
+ const {touches} = window as unknown as {touches: [string, number][]};
+ const lift = touches.findIndex(([type]) => type === 'touchend');
+ return touches[lift][1] - touches[lift - 1][1];
+ });
+ return {driver: await touchDriver(page), script, slowRead, pageLiftGap};
+ }
+
+ test('a play whose onEvent holds its lift back past the stillness interval fails by name, where the page stamps touches on arrival', async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'webkit',
+ 'only WebKit stamps a touch when it arrives; Chromium carries the scripted times',
+ );
+ const {driver, script, slowRead, pageLiftGap} = await liftStallPage(page);
+ const outcome = await driver.play(script, {onEvent: slowRead}).then(
+ () => 'resolved',
+ (error: unknown) => error,
+ );
+ expect(outcome).toBeInstanceOf(TouchLiftStallError);
+ expect(String(outcome)).toMatch(
+ /the lift reached the page \d+ ms after its finger's last move, where the script spaces \d+ ms: .* onEvent ran \d+ ms in that gap/,
+ );
+ const {numbers} = outcome as TouchLiftStallError;
+ expect(numbers.gapMs).toBeGreaterThan(40);
+ expect(numbers.scriptedGapMs).toBeLessThanOrEqual(40);
+ // The error gives the gap the page itself read.
+ const gap = await pageLiftGap();
+ expect(gap).toBeGreaterThan(40);
+ expect(Math.abs(numbers.gapMs - gap)).toBeLessThanOrEqual(2);
+ // The same flick with a quick onEvent passes.
+ await driver.play(script, {onEvent: () => {}});
+ // A proof that reads no release from the lift opts out.
+ await driver.play(script, {onEvent: slowRead, releaseUnread: true});
+ });
+
+ test('a default-paced play whose page holds its last move fails by name: TouchPageStallError, where the page stamps touches on arrival', async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'webkit',
+ 'only WebKit stamps a touch when it arrives; Chromium carries the scripted times',
+ );
+ const script = flick(
+ {x: 200, y: 400},
+ {velocity: {x: 0, y: -1500}, durationMs: 80, hz: 120},
+ );
+ const moves = toTouchEvents(script).filter(
+ step => step.type === 'touchMove',
+ ).length;
+ // The page's own listener holds its main thread for 80 ms on the last
+ // move. WebKit hands the page the lift only once it is free.
+ await page.route('http://last-move-hold.test/', route =>
+ route.fulfill({
+ contentType: 'text/html',
+ body: `<!doctype html><body style="margin:0;height:3000px;touch-action:none"><script>let n=0;addEventListener("touchmove",()=>{if(++n===${moves}){const end=performance.now()+80;while(performance.now()<end){}}},{passive:true})</script>`,
+ }),
+ );
+ await page.goto('http://last-move-hold.test/');
+ const driver = await touchDriver(page);
+ const outcome = await driver.play(script).then(
+ () => 'resolved',
+ (error: unknown) => error,
+ );
+ expect(outcome).toBeInstanceOf(TouchPageStallError);
+ expect(String(outcome)).toMatch(/the page read a resting finger/);
+ });
+
+ test('a play behind its schedule never shrinks a scripted gap, so page work in onEvent cannot eat the rest before its lift, where the page stamps touches on arrival', async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'webkit',
+ 'only WebKit stamps a touch when it arrives; Chromium carries the scripted times',
+ );
+ const {driver} = await liftStallPage(page);
+ // A slow drag that rests 150 ms before the lift. Page work on every move
+ // puts the play behind its schedule by more than that rest.
+ const script = drag(
+ {x: 200, y: 600},
+ {x: 200, y: 300},
+ {durationMs: 300, restMs: 150},
+ );
+ const steps = toTouchEvents(script);
+ await driver.play(script, {
+ onEvent: async step => {
+ if (step.type === 'touchMove') {
+ await page.evaluate(
+ () => new Promise(resolve => setTimeout(resolve, 30)),
+ );
+ }
+ },
+ });
+ const stamps = await page.evaluate(() =>
+ (window as unknown as {touches: [string, number][]}).touches.map(
+ ([, t]) => t,
+ ),
+ );
+ // The page stamps every move and the lift. No gap is more than the
+ // answer's slack short of the script's, the rest before the lift too.
+ expect(stamps).toHaveLength(steps.length - 1);
+ for (let index = 1; index < stamps.length; index++) {
+ const scripted = steps[index + 1].t - steps[index].t;
+ expect(
+ stamps[index] - stamps[index - 1],
+ `gap ${index} of ${stamps.length - 1}`,
+ ).toBeGreaterThanOrEqual(scripted - ANSWER_SLACK_MS - 1);
+ }
+ });
+
+ test('a Chromium play carries the scripted times, so a slow onEvent before its lift does not fail it', async ({
+ page,
+ browserName,
+ }) => {
+ test.skip(
+ browserName !== 'chromium',
+ 'Chromium carries the scripted times; WebKit stamps a touch when it arrives',
+ );
+ const {driver, script, slowRead} = await liftStallPage(page);
+ await driver.play(script, {onEvent: slowRead});
+ });
+
test("a touch play reaches a page that takes touches late with no gap more than 2 ms short of the script's", async ({
page,
browserName,
diff --git a/internal/interaction-harness/src/touch.ts b/internal/interaction-harness/src/touch.ts
index 292a7c8457..c9febc3381 100644
--- a/internal/interaction-harness/src/touch.ts
+++ b/internal/interaction-harness/src/touch.ts
@@ -62,8 +62,7 @@ export type Engine = 'chromium' | 'webkit';
* `'spacing'` an event also waits for its interval, less ANSWER_SLACK_MS,
* after the engine answered the event before. WebKit stamps a touch no later
* than its answer, so a stall stretches the gesture instead of bunching it.
- * A WebKit `'schedule'` play waits for the answer too, unless it watches each
- * step (`onEvent`).
+ * A WebKit `'schedule'` play waits for the answer too.
*/
export type TouchPacing = 'schedule' | 'spacing';
@@ -113,8 +112,11 @@ export interface PlayOptions {
* page read a resting finger where the script moves, a throw where the
* script rests, or fewer than two moves. Given `releaseTolerance`, it also
* throws when the page's release strays further than that share. The error
- * is `TouchPageStallError` when the page's own touch and pointer listeners
- * held an event back, else `TouchPacingError` (see `touchPacingFault`).
+ * is `TouchLiftStallError` when the play's own `onEvent` held the lift back.
+ * It is `TouchPageStallError` when the page's own touch and pointer
+ * listeners held an event back, else `TouchPacingError` (see
+ * `touchPacingFault`). A `'spacing'` play also throws when the page received
+ * fewer touch events than it sent; any other play is then not judged.
*/
readonly pacing?: TouchPacing;
/**
@@ -125,7 +127,19 @@ export interface PlayOptions {
* needs a throw reads the page's release from the lift.
*/
readonly releaseTolerance?: number;
- /** Called after each event is dispatched, before the next one (storyboards). */
+ /**
+ * The proof reads no release from this play's lift, such as a pinch's end
+ * (default false). Then the play does not judge the release its page read
+ * (see `pacing`).
+ */
+ readonly releaseUnread?: boolean;
+ /**
+ * Called after each event is dispatched, before the next one (storyboards).
+ * The next event waits for it, so page work here delays that event by a
+ * round trip. On a page that stamps touches on arrival, that delay before a
+ * lift can read as a resting finger. Record what the page did in a listener
+ * of its own instead.
+ */
readonly onEvent?: (
step: TouchEventStep,
index: number,
@@ -534,6 +548,72 @@ export class TouchPageStallError extends Error {
}
}
+/**
+ * A play's lift reached the page more than the stillness interval after its
+ * finger's last move, where the script throws. The play's own `onEvent` ran
+ * for that excess: page work there holds the next event back by a round trip.
+ * On a page that stamps touches on arrival (WebKit), the page then reads a
+ * resting finger and releases at 0.
+ */
+export class TouchLiftStallError extends Error {
+ constructor(
+ readonly numbers: {
+ /** The gap the script spaces from the last move to the lift, ms. */
+ readonly scriptedGapMs: number;
+ /** The same gap as the page received it, ms. */
+ readonly gapMs: number;
+ /** How long `onEvent` ran in that gap, ms. */
+ readonly onEventMs: number;
+ },
+ ) {
+ const {scriptedGapMs, gapMs, onEventMs} = numbers;
+ super(
+ `touchDriver: the lift reached the page ${gapMs.toFixed(0)} ms after its finger's last move, where the script spaces ${scriptedGapMs.toFixed(0)} ms: past the ${TOUCH_STILLNESS_MS} ms stillness interval, the page reads a resting finger. onEvent ran ${onEventMs.toFixed(0)} ms in that gap: page work there holds the next event back. Read the page in a listener of its own, or rest the script before the lift`,
+ );
+ this.name = 'TouchLiftStallError';
+ }
+}
+
+/**
+ * The `TouchLiftStallError` for a fault the play's own `onEvent` explains. The
+ * page read a rest, and `onEvent` ran for the lift gap's excess over the
+ * script. Null for any other fault.
+ */
+function liftStallError(
+ steps: readonly TouchEventStep[],
+ arrivals: readonly TouchArrival[],
+ fault: TouchPacingFault,
+ onEventMs: readonly number[],
+): TouchLiftStallError | null {
+ if (fault.kind !== 'rest') {
+ return null;
+ }
+ const lift = steps.length - 1;
+ const id = steps[lift].changed[0]?.id;
+ let lastMoved = 0;
+ let at = '';
+ steps.forEach((step, k) => {
+ const finger =
+ step.type === 'touchStart' || step.type === 'touchMove'
+ ? step.changed.find(f => f.id === id)
+ : undefined;
+ const key =
+ finger == null ? at : `${Math.round(finger.x)},${Math.round(finger.y)}`;
+ if (key !== at) {
+ lastMoved = k;
+ at = key;
+ }
+ });
+ const scriptedGapMs = steps[lift].t - steps[lastMoved].t;
+ const gapMs = arrivals[lift].stamp - arrivals[lastMoved].stamp;
+ const ranMs = onEventMs
+ .slice(lastMoved, lift)
+ .reduce((sum, ms) => sum + ms, 0);
+ return ranMs >= gapMs - scriptedGapMs
+ ? new TouchLiftStallError({scriptedGapMs, gapMs, onEventMs: ranMs})
+ : null;
+}
+
/** Records what the page saw of a paced play's touches (WebKit). */
interface TouchArrivalProbe {
arm(): Promise<void>;
@@ -552,20 +632,24 @@ async function playSteps(
onEvent,
pacing = 'schedule',
releaseTolerance,
+ releaseUnread = false,
} = options;
const steps = toTouchEvents(script);
// Only where the page reads a velocity from arrival times: scripted
- // timestamps carry it.
- const judges = probe != null && realtime && pacing === 'spacing';
- // A driver gives a probe only where the page stamps touches on arrival. A
- // play that watches each step keeps its scripted times.
- const keepsGaps = probe != null && onEvent == null;
+ // timestamps carry it. Every real-time play is judged there, unless its
+ // proof reads no release.
+ const judges = probe != null && realtime && !releaseUnread;
+ // A driver gives a probe only where the page stamps touches on arrival.
+ // There, a play behind its schedule never shrinks a scripted gap, so page
+ // work in `onEvent` cannot eat the rest before a lift.
+ const keepsGaps = probe != null;
if (judges) {
await probe.arm();
}
const played: PlayedEvent[] = [];
const origin = performance.now();
let previous: {sentAt: number; returnedAt: number} | null = null;
+ const onEventMs: number[] = [];
for (const [index, step] of steps.entries()) {
if (realtime) {
await until(
@@ -578,21 +662,29 @@ async function playSteps(
await dispatch(step, timestamp);
previous = {sentAt, returnedAt: performance.now()};
played.push({...step, dispatchedAt, returnedAt: Date.now(), timestamp});
+ const watched = performance.now();
await onEvent?.(step, index);
+ onEventMs.push(performance.now() - watched);
}
// Judged once the lift is out, so no finger is left down.
if (judges) {
const arrivals = await probe.read();
- if (arrivals.length !== steps.length) {
+ // A page that lost events, such as a target removed mid-gesture, gives no
+ // release to read. Only a `'spacing'` play, which asks for the
+ // judgment, fails then.
+ if (arrivals.length !== steps.length && pacing === 'spacing') {
throw new Error(
`touchDriver: the page received ${arrivals.length} of a paced play's ${steps.length} touch events`,
);
}
const fault = touchPacingFault(steps, arrivals, releaseTolerance);
if (fault != null) {
- throw fault.heldByPage == null
- ? new TouchPacingError(fault)
- : new TouchPageStallError({...fault, heldByPage: fault.heldByPage});
+ throw (
+ liftStallError(steps, arrivals, fault, onEventMs) ??
+ (fault.heldByPage == null
+ ? new TouchPacingError(fault)
+ : new TouchPageStallError({...fault, heldByPage: fault.heldByPage}))
+ );
}
const reading = readRelease(steps, arrivals);
if (reading != null) {