Merge pull request #7941 from Snuffleupagus/Page-idFactory

Replace direct lookup of `uniquePrefix`/`idCounters`, in `Page` instances, with an `idFactory` containing an `createObjId` method instead
This commit is contained in:
Tim van der Meij 2017-01-10 00:25:48 +01:00 committed by GitHub
commit f828f07ccd
7 changed files with 252 additions and 155 deletions

View File

@ -65,18 +65,15 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
* @param {XRef} xref * @param {XRef} xref
* @param {Object} ref * @param {Object} ref
* @param {PDFManager} pdfManager * @param {PDFManager} pdfManager
* @param {string} uniquePrefix * @param {Object} idFactory
* @param {Object} idCounters
* @returns {Annotation} * @returns {Annotation}
*/ */
create: function AnnotationFactory_create(xref, ref, pdfManager, create: function AnnotationFactory_create(xref, ref, pdfManager, idFactory) {
uniquePrefix, idCounters) {
var dict = xref.fetchIfRef(ref); var dict = xref.fetchIfRef(ref);
if (!isDict(dict)) { if (!isDict(dict)) {
return; return;
} }
var id = isRef(ref) ? ref.toString() : var id = isRef(ref) ? ref.toString() : 'annot_' + idFactory.createObjId();
'annot_' + (uniquePrefix || '') + (++idCounters.obj);
// Determine the annotation's subtype. // Determine the annotation's subtype.
var subtype = dict.get('Subtype'); var subtype = dict.get('Subtype');

View File

@ -78,12 +78,18 @@ var Page = (function PageClosure() {
this.xref = xref; this.xref = xref;
this.ref = ref; this.ref = ref;
this.fontCache = fontCache; this.fontCache = fontCache;
this.uniquePrefix = 'p' + this.pageIndex + '_';
this.idCounters = {
obj: 0
};
this.evaluatorOptions = pdfManager.evaluatorOptions; this.evaluatorOptions = pdfManager.evaluatorOptions;
this.resourcesPromise = null; this.resourcesPromise = null;
var uniquePrefix = 'p' + this.pageIndex + '_';
var idCounters = {
obj: 0,
};
this.idFactory = {
createObjId: function () {
return uniquePrefix + (++idCounters.obj);
},
};
} }
Page.prototype = { Page.prototype = {
@ -240,8 +246,7 @@ var Page = (function PageClosure() {
var partialEvaluator = new PartialEvaluator(pdfManager, this.xref, var partialEvaluator = new PartialEvaluator(pdfManager, this.xref,
handler, this.pageIndex, handler, this.pageIndex,
this.uniquePrefix, this.idFactory,
this.idCounters,
this.fontCache, this.fontCache,
this.evaluatorOptions); this.evaluatorOptions);
@ -308,8 +313,7 @@ var Page = (function PageClosure() {
var contentStream = data[0]; var contentStream = data[0];
var partialEvaluator = new PartialEvaluator(pdfManager, self.xref, var partialEvaluator = new PartialEvaluator(pdfManager, self.xref,
handler, self.pageIndex, handler, self.pageIndex,
self.uniquePrefix, self.idFactory,
self.idCounters,
self.fontCache, self.fontCache,
self.evaluatorOptions); self.evaluatorOptions);
@ -345,8 +349,7 @@ var Page = (function PageClosure() {
var annotationRef = annotationRefs[i]; var annotationRef = annotationRefs[i];
var annotation = annotationFactory.create(this.xref, annotationRef, var annotation = annotationFactory.create(this.xref, annotationRef,
this.pdfManager, this.pdfManager,
this.uniquePrefix, this.idFactory);
this.idCounters);
if (annotation) { if (annotation) {
annotations.push(annotation); annotations.push(annotation);
} }

View File

@ -169,13 +169,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}; };
function PartialEvaluator(pdfManager, xref, handler, pageIndex, function PartialEvaluator(pdfManager, xref, handler, pageIndex,
uniquePrefix, idCounters, fontCache, options) { idFactory, fontCache, options) {
this.pdfManager = pdfManager; this.pdfManager = pdfManager;
this.xref = xref; this.xref = xref;
this.handler = handler; this.handler = handler;
this.pageIndex = pageIndex; this.pageIndex = pageIndex;
this.uniquePrefix = uniquePrefix; this.idFactory = idFactory;
this.idCounters = idCounters;
this.fontCache = fontCache; this.fontCache = fontCache;
this.options = options || DefaultPartialEvaluatorOptions; this.options = options || DefaultPartialEvaluatorOptions;
} }
@ -391,8 +390,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// If there is no imageMask, create the PDFImage and a lot // If there is no imageMask, create the PDFImage and a lot
// of image processing can be done here. // of image processing can be done here.
var uniquePrefix = (this.uniquePrefix || ''); var objId = 'img_' + this.idFactory.createObjId();
var objId = 'img_' + uniquePrefix + (++this.idCounters.obj);
operatorList.addDependency(objId); operatorList.addDependency(objId);
args = [objId, w, h]; args = [objId, w, h];
@ -733,7 +731,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
this.fontCache.put(fontRef, fontCapability.promise); this.fontCache.put(fontRef, fontCapability.promise);
} else { } else {
if (!fontID) { if (!fontID) {
fontID = (this.uniquePrefix || 'F_') + (++this.idCounters.obj); fontID = this.idFactory.createObjId();
} }
this.fontCache.put('id_' + fontID, fontCapability.promise); this.fontCache.put('id_' + fontID, fontCapability.promise);
} }

View File

@ -17,21 +17,19 @@
(function (root, factory) { (function (root, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
define('pdfjs-test/unit/annotation_layer_spec', ['exports', define('pdfjs-test/unit/annotation_layer_spec', ['exports',
'pdfjs/core/primitives', 'pdfjs/core/annotation', 'pdfjs/core/primitives', 'pdfjs/core/annotation', 'pdfjs/core/stream',
'pdfjs/core/stream', 'pdfjs/core/parser', 'pdfjs/core/parser', 'pdfjs/shared/util', 'pdfjs/display/global'],
'pdfjs/shared/util', 'pdfjs/display/global'], factory); factory);
} else if (typeof exports !== 'undefined') { } else if (typeof exports !== 'undefined') {
factory(exports, require('../../src/core/primitives.js'), factory(exports, require('../../src/core/primitives.js'),
require('../../src/core/annotation.js'), require('../../src/core/annotation.js'),
require('../../src/core/stream.js'), require('../../src/core/stream.js'), require('../../src/core/parser.js'),
require('../../src/core/parser.js'), require('../../src/shared/util.js'),
require('../../src/shared/util.js'), require('../../src/display/global.js'));
require('../../src/display/global.js'));
} else { } else {
factory((root.pdfjsTestUnitAnnotationLayerSpec = {}), factory((root.pdfjsTestUnitAnnotationLayerSpec = {}),
root.pdfjsCorePrimitives, root.pdfjsCoreAnnotation, root.pdfjsCorePrimitives, root.pdfjsCoreAnnotation, root.pdfjsCoreStream,
root.pdfjsCoreStream, root.pdfjsCoreParser, root.pdfjsCoreParser, root.pdfjsSharedUtil, root.pdfjsDisplayGlobal);
root.pdfjsSharedUtil, root.pdfjsDisplayGlobal);
} }
}(this, function (exports, corePrimitives, coreAnnotation, coreStream, }(this, function (exports, corePrimitives, coreAnnotation, coreStream,
coreParser, sharedUtil, displayGlobal) { coreParser, sharedUtil, displayGlobal) {
@ -80,19 +78,34 @@ describe('Annotation layer', function() {
} }
PDFManagerMock.prototype = {}; PDFManagerMock.prototype = {};
var annotationFactory, pdfManagerMock; function IdFactoryMock(params) {
var uniquePrefix = params.prefix || 'p0_';
var idCounters = {
obj: params.startObjId || 0,
};
return {
createObjId: function () {
return uniquePrefix + (++idCounters.obj);
},
};
}
IdFactoryMock.prototype = {};
var annotationFactory, pdfManagerMock, idFactoryMock;
beforeAll(function (done) { beforeAll(function (done) {
annotationFactory = new AnnotationFactory(); annotationFactory = new AnnotationFactory();
pdfManagerMock = new PDFManagerMock({ pdfManagerMock = new PDFManagerMock({
docBaseUrl: null, docBaseUrl: null,
}); });
idFactoryMock = new IdFactoryMock({ });
done(); done();
}); });
afterAll(function () { afterAll(function () {
annotationFactory = null; annotationFactory = null;
pdfManagerMock = null; pdfManagerMock = null;
idFactoryMock = null;
}); });
describe('AnnotationFactory', function () { describe('AnnotationFactory', function () {
@ -107,7 +120,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -121,14 +134,15 @@ describe('Annotation layer', function() {
annotationDict.set('Subtype', Name.get('Link')); annotationDict.set('Subtype', Name.get('Link'));
var xref = new XRefMock(); var xref = new XRefMock();
var uniquePrefix = 'p0_', idCounters = { obj: 0, }; var idFactory = new IdFactoryMock({
prefix: 'p0_',
startObjId: 0,
});
var annotation1 = annotationFactory.create(xref, annotationDict, var annotation1 = annotationFactory.create(xref, annotationDict,
pdfManagerMock, pdfManagerMock, idFactory);
uniquePrefix, idCounters);
var annotation2 = annotationFactory.create(xref, annotationDict, var annotation2 = annotationFactory.create(xref, annotationDict,
pdfManagerMock, pdfManagerMock, idFactory);
uniquePrefix, idCounters);
var data1 = annotation1.data, data2 = annotation2.data; var data1 = annotation1.data, data2 = annotation2.data;
expect(data1.annotationType).toEqual(AnnotationType.LINK); expect(data1.annotationType).toEqual(AnnotationType.LINK);
expect(data2.annotationType).toEqual(AnnotationType.LINK); expect(data2.annotationType).toEqual(AnnotationType.LINK);
@ -147,7 +161,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toBeUndefined(); expect(data.annotationType).toBeUndefined();
}); });
@ -332,7 +346,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -360,7 +374,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -393,7 +407,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -423,7 +437,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -452,7 +466,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -484,7 +498,7 @@ describe('Annotation layer', function() {
}); });
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManager); pdfManager, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -513,7 +527,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -543,7 +557,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -583,7 +597,7 @@ describe('Annotation layer', function() {
}); });
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManager); pdfManager, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -617,7 +631,8 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock,
idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -665,7 +680,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -686,7 +701,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -709,7 +724,7 @@ describe('Annotation layer', function() {
]); ]);
var annotation = annotationFactory.create(xref, annotationRef, var annotation = annotationFactory.create(xref, annotationRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = annotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK); expect(data.annotationType).toEqual(AnnotationType.LINK);
@ -741,10 +756,11 @@ describe('Annotation layer', function() {
{ ref: widgetRef, data: widgetDict, } { ref: widgetRef, data: widgetDict, }
]); ]);
var widgetAnnotation = annotationFactory.create(xref, widgetRef, var annotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = widgetAnnotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual(''); expect(data.fieldName).toEqual('');
}); });
@ -757,10 +773,11 @@ describe('Annotation layer', function() {
{ ref: widgetRef, data: widgetDict, } { ref: widgetRef, data: widgetDict, }
]); ]);
var widgetAnnotation = annotationFactory.create(xref, widgetRef, var annotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = widgetAnnotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual('foo'); expect(data.fieldName).toEqual('foo');
}); });
@ -780,10 +797,11 @@ describe('Annotation layer', function() {
{ ref: widgetRef, data: widgetDict, } { ref: widgetRef, data: widgetDict, }
]); ]);
var widgetAnnotation = annotationFactory.create(xref, widgetRef, var annotation = annotationFactory.create(xref, widgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = widgetAnnotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldName).toEqual('foo.bar.baz'); expect(data.fieldName).toEqual('foo.bar.baz');
}); });
}); });
@ -811,13 +829,16 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
expect(textWidgetAnnotation.data.textAlignment).toEqual(null); var data = annotation.data;
expect(textWidgetAnnotation.data.maxLen).toEqual(null); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(textWidgetAnnotation.data.readOnly).toEqual(false);
expect(textWidgetAnnotation.data.multiLine).toEqual(false); expect(data.textAlignment).toEqual(null);
expect(textWidgetAnnotation.data.comb).toEqual(false); expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.multiLine).toEqual(false);
expect(data.comb).toEqual(false);
}); });
it('should not set invalid text alignment, maximum length and flags', it('should not set invalid text alignment, maximum length and flags',
@ -831,13 +852,16 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
expect(textWidgetAnnotation.data.textAlignment).toEqual(null); var data = annotation.data;
expect(textWidgetAnnotation.data.maxLen).toEqual(null); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(textWidgetAnnotation.data.readOnly).toEqual(false);
expect(textWidgetAnnotation.data.multiLine).toEqual(false); expect(data.textAlignment).toEqual(null);
expect(textWidgetAnnotation.data.comb).toEqual(false); expect(data.maxLen).toEqual(null);
expect(data.readOnly).toEqual(false);
expect(data.multiLine).toEqual(false);
expect(data.comb).toEqual(false);
}); });
it('should set valid text alignment, maximum length and flags', it('should set valid text alignment, maximum length and flags',
@ -852,12 +876,15 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
expect(textWidgetAnnotation.data.textAlignment).toEqual(1); var data = annotation.data;
expect(textWidgetAnnotation.data.maxLen).toEqual(20); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(textWidgetAnnotation.data.readOnly).toEqual(true);
expect(textWidgetAnnotation.data.multiLine).toEqual(true); expect(data.textAlignment).toEqual(1);
expect(data.maxLen).toEqual(20);
expect(data.readOnly).toEqual(true);
expect(data.multiLine).toEqual(true);
}); });
it('should reject comb fields without a maximum length', function() { it('should reject comb fields without a maximum length', function() {
@ -868,9 +895,12 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
expect(textWidgetAnnotation.data.comb).toEqual(false); var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.comb).toEqual(false);
}); });
it('should accept comb fields with a maximum length', function() { it('should accept comb fields with a maximum length', function() {
@ -882,9 +912,12 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
expect(textWidgetAnnotation.data.comb).toEqual(true); var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.comb).toEqual(true);
}); });
it('should only accept comb fields when the flags are valid', function() { it('should only accept comb fields when the flags are valid', function() {
@ -907,10 +940,14 @@ describe('Annotation layer', function() {
{ ref: textWidgetRef, data: textWidgetDict, } { ref: textWidgetRef, data: textWidgetDict, }
]); ]);
var textWidgetAnnotation = annotationFactory.create(xref, textWidgetRef, var annotation = annotationFactory.create(xref, textWidgetRef,
pdfManagerMock); pdfManagerMock,
idFactoryMock);
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
var valid = (invalidFieldFlags.length === 0); var valid = (invalidFieldFlags.length === 0);
expect(textWidgetAnnotation.data.comb).toEqual(valid); expect(data.comb).toEqual(valid);
// Remove the last invalid flag for the next iteration. // Remove the last invalid flag for the next iteration.
if (!valid) { if (!valid) {
@ -944,11 +981,14 @@ describe('Annotation layer', function() {
{ ref: buttonWidgetRef, data: buttonWidgetDict, } { ref: buttonWidgetRef, data: buttonWidgetDict, }
]); ]);
var buttonWidgetAnnotation = var annotation = annotationFactory.create(xref, buttonWidgetRef,
annotationFactory.create(xref, buttonWidgetRef); pdfManagerMock, idFactoryMock);
expect(buttonWidgetAnnotation.data.checkBox).toEqual(true); var data = annotation.data;
expect(buttonWidgetAnnotation.data.fieldValue).toEqual('1'); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(buttonWidgetAnnotation.data.radioButton).toEqual(false);
expect(data.checkBox).toEqual(true);
expect(data.fieldValue).toEqual('1');
expect(data.radioButton).toEqual(false);
}); });
it('should handle radio buttons', function() { it('should handle radio buttons', function() {
@ -970,12 +1010,15 @@ describe('Annotation layer', function() {
{ ref: buttonWidgetRef, data: buttonWidgetDict, } { ref: buttonWidgetRef, data: buttonWidgetDict, }
]); ]);
var buttonWidgetAnnotation = var annotation = annotationFactory.create(xref, buttonWidgetRef,
annotationFactory.create(xref, buttonWidgetRef); pdfManagerMock, idFactoryMock);
expect(buttonWidgetAnnotation.data.checkBox).toEqual(false); var data = annotation.data;
expect(buttonWidgetAnnotation.data.radioButton).toEqual(true); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(buttonWidgetAnnotation.data.fieldValue).toEqual('1');
expect(buttonWidgetAnnotation.data.buttonValue).toEqual('2'); expect(data.checkBox).toEqual(false);
expect(data.radioButton).toEqual(true);
expect(data.fieldValue).toEqual('1');
expect(data.buttonValue).toEqual('2');
}); });
}); });
@ -1001,11 +1044,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.options).toEqual([]); expect(data.options).toEqual([]);
}); });
@ -1036,11 +1079,11 @@ describe('Annotation layer', function() {
{ ref: optionOneRef, data: optionOneArr, }, { ref: optionOneRef, data: optionOneArr, },
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.options).toEqual(expected); expect(data.options).toEqual(expected);
}); });
@ -1068,11 +1111,11 @@ describe('Annotation layer', function() {
{ ref: optionBarRef, data: optionBarStr, } { ref: optionBarRef, data: optionBarStr, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.options).toEqual(expected); expect(data.options).toEqual(expected);
}); });
@ -1086,11 +1129,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldValue).toEqual(fieldValue); expect(data.fieldValue).toEqual(fieldValue);
}); });
@ -1104,11 +1147,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.fieldValue).toEqual([fieldValue]); expect(data.fieldValue).toEqual([fieldValue]);
}); });
@ -1118,11 +1161,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(false); expect(data.readOnly).toEqual(false);
expect(data.combo).toEqual(false); expect(data.combo).toEqual(false);
expect(data.multiSelect).toEqual(false); expect(data.multiSelect).toEqual(false);
@ -1136,11 +1179,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(false); expect(data.readOnly).toEqual(false);
expect(data.combo).toEqual(false); expect(data.combo).toEqual(false);
expect(data.multiSelect).toEqual(false); expect(data.multiSelect).toEqual(false);
@ -1156,11 +1199,11 @@ describe('Annotation layer', function() {
{ ref: choiceWidgetRef, data: choiceWidgetDict, } { ref: choiceWidgetRef, data: choiceWidgetDict, }
]); ]);
var choiceWidgetAnnotation = annotationFactory.create(xref, var annotation = annotationFactory.create(xref, choiceWidgetRef,
choiceWidgetRef, pdfManagerMock, idFactoryMock);
pdfManagerMock); var data = annotation.data;
var data = choiceWidgetAnnotation.data;
expect(data.annotationType).toEqual(AnnotationType.WIDGET); expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.readOnly).toEqual(true); expect(data.readOnly).toEqual(true);
expect(data.combo).toEqual(true); expect(data.combo).toEqual(true);
expect(data.multiSelect).toEqual(true); expect(data.multiSelect).toEqual(true);
@ -1217,15 +1260,15 @@ describe('Annotation layer', function() {
{ ref: popupRef, data: popupDict, } { ref: popupRef, data: popupDict, }
]); ]);
var popupAnnotation = annotationFactory.create(xref, popupRef, var annotation = annotationFactory.create(xref, popupRef,
pdfManagerMock); pdfManagerMock, idFactoryMock);
var data = popupAnnotation.data; var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.POPUP); expect(data.annotationType).toEqual(AnnotationType.POPUP);
// Should not modify the `annotationFlags` returned e.g. through the API. // Should not modify the `annotationFlags` returned e.g. through the API.
expect(data.annotationFlags).toEqual(25); expect(data.annotationFlags).toEqual(25);
// The Popup should inherit the `viewable` property of the parent. // The Popup should inherit the `viewable` property of the parent.
expect(popupAnnotation.viewable).toEqual(true); expect(annotation.viewable).toEqual(true);
}); });
}); });
}); });

