Merge pull request #12489 from calixteman/field_obj_test
Add a test for pdfDocument::fieldObjects
This commit is contained in:
		
						commit
						d2874d4ed4
					
				@ -45,17 +45,31 @@ describe("document", function () {
 | 
				
			|||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe("PDFDocument", function () {
 | 
					  describe("PDFDocument", function () {
 | 
				
			||||||
    const pdfManager = {
 | 
					 | 
				
			||||||
      get docId() {
 | 
					 | 
				
			||||||
        return "d0";
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    const stream = new StringStream("Dummy_PDF_data");
 | 
					    const stream = new StringStream("Dummy_PDF_data");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function getDocument(acroForm, xref = new XRefMock()) {
 | 
					    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);
 | 
					      const pdfDocument = new PDFDocument(pdfManager, stream);
 | 
				
			||||||
      pdfDocument.xref = xref;
 | 
					      pdfDocument.xref = xref;
 | 
				
			||||||
      pdfDocument.catalog = { acroForm };
 | 
					      pdfDocument.catalog = catalog;
 | 
				
			||||||
      return pdfDocument;
 | 
					      return pdfDocument;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -174,5 +188,56 @@ describe("document", function () {
 | 
				
			|||||||
      pdfDocument = getDocument(acroForm);
 | 
					      pdfDocument = getDocument(acroForm);
 | 
				
			||||||
      expect(pdfDocument.calculationOrderIds).toEqual(null);
 | 
					      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"]);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user