Skip to content

Commit cca0198

Browse files
committed
try createImageBitmap for integration tests
1 parent c13ca9b commit cca0198

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

test/integration/viewer_spec.mjs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,18 @@ describe("PDF viewer", () => {
482482
};
483483
}
484484

485-
function extractCanvases(pageNumber) {
485+
async function extractCanvases(pageNumber) {
486486
const pageOne = document.querySelector(
487487
`.page[data-page-number='${pageNumber}']`
488488
);
489-
function getContextFromCanvas(canvas) {
489+
async function getContextFromCanvas(canvas) {
490490
try {
491491
return canvas.getContext("2d", { willReadFrequently: true });
492492
} catch {
493493
// Can happen when the canvas has been transferred to OffscreenCanvas.
494494
}
495495

496+
const bitmap = await createImageBitmap(canvas);
496497
let tempCanvas;
497498
if (typeof OffscreenCanvas === "function") {
498499
tempCanvas = new OffscreenCanvas(canvas.width, canvas.height);
@@ -504,26 +505,30 @@ describe("PDF viewer", () => {
504505
const tempCtx = tempCanvas.getContext("2d", {
505506
willReadFrequently: true,
506507
});
507-
tempCtx.drawImage(canvas, 0, 0);
508+
tempCtx.drawImage(bitmap, 0, 0);
509+
bitmap.close();
508510
return tempCtx;
509511
}
510512

511513
if (!pageOne) {
512514
return [];
513515
}
514516

515-
return Array.from(pageOne.querySelectorAll("canvas"), canvas => {
517+
const canvases = pageOne.querySelectorAll("canvas");
518+
const results = [];
519+
for (const canvas of canvases) {
516520
const { width, height } = canvas;
517521
if (width === 0 || height === 0) {
518-
return {
522+
results.push({
519523
size: 0,
520524
width,
521525
height,
522526
topLeft: null,
523527
bottomRight: null,
524-
};
528+
});
529+
continue;
525530
}
526-
const ctx = getContextFromCanvas(canvas);
531+
const ctx = await getContextFromCanvas(canvas);
527532
const topLeft = ctx.getImageData(
528533
Math.min(2, width - 1),
529534
Math.min(2, height - 1),
@@ -536,14 +541,15 @@ describe("PDF viewer", () => {
536541
1,
537542
1
538543
).data;
539-
return {
544+
results.push({
540545
size: width * height,
541546
width,
542547
height,
543548
topLeft: globalThis.pdfjsLib.Util.makeHexColor(...topLeft),
544549
bottomRight: globalThis.pdfjsLib.Util.makeHexColor(...bottomRight),
545-
};
546-
});
550+
});
551+
}
552+
return results;
547553
}
548554

549555
function waitForDetailRendered(page) {

0 commit comments

Comments
 (0)