Skip to content

Commit 1527c64

Browse files
committed
test(BottomSheet): judge a soft flick by the release the page read
The two flick proofs that land on the half detent judged the release the page read against the script's, within a share of it. A page whose own listeners take a few ms reads a slower flick, so the play threw TouchPageStallError while the sheet still landed on half. Each now judges the page's release, as Core's velocity tracker reads it from the page's own event stamps, against the speeds at which UIKit's sheet rule lands it on half. The band comes from the UIKit sheet constants and the measured detents, so it follows the rule. A release outside it fails by name, and a unit checks the band for a long and a short travel.
1 parent c99ad4a commit 1527c64

1 file changed

Lines changed: 136 additions & 16 deletions

File tree

‎packages/core/src/BottomSheet/__tests__/BottomSheetDetent.interaction.browser.spec.ts‎

Lines changed: 136 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
markElement,
4949
openStory,
5050
readLongTasks,
51+
readRelease,
5152
releaseAnimations,
5253
releaseVelocity,
5354
sampleCurve,
@@ -66,21 +67,115 @@ import {
6667
waitForAnimation,
6768
waitForRest,
6869
writeEvidence,
70+
type PlayedEvent,
6971
type TouchDriver,
72+
type TouchScript,
7073
} from '@astryxdesign/interaction-harness';
74+
import {sheet as uikitSheet} from '../../motion/uikit/sheet';
7175

7276
const storybook = withStorybook(test);
7377

74-
// How far a flick's release, as WebKit stamps its touches on arrival, may
75-
// stray from the script's before the play fails by name. Each keeps the
76-
// release where the test's landing holds.
77-
// 1,200 px/s from tall: the script reads 1,225. It lands on half from the
78-
// flick speed, 1,008, up to 2,048, where its projection passes half.
79-
const HALF_FLICK_TOLERANCE = 0.17;
80-
// 2,000 px/s from tall, 160 px of travel: the 0.1 s projection passes half
81-
// above 1,409 px/s.
78+
// How far the dismissing flick's release, as WebKit stamps its touches on
79+
// arrival, may stray from the script's before the play fails by name. 2,000
80+
// px/s from tall, 160 px of travel: the 0.1 s projection passes half above
81+
// 1,409 px/s, so the sheet dismisses.
8282
const DISMISS_FLICK_TOLERANCE = 0.29;
8383

