@@ -132,6 +132,25 @@ async function checksums(
132132 }
133133}
134134
135+ /**
136+ * The time of one turn, in ms, and the turns in one iteration. One iteration
137+ * holds many turns, so the period is the iteration's duration divided by the
138+ * turns its last keyframe makes.
139+ */
140+ async function turnTiming (
141+ rotor : ReturnType < Page [ 'locator' ] > ,
142+ ) : Promise < { period : number ; turns : number } > {
143+ return rotor . evaluate ( element => {
144+ const [ animation ] = element . getAnimations ( ) ;
145+ const effect = animation . effect as KeyframeEffect ;
146+ const frames = effect . getKeyframes ( ) ;
147+ const end = String ( frames [ frames . length - 1 ] . transform ) ;
148+ const degrees = Number ( / r o t a t e \( ( - ? [ \d . ] + ) d e g \) / . exec ( end ) ?. [ 1 ] ) ;
149+ const turns = degrees / 360 ;
150+ return { period : Number ( effect . getTiming ( ) . duration ) / turns , turns} ;
151+ } ) ;
152+ }
153+
135154/** The rotor's angle in degrees, from 0 up to 360. */
136155function angleOf ( transform : string ) : number {
137156 return ( parseTransform ( transform ) . rotate + 360 ) % 360 ;
@@ -184,48 +203,54 @@ test('the ring keeps turning while a script holds the main thread', async ({
184203test ( 'turning paints nothing on the main thread' , async ( { page} ) => {
185204 await openSizes ( page ) ;
186205 await page . waitForTimeout ( 200 ) ;
187- // Four rings turn for a second. A paint animation repaints every frame; a
188- // transform animation paints none.
206+ // Four rings turn for two seconds. A paint animation repaints every frame;
207+ // a transform animation paints none. React listens for
208+ // `animationiteration`, so Chromium recalculates style a few times at each
209+ // iteration boundary. One turn per iteration gives about 14 recalculations
210+ // here. With many turns in one iteration, no boundary occurs, and the bound
211+ // allows only unrelated single recalculations.
189212 const trace = await traceRendering ( page , async ( ) =>
190- page . waitForTimeout ( 1000 ) ,
213+ page . waitForTimeout ( 2000 ) ,
191214 ) ;
192215 expect ( trace . paint ) . toBe ( 0 ) ;
193216 expect ( trace . layout ) . toBe ( 0 ) ;
217+ expect ( trace . styleRecalc ) . toBeLessThanOrEqual ( 2 ) ;
194218} ) ;
195219
196220test ( 'the arc turns clockwise at a constant rate, once per period' , async ( {
197221 page,
198222} ) => {
199223 const spinners = await openSizes ( page ) ;
200224 const rotor = spinners . first ( ) . locator ( 'span[aria-hidden="true"]' ) ;
201- const samples = await rotor . evaluate ( element => {
225+ const { period, turns} = await turnTiming ( rotor ) ;
226+ const samples = await rotor . evaluate ( ( element , periodMs ) => {
202227 const [ animation ] = element . getAnimations ( ) ;
203228 animation . pause ( ) ;
204- const duration = Number ( animation . effect ?. getTiming ( ) . duration ) ;
205229 const out : { t : number ; transform : string } [ ] = [ ] ;
206230 for ( let i = 0 ; i <= 8 ; i += 1 ) {
207231 // Inside the period at both ends: 0, P/8 … P − 1 ms.
208- const t = i === 8 ? duration - 1 : ( duration * i ) / 8 ;
232+ const t = i === 8 ? periodMs - 1 : ( periodMs * i ) / 8 ;
209233 animation . currentTime = t ;
210234 out . push ( { t, transform : getComputedStyle ( element ) . transform } ) ;
211235 }
212236 return {
213- duration,
237+ duration : Number ( animation . effect ?. getTiming ( ) . duration ) ,
214238 cssDuration : getComputedStyle ( element ) . animationDuration ,
215239 easing : animation . effect ?. getTiming ( ) . easing ,
216240 out,
217241 } ;
218- } ) ;
242+ } , period ) ;
219243 // The period is the `--duration-slow-min` token the theme resolves.
220- expect ( samples . duration ) . toBeGreaterThan ( 0 ) ;
244+ expect ( period ) . toBeGreaterThan ( 0 ) ;
245+ expect ( Number . isInteger ( turns ) && turns > 1 ) . toBe ( true ) ;
221246 expect ( Number . parseFloat ( samples . cssDuration ) * 1000 ) . toBeCloseTo (
222247 samples . duration ,
223248 3 ,
224249 ) ;
225250 expect ( samples . easing ) . toBe ( 'linear' ) ;
226251 // Angle = 360 × t / P, clockwise (positive in CSS), at every sample.
227252 for ( const { t, transform} of samples . out ) {
228- const expected = ( ( 360 * t ) / samples . duration ) % 360 ;
253+ const expected = ( ( 360 * t ) / period ) % 360 ;
229254 const actual = angleOf ( transform ) ;
230255 const error = Math . min (
231256 Math . abs ( actual - expected ) ,
@@ -241,28 +266,27 @@ test('reduced motion slows the ring to 3 s per turn, as before, still clockwise
241266 await page . emulateMedia ( { reducedMotion : 'reduce' } ) ;
242267 const spinners = await openSizes ( page ) ;
243268 const rotor = spinners . first ( ) . locator ( 'span[aria-hidden="true"]' ) ;
244- const samples = await rotor . evaluate ( element => {
269+ const { period} = await turnTiming ( rotor ) ;
270+ const samples = await rotor . evaluate ( ( element , periodMs ) => {
245271 const [ animation ] = element . getAnimations ( ) ;
246272 animation . pause ( ) ;
247- const duration = Number ( animation . effect ?. getTiming ( ) . duration ) ;
248273 const out : { t : number ; transform : string } [ ] = [ ] ;
249274 for ( let i = 0 ; i < 4 ; i += 1 ) {
250- const t = ( duration * i ) / 4 ;
275+ const t = ( periodMs * i ) / 4 ;
251276 animation . currentTime = t ;
252277 out . push ( { t, transform : getComputedStyle ( element ) . transform } ) ;
253278 }
254279 return {
255- duration,
256280 iterations : animation . effect ?. getTiming ( ) . iterations ,
257281 out,
258282 } ;
259- } ) ;
283+ } , period ) ;
260284 // The reduced-motion period this component had before the change: slowed,
261285 // never frozen (`Spinner.tsx`, `animationDuration` under the media query).
262- expect ( samples . duration ) . toBe ( 3000 ) ;
286+ expect ( period ) . toBeCloseTo ( 3000 , 6 ) ;
263287 expect ( samples . iterations ) . toBe ( Infinity ) ;
264288 for ( const { t, transform} of samples . out ) {
265- const expected = ( 360 * t ) / samples . duration ;
289+ const expected = ( 360 * t ) / period ;
266290 const actual = angleOf ( transform ) ;
267291 const error = Math . min (
268292 Math . abs ( actual - expected ) ,
@@ -298,9 +322,7 @@ test('storyboard: one revolution, frame by frame @storyboard', async ({
298322 const spinners = await openSizes ( page ) ;
299323 const xl = spinners . nth ( 3 ) ;
300324 const rotor = xl . locator ( 'span[aria-hidden="true"]' ) ;
301- const duration = await rotor . evaluate ( element =>
302- Number ( element . getAnimations ( ) [ 0 ] . effect ?. getTiming ( ) . duration ) ,
303- ) ;
325+ const { period : duration } = await turnTiming ( rotor ) ;
304326 const { storyboard} = await animationStoryboard ( page , {
305327 name : 'spinner-ring-cycle' ,
306328 clip : xl ,
0 commit comments