Skip to content

Commit

Permalink
Merge pull request #18617 from calixteman/early_event_handler
Browse files Browse the repository at this point in the history
Set the event handlers in the integration tests before any event is triggered
  • Loading branch information
timvandermeij authored Aug 18, 2024
2 parents f7d3add + 5e49480 commit b47c7ec
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 70 deletions.
38 changes: 12 additions & 26 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
}
);
});
Expand Down Expand Up @@ -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);
});
},
}
);
});
Expand Down
19 changes: 6 additions & 13 deletions test/integration/highlight_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
);
Expand Down
44 changes: 17 additions & 27 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1764,38 +1764,28 @@ 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(() => [
new Promise(resolve => {
globalThis.printResolve = resolve;
}),
window.PDFViewerApplication._testPrintResolver.promise,
])
);
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;
});
}
);
},
appSetup: app => {
app._testPrintResolver = Promise.withResolvers();
},
eventBusSetup: eventBus => {
eventBus.on(
"print",
() => {
window.PDFViewerApplication._testPrintResolver.resolve();
},
{ once: true }
);
},
});
});

afterAll(async () => {
Expand Down
50 changes: 46 additions & 4 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -52,11 +53,52 @@ 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) {
// page.evaluateOnNewDocument allows us to run code before the
// 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();
if (selector) {
await page.waitForSelector(selector, {
Expand Down

0 comments on commit b47c7ec

Please sign in to comment.