Skip to content

Commit 6728ff1

Browse files
committed
test(DateInput): MonthScroller's proofs read the dip as it is created, and the travel by positions
On a loaded machine the page's frames come too far apart to show a 200 ms dip's darkest frame: in Chromium at four workers on two cores the darkest sampled opacity was 0.54 to 0.89 in 5 of 10 runs. The touch case now reads the dip from its keyframes and timing as it is created, and waits for it to end. The mouse case counted frames of the smooth scroll: in WebKit's CI container each of its frames takes 67 to 83 ms, so its 220 to 258 ms show two or three offsets in between, and one run in ten showed two. It now asserts travel as at least one offset between the two months. Red-first: with every frame costing 110 ms (mouse) and a 180 ms block from the dip's creation (touch), the old proofs fail 5/5 in WebKit's CI lane (the touch case 5/5 in Chromium too); the new ones pass 5/5 in both.
1 parent 19a9cc7 commit 6728ff1

1 file changed

Lines changed: 108 additions & 39 deletions

File tree

‎packages/core/src/DateInput/__tests__/MonthScroller.interaction.browser.spec.ts‎

Lines changed: 108 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* (`*.interaction.browser.spec.ts`, run by `pnpm test:interaction`)
1313
*/
1414

15-
import {expect, test, type Page} from '@playwright/test';
15+
import {expect, test, type Locator, type Page} from '@playwright/test';
1616
import {
1717
openStory,
1818
touchDriver,
@@ -95,9 +95,18 @@ test.describe('with a mouse', () => {
9595
).click();
9696
}),
9797
);
98-
// Today's smooth scroll: the offsets pass through the month in between.
99-
expect(offsets[offsets.length - 1]).not.toBe(before);
100-
expect(new Set(offsets).size).toBeGreaterThan(3);
98+
// Today's smooth scroll travels: at least one frame shows the scroller
99+
// between the two months. This counts positions, not frames. In WebKit's CI
100+
// container each frame of this scroll takes 67 to 83 ms. So its 220 to 258
101+
// ms show two or three frames in between, and a slower frame leaves two.
102+
const end = offsets[offsets.length - 1];
103+
expect(end).not.toBe(before);
104+
expect(
105+
offsets.filter(
106+
left => Math.min(before, end) < left && left < Math.max(before, end),
107+
),
108+
`the offsets each frame showed: ${offsets.join(', ')}`,
109+
).not.toHaveLength(0);
101110
});
102111