84+
/** The release speeds, px/s, that land a downward release on one position. */
85+
interface ReleaseBand {
86+
readonly fromPxPerS: number;
87+
/** A release at exactly `fromPxPerS` lands there too. */
88+
readonly fromIncluded: boolean;
89+
readonly toPxPerS: number;
90+
}
91+
92+
// Core counts a release as a flick only after the drag moved more than this,
93+
// px.
94+
// SYNC: Keep equal to `DRAG_PROMOTION_SLOP` in ../useSheetGestures.ts.
95+
const DRAG_PROMOTION_SLOP_PX = 8;
96+
97+
/**
98+
* The release speeds at which UIKit's sheet rule lands a downward release
99+
* from tall on half. `travel` is how far below tall the finger lets go, and
100+
* `half` how far below tall the half detent is, px.
101+
*
102+
* A release slower than `fastSpeed` settles on the position nearest its
103+
* projection, `travel + speed × projectionSeconds`. Below
104+
* `minProjectionSpeed` nothing is projected. So half takes it once the
105+
* projection passes the midpoint. A release at `fastSpeed` or faster is a
106+
* flick. It goes at least to half, and on past it once its projection passes
107+
* half. The rule here holds while every slower release projects short of
108+
* half.
109+
*/
110+
function halfLandingBand(travel: number, half: number): ReleaseBand {
111+
const {fastSpeed, minProjectionSpeed, projectionSeconds} = uikitSheet.release;
112+
if (travel <= DRAG_PROMOTION_SLOP_PX) {
113+
throw new Error(
114+
`halfLandingBand: a release ${travel} px below tall is no flick for Core, which this rule does not cover`,
115+
);
116+
}
117+
if (travel + fastSpeed * projectionSeconds >= half) {
118+
throw new Error(
119+
`halfLandingBand: a release ${travel} px below tall can project past half (${half} px) below the flick speed, which this rule does not cover`,
120+
);
121+
}
122+
const nearerHalf =
123+
travel > half / 2
124+
? 0
125+
: Math.max(minProjectionSpeed, (half / 2 - travel) / projectionSeconds);
126+
return {
127+
fromPxPerS: Math.min(fastSpeed, nearerHalf),
128+
fromIncluded: nearerHalf >= fastSpeed,
129+
toPxPerS: (half - travel) / projectionSeconds,
130+
};
131+
}
132+
133+
/** How far down the script's finger travels before it lifts, in whole px. */
134+
function travelOf(script: TouchScript): number {
135+
const ys = toTouchEvents(script)
136+
.filter(step => step.type === 'touchStart' || step.type === 'touchMove')
137+
.map(step => Math.round(step.changed[0].y));
138+
return ys[ys.length - 1] - ys[0];
139+
}
140+
141+
/**
142+
* The release the page read, px/s, as Core's velocity tracker reads it. In
143+
* WebKit the touch driver reads it from the page's own event stamps. A page
144+
* whose listeners take a few ms reads a slower flick than the script's. In
145+
* Chromium the page reads the script's stamps, so the release is the
146+
* script's.
147+
*/
148+
function pageRelease(
149+
script: TouchScript,
150+
played: ReadonlyArray<PlayedEvent>,
151+
): number {
152+
const read = played[played.length - 1]?.release;
153+
if (read != null) {
154+
return read.pagePxPerS;
155+
}
156+
const steps = toTouchEvents(script);
157+
const reading = readRelease(
158+
steps,
159+
steps.map(step => ({stamp: step.t, handledAt: Number.NaN})),
160+
);
161+
return reading?.scriptPxPerS ?? Number.NaN;
162+
}
163+
164+
/**
165+
* Fails by name when the release the page read lies outside `band`, where
166+
* UIKit's rule lands it on the position the proof checks.
167+
*/
168+
function expectReleaseIn(release: number, band: ReleaseBand, on: string) {
169+
const aboveFloor = band.fromIncluded
170+
? release >= band.fromPxPerS
171+
: release > band.fromPxPerS;
172+
if (!(aboveFloor && release <= band.toPxPerS)) {
173+
throw new Error(
174+
`the page read a release of ${release.toFixed(0)} px/s, outside ${band.fromPxPerS.toFixed(0)} to ${band.toPxPerS.toFixed(0)} px/s, where UIKit's sheet rule lands it on ${on}`,
175+
);
176+
}
177+
}
178+
84179
// The settle's timing, from the theme the story renders in: `--ease-standard`
85180
// is the default cubic-bezier(0.24, 1, 0.4, 1), and `--duration-medium` is the
86181
// neutral theme's motion `medium` (300 ms; the base theme's is 410 ms).
@@ -207,7 +302,7 @@ test('a downward flick dismisses the sheet', async ({page}) => {
207302
test('a softer downward flick from the tall detent lands on the half detent', async ({
208303
page,
209304
}) => {
210-
const {driver, sheet, handle, halfTop} = await openSheet(page);
305+
const {driver, sheet, handle, tallTop, halfTop} = await openSheet(page);
211306
// 1,200 px/s: a flick, whose projection stops short of the half detent, so
212307
// it goes one stop.
213308
const script = flick(handle, {
@@ -216,10 +311,14 @@ test('a softer downward flick from the tall detent lands on the half detent', as
216311
hz: 120,
217312
});
218313
expect(releaseVelocity(script).y).toBeCloseTo(1200, -1);
219-
await driver.play(script, {
220-
pacing: 'spacing',
221-
releaseTolerance: HALF_FLICK_TOLERANCE,
222-
});
314+
const played = await driver.play(script, {pacing: 'spacing'});
315+
// The release the page read decides the landing. A slower read still lands
316+
// on half once its projection passes the midpoint, so the band is UIKit's.
317+
expectReleaseIn(
318+
pageRelease(script, played),
319+
halfLandingBand(travelOf(script), halfTop - tallTop),
320+
'half',
321+
);
223322
await waitForRest(sheet);
224323
await expect(page.getByRole('dialog', {name: 'Nearby places'})).toBeVisible();
225324
expect(await sheetTop(sheet)).toBeCloseTo(halfTop, 0);
@@ -233,7 +332,7 @@ test('a softer flick with one early move 70 ms late still lands on the half dete
233332
browserName !== 'webkit',
234333
'only WebKit stamps a touch when it arrives; Chromium carries the scripted times',
235334
);
236-
const {driver, sheet, handle, halfTop} = await openSheet(page);
335+
const {driver, sheet, handle, tallTop, halfTop} = await openSheet(page);
237336
const script = flick(handle, {
238337
velocity: {x: 0, y: 1200},
239338
durationMs: 80,
@@ -242,20 +341,41 @@ test('a softer flick with one early move 70 ms late still lands on the half dete
242341
// The move after this step reaches the page 70 ms late, as a stalled
243342
// dispatch delivers it. The last two moves keep their spacing.
244343
const late = toTouchEvents(script).length - 5;
245-
await driver.play(script, {
344+
const played = await driver.play(script, {
246345
pacing: 'spacing',
247-
releaseTolerance: HALF_FLICK_TOLERANCE,
248346
onEvent: async (_, index) => {
249347
if (index === late) {
250348
await new Promise(resolve => setTimeout(resolve, 70));
251349
}
252350
},
253351
});
352+
expectReleaseIn(
353+
pageRelease(script, played),
354+
halfLandingBand(travelOf(script), halfTop - tallTop),
355+
'half',
356+
);
254357
await waitForRest(sheet);
255358
await expect(page.getByRole('dialog', {name: 'Nearby places'})).toBeVisible();
256359
expect(await sheetTop(sheet)).toBeCloseTo(halfTop, 0);
257360
});
258361

362+
test("the half band follows UIKit's rule for a long and a short travel", () => {
363+
// 96 px of travel: below the flick speed the projection decides, and it
364+
// passes the midpoint from 540 px/s.
365+
const long = halfLandingBand(96, 300);
366+
expect(long.fromPxPerS).toBeCloseTo(540, 6);
367+
expect(long.fromIncluded).toBe(false);
368+
expect(long.toPxPerS).toBeCloseTo(2040, 6);
369+
// 32 px: the projection passes the midpoint only from 1,180 px/s. A flick
370+
// from the flick speed on goes to half anyway.
371+
const short = halfLandingBand(32, 300);
372+
expect(short.fromPxPerS).toBe(uikitSheet.release.fastSpeed);
373+
expect(short.fromIncluded).toBe(true);
374+
expect(short.toPxPerS).toBeCloseTo(2680, 6);
375+
// Within Core's drag slop no release is a flick.
376+
expect(() => halfLandingBand(8, 300)).toThrow(/no flick for Core/);
377+
});
378+
259379
test('the settle lands on the detent, its curve recorded frame by frame', async ({
260380
page,
261381
}, testInfo) => {

0 commit comments

Comments
 (0)