primitives_spec.js 12 KB

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