2
0

primitives_spec.js 12 KB

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