|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + type CaptureMotionBlurHost, |
| 4 | + initCaptureMotionBlur, |
| 5 | + paintCaptureMotionBlur, |
| 6 | + releaseCaptureMotionBlurFrame, |
| 7 | + resetCaptureMotionBlurForTests, |
| 8 | + trackAcceleratedCanvases, |
| 9 | +} from "."; |
| 10 | + |
| 11 | +type Win = Window & { |
| 12 | + __HF_PAGE_MOTION_BLUR_OFF__?: unknown; |
| 13 | + __hfMotionBlur?: { |
| 14 | + report: () => { |
| 15 | + state: string; |
| 16 | + reason: string | null; |
| 17 | + shutterDeg: number; |
| 18 | + moverNames: string[]; |
| 19 | + }; |
| 20 | + }; |
| 21 | + __hf_page_composite_pending?: boolean; |
| 22 | + __hf_page_composite_resolve?: () => boolean; |
| 23 | +}; |
| 24 | +const win = window as Win; |
| 25 | + |
| 26 | +// Every 2D context (page canvas or offscreen) is this recording fake. |
| 27 | +function fakeContext() { |
| 28 | + return { |
| 29 | + reset: vi.fn(), |
| 30 | + drawElementImage: vi.fn(), |
| 31 | + drawImage: vi.fn(), |
| 32 | + clearRect: vi.fn(), |
| 33 | + setTransform: vi.fn(), |
| 34 | + globalAlpha: 1, |
| 35 | + globalCompositeOperation: "source-over", |
| 36 | + }; |
| 37 | +} |
| 38 | +let contexts: Array<ReturnType<typeof fakeContext>> = []; |
| 39 | + |
| 40 | +function installHtmlInCanvas(): void { |
| 41 | + vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(() => { |
| 42 | + const ctx = fakeContext(); |
| 43 | + contexts.push(ctx); |
| 44 | + return ctx as never; |
| 45 | + }); |
| 46 | + vi.stubGlobal( |
| 47 | + "CanvasRenderingContext2D", |
| 48 | + class { |
| 49 | + drawElementImage(): void {} |
| 50 | + }, |
| 51 | + ); |
| 52 | + (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint = () => {}; |
| 53 | + vi.stubGlobal( |
| 54 | + "OffscreenCanvas", |
| 55 | + class { |
| 56 | + width: number; |
| 57 | + height: number; |
| 58 | + constructor(w: number, h: number) { |
| 59 | + this.width = w; |
| 60 | + this.height = h; |
| 61 | + } |
| 62 | + getContext() { |
| 63 | + return fakeContext(); |
| 64 | + } |
| 65 | + }, |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +function makeRoot(attrs: Record<string, string> = { "data-motion-blur": "on" }): HTMLElement { |
| 70 | + const root = document.createElement("div"); |
| 71 | + root.setAttribute("data-composition-id", "main"); |
| 72 | + root.setAttribute("data-width", "640"); |
| 73 | + root.setAttribute("data-height", "360"); |
| 74 | + for (const [k, v] of Object.entries(attrs)) root.setAttribute(k, v); |
| 75 | + document.body.append(root); |
| 76 | + return root; |
| 77 | +} |
| 78 | + |
| 79 | +function host( |
| 80 | + root: HTMLElement, |
| 81 | + seek = vi.fn(), |
| 82 | +): CaptureMotionBlurHost & { seek: ReturnType<typeof vi.fn> } { |
| 83 | + return { root, fps: 30, durationSeconds: () => 1, timeline: () => "tl", seek }; |
| 84 | +} |
| 85 | + |
| 86 | +const captureCanvas = (root: HTMLElement) => root.parentElement as HTMLCanvasElement; |
| 87 | + |
| 88 | +beforeEach(() => { |
| 89 | + contexts = []; |
| 90 | + vi.spyOn(console, "info").mockImplementation(() => {}); |
| 91 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 92 | +}); |
| 93 | + |
| 94 | +afterEach(() => { |
| 95 | + resetCaptureMotionBlurForTests(); |
| 96 | + vi.restoreAllMocks(); |
| 97 | + vi.unstubAllGlobals(); |
| 98 | + vi.useRealTimers(); |
| 99 | + delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint; |
| 100 | + delete win.__HF_PAGE_MOTION_BLUR_OFF__; |
| 101 | + delete win.__hfMotionBlur; |
| 102 | + delete win.__hf_page_composite_pending; |
| 103 | + delete win.__hf_page_composite_resolve; |
| 104 | + document.body.innerHTML = ""; |
| 105 | +}); |
| 106 | + |
| 107 | +/** A root with one 10×10 element that slides 300 px over the 1 s composition (10 px/frame). */ |
| 108 | +function movingHost(attrs?: Record<string, string>) { |
| 109 | + const root = makeRoot(attrs); |
| 110 | + const el = document.createElement("div"); |
| 111 | + el.id = "slider"; |
| 112 | + root.append(el); |
| 113 | + for (const [k, v] of Object.entries({ |
| 114 | + offsetWidth: 10, |
| 115 | + offsetHeight: 10, |
| 116 | + offsetLeft: 0, |
| 117 | + offsetTop: 0, |
| 118 | + offsetParent: root, |
| 119 | + })) { |
| 120 | + Object.defineProperty(el, k, { value: v, configurable: true }); |
| 121 | + } |
| 122 | + const x = () => Number(/,\s*([-\d.]+),\s*0\)$/.exec(el.style.transform)?.[1] ?? 0); |
| 123 | + el.getBoundingClientRect = () => |
| 124 | + ({ left: x(), top: 0, right: x() + 10, bottom: 10, width: 10, height: 10 }) as DOMRect; |
| 125 | + const seek = vi.fn((t: number) => { |
| 126 | + el.style.transform = `matrix(1, 0, 0, 1, ${300 * t}, 0)`; |
| 127 | + }); |
| 128 | + return { root, el, h: host(root, seek) }; |
| 129 | +} |
| 130 | + |
| 131 | +const PLAY = { playing: true }; |
| 132 | + |
| 133 | +describe("opt-in and off switches", () => { |
| 134 | + it("does nothing for a root that has not opted in", () => { |
| 135 | + installHtmlInCanvas(); |
| 136 | + const root = makeRoot({}); |
| 137 | + initCaptureMotionBlur(host(root)); |
| 138 | + expect(root.parentElement).toBe(document.body); |
| 139 | + expect(paintCaptureMotionBlur(0)).toBeNull(); |
| 140 | + expect(win.__hfMotionBlur).toBeUndefined(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("stays off without html-in-canvas and says why", () => { |
| 144 | + const root = makeRoot(); |
| 145 | + initCaptureMotionBlur(host(root)); |
| 146 | + expect(root.parentElement).toBe(document.body); |
| 147 | + expect(win.__hfMotionBlur?.report()).toMatchObject({ |
| 148 | + state: "off", |
| 149 | + reason: expect.stringContaining("html-in-canvas"), |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it("stays off while the engine's own sub-frame blur is on (never both)", () => { |
| 154 | + installHtmlInCanvas(); |
| 155 | + win.__HF_PAGE_MOTION_BLUR_OFF__ = "engine-motion-blur"; |
| 156 | + const { root, h } = movingHost(); |
| 157 | + initCaptureMotionBlur(h); |
| 158 | + expect(paintCaptureMotionBlur(0.5, PLAY)).toBeNull(); |
| 159 | + expect(root.parentElement).toBe(document.body); |
| 160 | + expect(win.__hfMotionBlur?.report().reason).toContain("engine"); |
| 161 | + }); |
| 162 | + |
| 163 | + it("stays off for a WebGL canvas, which html-in-canvas captures frozen, but not for a 2D one", () => { |
| 164 | + installHtmlInCanvas(); |
| 165 | + trackAcceleratedCanvases(); |
| 166 | + const root = makeRoot(); |
| 167 | + const flat = document.createElement("canvas"); |
| 168 | + root.append(flat); |
| 169 | + flat.getContext("2d"); |
| 170 | + initCaptureMotionBlur(host(root)); |
| 171 | + expect(win.__hfMotionBlur?.report().state).toBe("on"); |
| 172 | + |
| 173 | + resetCaptureMotionBlurForTests(); |
| 174 | + const gl = document.createElement("canvas"); |
| 175 | + root.append(gl); |
| 176 | + gl.getContext("webgl"); |
| 177 | + initCaptureMotionBlur(host(root)); |
| 178 | + expect(win.__hfMotionBlur?.report().reason).toMatch(/WebGL/); |
| 179 | + }); |
| 180 | + |
| 181 | + it("turns off instead of wrapping when a WebGL canvas appears after init", () => { |
| 182 | + installHtmlInCanvas(); |
| 183 | + trackAcceleratedCanvases(); |
| 184 | + const { root, h } = movingHost(); |
| 185 | + initCaptureMotionBlur(h); |
| 186 | + const late = document.createElement("canvas"); |
| 187 | + root.append(late); |
| 188 | + late.getContext("webgl2"); |
| 189 | + void paintCaptureMotionBlur(0.5, { engineMode: true }); |
| 190 | + expect(root.parentElement).toBe(document.body); |
| 191 | + expect(win.__hfMotionBlur?.report().reason).toMatch(/WebGL/); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +describe("wrapping", () => { |
| 196 | + it("wraps the root itself in a capture canvas, with an output canvas after it", () => { |
| 197 | + installHtmlInCanvas(); |
| 198 | + const { root, h } = movingHost(); |
| 199 | + initCaptureMotionBlur(h); |
| 200 | + expect(root.parentElement).toBe(document.body); // not until it plays |
| 201 | + void paintCaptureMotionBlur(0.5, PLAY); |
| 202 | + const src = captureCanvas(root); |
| 203 | + expect(src.tagName).toBe("CANVAS"); |
| 204 | + expect(src.hasAttribute("layoutsubtree")).toBe(true); |
| 205 | + expect(src.hasAttribute("data-hf-ignore")).toBe(true); |
| 206 | + expect([src.width, src.height]).toEqual([640, 360]); |
| 207 | + const out = src.nextElementSibling as HTMLElement; |
| 208 | + expect(out.getAttribute("data-hf-motion-blur")).toBe("output"); |
| 209 | + // The output sits over the composition: clicks must reach what is under it. |
| 210 | + expect(out.style.pointerEvents).toBe("none"); |
| 211 | + }); |
| 212 | + |
| 213 | + it("turns off instead of wrapping when a capture canvas appears after init", () => { |
| 214 | + installHtmlInCanvas(); |
| 215 | + const { root, h } = movingHost(); |
| 216 | + initCaptureMotionBlur(h); |
| 217 | + const late = document.createElement("canvas"); |
| 218 | + late.setAttribute("layoutsubtree", ""); |
| 219 | + root.append(late); |
| 220 | + void paintCaptureMotionBlur(0.5, PLAY); |
| 221 | + expect(root.parentElement).toBe(document.body); |
| 222 | + expect(win.__hfMotionBlur?.report().state).toBe("off"); |
| 223 | + }); |
| 224 | +}); |
| 225 | + |
| 226 | +describe("paint timeouts", () => { |
| 227 | + const HIDE = "data-hf-motion-blur-hide"; |
| 228 | + const info = () => vi.mocked(console.info); |
| 229 | + const engineFrame = (t: number): Promise<void> | null => { |
| 230 | + const done = paintCaptureMotionBlur(t, { engineMode: true }); |
| 231 | + releaseCaptureMotionBlurFrame(); |
| 232 | + return done; |
| 233 | + }; |
| 234 | + const plainDom = (root: HTMLElement): void => { |
| 235 | + expect(root.parentElement).toBe(document.body); |
| 236 | + expect(document.querySelectorAll("canvas")).toHaveLength(0); |
| 237 | + }; |
| 238 | + |
| 239 | + it("renders a released frame whose paint never comes plain, counts it, and leaves nothing for the engine to finish (first frame, output never drawn)", async () => { |
| 240 | + vi.useFakeTimers(); |
| 241 | + installHtmlInCanvas(); |
| 242 | + const { root, el, h } = movingHost(); |
| 243 | + initCaptureMotionBlur(h); |
| 244 | + const done = engineFrame(0.5); |
| 245 | + expect(captureCanvas(root).tagName).toBe("CANVAS"); |
| 246 | + await vi.advanceTimersByTimeAsync(2001); |
| 247 | + await done; |
| 248 | + plainDom(root); |
| 249 | + expect(el.hasAttribute(HIDE)).toBe(false); |
| 250 | + expect(win.__hfMotionBlur?.report()).toMatchObject({ |
| 251 | + state: "on", |
| 252 | + failures: 0, |
| 253 | + failedFrames: 1, |
| 254 | + }); |
| 255 | + expect(info()).toHaveBeenCalledWith( |
| 256 | + expect.stringMatching(/capture failed \(1 so far\).*paint/), |
| 257 | + ); |
| 258 | + win.__hf_page_composite_resolve?.(); // the engine's forced composite arrives late |
| 259 | + plainDom(root); |
| 260 | + expect(contexts[0]?.drawElementImage).not.toHaveBeenCalled(); |
| 261 | + expect(win.__hf_page_composite_pending).toBe(false); |
| 262 | + void paintCaptureMotionBlur(0.6, { engineMode: true }); // the next frame wraps again |
| 263 | + expect(captureCanvas(root).tagName).toBe("CANVAS"); |
| 264 | + }); |
| 265 | + |
| 266 | + it("does the same for a frame after one that painted, whose output already holds an earlier frame", async () => { |
| 267 | + vi.useFakeTimers(); |
| 268 | + installHtmlInCanvas(); |
| 269 | + const { root, el, h } = movingHost(); |
| 270 | + initCaptureMotionBlur(h); |
| 271 | + const first = engineFrame(0.5); |
| 272 | + captureCanvas(root).dispatchEvent(new Event("paint")); |
| 273 | + captureCanvas(root).dispatchEvent(new Event("paint")); |
| 274 | + await first; |
| 275 | + expect(win.__hfMotionBlur?.report()).toMatchObject({ failedFrames: 0, frames: 1 }); |
| 276 | + const captures = contexts[0]?.drawElementImage.mock.calls.length; |
| 277 | + const second = engineFrame(0.6); |
| 278 | + await vi.advanceTimersByTimeAsync(2001); |
| 279 | + await second; |
| 280 | + plainDom(root); |
| 281 | + expect(el.hasAttribute(HIDE)).toBe(false); |
| 282 | + expect(win.__hfMotionBlur?.report()).toMatchObject({ failures: 0, failedFrames: 1 }); |
| 283 | + win.__hf_page_composite_resolve?.(); |
| 284 | + plainDom(root); |
| 285 | + expect(contexts[0]?.drawElementImage.mock.calls.length).toBe(captures); |
| 286 | + }); |
| 287 | + |
| 288 | + it("keeps blurring through preview paint timeouts, each frame plain, and resumes when painting returns", async () => { |
| 289 | + vi.useFakeTimers(); |
| 290 | + installHtmlInCanvas(); |
| 291 | + const { root, h } = movingHost(); |
| 292 | + initCaptureMotionBlur(h); |
| 293 | + for (let i = 0; i < 4; i++) { |
| 294 | + void paintCaptureMotionBlur(i / 30, PLAY); |
| 295 | + expect(captureCanvas(root).tagName).toBe("CANVAS"); |
| 296 | + await vi.advanceTimersByTimeAsync(2000); |
| 297 | + plainDom(root); |
| 298 | + } |
| 299 | + expect(win.__hfMotionBlur?.report()).toMatchObject({ |
| 300 | + state: "on", |
| 301 | + failures: 0, |
| 302 | + failedFrames: 4, |
| 303 | + }); |
| 304 | + expect(info()).toHaveBeenCalledTimes(1); |
| 305 | + const done = paintCaptureMotionBlur(0.5, PLAY); |
| 306 | + captureCanvas(root).dispatchEvent(new Event("paint")); |
| 307 | + captureCanvas(root).dispatchEvent(new Event("paint")); |
| 308 | + await done; |
| 309 | + expect(contexts[0]?.drawElementImage).toHaveBeenCalled(); |
| 310 | + }); |
| 311 | +}); |
0 commit comments