Skip to content

Commit

Permalink
Merge pull request #7951 from Snuffleupagus/FileAttachmentAnnotation-…
Browse files Browse the repository at this point in the history
…simplified-unittest

Rename `annotation_layer_spec.js` to `annotation_spec.js` to better describe what is actually tested, and simplify the `FileAttachmentAnnotation` unit-test to avoid having to use the entire API in the test
  • Loading branch information
yurydelendik authored Jan 12, 2017
2 parents 1af35a6 + e88c9c7 commit c0d7029
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
90 changes: 55 additions & 35 deletions test/unit/annotation_layer_spec.js → test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,21 @@

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs-test/unit/annotation_layer_spec', ['exports',
define('pdfjs-test/unit/annotation_spec', ['exports',
'pdfjs/core/primitives', 'pdfjs/core/annotation', 'pdfjs/core/stream',
'pdfjs/core/parser', 'pdfjs/shared/util', 'pdfjs/display/global'],
factory);
'pdfjs/core/parser', 'pdfjs/shared/util'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('../../src/core/primitives.js'),
require('../../src/core/annotation.js'),
require('../../src/core/stream.js'), require('../../src/core/parser.js'),
require('../../src/shared/util.js'),
require('../../src/display/global.js'));
require('../../src/shared/util.js'));
} else {
factory((root.pdfjsTestUnitAnnotationLayerSpec = {}),
factory((root.pdfjsTestUnitAnnotationSpec = {}),
root.pdfjsCorePrimitives, root.pdfjsCoreAnnotation, root.pdfjsCoreStream,
root.pdfjsCoreParser, root.pdfjsSharedUtil, root.pdfjsDisplayGlobal);
root.pdfjsCoreParser, root.pdfjsSharedUtil);
}
}(this, function (exports, corePrimitives, coreAnnotation, coreStream,
coreParser, sharedUtil, displayGlobal) {
coreParser, sharedUtil) {

var Annotation = coreAnnotation.Annotation;
var AnnotationBorderStyle = coreAnnotation.AnnotationBorderStyle;
Expand All @@ -44,15 +42,14 @@ var Dict = corePrimitives.Dict;
var Name = corePrimitives.Name;
var Ref = corePrimitives.Ref;
var StringStream = coreStream.StringStream;
var PDFJS = displayGlobal.PDFJS;
var AnnotationType = sharedUtil.AnnotationType;
var AnnotationFlag = sharedUtil.AnnotationFlag;
var AnnotationBorderStyleType = sharedUtil.AnnotationBorderStyleType;
var AnnotationFieldFlag = sharedUtil.AnnotationFieldFlag;
var stringToBytes = sharedUtil.stringToBytes;
var stringToUTF8String = sharedUtil.stringToUTF8String;

describe('Annotation layer', function() {
describe('annotation', function() {
function XRefMock(array) {
this.map = Object.create(null);
for (var elem in array) {
Expand Down Expand Up @@ -1211,33 +1208,56 @@ describe('Annotation layer', function() {
});

describe('FileAttachmentAnnotation', function() {
var loadingTask;
var annotations;

beforeEach(function(done) {
var pdfUrl = new URL('../pdfs/annotation-fileattachment.pdf',
window.location).href;
loadingTask = PDFJS.getDocument(pdfUrl);
loadingTask.promise.then(function(pdfDocument) {
return pdfDocument.getPage(1).then(function(pdfPage) {
return pdfPage.getAnnotations().then(function (pdfAnnotations) {
annotations = pdfAnnotations;
done();
});
});
}).catch(function (reason) {
done.fail(reason);
});
});
it('should correctly parse a file attachment', function() {
var fileStream = new StringStream(
'<<\n' +
'/Type /EmbeddedFile\n' +
'/Subtype /text#2Fplain\n' +
'>>\n' +
'stream\n' +
'Test attachment' +
'endstream\n'
);
var lexer = new Lexer(fileStream);
var parser = new Parser(lexer, /* allowStreams = */ true);

afterEach(function() {
loadingTask.destroy();
});
var fileStreamRef = new Ref(18, 0);
var fileStreamDict = parser.getObj();

it('should correctly parse a file attachment', function() {
var annotation = annotations[0];
expect(annotation.file.filename).toEqual('Test.txt');
expect(annotation.file.content).toEqual(stringToBytes('Test attachment'));
var embeddedFileDict = new Dict();
embeddedFileDict.set('F', fileStreamRef);

var fileSpecRef = new Ref(19, 0);
var fileSpecDict = new Dict();
fileSpecDict.set('Type', Name.get('Filespec'));
fileSpecDict.set('Desc', '');
fileSpecDict.set('EF', embeddedFileDict);
fileSpecDict.set('UF', 'Test.txt');

var fileAttachmentRef = new Ref(20, 0);
var fileAttachmentDict = new Dict();
fileAttachmentDict.set('Type', Name.get('Annot'));
fileAttachmentDict.set('Subtype', Name.get('FileAttachment'));
fileAttachmentDict.set('FS', fileSpecRef);
fileAttachmentDict.set('T', 'Topic');
fileAttachmentDict.set('Contents', 'Test.txt');

var xref = new XRefMock([
{ ref: fileStreamRef, data: fileStreamDict, },
{ ref: fileSpecRef, data: fileSpecDict, },
{ ref: fileAttachmentRef, data: fileAttachmentDict, }
]);
embeddedFileDict.assignXref(xref);
fileSpecDict.assignXref(xref);
fileAttachmentDict.assignXref(xref);

var annotation = annotationFactory.create(xref, fileAttachmentRef,
pdfManagerMock, idFactoryMock);
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.FILEATTACHMENT);

expect(data.file.filename).toEqual('Test.txt');
expect(data.file.content).toEqual(stringToBytes('Test attachment'));
});
});

Expand Down
1 change: 1 addition & 0 deletions test/unit/clitests.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"spec_dir": "test/unit",
"spec_files": [
"annotation_spec.js",
"bidi_spec.js",
"cff_parser_spec.js",
"crypto_spec.js",
Expand Down
4 changes: 2 additions & 2 deletions test/unit/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var pdfjsLibs;
function initializePDFJS(callback) {
require.config({paths: {'pdfjs': '../../src', 'pdfjs-web': '../../web',
'pdfjs-test': '..'}});
require(['pdfjs/display/global', 'pdfjs-test/unit/annotation_layer_spec',
require(['pdfjs/display/global', 'pdfjs-test/unit/annotation_spec',
'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/bidi_spec',
'pdfjs-test/unit/cff_parser_spec', 'pdfjs-test/unit/cmap_spec',
'pdfjs-test/unit/crypto_spec', 'pdfjs-test/unit/document_spec',
Expand All @@ -57,7 +57,7 @@ function initializePDFJS(callback) {
'pdfjs-test/unit/type1_parser_spec',
'pdfjs-test/unit/ui_utils_spec', 'pdfjs-test/unit/unicode_spec',
'pdfjs-test/unit/util_spec'],
function (displayGlobal, testUnitAnnotationLayerSpec, testUnitApiSpec,
function (displayGlobal, testUnitAnnotationSpec, testUnitApiSpec,
testUnitBidiSpec, testUnitCFFParserSpec, testUnitCMapSpec,
testUnitCryptoSpec, testUnitDocumentSpec, testUnitDOMUtilsSpec,
testUnitEvaluatorSpec, testUnitFontsSpec, testUnitFunctionSpec,
Expand Down

0 comments on commit c0d7029

Please sign in to comment.