103112
test('under Reduced Motion an arrow changes the month at once', async ({
@@ -136,57 +145,117 @@ test.describe('with a mouse', () => {
136145
});
137146
});
138147

148+
/**
149+
* Records, as each is created, the animations made on `target`: what each
150+
* animates, its keyframes' opacity, and its duration in ms. Read at creation, a
151+
* dip is read on a loaded machine too. There, frames come too far apart to show
152+
* a 200 ms dip's darkest frame.
153+
*/
154+
async function recordCreatedRuns(target: Locator): Promise<void> {
155+
await target.evaluate(element => {
156+
const runs: CreatedRun[] = [];
157+
(window as unknown as {createdRuns: CreatedRun[]}).createdRuns = runs;
158+
const original = Element.prototype.animate;
159+
Element.prototype.animate = function (
160+
this: Element,
161+
...args: Parameters<Element['animate']>
162+
) {
163+
const animation = original.apply(this, args);
164+
if (this === element) {
165+
const effect = animation.effect as KeyframeEffect;
166+
const frames = effect.getKeyframes();
167+
runs.push({
168+
properties: [
169+
...new Set(
170+
frames.flatMap(frame =>
171+
Object.keys(frame).filter(
172+
key =>
173+
![
174+
'offset',
175+
'easing',
176+
'composite',
177+
'computedOffset',
178+
].includes(key),
179+
),
180+
),
181+
),
182+
],
183+
opacities: frames.map(frame => Number(frame.opacity)),
184+
duration: Number(effect.getTiming().duration),
185+
});
186+
}
187+
return animation;
188+
};
189+
});
190+
}
191+
192+
type CreatedRun = {properties: string[]; opacities: number[]; duration: number};
193+
139194
test('on a touch screen, under Reduced Motion an arrow dips the calendar and changes the month without travel', async ({
140195
page,
141196
}) => {
142197
const viewport = await openCalendar(page, 'touch');
143198
expect(
144199
await page.evaluate(() => matchMedia('(pointer: coarse)').matches),
145200
).toBe(true);
146-
// Every frame's opacity and scroll offset, with the page's clock, from the
147-
// tap on.
201+
// The dip is read as it is created; the page's frames record where the
202+
// scroller stands, from the tap until the dip is over.
203+
await recordCreatedRuns(viewport);
148204
await viewport.evaluate(element => {
149-
const log: [number, number, number][] = [];
150-
(window as unknown as {dip: typeof log}).dip = log;
151-
const t0 = performance.now();
205+
const w = window as unknown as {offsets: number[]; offsetsDone?: boolean};
206+
w.offsets = [];
152207
const tick = () => {
153-
log.push([
154-
performance.now() - t0,
155-
Number(getComputedStyle(element).opacity),
156-
element.scrollLeft,
157-
]);
158-
if (performance.now() - t0 < 800) {
208+
w.offsets.push(element.scrollLeft);
209+
if (w.offsetsDone !== true) {
159210
requestAnimationFrame(tick);
160211
}
161212
};
162213
requestAnimationFrame(tick);
163214
});
164215
const before = await viewport.evaluate(element => element.scrollLeft);
165216
await page.getByRole('button', {name: 'Next month'}).tap();
166-
await page.waitForFunction(
167-
() => {
168-
const log = (window as unknown as {dip?: unknown[]}).dip;
169-
const last = log?.[log.length - 1] as [number] | undefined;
170-
return last != null && last[0] >= 800;
171-
},
172-
undefined,
173-
{timeout: 10_000},
217+
await expect
218+
.poll(async () =>
219+
page.evaluate(
220+
() =>
221+
(window as unknown as {createdRuns: CreatedRun[]}).createdRuns.length,
222+
),
223+
)
224+
.toBeGreaterThan(0);
225+
// The dip is over, however long a loaded machine takes to end it.
226+
await expect
227+
.poll(async () =>
228+
viewport.evaluate(element => element.getAnimations().length),
229+
)
230+
.toBe(0);
231+
// design:reduced-motion DR2: one opacity dip, dark at its middle and back
232+
// at its end, in at most the cross-fade's 200 ms.
233+
const runs = await page.evaluate(
234+
() => (window as unknown as {createdRuns: CreatedRun[]}).createdRuns,
174235
);
175-
const log = await page.evaluate(
176-
() => (window as unknown as {dip: [number, number, number][]}).dip,
177-
);
178-
const offsets = [...new Set(log.map(([, , left]) => left))];
179-
const after = log[log.length - 1][2];
180-
// design:reduced-motion DR2: the month changes with no travel, one jump
181-
// from the old offset to the new one, at the dip's darkest point, and the
182-
// scroller stays there.
236+
expect(runs).toHaveLength(1);
237+
const [dip] = runs;
238+
expect(dip.properties).toEqual(['opacity']);
239+
expect(dip.opacities[0]).toBe(1);
240+
expect(dip.opacities[dip.opacities.length - 1]).toBe(1);
241+
expect(Math.min(...dip.opacities)).toBeLessThan(0.5);
242+
expect(dip.duration).toBeLessThanOrEqual(200);
243+
// The month changes with no travel: one jump from the old offset to the
244+
// new one, and the scroller stays there.
245+
const after = await viewport.evaluate(element => element.scrollLeft);
183246
expect(after).not.toBe(before);
184-
expect(offsets.every(left => left === before || left === after)).toBe(true);
185-
const dimmed = log.filter(([, opacity]) => opacity < 0.99);
186-
expect(dimmed.length).toBeGreaterThan(0);
187-
expect(Math.min(...log.map(([, opacity]) => opacity))).toBeLessThan(0.5);
188-
// The dip lasts at most the cross-fade's 200 ms, a frame either side.
189-
const span = dimmed[dimmed.length - 1][0] - dimmed[0][0];
190-
expect(span).toBeLessThanOrEqual(200 + 2 * 17);
191-
expect(log[log.length - 1][1]).toBe(1);
247+
const offsets = await page.evaluate(() => {
248+
const w = window as unknown as {offsets: number[]; offsetsDone?: boolean};
249+
w.offsetsDone = true;
250+
return w.offsets;
251+
});
252+
expect(
253+
offsets.every(left => left === before || left === after),
254+
`the offsets each frame showed: ${offsets.join(', ')}`,
255+
).toBe(true);
256+
expect(
257+
await viewport.evaluate(element =>
258+
Number(getComputedStyle(element).opacity),
259+
),
260+
).toBe(1);
192261
});

0 commit comments

Comments
 (0)