primitives_spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.js");
  24. var _test_utils = require("./test_utils.js");
  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 (dict) {
  60. expect(dict.has()).toBeFalsy();
  61. expect(dict.has("Prev")).toBeFalsy();
  62. };
  63. var checkInvalidKeyValues = function (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")).toBeUndefined();
  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 not accept to set a key with an undefined value", function () {
  102. const dict = new _primitives.Dict();
  103. expect(function () {
  104. dict.set("Size");
  105. }).toThrow(new Error('Dict.set: The "value" cannot be undefined.'));
  106. expect(dict.has("Size")).toBeFalsy();
  107. checkInvalidKeyValues(dict);
  108. });
  109. it("should return correct values for multiple stored keys", function () {
  110. expect(dictWithManyKeys.has("FontFile")).toBeTruthy();
  111. expect(dictWithManyKeys.has("FontFile2")).toBeTruthy();
  112. expect(dictWithManyKeys.has("FontFile3")).toBeTruthy();
  113. expect(dictWithManyKeys.get("FontFile3")).toEqual(testFontFile3);
  114. expect(dictWithManyKeys.get("FontFile2", "FontFile3")).toEqual(testFontFile2);
  115. expect(dictWithManyKeys.get("FontFile", "FontFile2", "FontFile3")).toEqual(testFontFile);
  116. });
  117. it("should asynchronously fetch unknown keys", function (done) {
  118. var keyPromises = [dictWithManyKeys.getAsync("Size"), dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3")];
  119. Promise.all(keyPromises).then(function (values) {
  120. expect(values[0]).toBeUndefined();
  121. expect(values[1]).toBeUndefined();
  122. done();
  123. }).catch(function (reason) {
  124. done.fail(reason);
  125. });
  126. });
  127. it("should asynchronously fetch correct values for multiple stored keys", function (done) {
  128. var keyPromises = [dictWithManyKeys.getAsync("FontFile3"), dictWithManyKeys.getAsync("FontFile2", "FontFile3"), dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3")];
  129. Promise.all(keyPromises).then(function (values) {
  130. expect(values[0]).toEqual(testFontFile3);
  131. expect(values[1]).toEqual(testFontFile2);
  132. expect(values[2]).toEqual(testFontFile);
  133. done();
  134. }).catch(function (reason) {
  135. done.fail(reason);
  136. });
  137. });
  138. it("should callback for each stored key", function () {
  139. var callbackSpy = jasmine.createSpy("spy on callback in dictionary");
  140. dictWithManyKeys.forEach(callbackSpy);
  141. expect(callbackSpy).toHaveBeenCalled();
  142. var callbackSpyCalls = callbackSpy.calls;
  143. expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
  144. expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
  145. expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
  146. expect(callbackSpyCalls.count()).toEqual(3);
  147. });
  148. it("should handle keys pointing to indirect objects, both sync and async", function (done) {
  149. var fontRef = _primitives.Ref.get(1, 0);
  150. var xref = new _test_utils.XRefMock([{
  151. ref: fontRef,
  152. data: testFontFile
  153. }]);
  154. var fontDict = new _primitives.Dict(xref);
  155. fontDict.set("FontFile", fontRef);
  156. expect(fontDict.getRaw("FontFile")).toEqual(fontRef);
  157. expect(fontDict.get("FontFile", "FontFile2", "FontFile3")).toEqual(testFontFile);
  158. fontDict.getAsync("FontFile", "FontFile2", "FontFile3").then(function (value) {
  159. expect(value).toEqual(testFontFile);
  160. done();
  161. }).catch(function (reason) {
  162. done.fail(reason);
  163. });
  164. });
  165. it("should handle arrays containing indirect objects", function () {
  166. var minCoordRef = _primitives.Ref.get(1, 0),
  167. maxCoordRef = _primitives.Ref.get(2, 0);
  168. var minCoord = 0,
  169. maxCoord = 1;
  170. var xref = new _test_utils.XRefMock([{
  171. ref: minCoordRef,
  172. data: minCoord
  173. }, {
  174. ref: maxCoordRef,
  175. data: maxCoord
  176. }]);
  177. var xObjectDict = new _primitives.Dict(xref);
  178. xObjectDict.set("BBox", [minCoord, maxCoord, minCoordRef, maxCoordRef]);
  179. expect(xObjectDict.get("BBox")).toEqual([minCoord, maxCoord, minCoordRef, maxCoordRef]);
  180. expect(xObjectDict.getArray("BBox")).toEqual([minCoord, maxCoord, minCoord, maxCoord]);
  181. });
  182. it("should get all key names", function () {
  183. var expectedKeys = ["FontFile", "FontFile2", "FontFile3"];
  184. var keys = dictWithManyKeys.getKeys();
  185. expect(keys.sort()).toEqual(expectedKeys);
  186. });
  187. it("should create only one object for Dict.empty", function () {
  188. var firstDictEmpty = _primitives.Dict.empty;
  189. var secondDictEmpty = _primitives.Dict.empty;
  190. expect(firstDictEmpty).toBe(secondDictEmpty);
  191. expect(firstDictEmpty).not.toBe(emptyDict);
  192. });
  193. it("should correctly merge dictionaries", function () {
  194. var expectedKeys = ["FontFile", "FontFile2", "FontFile3", "Size"];
  195. var fontFileDict = new _primitives.Dict();
  196. fontFileDict.set("FontFile", "Type1 font file");
  197. var mergedDict = _primitives.Dict.merge(null, [dictWithManyKeys, dictWithSizeKey, fontFileDict]);
  198. var mergedKeys = mergedDict.getKeys();
  199. expect(mergedKeys.sort()).toEqual(expectedKeys);
  200. expect(mergedDict.get("FontFile")).toEqual(testFontFile);
  201. });
  202. });
  203. describe("Ref", function () {
  204. it("should retain the stored values", function () {
  205. var storedNum = 4;
  206. var storedGen = 2;
  207. var ref = _primitives.Ref.get(storedNum, storedGen);
  208. expect(ref.num).toEqual(storedNum);
  209. expect(ref.gen).toEqual(storedGen);
  210. });
  211. });
  212. describe("RefSet", function () {
  213. it("should have a stored value", function () {
  214. var ref = _primitives.Ref.get(4, 2);
  215. var refset = new _primitives.RefSet();
  216. refset.put(ref);
  217. expect(refset.has(ref)).toBeTruthy();
  218. });
  219. it("should not have an unknown value", function () {
  220. var ref = _primitives.Ref.get(4, 2);
  221. var refset = new _primitives.RefSet();
  222. expect(refset.has(ref)).toBeFalsy();
  223. refset.put(ref);
  224. var anotherRef = _primitives.Ref.get(2, 4);
  225. expect(refset.has(anotherRef)).toBeFalsy();
  226. });
  227. });
  228. describe("isName", function () {
  229. it("handles non-names", function () {
  230. var nonName = {};
  231. expect((0, _primitives.isName)(nonName)).toEqual(false);
  232. });
  233. it("handles names", function () {
  234. var name = _primitives.Name.get("Font");
  235. expect((0, _primitives.isName)(name)).toEqual(true);
  236. });
  237. it("handles names with name check", function () {
  238. var name = _primitives.Name.get("Font");
  239. expect((0, _primitives.isName)(name, "Font")).toEqual(true);
  240. expect((0, _primitives.isName)(name, "Subtype")).toEqual(false);
  241. });
  242. });
  243. describe("isCmd", function () {
  244. it("handles non-commands", function () {
  245. var nonCmd = {};
  246. expect((0, _primitives.isCmd)(nonCmd)).toEqual(false);
  247. });
  248. it("handles commands", function () {
  249. var cmd = _primitives.Cmd.get("BT");
  250. expect((0, _primitives.isCmd)(cmd)).toEqual(true);
  251. });
  252. it("handles commands with cmd check", function () {
  253. var cmd = _primitives.Cmd.get("BT");
  254. expect((0, _primitives.isCmd)(cmd, "BT")).toEqual(true);
  255. expect((0, _primitives.isCmd)(cmd, "ET")).toEqual(false);
  256. });
  257. });
  258. describe("isDict", function () {
  259. it("handles non-dictionaries", function () {
  260. var nonDict = {};
  261. expect((0, _primitives.isDict)(nonDict)).toEqual(false);
  262. });
  263. it("handles empty dictionaries with type check", function () {
  264. var dict = _primitives.Dict.empty;
  265. expect((0, _primitives.isDict)(dict)).toEqual(true);
  266. expect((0, _primitives.isDict)(dict, "Page")).toEqual(false);
  267. });
  268. it("handles dictionaries with type check", function () {
  269. var dict = new _primitives.Dict();
  270. dict.set("Type", _primitives.Name.get("Page"));
  271. expect((0, _primitives.isDict)(dict, "Page")).toEqual(true);
  272. expect((0, _primitives.isDict)(dict, "Contents")).toEqual(false);
  273. });
  274. });
  275. describe("isRef", function () {
  276. it("handles non-refs", function () {
  277. var nonRef = {};
  278. expect((0, _primitives.isRef)(nonRef)).toEqual(false);
  279. });
  280. it("handles refs", function () {
  281. var ref = _primitives.Ref.get(1, 0);
  282. expect((0, _primitives.isRef)(ref)).toEqual(true);
  283. });
  284. });
  285. describe("isRefsEqual", function () {
  286. it("should handle Refs pointing to the same object", function () {
  287. var ref1 = _primitives.Ref.get(1, 0);
  288. var ref2 = _primitives.Ref.get(1, 0);
  289. expect((0, _primitives.isRefsEqual)(ref1, ref2)).toEqual(true);
  290. });
  291. it("should handle Refs pointing to different objects", function () {
  292. var ref1 = _primitives.Ref.get(1, 0);
  293. var ref2 = _primitives.Ref.get(2, 0);
  294. expect((0, _primitives.isRefsEqual)(ref1, ref2)).toEqual(false);
  295. });
  296. });
  297. });