display_utils_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 _display_utils = require("../../display/display_utils.js");
  24. var _is_node = require("../../shared/is_node.js");
  25. describe("display_utils", function () {
  26. describe("DOMCanvasFactory", function () {
  27. let canvasFactory;
  28. beforeAll(function (done) {
  29. canvasFactory = new _display_utils.DOMCanvasFactory();
  30. done();
  31. });
  32. afterAll(function () {
  33. canvasFactory = null;
  34. });
  35. it("`create` should throw an error if the dimensions are invalid", function () {
  36. expect(function () {
  37. return canvasFactory.create(-1, 1);
  38. }).toThrow(new Error("Invalid canvas size"));
  39. expect(function () {
  40. return canvasFactory.create(1, -1);
  41. }).toThrow(new Error("Invalid canvas size"));
  42. });
  43. it("`create` should return a canvas if the dimensions are valid", function () {
  44. if (_is_node.isNodeJS) {
  45. pending("Document is not supported in Node.js.");
  46. }
  47. const {
  48. canvas,
  49. context
  50. } = canvasFactory.create(20, 40);
  51. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  52. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  53. expect(canvas.width).toBe(20);
  54. expect(canvas.height).toBe(40);
  55. });
  56. it("`reset` should throw an error if no canvas is provided", function () {
  57. const canvasAndContext = {
  58. canvas: null,
  59. context: null
  60. };
  61. expect(function () {
  62. return canvasFactory.reset(canvasAndContext, 20, 40);
  63. }).toThrow(new Error("Canvas is not specified"));
  64. });
  65. it("`reset` should throw an error if the dimensions are invalid", function () {
  66. const canvasAndContext = {
  67. canvas: "foo",
  68. context: "bar"
  69. };
  70. expect(function () {
  71. return canvasFactory.reset(canvasAndContext, -1, 1);
  72. }).toThrow(new Error("Invalid canvas size"));
  73. expect(function () {
  74. return canvasFactory.reset(canvasAndContext, 1, -1);
  75. }).toThrow(new Error("Invalid canvas size"));
  76. });
  77. it("`reset` should alter the canvas/context if the dimensions are valid", function () {
  78. if (_is_node.isNodeJS) {
  79. pending("Document is not supported in Node.js.");
  80. }
  81. const canvasAndContext = canvasFactory.create(20, 40);
  82. canvasFactory.reset(canvasAndContext, 60, 80);
  83. const {
  84. canvas,
  85. context
  86. } = canvasAndContext;
  87. expect(canvas instanceof HTMLCanvasElement).toBe(true);
  88. expect(context instanceof CanvasRenderingContext2D).toBe(true);
  89. expect(canvas.width).toBe(60);
  90. expect(canvas.height).toBe(80);
  91. });
  92. it("`destroy` should throw an error if no canvas is provided", function () {
  93. expect(function () {
  94. return canvasFactory.destroy({});
  95. }).toThrow(new Error("Canvas is not specified"));
  96. });
  97. it("`destroy` should clear the canvas/context", function () {
  98. if (_is_node.isNodeJS) {
  99. pending("Document is not supported in Node.js.");
  100. }
  101. const canvasAndContext = canvasFactory.create(20, 40);
  102. canvasFactory.destroy(canvasAndContext);
  103. const {
  104. canvas,
  105. context
  106. } = canvasAndContext;
  107. expect(canvas).toBe(null);
  108. expect(context).toBe(null);
  109. });
  110. });
  111. describe("DOMSVGFactory", function () {
  112. let svgFactory;
  113. beforeAll(function (done) {
  114. svgFactory = new _display_utils.DOMSVGFactory();
  115. done();
  116. });
  117. afterAll(function () {
  118. svgFactory = null;
  119. });
  120. it("`create` should throw an error if the dimensions are invalid", function () {
  121. expect(function () {
  122. return svgFactory.create(-1, 0);
  123. }).toThrow(new Error("Invalid SVG dimensions"));
  124. expect(function () {
  125. return svgFactory.create(0, -1);
  126. }).toThrow(new Error("Invalid SVG dimensions"));
  127. });
  128. it("`create` should return an SVG element if the dimensions are valid", function () {
  129. if (_is_node.isNodeJS) {
  130. pending("Document is not supported in Node.js.");
  131. }
  132. const svg = svgFactory.create(20, 40);
  133. expect(svg instanceof SVGSVGElement).toBe(true);
  134. expect(svg.getAttribute("version")).toBe("1.1");
  135. expect(svg.getAttribute("width")).toBe("20px");
  136. expect(svg.getAttribute("height")).toBe("40px");
  137. expect(svg.getAttribute("preserveAspectRatio")).toBe("none");
  138. expect(svg.getAttribute("viewBox")).toBe("0 0 20 40");
  139. });
  140. it("`createElement` should throw an error if the type is not a string", function () {
  141. expect(function () {
  142. return svgFactory.createElement(true);
  143. }).toThrow(new Error("Invalid SVG element type"));
  144. });
  145. it("`createElement` should return an SVG element if the type is valid", function () {
  146. if (_is_node.isNodeJS) {
  147. pending("Document is not supported in Node.js.");
  148. }
  149. const svg = svgFactory.createElement("svg:rect");
  150. expect(svg instanceof SVGRectElement).toBe(true);
  151. });
  152. });
  153. describe("getFilenameFromUrl", function () {
  154. it("should get the filename from an absolute URL", function () {
  155. const url = "https://server.org/filename.pdf";
  156. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  157. });
  158. it("should get the filename from a relative URL", function () {
  159. const url = "../../filename.pdf";
  160. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  161. });
  162. it("should get the filename from a URL with an anchor", function () {
  163. const url = "https://server.org/filename.pdf#foo";
  164. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  165. });
  166. it("should get the filename from a URL with query parameters", function () {
  167. const url = "https://server.org/filename.pdf?foo=bar";
  168. expect((0, _display_utils.getFilenameFromUrl)(url)).toEqual("filename.pdf");
  169. });
  170. });
  171. describe("isValidFetchUrl", function () {
  172. it("handles invalid Fetch URLs", function () {
  173. expect((0, _display_utils.isValidFetchUrl)(null)).toEqual(false);
  174. expect((0, _display_utils.isValidFetchUrl)(100)).toEqual(false);
  175. expect((0, _display_utils.isValidFetchUrl)("foo")).toEqual(false);
  176. expect((0, _display_utils.isValidFetchUrl)("/foo", 100)).toEqual(false);
  177. });
  178. it("handles relative Fetch URLs", function () {
  179. expect((0, _display_utils.isValidFetchUrl)("/foo", "file://www.example.com")).toEqual(false);
  180. expect((0, _display_utils.isValidFetchUrl)("/foo", "http://www.example.com")).toEqual(true);
  181. });
  182. it("handles unsupported Fetch protocols", function () {
  183. expect((0, _display_utils.isValidFetchUrl)("file://www.example.com")).toEqual(false);
  184. expect((0, _display_utils.isValidFetchUrl)("ftp://www.example.com")).toEqual(false);
  185. });
  186. it("handles supported Fetch protocols", function () {
  187. expect((0, _display_utils.isValidFetchUrl)("http://www.example.com")).toEqual(true);
  188. expect((0, _display_utils.isValidFetchUrl)("https://www.example.com")).toEqual(true);
  189. });
  190. });
  191. describe("PDFDateString", function () {
  192. describe("toDateObject", function () {
  193. it("converts PDF date strings to JavaScript `Date` objects", function () {
  194. const expectations = {
  195. undefined: null,
  196. null: null,
  197. 42: null,
  198. "2019": null,
  199. D2019: null,
  200. "D:": null,
  201. "D:201": null,
  202. "D:2019": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  203. "D:20190": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  204. "D:201900": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  205. "D:201913": new Date(Date.UTC(2019, 0, 1, 0, 0, 0)),
  206. "D:201902": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  207. "D:2019020": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  208. "D:20190200": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  209. "D:20190232": new Date(Date.UTC(2019, 1, 1, 0, 0, 0)),
  210. "D:20190203": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  211. "D:20190431": new Date(Date.UTC(2019, 4, 1, 0, 0, 0)),
  212. "D:201902030": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  213. "D:2019020300": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  214. "D:2019020324": new Date(Date.UTC(2019, 1, 3, 0, 0, 0)),
  215. "D:2019020304": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  216. "D:20190203040": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  217. "D:201902030400": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  218. "D:201902030460": new Date(Date.UTC(2019, 1, 3, 4, 0, 0)),
  219. "D:201902030405": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  220. "D:2019020304050": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  221. "D:20190203040500": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  222. "D:20190203040560": new Date(Date.UTC(2019, 1, 3, 4, 5, 0)),
  223. "D:20190203040506": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  224. "D:20190203040506F": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  225. "D:20190203040506Z": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  226. "D:20190203040506-": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  227. "D:20190203040506+": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  228. "D:20190203040506+'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  229. "D:20190203040506+0": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  230. "D:20190203040506+01": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  231. "D:20190203040506+00'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  232. "D:20190203040506+24'": new Date(Date.UTC(2019, 1, 3, 4, 5, 6)),
  233. "D:20190203040506+01'": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  234. "D:20190203040506+01'0": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  235. "D:20190203040506+01'00": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  236. "D:20190203040506+01'60": new Date(Date.UTC(2019, 1, 3, 3, 5, 6)),
  237. "D:20190203040506+0102": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  238. "D:20190203040506+01'02": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  239. "D:20190203040506+01'02'": new Date(Date.UTC(2019, 1, 3, 3, 3, 6)),
  240. "D:20190203040506+05'07": new Date(Date.UTC(2019, 1, 2, 22, 58, 6))
  241. };
  242. for (const [input, expectation] of Object.entries(expectations)) {
  243. const result = _display_utils.PDFDateString.toDateObject(input);
  244. if (result) {
  245. expect(result.getTime()).toEqual(expectation.getTime());
  246. } else {
  247. expect(result).toEqual(expectation);
  248. }
  249. }
  250. });
  251. });
  252. });
  253. });