From 3af754d196101d486c8ba91ecd3c1c7a699b4aa0 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 16 Aug 2024 18:06:53 +0100 Subject: [PATCH] Set the event handlers in the integration tests before any event is triggered The function evaluateOnNewDocument in Puppeteer allow us to execute some js before the pdf.js one is loaded. It allows us to stub some setters before there are used and then set some event handlers very soon. --- test/integration/freetext_editor_spec.mjs | 38 +++++++-------------- test/integration/highlight_editor_spec.mjs | 19 ++++------- test/integration/scripting_spec.mjs | 39 +++++++++------------- test/integration/test_utils.mjs | 35 ++++++++++++++++--- 4 files changed, 64 insertions(+), 67 deletions(-) diff --git a/test/integration/freetext_editor_spec.mjs b/test/integration/freetext_editor_spec.mjs index d03df9fa69feb4..609d6761bc944a 100644 --- a/test/integration/freetext_editor_spec.mjs +++ b/test/integration/freetext_editor_spec.mjs @@ -2171,20 +2171,13 @@ describe("FreeText Editor", () => { "tracemonkey.pdf", ".annotationEditorLayer", 100, - async page => { - await page.waitForFunction(async () => { - await window.PDFViewerApplication.initializedPromise; - return true; - }); - await page.evaluate(() => { + { + eventBusSetup: eventBus => { window.visitedPages = []; - window.PDFViewerApplication.eventBus.on( - "pagechanging", - ({ pageNumber }) => { - window.visitedPages.push(pageNumber); - } - ); - }); + eventBus.on("pagechanging", ({ pageNumber }) => { + window.visitedPages.push(pageNumber); + }); + }, } ); }); @@ -2403,19 +2396,12 @@ describe("FreeText Editor", () => { "tracemonkey.pdf", ".annotationEditorLayer", 100, - async page => { - await page.waitForFunction(async () => { - await window.PDFViewerApplication.initializedPromise; - return true; - }); - await page.evaluate(() => { - window.PDFViewerApplication.eventBus.on( - "annotationeditorstateschanged", - ({ details }) => { - window.editingEvents?.push(details); - } - ); - }); + { + eventBusSetup: eventBus => { + eventBus.on("annotationeditorstateschanged", ({ details }) => { + window.editingEvents?.push(details); + }); + }, } ); }); diff --git a/test/integration/highlight_editor_spec.mjs b/test/integration/highlight_editor_spec.mjs index 635c81843a9c17..5533d37a9c3074 100644 --- a/test/integration/highlight_editor_spec.mjs +++ b/test/integration/highlight_editor_spec.mjs @@ -910,20 +910,13 @@ describe("Highlight Editor", () => { "tracemonkey.pdf", `.page[data-page-number = "1"] .endOfContent`, null, - async page => { - await page.waitForFunction(async () => { - await window.PDFViewerApplication.initializedPromise; - return true; - }); - await page.evaluate(() => { + { + eventBusSetup: eventBus => { window.editingEvents = []; - window.PDFViewerApplication.eventBus.on( - "annotationeditorstateschanged", - ({ details }) => { - window.editingEvents.push(details); - } - ); - }); + eventBus.on("annotationeditorstateschanged", ({ details }) => { + window.editingEvents.push(details); + }); + }, }, { highlightEditorColors: "red=#AB0000" } ); diff --git a/test/integration/scripting_spec.mjs b/test/integration/scripting_spec.mjs index 8b0a6002feb7be..59a817603beb4e 100644 --- a/test/integration/scripting_spec.mjs +++ b/test/integration/scripting_spec.mjs @@ -1764,11 +1764,8 @@ describe("Interaction", () => { // it is usually very fast and therefore activating the selector check // too late will cause it to never resolve because printing is already // done (and the printed page div removed) before we even get to it. - pages = await loadAndWait( - "autoprint.pdf", - "", - null /* zoom = */, - async page => { + pages = await loadAndWait("autoprint.pdf", "", null /* zoom = */, { + postPageSetup: async page => { printHandles.set( page, page.evaluateHandle(() => [ @@ -1777,25 +1774,19 @@ describe("Interaction", () => { }), ]) ); - await page.waitForFunction(() => { - // We don't really need to print the document. - window.print = () => {}; - if (!window.PDFViewerApplication?.eventBus) { - return false; - } - window.PDFViewerApplication.eventBus.on( - "print", - () => { - const resolve = globalThis.printResolve; - delete globalThis.printResolve; - resolve(); - }, - { once: true } - ); - return true; - }); - } - ); + }, + eventBusSetup: eventBus => { + eventBus.on( + "print", + () => { + const resolve = globalThis.printResolve; + delete globalThis.printResolve; + resolve(); + }, + { once: true } + ); + }, + }); }); afterAll(async () => { diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index 89281a33b6a600..b4b1eca4e8ddef 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -14,9 +14,10 @@ */ import os from "os"; + const isMac = os.platform() === "darwin"; -function loadAndWait(filename, selector, zoom, pageSetup, options) { +function loadAndWait(filename, selector, zoom, setups, options) { return Promise.all( global.integrationSessions.map(async session => { const page = await session.browser.newPage(); @@ -52,11 +53,37 @@ function loadAndWait(filename, selector, zoom, pageSetup, options) { global.integrationBaseUrl }?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`; - await page.goto(url); - if (pageSetup) { - await pageSetup(page); + if (setups) { + await setups.prePageSetup?.(page); + if (setups.eventBusSetup) { + await page.evaluateOnNewDocument(setup => { + let app; + let eventBus; + Object.defineProperty(window, "PDFViewerApplication", { + get() { + return app; + }, + set(newValue) { + app = newValue; + Object.defineProperty(app, "eventBus", { + get() { + return eventBus; + }, + set(newV) { + eventBus = newV; + // eslint-disable-next-line no-eval + eval(`(${setup})`)(eventBus); + }, + }); + }, + }); + }, setups.eventBusSetup.toString()); + } } + await page.goto(url); + await setups?.postPageSetup?.(page); + await page.bringToFront(); if (selector) { await page.waitForSelector(selector, {