Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set up a hook at the end of the initialization (bug 1881319) #17709

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@
"description": "The color is a string as defined in CSS. Its goal is to help improve readability in high contrast mode",
"type": "string",
"default": "CanvasText"
},
"hasInitializationHook": {
"type": "boolean",
"default": false
}
}
}
21 changes: 7 additions & 14 deletions test/integration/copy_paste_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import {
awaitPromise,
closePages,
kbCopy,
kbSelectAll,
Expand Down Expand Up @@ -44,12 +45,7 @@ describe("Copy and paste", () => {
"tracemonkey.pdf",
"#hiddenCopyElement",
100,
async page => {
await page.waitForFunction(
() => !!window.PDFViewerApplication.eventBus
);
await waitForTextLayer(page);
}
waitForTextLayer
);
await mockClipboard(pages);
});
Expand All @@ -60,7 +56,8 @@ describe("Copy and paste", () => {

it("must check that we've all the contents on copy/paste", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
pages.map(async ([browserName, page, pageSetupPromise]) => {
awaitPromise(pageSetupPromise);
await selectAll(page);

const promise = waitForEvent(page, "copy");
Expand Down Expand Up @@ -151,12 +148,7 @@ describe("Copy and paste", () => {
"copy_paste_ligatures.pdf",
"#hiddenCopyElement",
100,
async page => {
await page.waitForFunction(
() => !!window.PDFViewerApplication.eventBus
);
await waitForTextLayer(page);
}
waitForTextLayer
);
await mockClipboard(pages);
});
Expand All @@ -167,7 +159,8 @@ describe("Copy and paste", () => {

it("must check that the ligatures have been removed when the text has been copied", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
pages.map(async ([browserName, page, pageSetupPromise]) => {
awaitPromise(pageSetupPromise);
await selectAll(page);

const promise = waitForEvent(page, "copy");
Expand Down
42 changes: 15 additions & 27 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,20 +2216,14 @@ describe("FreeText Editor", () => {
"tracemonkey.pdf",
".annotationEditorLayer",
100,
async page => {
await page.waitForFunction(async () => {
await window.PDFViewerApplication.initializedPromise;
return true;
});
await page.evaluate(() => {
window.visitedPages = [];
window.PDFViewerApplication.eventBus.on(
"pagechanging",
({ pageNumber }) => {
window.visitedPages.push(pageNumber);
}
);
});
() => {
window.visitedPages = [];
window.PDFViewerApplication.eventBus.on(
"pagechanging",
({ pageNumber }) => {
window.visitedPages.push(pageNumber);
}
);
}
);
});
Expand Down Expand Up @@ -2469,19 +2463,13 @@ 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);
}
);
});
() => {
window.PDFViewerApplication.eventBus.on(
"annotationeditorstateschanged",
({ details }) => {
window.editingEvents?.push(details);
}
);
}
);
});
Expand Down
32 changes: 5 additions & 27 deletions test/integration/scripting_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,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 @@ -1788,43 +1787,22 @@ describe("Interaction", () => {
"autoprint.pdf",
"",
null /* pageSetup = */,
async page => {
printHandles.set(
page,
await page.evaluateHandle(() => [
new Promise(resolve => {
globalThis.printResolve = resolve;
}),
])
);
await page.waitForFunction(() => {
if (!window.PDFViewerApplication?.eventBus) {
return false;
}
window.PDFViewerApplication.eventBus.on(
"print",
() => {
const resolve = globalThis.printResolve;
delete globalThis.printResolve;
resolve();
},
{ once: true }
);
return true;
resolve => {
window.PDFViewerApplication.eventBus.on("print", resolve, {
once: true,
});
}
);
});

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 awaitPromise(printHandles.get(page));
pages.map(async ([browserName, page, pageSetupPromise]) => {
await awaitPromise(pageSetupPromise);
})
);
});
Expand Down
44 changes: 30 additions & 14 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function loadAndWait(filename, selector, zoom, pageSetup, options) {
});

let app_options = "";
if (pageSetup) {
options ||= {};
options.initializationhook = "true";
}
if (options) {
// Options must be handled in app.js::_parseHashParams.
for (const [key, value] of Object.entries(options)) {
Expand All @@ -48,8 +52,25 @@ function loadAndWait(filename, selector, zoom, pageSetup, options) {
}?file=/test/pdfs/${filename}#zoom=${zoom ?? "page-fit"}${app_options}`;

await page.goto(url);
let pageSetupPromise = null;
if (pageSetup) {
await pageSetup(page);
await page.waitForFunction(
() => !!window.PDFViewerApplication.eventBus
);
pageSetupPromise = await page.evaluateHandle(
cb => [
new Promise(resolve => {
window.PDFViewerApplication.eventBus.dispatch(
"initializationhook",
{
// eslint-disable-next-line no-eval
callback: () => eval(`(${cb})`)(resolve),
}
);
}),
],
pageSetup.toString()
);
}

await page.bringToFront();
Expand All @@ -58,7 +79,7 @@ function loadAndWait(filename, selector, zoom, pageSetup, options) {
timeout: 0,
});
}
return [session.name, page];
return [session.name, page, pageSetupPromise];
})
);
}
Expand Down Expand Up @@ -298,20 +319,15 @@ async function dragAndDropAnnotation(page, startX, startY, tX, tY) {
await page.waitForSelector("#viewer:not(.noUserSelect)");
}

function waitForAnnotationEditorLayer(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on(
"annotationeditorlayerrendered",
resolve
);
});
function waitForAnnotationEditorLayer(resolve) {
window.PDFViewerApplication.eventBus.on(
"annotationeditorlayerrendered",
resolve
);
}

async function waitForTextLayer(page) {
const handle = await createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on("textlayerrendered", resolve);
});
return awaitPromise(handle);
function waitForTextLayer(resolve) {
window.PDFViewerApplication.eventBus.on("textlayerrendered", resolve);
}

async function scrollIntoView(page, selector) {
Expand Down
20 changes: 20 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,23 @@ const PDFViewerApplication = {
this.bindEvents();
this.bindWindowEvents();

if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("TESTING || MOZCENTRAL") &&
AppOptions.get("hasInitializationHook")
) {
await new Promise(resolve => {
this.eventBus.on(
"initializationhook",
({ callback }) => {
callback();
resolve();
},
{ once: true }
);
});
}

this._initializedCapability.resolve();
},

Expand Down Expand Up @@ -329,6 +346,9 @@ const PDFViewerApplication = {
params.get("highlighteditorcolors")
);
}
if (params.get("initializationhook") === "true") {
AppOptions.set("hasInitializationHook", true);
}
}
},

Expand Down
5 changes: 5 additions & 0 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ const defaultOptions = {
value: 0,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
},
hasInitializationHook: {
/** @type {boolean} */
value: false,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
},
highlightEditorColors: {
/** @type {string} */
value: "yellow=#FFFF98,green=#53FFBC,blue=#80EBFF,pink=#FFCBE6,red=#FF4F5F",
Expand Down