Skip to content

Commit 52cd5ef

Browse files
committed
NOT FOR LANDING: the easing proofs on meta#10094's head, no reader change
The FR4, theme-timing, jump-speed and segmented-control cases, without the reader change.
1 parent c36b15d commit 52cd5ef

4 files changed

Lines changed: 195 additions & 35 deletions

File tree

‎packages/core/src/SegmentedControl/__tests__/SegmentedControl.interaction.browser.spec.ts‎

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* the interaction harness
77
* @output Browser proofs for the touch layer: the slide on UIKit's spring,
88
* the hand-back, Reduce Motion, a blocked main thread, the press, the drag,
9-
* the commit at the lift, scrolling, text selection and a mouse
9+
* the commit at the lift, scrolling, text selection and a mouse, whose
10+
* slide plays the theme's easing, a `linear()` one included
1011
* @position Browser layer of useSegmentedSelectionMotion.ts; the unit layers
1112
* are useSegmentedSelectionMotion.test.ts and motion/uikit/segmented.test.ts
1213
*
@@ -1104,6 +1105,34 @@ test('a hand-written calc() duration token times a click’s slide as the segmen
11041105
}
11051106
});
11061107

1108+
test('with a mouse a click’s slide plays a theme’s linear() easing as the browser plays it, instead of landing at once', async ({
1109+
browser,
1110+
}) => {
1111+
const {context, group} = await openWithMouse(browser);
1112+
try {
1113+
// The motion scale's easing option writes such an easing.
1114+
await group.evaluate(element => {
1115+
element.style.setProperty('--ease-standard', 'linear(0, 0.8 50%, 1)');
1116+
});
1117+
const {from, to, durationMs, themeMs, frames} = await clickSlide(
1118+
group,
1119+
'table',
1120+
);
1121+
expect(Math.abs(to - from), JSON.stringify({from, to})).toBeGreaterThan(10);
1122+
expect(Math.abs(durationMs - themeMs), String(durationMs)).toBeLessThan(25);
1123+
// Two straight runs: 80% of the way at half the time, then the rest.
1124+
const linear = (x: number) => (x < 0.5 ? 1.6 * x : 0.8 + 0.4 * (x - 0.5));
1125+
for (const {t, value} of frames.filter(frame => frame.t <= themeMs)) {
1126+
expect(
1127+
Math.abs((value - from) / (to - from) - linear(t / themeMs)),
1128+
`${t} ms`,
1129+
).toBeLessThan(0.02);
1130+
}
1131+
} finally {
1132+
await context.close();
1133+
}
1134+
});
1135+
11071136
test('the slide plays UISegmentedControl’s measured spring, an internal motion role: no theme setting retunes it', async ({
11081137
page,
11091138
}) => {
@@ -2341,22 +2370,28 @@ test('a drag commits through the destination item’s click too: its onClick run
23412370
await expectLensHidden(control);
23422371
});
23432372

2373+
/** The lens's slide as the page reads it: its ends, its length, its frames. */
2374+
interface SlideRead {
2375+
from: number;
2376+
to: number;
2377+
durationMs: number;
2378+
frames: {t: number; value: number}[];
2379+
}
2380+
23442381
/**
23452382
* Clicks the segment `value` and reads the lens's slide: where its first
2346-
* running translate starts and ends, and how long it runs. Also reads the
2347-
* duration that the segments' own transition resolves.
2383+
* running translate starts and ends, how long it runs, and its frames. Also
2384+
* reads the duration that the segments' own transition resolves.
23482385
*/
23492386
async function clickSlide(
23502387
group: Locator,
23512388
value: string,
2352-
): Promise<{from: number; to: number; durationMs: number; themeMs: number}> {
2389+
): Promise<SlideRead & {themeMs: number}> {
23532390
// From the click on, the page reads the lens's first running translate
23542391
// animation on each frame. It notes where the animation starts and ends,
2355-
// and how long it runs.
2392+
// how long it runs, and each keyframe's time and value.
23562393
await group.evaluate(element => {
2357-
const w = window as unknown as {
2358-
__slide: {from: number; to: number; durationMs: number} | null;
2359-
};
2394+
const w = window as unknown as {__slide: SlideRead | null};
23602395
w.__slide = null;
23612396
const read = () => {
23622397
for (const animation of element.getAnimations({subtree: true})) {
@@ -2377,12 +2412,15 @@ async function clickSlide(
23772412
const firstAtEnd = frames.find(
23782413
(frame, i) => Math.abs(values[i] - to) < 0.01,
23792414
);
2415+
const total = Number(effect.getComputedTiming().duration ?? 0);
23802416
w.__slide = {
23812417
from: values[0],
23822418
to,
2383-
durationMs:
2384-
(firstAtEnd?.computedOffset ?? 1) *
2385-
Number(effect.getComputedTiming().duration ?? 0),
2419+
durationMs: (firstAtEnd?.computedOffset ?? 1) * total,
2420+
frames: frames.map((frame, i) => ({
2421+
t: (frame.computedOffset ?? 0) * total,
2422+
value: values[i],
2423+
})),
23862424
};
23872425
return;
23882426
}
@@ -2404,22 +2442,15 @@ async function clickSlide(
24042442
'aria-checked',
24052443
'true',
24062444
);
2407-
const slide = await group.page().waitForFunction(
2408-
() =>
2409-
(
2410-
window as unknown as {
2411-
__slide: {from: number; to: number; durationMs: number} | null;
2412-
}
2413-
).__slide,
2414-
undefined,
2415-
{timeout: 3000},
2416-
);
2417-
const {from, to, durationMs} = (await slide.jsonValue()) as {
2418-
from: number;
2419-
to: number;
2420-
durationMs: number;
2421-
};
2422-
return {from, to, durationMs, themeMs};
2445+
const slide = await group
2446+
.page()
2447+
.waitForFunction(
2448+
() => (window as unknown as {__slide: SlideRead | null}).__slide,
2449+
undefined,
2450+
{timeout: 3000},
2451+
);
2452+
const {from, to, durationMs, frames} = (await slide.jsonValue()) as SlideRead;
2453+
return {from, to, durationMs, themeMs, frames};
24232454
}
24242455

24252456
/** A desktop context: a mouse, no touch, a fine primary pointer. */

‎packages/core/src/motion/__tests__/animatedLayout.interaction.browser.spec.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* written inline, real Chromium and WebKit (the interaction projects: a
77
* phone with a coarse pointer) plus a fine-pointer describe
88
* @output Proof of which elements glide after a reported size change, by how
9-
* much, and on one shared start and the theme's move curve. It checks that
9+
* much, and on one shared start and the theme's move curve, a `linear()` or
10+
* `steps()` easing included. It checks that
1011
* the glide never passes the area or scrolls it, and that nothing moves
1112
* without a handle. It checks Reduce Motion by pointer, and the edge reveal:
1213
* a block-end and an inline-edge reveal and conceal, and the two-state form.
@@ -299,7 +300,19 @@ function expectOnCurve(
299300
points: readonly [number, number, number, number],
300301
ms: number,
301302
) {
302-
const ease = cubicBezier([...points] as [number, number, number, number]);
303+
expectOnEase(
304+
curve,
305+
cubicBezier([...points] as [number, number, number, number]),
306+
ms,
307+
);
308+
}
309+
310+
/** Every keyframe sits on `ease` over `ms`, from the first one's offset. */
311+
function expectOnEase(
312+
curve: {t: number; y: number}[],
313+
ease: (x: number) => number,
314+
ms: number,
315+
) {
303316
const from = curve[0].y;
304317
// The glide moves the row: a curve read as all zeros proves nothing.
305318
expect(curve.length).toBeGreaterThan(2);
@@ -2374,6 +2387,34 @@ test.describe('spec:AST-105 FR2–FR4', () => {
23742387
});
23752388
}
23762389

2390+
for (const theme of [
2391+
{
2392+
easing: 'linear(0, 0.8 50%, 1)',
2393+
// Two straight runs: to 0.8 at half the time, then on to 1.
2394+
ease: (x: number) => (x < 0.5 ? 1.6 * x : 0.8 + 0.4 * (x - 0.5)),
2395+
},
2396+
{
2397+
easing: 'steps(4, jump-end)',
2398+
ease: (x: number) => Math.floor(4 * x) / 4,
2399+
},
2400+
]) {
2401+
test(`a theme whose --ease-move is ${theme.easing}: the glide plays it as the browser plays it (FR4)`, async ({
2402+
page,
2403+
}) => {
2404+
// The motion scale's easing option writes such an easing.
2405+
await open(page, PAGE, `#area { --ease-move: ${theme.easing}; }`);
2406+
await change(page);
2407+
const motions = await page.evaluate(() => window.motionOf('#a'));
2408+
expect(motions, 'the content after the change glides').toHaveLength(1);
2409+
expect(motions[0].duration).toBeCloseTo(310, 3);
2410+
expectOnEase(
2411+
await page.evaluate(() => window.curveOf('#a')),
2412+
theme.ease,
2413+
310,
2414+
);
2415+
});
2416+
}
2417+
23772418
test('under Reduce Motion with a coarse pointer, both cross-fade with no travel (FR4)', async ({
23782419
page,
23792420
}) => {

‎packages/core/src/motion/__tests__/themeTiming.interaction.browser.spec.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
* patterns. In one, the element's own transition carries the token. In the
1111
* other, the token is read where the element is. That length is the
1212
* element's computed transition. A `calc()` value resolves the same way
13-
* for a caller that needs the values. With no stylesheet,
14-
* `--duration-exit` reads its declared default where the element is
13+
* for a caller that needs the values. An easing that is not a cubic
14+
* Bézier, `linear()` or `steps()`, plays as the browser plays it in both
15+
* patterns. With no stylesheet, `--duration-exit` reads its declared
16+
* default where the element is
1517
* @position Browser proof for themeTiming.ts, run by `pnpm test:interaction`;
1618
* jsdom resolves no `calc()`
1719
*/
@@ -164,6 +166,32 @@ test('both patterns play the same easing, a calc() argument included', async ({
164166
expect(outer.tokensAt).toEqual(outer.ownAt);
165167
});
166168

169+
test('an easing that is not a cubic Bézier plays as the browser plays it, in both patterns', async ({
170+
page,
171+
}) => {
172+
await open(page);
173+
// The eased progress at a quarter and three quarters of the length.
174+
for (const [easing, expected] of [
175+
['linear(0, 0.8 50%, 1)', [0.4, 0.9]],
176+
['steps(3, jump-end)', [0, 2 / 3]],
177+
] as const) {
178+
const {outer, nested} = await readBoth(page, {
179+
theme: {'--ease-standard': easing},
180+
});
181+
for (const at of [
182+
outer.ownAt,
183+
outer.tokensAt,
184+
nested.ownAt,
185+
nested.tokensAt,
186+
]) {
187+
expect(at, easing).toHaveLength(2);
188+
at.forEach((value, i) =>
189+
expect(value, easing).toBeCloseTo(expected[i], 6),
190+
);
191+
}
192+
}
193+
});
194+
167195
test('a duration that is not a positive length gives no curve in either pattern', async ({
168196
page,
169197
}) => {

‎packages/core/src/motion/themeTiming.test.ts‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
* transition, and from tokens set where the element is or at their
99
* defaults. A default that refers to a token reads it there. Also checks
1010
* the values that give no curve, the progress velocity and easedProgress's
11-
* curve. jsdom resolves no `calc()`; the browser proof covers it.
11+
* curve, and that a jump in an easing carries no speed. jsdom resolves no
12+
* `calc()` and plays no `linear()` or `steps()`: a stand-in plays one, and
13+
* the browser proof covers both.
1214
* @position Testing; validates themeTiming.ts
1315
*/
1416

@@ -124,16 +126,61 @@ describe('readThemeTiming', () => {
124126
);
125127
});
126128

129+
it('plays an easing that is not a cubic Bézier as the browser does, and a jump in it hands on no speed', () => {
130+
// jsdom has no Web Animations, so a stand-in plays steps(4, jump-end).
131+
class StandInEffect {
132+
time = 0;
133+
constructor(_target: null, _frames: null, timing: {easing: string}) {
134+
if (timing.easing !== 'steps(4, jump-end)') {
135+
throw new TypeError(timing.easing);
136+
}
137+
}
138+
getComputedTiming() {
139+
return {progress: Math.floor((4 * this.time) / 1000) / 4};
140+
}
141+
}
142+
class StandInAnimation {
143+
constructor(readonly effect: StandInEffect) {}
144+
set currentTime(ms: number) {
145+
this.effect.time = ms;
146+
}
147+
}
148+
const view = window as unknown as Record<string, unknown>;
149+
const before = {
150+
Animation: view.Animation,
151+
KeyframeEffect: view.KeyframeEffect,
152+
};
153+
Object.assign(view, {
154+
Animation: StandInAnimation,
155+
KeyframeEffect: StandInEffect,
156+
});
157+
try {
158+
const curve = readThemeTiming(
159+
element({'--ease-standard': 'steps(4, jump-end)'}),
160+
FAST,
161+
);
162+
// 175 ms at the default, so the jumps fall at 43.75, 87.5 and 131.25 ms.
163+
expect(curve?.duration).toBeCloseTo(0.175, 6);
164+
expect(curve?.at(0.05)).toBe(0.25);
165+
// A retarget at a jump inherits this speed.
166+
for (const t of [0.04375, 0.0875, 0.13125, 0.1749]) {
167+
expect(curve?.velocity(t), `${t} s`).toBe(0);
168+
}
169+
} finally {
170+
Object.assign(view, before);
171+
}
172+
});
173+
127174
it('returns no curve for a duration that is not a positive length, or an easing it does not read', () => {
128175
expect(readThemeTiming(element({'--duration-fast': '0ms'}), FAST)).toBe(
129176
null,
130177
);
131178
expect(readThemeTiming(element({'--duration-fast': 'fast'}), FAST)).toBe(
132179
null,
133180
);
134-
expect(
135-
readThemeTiming(element({'--ease-standard': 'steps(4)'}), FAST),
136-
).toBe(null);
181+
expect(readThemeTiming(element({'--ease-standard': 'bounce'}), FAST)).toBe(
182+
null,
183+
);
137184
expect(readThemeTiming(element({'transition-duration': '0s'}))).toBe(null);
138185
});
139186
});
@@ -170,4 +217,17 @@ describe('easedProgress', () => {
170217
expect(none.at(0)).toBe(1);
171218
expect(none.velocity(0)).toBe(0);
172219
});
220+
221+
it('with jumps, a jump carries no speed and a smooth stretch keeps its slope', () => {
222+
const steps = (x: number) => Math.floor(4 * x) / 4;
223+
// Without the option, the jump at a quarter of 300 ms reads as a spike.
224+
expect(easedProgress(0.3, steps).velocity(0.075)).toBeGreaterThan(100);
225+
const jumping = easedProgress(0.3, steps, {jumps: true});
226+
for (const t of [0, 0.0749, 0.075, 0.0751, 0.15, 0.225, 0.2995]) {
227+
expect(jumping.velocity(t), `${t} s`).toBe(0);
228+
}
229+
expect(
230+
easedProgress(0.2, x => x * x, {jumps: true}).velocity(0.1),
231+
).toBeCloseTo(5, 1);
232+
});
173233
});

0 commit comments

Comments
 (0)