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

JS -- Add tests for print/save actions #12774

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
81 changes: 81 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,76 @@ describe("Interaction", () => {
);
});
});

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

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

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

it("must execute WillPrint and DidPrint actions", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
if (process.platform === "win32" && browserName === "firefox") {
// Doesn't work because of bug 1662471
return;
Comment on lines +308 to +309
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I just noticed this now and that's really not how you temporarily disable a test, since that pretty much guarantees that no one will ever fix it.
The correct way, since we're using Jasmine, is to add a pending() statement; i.e.

pending("Disabled in Firefox on Windows, because of bug 1662471.");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I also missed this one. @calixteman Let's address this one in a follow-up PR.

}
let text = await actAndWaitForInput(page, "#\\34 7R", async () => {
await page.click("#print");
});
expect(text).withContext(`In ${browserName}`).toEqual("WillPrint");

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

text = await page.$eval("#\\35 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", "#\\34 7R");
});

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

it("must execute WillSave and DidSave actions", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
try {
// Disable download in chrome
// (it leads to an error in firefox so the try...)
await page._client.send("Page.setDownloadBehavior", {
behavior: "deny",
});
} catch (_) {}
await clearInput(page, "#\\34 7R");
let text = await actAndWaitForInput(page, "#\\34 7R", async () => {
await page.click("#download");
});
expect(text).withContext(`In ${browserName}`).toEqual("WillSave");

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

text = await page.$eval("#\\35 0R", el => el.value);
expect(text).withContext(`In ${browserName}`).toEqual("DidSave");
})
);
});
});
});
2 changes: 1 addition & 1 deletion 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 @@ -179,7 +180,6 @@
!pattern_text_embedded_font.pdf
!devicen.pdf
!cmykjpeg.pdf
!docactions.pdf
!issue840.pdf
!160F-2019.pdf
!issue4402_reduced.pdf
Expand Down
Binary file added test/pdfs/doc_actions.pdf
Binary file not shown.
Binary file removed test/pdfs/docactions.pdf
Binary file not shown.
33 changes: 31 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,15 +836,36 @@ async function startBrowser(browserName, startUrl = "") {
ignoreDefaultArgs: ["--disable-extensions"],
};

if (!tempDir) {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pdfjs-"));
}
const printFile = path.join(tempDir, "print.pdf");

if (browserName === "chrome") {
// avoid crash
options.args = ["--no-sandbox", "--disable-setuid-sandbox"];
// silent printing in a pdf
options.args.push("--kiosk-printing");
}

if (browserName === "firefox") {
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,
// Save file in output
"browser.download.folderList": 2,
"browser.download.dir": tempDir,
// Print silently in a pdf
"print.always_print_silent": true,
"print.show_print_progress": false,
print_printer: "PDF",
"print.printer_PDF.print_to_file": true,
"print.printer_PDF.print_to_filename": printFile,
};
}

Expand Down Expand Up @@ -928,8 +949,15 @@ async function closeSession(browser) {
const allClosed = sessions.every(function (s) {
return s.closed;
});
if (allClosed && onAllSessionsClosed) {
onAllSessionsClosed();
if (allClosed) {
if (tempDir) {
const rimraf = require("rimraf");
rimraf.sync(tempDir);
}

if (onAllSessionsClosed) {
onAllSessionsClosed();
}
}
}
}
Expand Down Expand Up @@ -984,5 +1012,6 @@ var onAllSessionsClosed;
var host = "127.0.0.1";
var options = parseOptions();
var stats;
var tempDir = null;

main();
12 changes: 6 additions & 6 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,6 @@
"link": false,
"type": "eq"
},
{ "id": "docactions",
"file": "pdfs/docactions.pdf",
"md5": "28ea940349cf7cb793cfe167d04b097c",
"rounds": 1,
"type": "eq"
},
{ "id": "issue9084",
"file": "pdfs/issue9084.pdf",
"md5": "5570ec01cc869d299fec1b2f68926a08",
Expand Down Expand Up @@ -1450,6 +1444,12 @@
"type": "eq",
"about": "Type3 fonts with image resources; both pages need to be tested, otherwise the bug won't manifest."
},
{ "id": "doc_actions",
"file": "pdfs/doc_actions.pdf",
"md5": "ceae4eb405a0b40394f4d63d7525a870",
"rounds": 1,
"type": "eq"
},
{ "id": "issue12504",
"file": "pdfs/issue12504.pdf",
"md5": "04fcc87f3e7e9e925e3ef83cf0bf49f4",
Expand Down
34 changes: 20 additions & 14 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,27 +1006,33 @@ describe("api", function () {
});
it("gets JSActions", function (done) {
// PDF document with "JavaScript" action in the OpenAction dictionary.
const loadingTask = getDocument(buildGetDocumentParams("docactions.pdf"));
const loadingTask = getDocument(
buildGetDocumentParams("doc_actions.pdf")
);
const promise = loadingTask.promise.then(async pdfDoc => {
const docActions = await pdfDoc.getJSActions();
const page5 = await pdfDoc.getPage(5);
const page12 = await pdfDoc.getPage(12);
const page5Actions = await page5.getJSActions();
const page12Actions = await page12.getJSActions();
return [docActions, page5Actions, page12Actions];
const page1 = await pdfDoc.getPage(1);
const page3 = await pdfDoc.getPage(3);
const page1Actions = await page1.getJSActions();
const page3Actions = await page3.getJSActions();
return [docActions, page1Actions, page3Actions];
});
promise
.then(async ([docActions, page5Actions, page12Actions]) => {
.then(async ([docActions, page1Actions, page3Actions]) => {
expect(docActions).toEqual({
Open: ["console.println('Open Action');"],
DidPrint: [`this.getField("Text2").value = "DidPrint";`],
DidSave: [`this.getField("Text2").value = "DidSave";`],
WillClose: [`this.getField("Text1").value = "WillClose";`],
WillPrint: [`this.getField("Text1").value = "WillPrint";`],
WillSave: [`this.getField("Text1").value = "WillSave";`],
});
expect(page5Actions).toEqual({
PageOpen: ["console.println('Open page 5');"],
PageClose: ["console.println('Close page 5');"],
expect(page1Actions).toEqual({
PageOpen: [`this.getField("Text1").value = "PageOpen 1";`],
PageClose: [`this.getField("Text2").value = "PageClose 1";`],
});
expect(page12Actions).toEqual({
PageOpen: ["console.println('Open page 12');"],
PageClose: ["console.println('Close page 12');"],
expect(page3Actions).toEqual({
PageOpen: [`this.getField("Text5").value = "PageOpen 3";`],
PageClose: [`this.getField("Text6").value = "PageClose 3";`],
});
loadingTask.destroy().then(done);
})
Expand Down