|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + htmlInCanvasForm, |
| 5 | + markCaptureCanvas, |
| 6 | + markCaptured, |
| 7 | + unmarkCaptured, |
| 8 | + CAPTURE_CANVAS_SELECTOR, |
| 9 | +} from "./htmlInCanvas"; |
| 10 | +import { supportsCaptureMotionBlur } from "./pageMotionBlur"; |
| 11 | + |
| 12 | +const canvasProto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>; |
| 13 | +const CANVAS_MEMBERS = ["requestPaint", "layoutSubtree", "updateElementGeometry"]; |
| 14 | + |
| 15 | +function install(canvasMembers: string[], drawElementImage = true): void { |
| 16 | + vi.stubGlobal( |
| 17 | + "CanvasRenderingContext2D", |
| 18 | + drawElementImage |
| 19 | + ? class { |
| 20 | + drawElementImage(): void {} |
| 21 | + } |
| 22 | + : class {}, |
| 23 | + ); |
| 24 | + for (const name of canvasMembers) canvasProto[name] = () => {}; |
| 25 | +} |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + vi.unstubAllGlobals(); |
| 29 | + for (const name of CANVAS_MEMBERS) delete canvasProto[name]; |
| 30 | +}); |
| 31 | + |
| 32 | +describe("htmlInCanvasForm", () => { |
| 33 | + it("is layoutsubtree where the canvas has layoutSubtree, even if it also has updateElementGeometry", () => { |
| 34 | + install(["requestPaint", "layoutSubtree", "updateElementGeometry"]); |
| 35 | + expect(htmlInCanvasForm()).toBe("layoutsubtree"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("is drawable where layoutSubtree is gone and updateElementGeometry is there", () => { |
| 39 | + install(["requestPaint", "updateElementGeometry"]); |
| 40 | + expect(htmlInCanvasForm()).toBe("drawable"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("is null when neither form is there", () => { |
| 44 | + install(["requestPaint"]); |
| 45 | + expect(htmlInCanvasForm()).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("is null without drawElementImage or without requestPaint", () => { |
| 49 | + install(["requestPaint", "layoutSubtree"], false); |
| 50 | + expect(htmlInCanvasForm()).toBeNull(); |
| 51 | + vi.unstubAllGlobals(); |
| 52 | + delete canvasProto.requestPaint; |
| 53 | + install(["layoutSubtree"]); |
| 54 | + expect(htmlInCanvasForm()).toBeNull(); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("supportsCaptureMotionBlur", () => { |
| 59 | + it.each([ |
| 60 | + ["old form", ["requestPaint", "layoutSubtree"], true], |
| 61 | + ["new form", ["requestPaint", "updateElementGeometry"], true], |
| 62 | + ["API present but no known form", ["requestPaint"], false], |
| 63 | + ])("%s", (_name, members, expected) => { |
| 64 | + install(members); |
| 65 | + expect(supportsCaptureMotionBlur()).toBe(expected); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe("marking", () => { |
| 70 | + it("marks the old form with layoutsubtree and leaves children alone", () => { |
| 71 | + const canvas = document.createElement("canvas"); |
| 72 | + const child = document.createElement("div"); |
| 73 | + markCaptureCanvas(canvas, "layoutsubtree"); |
| 74 | + markCaptured(child, "layoutsubtree"); |
| 75 | + expect(canvas.hasAttribute("layoutsubtree")).toBe(true); |
| 76 | + expect(canvas.hasAttribute("content")).toBe(false); |
| 77 | + expect(child.hasAttribute("drawable")).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + it("marks the new form with content=drawable and drawable children, and unmarks them", () => { |
| 81 | + const canvas = document.createElement("canvas"); |
| 82 | + const child = document.createElement("div"); |
| 83 | + markCaptureCanvas(canvas, "drawable"); |
| 84 | + markCaptured(child, "drawable"); |
| 85 | + expect(canvas.getAttribute("content")).toBe("drawable"); |
| 86 | + expect(canvas.hasAttribute("layoutsubtree")).toBe(false); |
| 87 | + expect(child.hasAttribute("drawable")).toBe(true); |
| 88 | + unmarkCaptured(child, "drawable"); |
| 89 | + expect(child.hasAttribute("drawable")).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it("finds a nested capture canvas of either form", () => { |
| 93 | + for (const mark of [ |
| 94 | + (c: HTMLElement) => c.setAttribute("layoutsubtree", ""), |
| 95 | + (c: HTMLElement) => c.setAttribute("content", "drawable"), |
| 96 | + ]) { |
| 97 | + const root = document.createElement("div"); |
| 98 | + const canvas = document.createElement("canvas"); |
| 99 | + mark(canvas); |
| 100 | + root.append(canvas); |
| 101 | + expect(root.querySelector(CAPTURE_CANVAS_SELECTOR)).toBe(canvas); |
| 102 | + } |
| 103 | + const plain = document.createElement("div"); |
| 104 | + plain.append(document.createElement("canvas")); |
| 105 | + expect(plain.querySelector(CAPTURE_CANVAS_SELECTOR)).toBeNull(); |
| 106 | + }); |
| 107 | +}); |
0 commit comments