View File

@ -3,6 +3,7 @@
"spec_files": [ "spec_files": [
"cff_parser_spec.js", "cff_parser_spec.js",
"crypto_spec.js", "crypto_spec.js",
"document_spec.js",
"dom_utils_spec.js", "dom_utils_spec.js",
"evaluator_spec.js", "evaluator_spec.js",
"fonts_spec.js", "fonts_spec.js",

View File

@ -0,0 +1,56 @@
/* Copyright 2017 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs-test/unit/document_spec', ['exports', 'pdfjs/core/document'],
factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('../../src/core/document.js'));
} else {
factory((root.pdfjsTestUnitDocumentSpec = {}),
root.pdfjsCoreDocument);
}
}(this, function (exports, coreDocument) {
var Page = coreDocument.Page;
describe('document', function () {
describe('Page', function () {
it('should create correct objId using the idFactory', function () {
var page1 = new Page(/* pdfManager = */ { }, /* xref = */ null,
/* pageIndex = */ 0,
/* pageDict = */ null, /* ref = */ null,
/* fontCache = */ null);
var page2 = new Page(/* pdfManager = */ { }, /* xref = */ null,
/* pageIndex = */ 1,
/* pageDict = */ null, /* ref = */ null,
/* fontCache = */ null);
var idFactory1 = page1.idFactory, idFactory2 = page2.idFactory;
expect(idFactory1.createObjId()).toEqual('p0_1');
expect(idFactory1.createObjId()).toEqual('p0_2');
expect(idFactory2.createObjId()).toEqual('p1_1');
expect(idFactory2.createObjId()).toEqual('p1_2');
expect(idFactory1.createObjId()).toEqual('p0_3');
expect(idFactory1.createObjId()).toEqual('p0_4');
});
});
});
}));

