Skip to content

Commit

Permalink
Merge pull request #18635 from calixteman/avoid_print_dialog
Browse files Browse the repository at this point in the history
Don't show the print dialog when printing in some integration tests
  • Loading branch information
timvandermeij committed Aug 23, 2024
2 parents 584fef5 + b0c239a commit 5f7637b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
72 changes: 36 additions & 36 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -439,38 +439,40 @@ describe("Interaction", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("doc_actions.pdf", getSelector("47R"));
pages = await loadAndWait("doc_actions.pdf", getSelector("47R"), null, {
earlySetup: () => {
// No need to trigger the print dialog.
window.print = () => {};
},
});
});

it("must execute WillPrint and DidPrint actions", async () => {
// Run the tests sequentially to avoid to use the same printer at the same
// time.
// And to make sure that a printer isn't locked by a process we close the
// page before running the next test.
for (const [browserName, page] of pages) {
await waitForScripting(page);
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForScripting(page);

await clearInput(page, getSelector("47R"));
await page.evaluate(_ => {
window.document.activeElement.blur();
});
await page.waitForFunction(`${getQuerySelector("47R")}.value === ""`);
await clearInput(page, getSelector("47R"));
await page.evaluate(_ => {
window.document.activeElement.blur();
});
await page.waitForFunction(`${getQuerySelector("47R")}.value === ""`);

const text = await actAndWaitForInput(
page,
getSelector("47R"),
async () => {
await page.click("#print");
}
);
expect(text).withContext(`In ${browserName}`).toEqual("WillPrint");
await page.keyboard.press("Escape");
const text = await actAndWaitForInput(
page,
getSelector("47R"),
async () => {
await page.click("#print");
}
);
expect(text).withContext(`In ${browserName}`).toEqual("WillPrint");

await page.waitForFunction(
`${getQuerySelector("50R")}.value === "DidPrint"`
);
await closeSinglePage(page);
}
await page.waitForFunction(
`${getQuerySelector("50R")}.value === "DidPrint"`
);
await closeSinglePage(page);
})
);
});
});

Expand Down Expand Up @@ -1742,7 +1744,6 @@ describe("Interaction", () => {

describe("in autoprint.pdf", () => {
let pages;
const printHandles = new Map();

beforeAll(async () => {
// Autoprinting is triggered by the `Open` event, which is one of the
Expand All @@ -1754,13 +1755,9 @@ describe("Interaction", () => {
// 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 = */, {
postPageSetup: async page => {
printHandles.set(
page,
page.evaluateHandle(() => [
window.PDFViewerApplication._testPrintResolver.promise,
])
);
earlySetup: () => {
// No need to trigger the print dialog.
window.print = () => {};
},
appSetup: app => {
app._testPrintResolver = Promise.withResolvers();
Expand All @@ -1779,15 +1776,18 @@ describe("Interaction", () => {

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

it("must check if printing is triggered when the document is open", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await waitForScripting(page);

await awaitPromise(await printHandles.get(page));
await awaitPromise(
await page.evaluateHandle(() => [
window.PDFViewerApplication._testPrintResolver.promise,
])
);
})
);
});
Expand Down
17 changes: 12 additions & 5 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ function loadAndWait(filename, selector, zoom, setups, options) {
// 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;
const { prePageSetup, appSetup, earlySetup, eventBusSetup } = setups;
await prePageSetup?.(page);
if (appSetup || eventBusSetup) {
if (earlySetup || appSetup || eventBusSetup) {
await page.evaluateOnNewDocument(
(aSetup, eSetup) => {
(eaSetup, aSetup, evSetup) => {
if (eaSetup) {
// eslint-disable-next-line no-eval
eval(`(${eaSetup})`)();
}
let app;
let eventBus;
Object.defineProperty(window, "PDFViewerApplication", {
Expand All @@ -83,13 +87,16 @@ function loadAndWait(filename, selector, zoom, setups, options) {
},
set(newV) {
eventBus = newV;
// eslint-disable-next-line no-eval
eval(`(${eSetup})`)(eventBus);
if (evSetup) {
// eslint-disable-next-line no-eval
eval(`(${evSetup})`)(eventBus);
}
},
});
},
});
},
earlySetup?.toString(),
appSetup?.toString(),
eventBusSetup?.toString()
);
Expand Down

0 comments on commit 5f7637b

Please sign in to comment.