Skip to content

Commit 6fc2bff

Browse files
cixzhangvjeux
andauthored
test(interaction-harness): sampleCurve samples a ::before or ::after (#10052)
* test(interaction-harness): sampleCurve samples a ::before or ::after A component that animates a pseudo-element (animatorFor(element, {pseudoElement: '::before'})) drew its motion where sampleCurve could not read it: it labelled samples by the element's own animations only and read the element's computed style. sampleCurve(locator, times, {pseudoElement}) now reads that pseudo-element's presented values, labelled by its own animations (those whose effect targets the element with that pseudo-element). Without the option nothing changes. waitForRest already waits a pseudo-element's animation out: its read of getAnimations({subtree: true}) includes the element's pseudo-elements in Chromium and WebKit. A self-test now guards that. Red first: on main's curves.ts the new self-test samples the box itself (opacity 1 at t 0, where the veil's is 0) in both engines; with the option it matches the veil's linear fade to 0.005 and its 40 px move to 0.05 px. The README describes the option in the curve-sampling paragraph, outside the module table. * docs(interaction-harness): sampleCurve's pseudoElement note names the call that exists on main astracat found that the note cited `animatorFor(element, {pseudoElement})`, an animator option that arrives with #8242 and isn't on main. It now names `element.animate(keyframes, {pseudoElement})`, the call the self-test uses. Co-authored-by: Christopher Chedeau <vjeuxx@gmail.com>
1 parent 159aceb commit 6fc2bff

3 files changed

Lines changed: 101 additions & 8 deletions

File tree

‎internal/interaction-harness/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ seeking and sampling run a curve forward. The recorder keeps one entry per
129129
`Animation` object, so one reversed or replayed mid-run is misread: a watch
130130
taken before the reversal rejects, and an unwatched wait returns at once with
131131
a curve dated before it. Take a fresh animation for each run a proof reads.
132+
`sampleCurve(locator, times, {pseudoElement: '::before'})` samples the
133+
element's `::before` or `::after` instead: its presented values, labelled by
134+
that pseudo-element's own animations.
132135

133136
**Waiting for an animation.** The page records every animation from the first
134137
call into `curves.ts` on it (and on every later document): a frame loop on the

‎internal/interaction-harness/src/curves.ts‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ export interface CurveSample {
897897
* The time the sampled element presents: `t`, or just inside the end of
898898
* its own animations when the sample was taken at or past it (see
899899
* `seekAnimations`). Read from the element's own animations only (from
900-
* every held one if none targets it).
900+
* every held one if none targets it); when sampling a pseudo-element, from
901+
* that pseudo-element's.
901902
*/
902903
readonly presentedAt: number;
903904
/**
@@ -931,13 +932,19 @@ export interface CurveSample {
931932
* held origin, as for `seekAnimations`). Hold the animations first. An animation of the
932933
* individual `translate`, `rotate` or `scale` properties is included: they
933934
* are composed with `transform` into one matrix.
935+
*
936+
* `pseudoElement` samples the element's `::before` or `::after` instead:
937+
* its presented values, labelled by its own animations (those whose effect
938+
* targets the element with that pseudo-element), as a component that animates
939+
* a pseudo-element (`element.animate(keyframes, {pseudoElement})`) draws it.
934940
*/
935941
export async function sampleCurve(
936942
locator: Locator,
937943
times: readonly number[],
944+
{pseudoElement}: {pseudoElement?: '::before' | '::after'} = {},
938945
): Promise<CurveSample[]> {
939946
const raw = await locator.evaluate(
940-
(element, {ts, inset}) => {
947+
(element, {ts, inset, pseudo}) => {
941948
const w = window as unknown as {
942949
__harnessHeld?: Animation[];
943950
__harnessHeldStarts?: number[];
@@ -960,12 +967,17 @@ export async function sampleCurve(
960967
'sampleCurve: no held animations (call holdAnimations first)',
961968
);
962969
}
963-
// The properties each held animation of this element animates. A
964-
// `::before` or `::after` animation is not the element's own: its
965-
// clock would label the element's samples with another box's time.
970+
// The properties each held animation of the sampled box animates: the
971+
// element's own, or its pseudo-element's when one is sampled. Another
972+
// box's animation (the element's `::before` when the element is
973+
// sampled) is not its own: its clock would label the samples with
974+
// another box's time.
966975
const own = held.map(animation => {
967976
const effect = animation.effect as KeyframeEffect | null;
968-
if (effect?.target !== element || effect.pseudoElement != null) {
977+
if (
978+
effect?.target !== element ||
979+
(effect.pseudoElement ?? null) !== pseudo
980+
) {
969981
return null;
970982
}
971983
const properties = new Set<string>();
@@ -1014,7 +1026,10 @@ export async function sampleCurve(
10141026
const or = (value: number, fallback: number) =>
10151027
Number.isFinite(value) ? value : fallback;
10161028
const presentedAt = or(presentedOwn, or(presented, t));
1017-
const style = getComputedStyle(element);
1029+
const style =
1030+
pseudo == null
1031+
? getComputedStyle(element)
1032+
: getComputedStyle(element, pseudo);
10181033
// translate · rotate · scale · transform, as CSS applies them (all
10191034
// about the same transform origin, which `transform` excludes too).
10201035
// A percentage resolves against the border box, unrounded
@@ -1118,7 +1133,7 @@ export async function sampleCurve(
11181133
};
11191134
});
11201135
},
1121-
{ts: [...times], inset: END_INSET_MS},
1136+
{ts: [...times], inset: END_INSET_MS, pseudo: pseudoElement ?? null},
11221137
);
11231138
return raw.map(r => ({
11241139
t: r.t,

‎internal/interaction-harness/src/harness.interaction.browser.spec.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,81 @@ test.describe('curve sampling', () => {
25962596
});
25972597
});
25982598

2599+
// A full-size veil on the fixture's box, drawn by its ::before.
2600+
const VEIL_STYLE =
2601+
"#box::before { content: ''; position: absolute; inset: 0; background: #000; opacity: 0; }";
2602+
2603+
test.describe('pseudo-element animations', () => {
2604+
test('sampleCurve reads a ::before animation when given its pseudo-element', async ({
2605+
page,
2606+
}) => {
2607+
await fixture(page);
2608+
const box = page.locator('#box');
2609+
await box.evaluate((el, veil) => {
2610+
const style = document.createElement('style');
2611+
style.textContent = veil;
2612+
document.head.append(style);
2613+
el.animate(
2614+
[
2615+
{opacity: 0, transform: 'translateY(0px)'},
2616+
{opacity: 1, transform: 'translateY(40px)'},
2617+
],
2618+
{duration: 300, easing: 'linear', pseudoElement: '::before'},
2619+
);
2620+
}, VEIL_STYLE);
2621+
await holdAnimations(page, box);
2622+
const samples = await sampleCurve(box, [0, 75, 150, 225, 300], {
2623+
pseudoElement: '::before',
2624+
});
2625+
// Labelled by the veil's own clock: the last read just inside its end.
2626+
expect(samples.at(-1)).toMatchObject({t: 300, presentedAt: 299.5});
2627+
const fade = compareCurve(curveOf(samples, 'opacity'), t => t / 300, 0.005);
2628+
expect(fade.maxError, JSON.stringify(fade.worst)).toBeLessThanOrEqual(
2629+
0.005,
2630+
);
2631+
const move = compareCurve(
2632+
curveOf(samples, 'translateY'),
2633+
t => (40 * t) / 300,
2634+
0.05,
2635+
);
2636+
expect(move.maxError, JSON.stringify(move.worst)).toBeLessThanOrEqual(0.05);
2637+
// Without it, the box itself, which does not move.
2638+
const [boxAt150] = await sampleCurve(box, [150]);
2639+
expect(boxAt150.opacity).toBe(1);
2640+
expect(boxAt150.transform.translateY).toBe(0);
2641+
});
2642+
2643+
test('waitForRest waits out a ::before animation', async ({page}) => {
2644+
await fixture(page);
2645+
const box = page.locator('#box');
2646+
await box.evaluate((el, veil) => {
2647+
const style = document.createElement('style');
2648+
style.textContent = veil;
2649+
document.head.append(style);
2650+
(window as unknown as {__veil: Animation}).__veil = el.animate(
2651+
[{opacity: 0}, {opacity: 1}],
2652+
{
2653+
duration: 600,
2654+
easing: 'linear',
2655+
fill: 'forwards',
2656+
pseudoElement: '::before',
2657+
},
2658+
);
2659+
}, VEIL_STYLE);
2660+
// Its subtree read includes the element's pseudo-elements.
2661+
await waitForRest(box);
2662+
const veil = await box.evaluate(element => {
2663+
const a = (window as unknown as {__veil: Animation}).__veil;
2664+
return {
2665+
playState: a.playState,
2666+
currentTime: Number(a.currentTime),
2667+
opacity: getComputedStyle(element, '::before').opacity,
2668+
};
2669+
});
2670+
expect(veil).toMatchObject({playState: 'finished', opacity: '1'});
2671+
});
2672+
});
2673+
25992674
test.describe('holding', () => {
26002675
test('an armed hold takes a short animation on its first frames, from the page itself', async ({
26012676
page,

0 commit comments

Comments
 (0)