|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file rectMorph.interaction.browser.spec.ts |
| 5 | + * @input The rect morph and the motion source bundled with esbuild onto a |
| 6 | + * blank fixture page; the interaction harness's busy-main-thread, |
| 7 | + * screencast and trace instruments; Chromium and WebKit on the phone |
| 8 | + * profile |
| 9 | + * @output Proof that the engine shows the morph's path: seeked frame by |
| 10 | + * frame, a present and a dismissal put the element's box on screen on the |
| 11 | + * path's rect within 0.5 px (a straight line with a fade, a popover's edge |
| 12 | + * growth); the morph keeps moving through a 1 s main-thread block; no |
| 13 | + * Layout or Paint runs while it animates (Chromium trace) |
| 14 | + * @position Browser proof for rectMorph.ts, run by `pnpm test:interaction`; |
| 15 | + * jsdom runs no animations |
| 16 | + */ |
| 17 | + |
| 18 | +import {fileURLToPath} from 'node:url'; |
| 19 | +import { |
| 20 | + blockMainThread, |
| 21 | + busyWindowProof, |
| 22 | + countBetween, |
| 23 | + markElement, |
| 24 | + startScreencast, |
| 25 | + traceRendering, |
| 26 | + trackMarker, |
| 27 | +} from '@astryxdesign/interaction-harness'; |
| 28 | +import {expect, test, type Page} from '@playwright/test'; |
| 29 | +import {build} from 'esbuild'; |
| 30 | +// The page's `Motion` global (the bundle) is declared beside the animator's |
| 31 | +// proofs, in animator.interaction.browser.spec.ts. |
| 32 | + |
| 33 | +let script = ''; |
| 34 | + |
| 35 | +test.beforeAll(async () => { |
| 36 | + const result = await build({ |
| 37 | + entryPoints: [fileURLToPath(new URL('./motionBundle.ts', import.meta.url))], |
| 38 | + bundle: true, |
| 39 | + write: false, |
| 40 | + format: 'iife', |
| 41 | + globalName: 'Motion', |
| 42 | + target: 'es2022', |
| 43 | + }); |
| 44 | + script = result.outputFiles[0].text; |
| 45 | +}); |
| 46 | + |
| 47 | +/** The surface at rest, and the 22 px square on its control it grows from. */ |
| 48 | +const BOX = {x: 40, y: 204, width: 320, height: 253}; |
| 49 | +const SQUARE = {x: 179, y: 160, width: 22, height: 22}; |
| 50 | +/** UIKit's popover numbers (#8187): 1 % scale, 100 px of length. */ |
| 51 | +const POPOVER = {origin: {x: 160, y: 0}, collapsedScale: 0.01}; |
| 52 | +const SPRING = {response: 0.45, dampingRatio: 0.8}; |
| 53 | +/** A slow spring for the block: well over a px per frame throughout. */ |
| 54 | +const SLOW = {response: 2, dampingRatio: 1}; |
| 55 | +const MARKER_INSET = 8; |
| 56 | + |
| 57 | +// A real navigation (not setContent): the page keeps its own window. |
| 58 | +async function open(page: Page) { |
| 59 | + const html = `<!doctype html><meta name="viewport" content="width=device-width,initial-scale=1"> |
| 60 | +<style> |
| 61 | + html, body { margin: 0; background: #fff; } |
| 62 | + .surface { position: absolute; left: ${BOX.x}px; top: ${BOX.y}px; width: ${BOX.width}px; height: ${BOX.height}px; background: #36c; } |
| 63 | +</style> |
| 64 | +<div id="surface" class="surface"></div> |
| 65 | +<script>${script}</script>`; |
| 66 | + await page.route('http://motion.test/', async route => |
| 67 | + route.fulfill({contentType: 'text/html', body: html}), |
| 68 | + ); |
| 69 | + await page.goto('http://motion.test/'); |
| 70 | +} |
| 71 | + |
| 72 | +type PathKind = 'line' | 'edge'; |
| 73 | + |
| 74 | +/** |
| 75 | + * Plays a present (or, after it, a dismissal on a copy) of `kind`, pauses |
| 76 | + * the engine, seeks it every 1/60 s, and returns the worst gap between the |
| 77 | + * box on screen and the path's rect, px, and between the opacity on screen |
| 78 | + * and the path's. |
| 79 | + */ |
| 80 | +async function seekedGap( |
| 81 | + page: Page, |
| 82 | + kind: PathKind, |
| 83 | + phase: 'present' | 'dismiss', |
| 84 | +) { |
| 85 | + return page.evaluate( |
| 86 | + async ({kind, phase, box, square, popover, spring}) => { |
| 87 | + const surface = document.getElementById('surface') as HTMLElement; |
| 88 | + const morph = Motion.createRectMorph(surface, { |
| 89 | + prefersReducedMotion: () => false, |
| 90 | + }); |
| 91 | + const path = |
| 92 | + kind === 'line' |
| 93 | + ? Motion.lerpPath(square, box, {opacity: [0, 1]}) |
| 94 | + : Motion.edgeGrowPath({ |
| 95 | + box, |
| 96 | + origin: popover.origin, |
| 97 | + axis: 'y', |
| 98 | + collapsedScale: popover.collapsedScale, |
| 99 | + collapsedLength: 100 / box.height, |
| 100 | + }); |
| 101 | + let target = surface; |
| 102 | + let handle = morph.present(path, spring); |
| 103 | + if (phase === 'dismiss') { |
| 104 | + await handle.finished; |
| 105 | + target = surface.cloneNode(true) as HTMLElement; |
| 106 | + target.id = 'copy'; |
| 107 | + surface.after(target); |
| 108 | + handle = morph.dismiss(path, spring, {target}); |
| 109 | + } |
| 110 | + await handle.started; |
| 111 | + const animations = target.getAnimations(); |
| 112 | + const progress = Motion.progressCurve(spring); |
| 113 | + for (const animation of animations) { |
| 114 | + animation.pause(); |
| 115 | + } |
| 116 | + let worstRect = 0; |
| 117 | + let worstOpacity = 0; |
| 118 | + const step = 1000 / 60; |
| 119 | + for (let t = 0; t <= progress.duration * 1000 + step; t += step) { |
| 120 | + for (const animation of animations) { |
| 121 | + animation.currentTime = t; |
| 122 | + } |
| 123 | + const p = progress.at(t / 1000); |
| 124 | + // The dismissal runs the path backward, rest to source. |
| 125 | + const at = phase === 'present' ? p : 1 - p; |
| 126 | + // Past the source on an overshoot the scale crosses 0 (the copy is |
| 127 | + // transparent there): the box on screen is the flipped rect. |
| 128 | + const raw = path.rectAt(at); |
| 129 | + const expected = { |
| 130 | + x: Math.min(raw.x, raw.x + raw.width), |
| 131 | + y: Math.min(raw.y, raw.y + raw.height), |
| 132 | + width: Math.abs(raw.width), |
| 133 | + height: Math.abs(raw.height), |
| 134 | + }; |
| 135 | + const rect = target.getBoundingClientRect(); |
| 136 | + worstRect = Math.max( |
| 137 | + worstRect, |
| 138 | + Math.abs(rect.x - expected.x), |
| 139 | + Math.abs(rect.y - expected.y), |
| 140 | + Math.abs(rect.width - expected.width), |
| 141 | + Math.abs(rect.height - expected.height), |
| 142 | + ); |
| 143 | + const opacity = Number.parseFloat(getComputedStyle(target).opacity); |
| 144 | + const expectedOpacity = Math.min( |
| 145 | + 1, |
| 146 | + Math.max(0, path.opacityAt?.(at) ?? 1), |
| 147 | + ); |
| 148 | + worstOpacity = Math.max( |
| 149 | + worstOpacity, |
| 150 | + Math.abs(opacity - expectedOpacity), |
| 151 | + ); |
| 152 | + } |
| 153 | + return { |
| 154 | + animations: animations.length, |
| 155 | + worstRect, |
| 156 | + worstOpacity, |
| 157 | + origin: getComputedStyle(target).transformOrigin, |
| 158 | + }; |
| 159 | + }, |
| 160 | + {kind, phase, box: BOX, square: SQUARE, popover: POPOVER, spring: SPRING}, |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +for (const kind of ['line', 'edge'] as const) { |
| 165 | + for (const phase of ['present', 'dismiss'] as const) { |
| 166 | + test(`the engine shows the path\u2019s rect at every frame: ${kind === 'line' ? 'a straight line with a fade' : 'a popover\u2019s edge growth'}, ${phase}`, async ({ |
| 167 | + page, |
| 168 | + }) => { |
| 169 | + await open(page); |
| 170 | + const result = await seekedGap(page, kind, phase); |
| 171 | + test.info().annotations.push({ |
| 172 | + type: 'seeked gap', |
| 173 | + description: JSON.stringify(result), |
| 174 | + }); |
| 175 | + // translate, scale and opacity, one animation each. |
| 176 | + expect(result.animations).toBe(3); |
| 177 | + expect(result.origin).toBe('0px 0px'); |
| 178 | + expect(result.worstRect).toBeLessThanOrEqual(0.5); |
| 179 | + expect(result.worstOpacity).toBeLessThanOrEqual(0.005); |
| 180 | + }); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +test('a present keeps growing every frame through a 1 s main-thread block', async ({ |
| 185 | + page, |
| 186 | +}) => { |
| 187 | + await open(page); |
| 188 | + const surface = page.locator('#surface'); |
| 189 | + await markElement(surface, {inset: MARKER_INSET}); |
| 190 | + const cast = await startScreencast(page); |
| 191 | + await page.evaluate( |
| 192 | + async ({box, spring}) => { |
| 193 | + const element = document.getElementById('surface') as HTMLElement; |
| 194 | + const morph = Motion.createRectMorph(element, { |
| 195 | + prefersReducedMotion: () => false, |
| 196 | + }); |
| 197 | + // From 80 % of its size, 240 px lower (on screen the whole way): the |
| 198 | + // marker in the box's corner rises the whole way, over a px a frame. |
| 199 | + const from = { |
| 200 | + x: box.x, |
| 201 | + y: box.y + 240, |
| 202 | + width: box.width * 0.8, |
| 203 | + height: box.height * 0.8, |
| 204 | + }; |
| 205 | + await morph.present(Motion.lerpPath(from, box), spring).started; |
| 206 | + }, |
| 207 | + {box: BOX, spring: SLOW}, |
| 208 | + ); |
| 209 | + const busy = await blockMainThread(page, 1000, { |
| 210 | + during: surface, |
| 211 | + minProgressMs: 40, |
| 212 | + }); |
| 213 | + await page.waitForTimeout(200); |
| 214 | + const tracked = await trackMarker(page, await cast.stop()); |
| 215 | + const proof = busyWindowProof(tracked, busy, {axis: 'y', settledWithin: 2}); |
| 216 | + test |
| 217 | + .info() |
| 218 | + .annotations.push({type: 'busy window', description: proof.summary}); |
| 219 | + expect(busy.to - busy.from).toBeGreaterThanOrEqual(1000); |
| 220 | + expect(proof.verdict.failures, proof.summary).toEqual([]); |
| 221 | + expect(proof.monotonic).toBe(true); |
| 222 | +}); |
| 223 | + |
| 224 | +test('no Layout or Paint runs while it animates', async ({ |
| 225 | + page, |
| 226 | + browserName, |
| 227 | +}) => { |
| 228 | + test.skip(browserName !== 'chromium', 'tracing is a Chromium protocol'); |
| 229 | + await open(page); |
| 230 | + let start = 0; |
| 231 | + const trace = await traceRendering(page, async () => { |
| 232 | + start = await page.evaluate( |
| 233 | + ({box, square, spring}) => { |
| 234 | + const element = document.getElementById('surface') as HTMLElement; |
| 235 | + const morph = Motion.createRectMorph(element, { |
| 236 | + prefersReducedMotion: () => false, |
| 237 | + }); |
| 238 | + morph.present(Motion.lerpPath(square, box, {opacity: [0, 1]}), spring); |
| 239 | + return performance.timeOrigin + performance.now(); |
| 240 | + }, |
| 241 | + {box: BOX, square: SQUARE, spring: SLOW}, |
| 242 | + ); |
| 243 | + await page.waitForTimeout(1900); |
| 244 | + }); |
| 245 | + // After the frame that starts the animation. |
| 246 | + const counts = countBetween(trace, start + 50, trace.window.to); |
| 247 | + test.info().annotations.push({ |
| 248 | + type: 'rendering work while animating', |
| 249 | + description: JSON.stringify(counts), |
| 250 | + }); |
| 251 | + expect(counts.Layout ?? 0).toBe(0); |
| 252 | + expect(counts.Paint ?? 0).toBe(0); |
| 253 | +}); |
0 commit comments