|
12 | 12 | * (`*.interaction.browser.spec.ts`, run by `pnpm test:interaction`) |
13 | 13 | */ |
14 | 14 |
|
15 | | -import {expect, test, type Page} from '@playwright/test'; |
| 15 | +import {expect, test, type Locator, type Page} from '@playwright/test'; |
16 | 16 | import { |
17 | 17 | openStory, |
18 | 18 | touchDriver, |
@@ -95,9 +95,18 @@ test.describe('with a mouse', () => { |
95 | 95 | ).click(); |
96 | 96 | }), |
97 | 97 | ); |
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); |
101 | 110 | }); |
102 | 111 |
|
103 | 112 | test('under Reduced Motion an arrow changes the month at once', async ({ |
@@ -136,57 +145,117 @@ test.describe('with a mouse', () => { |
136 | 145 | }); |
137 | 146 | }); |
138 | 147 |
|
| 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 | + |
139 | 194 | test('on a touch screen, under Reduced Motion an arrow dips the calendar and changes the month without travel', async ({ |
140 | 195 | page, |
141 | 196 | }) => { |
142 | 197 | const viewport = await openCalendar(page, 'touch'); |
143 | 198 | expect( |
144 | 199 | await page.evaluate(() => matchMedia('(pointer: coarse)').matches), |
145 | 200 | ).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); |
148 | 204 | 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 = []; |
152 | 207 | 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) { |
159 | 210 | requestAnimationFrame(tick); |
160 | 211 | } |
161 | 212 | }; |
162 | 213 | requestAnimationFrame(tick); |
163 | 214 | }); |
164 | 215 | const before = await viewport.evaluate(element => element.scrollLeft); |
165 | 216 | 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, |
174 | 235 | ); |
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); |
183 | 246 | 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); |
192 | 261 | }); |
0 commit comments