Skip to content

Commit

Permalink
JS -- Add tests for print/save actions
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Dec 23, 2020
1 parent d060cd2 commit 53ab319
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,11 @@ class PDFDocument {
"hasJSActions",
this.fieldObjects.then(fieldObjects => {
return (
fieldObjects !== null &&
Object.values(fieldObjects).some(fieldObject =>
fieldObject.some(object => object.actions !== null)
)
(fieldObjects !== null &&
Object.values(fieldObjects).some(fieldObject =>
fieldObject.some(object => object.actions !== null)
)) ||
!!this.catalog.jsActions
);
})
);
Expand Down
70 changes: 70 additions & 0 deletions test/integration/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
const { clearInput, closePages, loadAndWait } = require("./test_utils.js");

describe("Interaction", () => {
async function actAndWaitForInput(page, selector, action) {
await clearInput(page, selector);
await action();
await page.waitForFunction(
`document.querySelector("${selector.replace("\\", "\\\\")}").value !== ""`
);
return page.$eval(selector, el => el.value);
}

describe("in 160F-2019.pdf", () => {
let pages;

Expand Down Expand Up @@ -280,4 +289,65 @@ describe("Interaction", () => {
);
});
});

describe("in doc_actions.pdf for printing", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("doc_actions.pdf", "#\\33 7R");
});

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

it("must execute WillPrint and DidPrint actions when printing", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
let text = await actAndWaitForInput(page, "#\\33 7R", async () => {
await page.click("#print");
});
expect(text).withContext(`In ${browserName}`).toEqual("WillPrint");

await page.waitForFunction(
`document.querySelector("#\\\\34 0R").value !== ""`
);

text = await page.$eval("#\\34 0R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("DidPrint");
})
);
});
});

describe("in doc_actions.pdf for saving", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("doc_actions.pdf", "#\\33 7R");
});

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

it("must execute WillSave and DidSave actions when saving", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await clearInput(page, "#\\34 0R");
let text = await actAndWaitForInput(page, "#\\33 7R", async () => {
await page.click("#download");
});
expect(text).withContext(`In ${browserName}`).toEqual("WillSave");

await page.waitForFunction(
`document.querySelector("#\\\\34 0R").value !== ""`
);

text = await page.$eval("#\\34 0R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("DidSave");
})
);
});
});
});
2 changes: 2 additions & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
!issue7544.pdf
!issue7507.pdf
!issue6931_reduced.pdf
!doc_actions.pdf
!issue7580.pdf
!issue7598.pdf
!issue12750.pdf
Expand Down Expand Up @@ -225,6 +226,7 @@
!issue4875.pdf
!issue11740_reduced.pdf
!issue12705.pdf
!doc_actions.pdf
!issue4881.pdf
!issue5994.pdf
!issue6151.pdf
Expand Down
Binary file added test/pdfs/doc_actions.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,11 @@ async function startBrowser(browserName, startUrl = "") {
options.extraPrefsFirefox = {
// avoid to have a prompt when leaving a page with a form
"dom.disable_beforeunload": true,
// Disable dialog when saving a pdf
"pdfjs.disabled": true,
"browser.helperApps.neverAsk.saveToDisk": "application/pdf",
// Avoid popup when saving is done
"browser.download.panel.shown": true,
};
}

Expand Down
6 changes: 6 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,12 @@
"firstPage": 2,
"type": "eq"
},
{ "id": "doc_actions",
"file": "pdfs/doc_actions.pdf",
"md5": "7ecce46174a005050bffe9c722b935d3",
"rounds": 1,
"type": "eq"
},
{ "id": "issue10542",
"file": "pdfs/issue10542_reduced.pdf",
"md5": "92406cb903be6c7a63221ba61fcb8eaf",
Expand Down

0 comments on commit 53ab319

Please sign in to comment.