primitives_spec.js 13 KB

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