Skip to content

Commit

Permalink
Add test for drawing delay with CSS-only zoom
Browse files Browse the repository at this point in the history
This commit adds a test for 0603d1a.
Before the fix the test fails with 10 `pagerendered` events fired
synchronously during the test loop.
  • Loading branch information
nicolo-ribaudo committed May 21, 2024
1 parent 9ee7c07 commit 1590b9e
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
130 changes: 129 additions & 1 deletion test/integration/viewer_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* limitations under the License.
*/

import { closePages, loadAndWait } from "./test_utils.mjs";
import {
awaitPromise,
closePages,
createPromise,
loadAndWait,
} from "./test_utils.mjs";

describe("PDF viewer", () => {
describe("Zoom with the mouse wheel", () => {
Expand Down Expand Up @@ -87,4 +92,127 @@ describe("PDF viewer", () => {
);
});
});

describe("CSS-only zoom", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".textLayer .endOfContent",
null,
null,
{
maxCanvasPixels: 0,
}
);
});

afterAll(async () => {
await closePages(pages);
});

beforeEach(() =>
Promise.all(
pages.map(async ([browserName, page]) => {
await page.evaluate(() => {
window.PDFViewerApplication.pdfViewer.currentScaleValue = 1;

window.pageRenderedListener = new AbortController();
window.firstPageRenders = 0;
window.PDFViewerApplication.eventBus.on(
"pagerendered",
({ pageNumber }) => {
if (pageNumber === 1) {
window.firstPageRenders++;
}
},
{ signal: window.pageRenderedListener.signal }
);
});
})
)
);

afterEach(() =>
Promise.all(
pages.map(async ([browserName, page]) => {
await page.evaluate(() => {
window.pageRenderedListener.abort();
});
})
)
);

function createPromiseForFirstPageRendered(page) {
return createPromise(page, (resolve, reject) => {
const controller = new AbortController();
window.PDFViewerApplication.eventBus.on(
"pagerendered",
({ pageNumber }) => {
if (pageNumber === 1) {
resolve();
controller.abort();
}
},
{ signal: controller.signal }
);
setTimeout(reject, 1000, new Error("Timeout"));
});
}

it("respects drawing delay when zooming out", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
for (let i = 0; i < 10; i++) {
await page.evaluate(() => {
window.PDFViewerApplication.pdfViewer.decreaseScale({
drawingDelay: 100,
scaleFactor: 0.9,
});
});
}

const promise = await createPromiseForFirstPageRendered(page);

let firstPageRenders = await page.evaluate(
() => window.firstPageRenders
);
expect(firstPageRenders).withContext(`In ${browserName}`).toBe(0);

await awaitPromise(promise);

firstPageRenders = await page.evaluate(() => window.firstPageRenders);
expect(firstPageRenders).withContext(`In ${browserName}`).toBe(1);
})
);
});

it("respects drawing delay when zooming in", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
for (let i = 0; i < 10; i++) {
await page.evaluate(() => {
window.PDFViewerApplication.pdfViewer.increaseScale({
drawingDelay: 100,
scaleFactor: 1.1,
});
});
}

const promise = await createPromiseForFirstPageRendered(page);

let firstPageRenders = await page.evaluate(
() => window.firstPageRenders
);
expect(firstPageRenders).withContext(`In ${browserName}`).toBe(0);

await awaitPromise(promise);

firstPageRenders = await page.evaluate(() => window.firstPageRenders);
expect(firstPageRenders).withContext(`In ${browserName}`).toBe(1);
})
);
});
});
});
6 changes: 6 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ const PDFViewerApplication = {
params.get("highlighteditorcolors")
);
}
if (params.has("maxcanvaspixels")) {
AppOptions.set(
"maxCanvasPixels",
Number(params.get("maxcanvaspixels"))
);
}
if (params.has("supportscaretbrowsingmode")) {
AppOptions.set(
"supportsCaretBrowsingMode",
Expand Down

0 comments on commit 1590b9e

Please sign in to comment.