View File

@ -48,23 +48,22 @@ function initializePDFJS(callback) {
require(['pdfjs/display/global', 'pdfjs-test/unit/annotation_layer_spec', require(['pdfjs/display/global', 'pdfjs-test/unit/annotation_layer_spec',
'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/cff_parser_spec', 'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/cff_parser_spec',
'pdfjs-test/unit/cmap_spec', 'pdfjs-test/unit/crypto_spec', 'pdfjs-test/unit/cmap_spec', 'pdfjs-test/unit/crypto_spec',
'pdfjs-test/unit/dom_utils_spec', 'pdfjs-test/unit/evaluator_spec', 'pdfjs-test/unit/document_spec', 'pdfjs-test/unit/dom_utils_spec',
'pdfjs-test/unit/fonts_spec', 'pdfjs-test/unit/function_spec', 'pdfjs-test/unit/evaluator_spec', 'pdfjs-test/unit/fonts_spec',
'pdfjs-test/unit/metadata_spec', 'pdfjs-test/unit/murmurhash3_spec', 'pdfjs-test/unit/function_spec', 'pdfjs-test/unit/metadata_spec',
'pdfjs-test/unit/network_spec', 'pdfjs-test/unit/parser_spec', 'pdfjs-test/unit/murmurhash3_spec', 'pdfjs-test/unit/network_spec',
'pdfjs-test/unit/primitives_spec', 'pdfjs-test/unit/stream_spec', 'pdfjs-test/unit/parser_spec', 'pdfjs-test/unit/primitives_spec',
'pdfjs-test/unit/type1_parser_spec', 'pdfjs-test/unit/ui_utils_spec', 'pdfjs-test/unit/stream_spec', 'pdfjs-test/unit/type1_parser_spec',
'pdfjs-test/unit/unicode_spec', 'pdfjs-test/unit/util_spec'], 'pdfjs-test/unit/ui_utils_spec', 'pdfjs-test/unit/unicode_spec',
function (displayGlobal, testUnitAnnotationLayerSpec, 'pdfjs-test/unit/util_spec'],
testUnitApiSpec, testUnitCFFParserSpec, function (displayGlobal, testUnitAnnotationLayerSpec, testUnitApiSpec,
testUnitCMapSpec, testUnitCryptoSpec, testUnitCFFParserSpec, testUnitCMapSpec, testUnitCryptoSpec,
testUnitDOMUtilsSpec, testUnitEvaluatorSpec, testUnitDocumentSpec, testUnitDOMUtilsSpec, testUnitEvaluatorSpec,
testUnitFontsSpec, testUnitFunctionSpec, testUnitFontsSpec, testUnitFunctionSpec, testUnitMetadataSpec,
testUnitMetadataSpec, testUnitMurmurHash3Spec, testUnitMurmurHash3Spec, testUnitNetworkSpec, testUnitParserSpec,
testUnitNetworkSpec, testUnitParserSpec,
testUnitPrimitivesSpec, testUnitStreamSpec, testUnitPrimitivesSpec, testUnitStreamSpec,
testUnitType1ParserSpec, testUnitUiUtilsSpec, testUnitType1ParserSpec, testUnitUiUtilsSpec, testUnitUnicodeSpec,
testUnitUnicodeSpec, testUnitUtilSpec) { testUnitUtilSpec) {
// Configure the worker. // Configure the worker.
displayGlobal.PDFJS.workerSrc = '../../src/worker_loader.js'; displayGlobal.PDFJS.workerSrc = '../../src/worker_loader.js';