From b0c239a20071a62725318c04eb5bfc9813c67c67 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 20 Aug 2024 22:24:08 +0100 Subject: [PATCH] Don't show the print dialog when printing in some integration tests --- test/integration/scripting_spec.mjs | 72 ++++++++++++++--------------- test/integration/test_utils.mjs | 17 +++++-- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/test/integration/scripting_spec.mjs b/test/integration/scripting_spec.mjs index d6c8df0e8a483..7ae1e7b46cae5 100644 --- a/test/integration/scripting_spec.mjs +++ b/test/integration/scripting_spec.mjs @@ -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); + }) + ); }); }); @@ -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 @@ -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(); @@ -1779,7 +1776,6 @@ describe("Interaction", () => { afterAll(async () => { await closePages(pages); - printHandles.clear(); }); it("must check if printing is triggered when the document is open", async () => { @@ -1787,7 +1783,11 @@ describe("Interaction", () => { pages.map(async ([browserName, page]) => { await waitForScripting(page); - await awaitPromise(await printHandles.get(page)); + await awaitPromise( + await page.evaluateHandle(() => [ + window.PDFViewerApplication._testPrintResolver.promise, + ]) + ); }) ); }); diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index 1a1a4ab2bf5c1..eb31ad44b2c69 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -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", { @@ -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() );