|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { downscaleSquareImageToDataUrl } from "./downscaleSquareImage.ts"; |
| 5 | + |
| 6 | +async function withImageEnvironment({ bitmap, context, toDataURL }, run) { |
| 7 | + const bitmapDescriptor = Object.getOwnPropertyDescriptor( |
| 8 | + globalThis, |
| 9 | + "createImageBitmap", |
| 10 | + ); |
| 11 | + const documentDescriptor = Object.getOwnPropertyDescriptor( |
| 12 | + globalThis, |
| 13 | + "document", |
| 14 | + ); |
| 15 | + const canvas = { |
| 16 | + height: 0, |
| 17 | + width: 0, |
| 18 | + getContext: () => context, |
| 19 | + toDataURL, |
| 20 | + }; |
| 21 | + Object.defineProperty(globalThis, "createImageBitmap", { |
| 22 | + configurable: true, |
| 23 | + value: async () => bitmap, |
| 24 | + }); |
| 25 | + Object.defineProperty(globalThis, "document", { |
| 26 | + configurable: true, |
| 27 | + value: { |
| 28 | + createElement: (tag) => { |
| 29 | + assert.equal(tag, "canvas"); |
| 30 | + return canvas; |
| 31 | + }, |
| 32 | + }, |
| 33 | + }); |
| 34 | + try { |
| 35 | + await run(canvas); |
| 36 | + } finally { |
| 37 | + if (bitmapDescriptor) { |
| 38 | + Object.defineProperty(globalThis, "createImageBitmap", bitmapDescriptor); |
| 39 | + } else { |
| 40 | + delete globalThis.createImageBitmap; |
| 41 | + } |
| 42 | + if (documentDescriptor) { |
| 43 | + Object.defineProperty(globalThis, "document", documentDescriptor); |
| 44 | + } else { |
| 45 | + delete globalThis.document; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +test("center-crops and downsizes a landscape image to 128px WebP", async () => { |
| 51 | + const drawCalls = []; |
| 52 | + const encodes = []; |
| 53 | + let closed = false; |
| 54 | + const bitmap = { |
| 55 | + height: 200, |
| 56 | + width: 400, |
| 57 | + close: () => { |
| 58 | + closed = true; |
| 59 | + }, |
| 60 | + }; |
| 61 | + const context = { |
| 62 | + imageSmoothingQuality: "low", |
| 63 | + drawImage: (...args) => drawCalls.push(args), |
| 64 | + }; |
| 65 | + |
| 66 | + await withImageEnvironment( |
| 67 | + { |
| 68 | + bitmap, |
| 69 | + context, |
| 70 | + toDataURL: (type, quality) => { |
| 71 | + encodes.push([type, quality]); |
| 72 | + return "data:image/webp;base64,processed"; |
| 73 | + }, |
| 74 | + }, |
| 75 | + async (canvas) => { |
| 76 | + assert.equal( |
| 77 | + await downscaleSquareImageToDataUrl({ name: "avatar.png" }), |
| 78 | + "data:image/webp;base64,processed", |
| 79 | + ); |
| 80 | + assert.equal(canvas.width, 128); |
| 81 | + assert.equal(canvas.height, 128); |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + assert.equal(context.imageSmoothingQuality, "high"); |
| 86 | + assert.deepEqual(drawCalls, [[bitmap, 100, 0, 200, 200, 0, 0, 128, 128]]); |
| 87 | + assert.deepEqual(encodes, [["image/webp", 0.85]]); |
| 88 | + assert.equal(closed, true); |
| 89 | +}); |
| 90 | + |
| 91 | +test("falls back to PNG when the WebView cannot encode WebP", async () => { |
| 92 | + const formats = []; |
| 93 | + const bitmap = { height: 120, width: 120, close() {} }; |
| 94 | + const context = { drawImage() {}, imageSmoothingQuality: "low" }; |
| 95 | + |
| 96 | + await withImageEnvironment( |
| 97 | + { |
| 98 | + bitmap, |
| 99 | + context, |
| 100 | + toDataURL: (type) => { |
| 101 | + formats.push(type); |
| 102 | + return type === "image/png" |
| 103 | + ? "data:image/png;base64,processed" |
| 104 | + : "data:image/png;base64,webp-unsupported"; |
| 105 | + }, |
| 106 | + }, |
| 107 | + async () => { |
| 108 | + assert.equal( |
| 109 | + await downscaleSquareImageToDataUrl({ name: "avatar.png" }), |
| 110 | + "data:image/png;base64,processed", |
| 111 | + ); |
| 112 | + }, |
| 113 | + ); |
| 114 | + |
| 115 | + assert.deepEqual(formats, ["image/webp", "image/png"]); |
| 116 | +}); |
| 117 | + |
| 118 | +test("closes the decoded bitmap when canvas processing fails", async () => { |
| 119 | + let closed = false; |
| 120 | + const bitmap = { |
| 121 | + height: 120, |
| 122 | + width: 120, |
| 123 | + close: () => { |
| 124 | + closed = true; |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + await withImageEnvironment( |
| 129 | + { |
| 130 | + bitmap, |
| 131 | + context: null, |
| 132 | + toDataURL: () => "", |
| 133 | + }, |
| 134 | + async () => { |
| 135 | + await assert.rejects( |
| 136 | + downscaleSquareImageToDataUrl({ name: "avatar.png" }), |
| 137 | + /Could not process that image/, |
| 138 | + ); |
| 139 | + }, |
| 140 | + ); |
| 141 | + |
| 142 | + assert.equal(closed, true); |
| 143 | +}); |
0 commit comments