Skip to content

Commit f251e60

Browse files
committed
Fix intermittent failure in the "supports specifying a custom origin" viewer integration test
The problem in the original code is that `getTextAt` is called too early and therefore returns unexpected text content. This can happen because we call `increaseScale` and then wait for the page's text layer to be visible, but it can take some time before the zoom actually occurs/completes in the viewer and in the meantime the old (pre-zoom) text layer may still be visible, causing us to continue too soon because we don't validate that we're dealing with the post-zoom text layer. This commit fixes the issue by simply waiting for the expected text to show up at the given origin coordinates, which makes the test work independent of viewer actions/timing.
1 parent d63aabd commit f251e60

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

test/integration/viewer_spec.mjs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,40 @@ describe("PDF viewer", () => {
4242
await closePages(pages);
4343
});
4444

45-
async function getTextAt(page, pageNumber, coordX, coordY) {
46-
await page.waitForFunction(
47-
pageNum =>
48-
!document.querySelector(
49-
`.page[data-page-number="${pageNum}"] > .textLayer`
50-
).hidden,
51-
{},
52-
pageNumber
45+
async function waitForTextAfterZoom(page, originX, originY, scale, text) {
46+
const handlePromise = await createPromise(page, resolve => {
47+
const callback = e => {
48+
if (e.pageNumber === 2) {
49+
window.PDFViewerApplication.eventBus.off(
50+
"textlayerrendered",
51+
callback
52+
);
53+
resolve();
54+
}
55+
};
56+
window.PDFViewerApplication.eventBus.on("textlayerrendered", callback);
57+
});
58+
59+
await page.evaluate(
60+
(scaleFactor, origin) => {
61+
window.PDFViewerApplication.pdfViewer.updateScale({
62+
drawingDelay: 0,
63+
scaleFactor,
64+
origin,
65+
});
66+
},
67+
scale,
68+
[originX, originY]
5369
);
54-
return page.evaluate(
55-
(x, y) => document.elementFromPoint(x, y)?.textContent,
56-
coordX,
57-
coordY
70+
71+
await awaitPromise(handlePromise);
72+
73+
await page.waitForFunction(
74+
`document.elementFromPoint(${originX}, ${originY})?.textContent === "${text}"`
5875
);
5976
}
6077

61-
it("supports specifiying a custom origin", async () => {
78+
it("supports specifying a custom origin", async () => {
6279
await Promise.all(
6380
pages.map(async ([browserName, page]) => {
6481
// We use this text span of page 2 because:
@@ -72,33 +89,8 @@ describe("PDF viewer", () => {
7289
const originX = rect.x + rect.width / 2;
7390
const originY = rect.y + rect.height / 2;
7491

75-
await page.evaluate(
76-
origin => {
77-
window.PDFViewerApplication.pdfViewer.increaseScale({
78-
scaleFactor: 2,
79-
origin,
80-
});
81-
},
82-
[originX, originY]
83-
);
84-
const textAfterZoomIn = await getTextAt(page, 2, originX, originY);
85-
expect(textAfterZoomIn)
86-
.withContext(`In ${browserName}, zoom in`)
87-
.toBe(text);
88-
89-
await page.evaluate(
90-
origin => {
91-
window.PDFViewerApplication.pdfViewer.decreaseScale({
92-
scaleFactor: 0.8,
93-
origin,
94-
});
95-
},
96-
[originX, originY]
97-
);
98-
const textAfterZoomOut = await getTextAt(page, 2, originX, originY);
99-
expect(textAfterZoomOut)
100-
.withContext(`In ${browserName}, zoom out`)
101-
.toBe(text);
92+
await waitForTextAfterZoom(page, originX, originY, 2, text);
93+
await waitForTextAfterZoom(page, originX, originY, 0.8, text);
10294
})
10395
);
10496
});

0 commit comments

Comments
 (0)