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.
This commit is contained in:
Calixte Denizet 2024-08-16 18:06:53 +01:00
parent d8d9cff715
commit 5e4948062c
4 changed files with 81 additions and 70 deletions

View file

@ -2171,20 +2171,13 @@ describe("FreeText Editor", () => {
"tracemonkey.pdf", "tracemonkey.pdf",
".annotationEditorLayer", ".annotationEditorLayer",
100, 100,
async page => { {
await page.waitForFunction(async () => { eventBusSetup: eventBus => {
await window.PDFViewerApplication.initializedPromise;
return true;
});
await page.evaluate(() => {
window.visitedPages = []; window.visitedPages = [];
window.PDFViewerApplication.eventBus.on( eventBus.on("pagechanging", ({ pageNumber }) => {
"pagechanging", window.visitedPages.push(pageNumber);
({ pageNumber }) => { });
window.visitedPages.push(pageNumber); },
}
);
});
} }
); );
}); });
@ -2403,19 +2396,12 @@ describe("FreeText Editor", () => {
"tracemonkey.pdf", "tracemonkey.pdf",
".annotationEditorLayer", ".annotationEditorLayer",
100, 100,
async page => { {
await page.waitForFunction(async () => { eventBusSetup: eventBus => {
await window.PDFViewerApplication.initializedPromise; eventBus.on("annotationeditorstateschanged", ({ details }) => {
return true; window.editingEvents?.push(details);
}); });
await page.evaluate(() => { },
window.PDFViewerApplication.eventBus.on(
"annotationeditorstateschanged",
({ details }) => {
window.editingEvents?.push(details);
}
);
});
} }
); );
}); });

View file

@ -910,20 +910,13 @@ describe("Highlight Editor", () => {
"tracemonkey.pdf", "tracemonkey.pdf",
`.page[data-page-number = "1"] .endOfContent`, `.page[data-page-number = "1"] .endOfContent`,
null, null,
async page => { {
await page.waitForFunction(async () => { eventBusSetup: eventBus => {
await window.PDFViewerApplication.initializedPromise;
return true;
});
await page.evaluate(() => {
window.editingEvents = []; window.editingEvents = [];
window.PDFViewerApplication.eventBus.on( eventBus.on("annotationeditorstateschanged", ({ details }) => {
"annotationeditorstateschanged", window.editingEvents.push(details);
({ details }) => { });
window.editingEvents.push(details); },
}
);
});
}, },
{ highlightEditorColors: "red=#AB0000" } { highlightEditorColors: "red=#AB0000" }
); );

View file

@ -1764,38 +1764,28 @@ describe("Interaction", () => {
// it is usually very fast and therefore activating the selector check // it is usually very fast and therefore activating the selector check
// too late will cause it to never resolve because printing is already // 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. // done (and the printed page div removed) before we even get to it.
pages = await loadAndWait( pages = await loadAndWait("autoprint.pdf", "", null /* zoom = */, {
"autoprint.pdf", postPageSetup: async page => {
"",
null /* zoom = */,
async page => {
printHandles.set( printHandles.set(
page, page,
page.evaluateHandle(() => [ page.evaluateHandle(() => [
new Promise(resolve => { window.PDFViewerApplication._testPrintResolver.promise,
globalThis.printResolve = resolve;
}),
]) ])
); );
await page.waitForFunction(() => { },
// We don't really need to print the document. appSetup: app => {
window.print = () => {}; app._testPrintResolver = Promise.withResolvers();
if (!window.PDFViewerApplication?.eventBus) { },
return false; eventBusSetup: eventBus => {
} eventBus.on(
window.PDFViewerApplication.eventBus.on( "print",
"print", () => {
() => { window.PDFViewerApplication._testPrintResolver.resolve();
const resolve = globalThis.printResolve; },
delete globalThis.printResolve; { once: true }
resolve(); );
}, },
{ once: true } });
);
return true;
});
}
);
}); });
afterAll(async () => { afterAll(async () => {

View file

@ -14,9 +14,10 @@
*/ */
import os from "os"; import os from "os";
const isMac = os.platform() === "darwin"; const isMac = os.platform() === "darwin";
function loadAndWait(filename, selector, zoom, pageSetup, options) { function loadAndWait(filename, selector, zoom, setups, options) {
return Promise.all( return Promise.all(
global.integrationSessions.map(async session => { global.integrationSessions.map(async session => {
const page = await session.browser.newPage(); const page = await session.browser.newPage();
@ -52,11 +53,52 @@ function loadAndWait(filename, selector, zoom, pageSetup, options) {
global.integrationBaseUrl global.integrationBaseUrl
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`; }?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;
await page.goto(url); if (setups) {
if (pageSetup) { // page.evaluateOnNewDocument allows us to run code before the
await pageSetup(page); // first js script is executed.
// The idea here is to set up some setters for PDFViewerApplication
// and EventBus, so we can inject some code to do whatever we want
// soon enough especially before the first event in the eventBus is
// dispatched.
const { prePageSetup, appSetup, eventBusSetup } = setups;
await prePageSetup?.(page);
if (appSetup || eventBusSetup) {
await page.evaluateOnNewDocument(
(aSetup, eSetup) => {
let app;
let eventBus;
Object.defineProperty(window, "PDFViewerApplication", {
get() {
return app;
},
set(newValue) {
app = newValue;
if (aSetup) {
// eslint-disable-next-line no-eval
eval(`(${aSetup})`)(app);
}
Object.defineProperty(app, "eventBus", {
get() {
return eventBus;
},
set(newV) {
eventBus = newV;
// eslint-disable-next-line no-eval
eval(`(${eSetup})`)(eventBus);
},
});
},
});
},
appSetup?.toString(),
eventBusSetup?.toString()
);
}
} }
await page.goto(url);
await setups?.postPageSetup?.(page);
await page.bringToFront(); await page.bringToFront();
if (selector) { if (selector) {
await page.waitForSelector(selector, { await page.waitForSelector(selector, {