From e46e31486719178b35377470f5243ffd29a03439 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sat, 17 Oct 2020 15:15:17 +0200 Subject: [PATCH] Add a test for pdfDocument::fieldObjects --- test/unit/document_spec.js | 77 +++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/test/unit/document_spec.js b/test/unit/document_spec.js index fb18029a0fa14..5d3442a3d8fa6 100644 --- a/test/unit/document_spec.js +++ b/test/unit/document_spec.js @@ -45,17 +45,31 @@ describe("document", function () { }); describe("PDFDocument", function () { - const pdfManager = { - get docId() { - return "d0"; - }, - }; const stream = new StringStream("Dummy_PDF_data"); function getDocument(acroForm, xref = new XRefMock()) { + const catalog = { acroForm }; + const pdfManager = { + get docId() { + return "d0"; + }, + ensureCatalog(prop, args) { + return pdfManager.ensure(catalog, prop, args); + }, + ensure(obj, prop, args) { + return new Promise(function (resolve) { + const value = obj[prop]; + if (typeof value === "function") { + resolve(value.apply(obj, args)); + } else { + resolve(value); + } + }); + }, + }; const pdfDocument = new PDFDocument(pdfManager, stream); pdfDocument.xref = xref; - pdfDocument.catalog = { acroForm }; + pdfDocument.catalog = catalog; return pdfDocument; } @@ -174,5 +188,56 @@ describe("document", function () { pdfDocument = getDocument(acroForm); expect(pdfDocument.calculationOrderIds).toEqual(null); }); + + it("should get field objects array or null", async function () { + const acroForm = new Dict(); + + let pdfDocument = getDocument(acroForm); + let fields = await pdfDocument.fieldObjects; + expect(fields).toEqual(null); + + acroForm.set("Fields", []); + pdfDocument = getDocument(acroForm); + fields = await pdfDocument.fieldObjects; + expect(fields).toEqual(null); + + const kid1Ref = Ref.get(314, 0); + const kid11Ref = Ref.get(159, 0); + const kid2Ref = Ref.get(265, 0); + const parentRef = Ref.get(358, 0); + + const allFields = Object.create(null); + for (const name of ["parent", "kid1", "kid2", "kid11"]) { + const buttonWidgetDict = new Dict(); + buttonWidgetDict.set("Type", Name.get("Annot")); + buttonWidgetDict.set("Subtype", Name.get("Widget")); + buttonWidgetDict.set("FT", Name.get("Btn")); + buttonWidgetDict.set("T", name); + allFields[name] = buttonWidgetDict; + } + + allFields.kid1.set("Kids", [kid11Ref]); + allFields.parent.set("Kids", [kid1Ref, kid2Ref]); + + const xref = new XRefMock([ + { ref: parentRef, data: allFields.parent }, + { ref: kid1Ref, data: allFields.kid1 }, + { ref: kid11Ref, data: allFields.kid11 }, + { ref: kid2Ref, data: allFields.kid2 }, + ]); + + acroForm.set("Fields", [parentRef]); + pdfDocument = getDocument(acroForm, xref); + fields = await pdfDocument.fieldObjects; + + for (const [name, objs] of Object.entries(fields)) { + fields[name] = objs.map(obj => obj.id); + } + + expect(fields["parent.kid1"]).toEqual(["314R"]); + expect(fields["parent.kid1.kid11"]).toEqual(["159R"]); + expect(fields["parent.kid2"]).toEqual(["265R"]); + expect(fields.parent).toEqual(["358R"]); + }); }); });