// Touch input over one DevTools page session, for phone/tablet review: // a touch-screen emulation switch, one-finger swipes and two-finger pinches. // The events are trusted browser input, so they reach whatever the page puts // under the finger (a Flutter canvas or an iframe platform view alike). import { setTimeout as delay } from 'node:timers/promises'; /** Emulates a touch screen (mobile metrics, 5 touch points). */ export async function enableTouch(send, { width, height }) { await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: true }); await send('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 }); } /** One finger down at [at], moved by dx/dy in [steps] moves [gap] ms apart, lifted. */ export async function swipe(send, at, { dx = 0, dy = 0, steps = 12, gap = 16 } = {}) { const touch = (type, x, y) => send('Input.dispatchTouchEvent', { type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }], }); await touch('touchStart', at.x, at.y); for (let i = 1; i <= steps; i++) { await delay(gap); await touch('touchMove', at.x + (dx * i) / steps, at.y + (dy * i) / steps); } await touch('touchEnd'); return { from: at, to: { x: at.x + dx, y: at.y + dy } }; } /** Two fingers spread (scale > 1) or closed (< 1) around [at]. */ export async function pinch(send, at, scale) { await send('Input.synthesizePinchGesture', { x: at.x, y: at.y, scaleFactor: scale, relativeSpeed: 300, gestureSourceType: 'touch', }); return at; }