UNPKG

12.5 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22'use strict';
23
24var _primitives = require('../../core/primitives');
25
26var _test_utils = require('./test_utils');
27
28describe('primitives', function () {
29 describe('Name', function () {
30 it('should retain the given name', function () {
31 var givenName = 'Font';
32 var name = _primitives.Name.get(givenName);
33 expect(name.name).toEqual(givenName);
34 });
35 it('should create only one object for a name and cache it', function () {
36 var firstFont = _primitives.Name.get('Font');
37 var secondFont = _primitives.Name.get('Font');
38 var firstSubtype = _primitives.Name.get('Subtype');
39 var secondSubtype = _primitives.Name.get('Subtype');
40 expect(firstFont).toBe(secondFont);
41 expect(firstSubtype).toBe(secondSubtype);
42 expect(firstFont).not.toBe(firstSubtype);
43 });
44 });
45 describe('Cmd', function () {
46 it('should retain the given cmd name', function () {
47 var givenCmd = 'BT';
48 var cmd = _primitives.Cmd.get(givenCmd);
49 expect(cmd.cmd).toEqual(givenCmd);
50 });
51 it('should create only one object for a command and cache it', function () {
52 var firstBT = _primitives.Cmd.get('BT');
53 var secondBT = _primitives.Cmd.get('BT');
54 var firstET = _primitives.Cmd.get('ET');
55 var secondET = _primitives.Cmd.get('ET');
56 expect(firstBT).toBe(secondBT);
57 expect(firstET).toBe(secondET);
58 expect(firstBT).not.toBe(firstET);
59 });
60 });
61 describe('Dict', function () {
62 var checkInvalidHasValues = function checkInvalidHasValues(dict) {
63 expect(dict.has()).toBeFalsy();
64 expect(dict.has('Prev')).toBeFalsy();
65 };
66 var checkInvalidKeyValues = function checkInvalidKeyValues(dict) {
67 expect(dict.get()).toBeUndefined();
68 expect(dict.get('Prev')).toBeUndefined();
69 expect(dict.get('Decode', 'D')).toBeUndefined();
70 expect(dict.get('FontFile', 'FontFile2', 'FontFile3')).toBeNull();
71 };
72 var emptyDict, dictWithSizeKey, dictWithManyKeys;
73 var storedSize = 42;
74 var testFontFile = 'file1';
75 var testFontFile2 = 'file2';
76 var testFontFile3 = 'file3';
77 beforeAll(function (done) {
78 emptyDict = new _primitives.Dict();
79 dictWithSizeKey = new _primitives.Dict();
80 dictWithSizeKey.set('Size', storedSize);
81 dictWithManyKeys = new _primitives.Dict();
82 dictWithManyKeys.set('FontFile', testFontFile);
83 dictWithManyKeys.set('FontFile2', testFontFile2);
84 dictWithManyKeys.set('FontFile3', testFontFile3);
85 done();
86 });
87 afterAll(function () {
88 emptyDict = dictWithSizeKey = dictWithManyKeys = null;
89 });
90 it('should return invalid values for unknown keys', function () {
91 checkInvalidHasValues(emptyDict);
92 checkInvalidKeyValues(emptyDict);
93 });
94 it('should return correct value for stored Size key', function () {
95 expect(dictWithSizeKey.has('Size')).toBeTruthy();
96 expect(dictWithSizeKey.get('Size')).toEqual(storedSize);
97 expect(dictWithSizeKey.get('Prev', 'Size')).toEqual(storedSize);
98 expect(dictWithSizeKey.get('Prev', 'Root', 'Size')).toEqual(storedSize);
99 });
100 it('should return invalid values for unknown keys when Size key is stored', function () {
101 checkInvalidHasValues(dictWithSizeKey);
102 checkInvalidKeyValues(dictWithSizeKey);
103 });
104 it('should return correct value for stored Size key with undefined value', function () {
105 var dict = new _primitives.Dict();
106 dict.set('Size');
107 expect(dict.has('Size')).toBeTruthy();
108 checkInvalidKeyValues(dict);
109 });
110 it('should return correct values for multiple stored keys', function () {
111 expect(dictWithManyKeys.has('FontFile')).toBeTruthy();
112 expect(dictWithManyKeys.has('FontFile2')).toBeTruthy();
113 expect(dictWithManyKeys.has('FontFile3')).toBeTruthy();
114 expect(dictWithManyKeys.get('FontFile3')).toEqual(testFontFile3);
115 expect(dictWithManyKeys.get('FontFile2', 'FontFile3')).toEqual(testFontFile2);
116 expect(dictWithManyKeys.get('FontFile', 'FontFile2', 'FontFile3')).toEqual(testFontFile);
117 });
118 it('should asynchronously fetch unknown keys', function (done) {
119 var keyPromises = [dictWithManyKeys.getAsync('Size'), dictWithSizeKey.getAsync('FontFile', 'FontFile2', 'FontFile3')];
120 Promise.all(keyPromises).then(function (values) {
121 expect(values[0]).toBeUndefined();
122 expect(values[1]).toBeNull();
123 done();
124 }).catch(function (reason) {
125 done.fail(reason);
126 });
127 });
128 it('should asynchronously fetch correct values for multiple stored keys', function (done) {
129 var keyPromises = [dictWithManyKeys.getAsync('FontFile3'), dictWithManyKeys.getAsync('FontFile2', 'FontFile3'), dictWithManyKeys.getAsync('FontFile', 'FontFile2', 'FontFile3')];
130 Promise.all(keyPromises).then(function (values) {
131 expect(values[0]).toEqual(testFontFile3);
132 expect(values[1]).toEqual(testFontFile2);
133 expect(values[2]).toEqual(testFontFile);
134 done();
135 }).catch(function (reason) {
136 done.fail(reason);
137 });
138 });
139 it('should callback for each stored key', function () {
140 var callbackSpy = jasmine.createSpy('spy on callback in dictionary');
141 dictWithManyKeys.forEach(callbackSpy);
142 expect(callbackSpy).toHaveBeenCalled();
143 var callbackSpyCalls = callbackSpy.calls;
144 expect(callbackSpyCalls.argsFor(0)).toEqual(['FontFile', testFontFile]);
145 expect(callbackSpyCalls.argsFor(1)).toEqual(['FontFile2', testFontFile2]);
146 expect(callbackSpyCalls.argsFor(2)).toEqual(['FontFile3', testFontFile3]);
147 expect(callbackSpyCalls.count()).toEqual(3);
148 });
149 it('should handle keys pointing to indirect objects, both sync and async', function (done) {
150 var fontRef = new _primitives.Ref(1, 0);
151 var xref = new _test_utils.XRefMock([{
152 ref: fontRef,
153 data: testFontFile
154 }]);
155 var fontDict = new _primitives.Dict(xref);
156 fontDict.set('FontFile', fontRef);
157 expect(fontDict.getRaw('FontFile')).toEqual(fontRef);
158 expect(fontDict.get('FontFile', 'FontFile2', 'FontFile3')).toEqual(testFontFile);
159 fontDict.getAsync('FontFile', 'FontFile2', 'FontFile3').then(function (value) {
160 expect(value).toEqual(testFontFile);
161 done();
162 }).catch(function (reason) {
163 done.fail(reason);
164 });
165 });
166 it('should handle arrays containing indirect objects', function () {
167 var minCoordRef = new _primitives.Ref(1, 0),
168 maxCoordRef = new _primitives.Ref(2, 0);
169 var minCoord = 0,
170 maxCoord = 1;
171 var xref = new _test_utils.XRefMock([{
172 ref: minCoordRef,
173 data: minCoord
174 }, {
175 ref: maxCoordRef,
176 data: maxCoord
177 }]);
178 var xObjectDict = new _primitives.Dict(xref);
179 xObjectDict.set('BBox', [minCoord, maxCoord, minCoordRef, maxCoordRef]);
180 expect(xObjectDict.get('BBox')).toEqual([minCoord, maxCoord, minCoordRef, maxCoordRef]);
181 expect(xObjectDict.getArray('BBox')).toEqual([minCoord, maxCoord, minCoord, maxCoord]);
182 });
183 it('should get all key names', function () {
184 var expectedKeys = ['FontFile', 'FontFile2', 'FontFile3'];
185 var keys = dictWithManyKeys.getKeys();
186 expect(keys.sort()).toEqual(expectedKeys);
187 });
188 it('should create only one object for Dict.empty', function () {
189 var firstDictEmpty = _primitives.Dict.empty;
190 var secondDictEmpty = _primitives.Dict.empty;
191 expect(firstDictEmpty).toBe(secondDictEmpty);
192 expect(firstDictEmpty).not.toBe(emptyDict);
193 });
194 it('should correctly merge dictionaries', function () {
195 var expectedKeys = ['FontFile', 'FontFile2', 'FontFile3', 'Size'];
196 var fontFileDict = new _primitives.Dict();
197 fontFileDict.set('FontFile', 'Type1 font file');
198 var mergedDict = _primitives.Dict.merge(null, [dictWithManyKeys, dictWithSizeKey, fontFileDict]);
199 var mergedKeys = mergedDict.getKeys();
200 expect(mergedKeys.sort()).toEqual(expectedKeys);
201 expect(mergedDict.get('FontFile')).toEqual(testFontFile);
202 });
203 });
204 describe('Ref', function () {
205 it('should retain the stored values', function () {
206 var storedNum = 4;
207 var storedGen = 2;
208 var ref = new _primitives.Ref(storedNum, storedGen);
209 expect(ref.num).toEqual(storedNum);
210 expect(ref.gen).toEqual(storedGen);
211 });
212 });
213 describe('RefSet', function () {
214 it('should have a stored value', function () {
215 var ref = new _primitives.Ref(4, 2);
216 var refset = new _primitives.RefSet();
217 refset.put(ref);
218 expect(refset.has(ref)).toBeTruthy();
219 });
220 it('should not have an unknown value', function () {
221 var ref = new _primitives.Ref(4, 2);
222 var refset = new _primitives.RefSet();
223 expect(refset.has(ref)).toBeFalsy();
224 refset.put(ref);
225 var anotherRef = new _primitives.Ref(2, 4);
226 expect(refset.has(anotherRef)).toBeFalsy();
227 });
228 });
229 describe('isName', function () {
230 it('handles non-names', function () {
231 var nonName = {};
232 expect((0, _primitives.isName)(nonName)).toEqual(false);
233 });
234 it('handles names', function () {
235 var name = _primitives.Name.get('Font');
236 expect((0, _primitives.isName)(name)).toEqual(true);
237 });
238 it('handles names with name check', function () {
239 var name = _primitives.Name.get('Font');
240 expect((0, _primitives.isName)(name, 'Font')).toEqual(true);
241 expect((0, _primitives.isName)(name, 'Subtype')).toEqual(false);
242 });
243 });
244 describe('isCmd', function () {
245 it('handles non-commands', function () {
246 var nonCmd = {};
247 expect((0, _primitives.isCmd)(nonCmd)).toEqual(false);
248 });
249 it('handles commands', function () {
250 var cmd = _primitives.Cmd.get('BT');
251 expect((0, _primitives.isCmd)(cmd)).toEqual(true);
252 });
253 it('handles commands with cmd check', function () {
254 var cmd = _primitives.Cmd.get('BT');
255 expect((0, _primitives.isCmd)(cmd, 'BT')).toEqual(true);
256 expect((0, _primitives.isCmd)(cmd, 'ET')).toEqual(false);
257 });
258 });
259 describe('isDict', function () {
260 it('handles non-dictionaries', function () {
261 var nonDict = {};
262 expect((0, _primitives.isDict)(nonDict)).toEqual(false);
263 });
264 it('handles empty dictionaries with type check', function () {
265 var dict = _primitives.Dict.empty;
266 expect((0, _primitives.isDict)(dict)).toEqual(true);
267 expect((0, _primitives.isDict)(dict, 'Page')).toEqual(false);
268 });
269 it('handles dictionaries with type check', function () {
270 var dict = new _primitives.Dict();
271 dict.set('Type', _primitives.Name.get('Page'));
272 expect((0, _primitives.isDict)(dict, 'Page')).toEqual(true);
273 expect((0, _primitives.isDict)(dict, 'Contents')).toEqual(false);
274 });
275 });
276 describe('isRef', function () {
277 it('handles non-refs', function () {
278 var nonRef = {};
279 expect((0, _primitives.isRef)(nonRef)).toEqual(false);
280 });
281 it('handles refs', function () {
282 var ref = new _primitives.Ref(1, 0);
283 expect((0, _primitives.isRef)(ref)).toEqual(true);
284 });
285 });
286 describe('isRefsEqual', function () {
287 it('should handle different Refs pointing to the same object', function () {
288 var ref1 = new _primitives.Ref(1, 0);
289 var ref2 = new _primitives.Ref(1, 0);
290 expect((0, _primitives.isRefsEqual)(ref1, ref2)).toEqual(true);
291 });
292 it('should handle Refs pointing to different objects', function () {
293 var ref1 = new _primitives.Ref(1, 0);
294 var ref2 = new _primitives.Ref(2, 0);
295 expect((0, _primitives.isRefsEqual)(ref1, ref2)).toEqual(false);
296 });
297 });
298});
\No newline at end of file