Skip to content

Commit

Permalink
Merge pull request #12489 from calixteman/field_obj_test
Browse files Browse the repository at this point in the history
Add a test for pdfDocument::fieldObjects
  • Loading branch information
timvandermeij authored Oct 17, 2020
2 parents 3fa3cb6 + e46e314 commit d2874d4
Showing 1 changed file with 71 additions and 6 deletions.
77 changes: 71 additions & 6 deletions test/unit/document_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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"]);
});
});
});

0 comments on commit d2874d4

Please sign in to comment.