primitives_spec.js 12 KB

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