jasmine-boot.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. /*
  16. Copyright (c) 2008-2016 Pivotal Labs
  17. Permission is hereby granted, free of charge, to any person obtaining
  18. a copy of this software and associated documentation files (the
  19. "Software"), to deal in the Software without restriction, including
  20. without limitation the rights to use, copy, modify, merge, publish,
  21. distribute, sublicense, and/or sell copies of the Software, and to
  22. permit persons to whom the Software is furnished to do so, subject to
  23. the following conditions:
  24. The above copyright notice and this permission notice shall be
  25. included in all copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. /* globals jasmineRequire */
  35. // Modified jasmine's boot.js file to load PDF.js libraries async.
  36. "use strict";
  37. import { GlobalWorkerOptions } from "pdfjs/display/worker_options.js";
  38. import { isNodeJS } from "pdfjs/shared/is_node.js";
  39. import { isValidFetchUrl } from "pdfjs/display/display_utils.js";
  40. import { PDFFetchStream } from "pdfjs/display/fetch_stream.js";
  41. import { PDFNetworkStream } from "pdfjs/display/network.js";
  42. import { setPDFNetworkStreamFactory } from "pdfjs/display/api.js";
  43. import { TestReporter } from "./testreporter.js";
  44. async function initializePDFJS(callback) {
  45. await Promise.all(
  46. [
  47. "pdfjs-test/unit/annotation_spec.js",
  48. "pdfjs-test/unit/annotation_storage_spec.js",
  49. "pdfjs-test/unit/api_spec.js",
  50. "pdfjs-test/unit/bidi_spec.js",
  51. "pdfjs-test/unit/cff_parser_spec.js",
  52. "pdfjs-test/unit/cmap_spec.js",
  53. "pdfjs-test/unit/colorspace_spec.js",
  54. "pdfjs-test/unit/core_utils_spec.js",
  55. "pdfjs-test/unit/crypto_spec.js",
  56. "pdfjs-test/unit/custom_spec.js",
  57. "pdfjs-test/unit/default_appearance_spec.js",
  58. "pdfjs-test/unit/display_svg_spec.js",
  59. "pdfjs-test/unit/display_utils_spec.js",
  60. "pdfjs-test/unit/document_spec.js",
  61. "pdfjs-test/unit/editor_spec.js",
  62. "pdfjs-test/unit/encodings_spec.js",
  63. "pdfjs-test/unit/evaluator_spec.js",
  64. "pdfjs-test/unit/event_utils_spec.js",
  65. "pdfjs-test/unit/function_spec.js",
  66. "pdfjs-test/unit/fetch_stream_spec.js",
  67. "pdfjs-test/unit/message_handler_spec.js",
  68. "pdfjs-test/unit/metadata_spec.js",
  69. "pdfjs-test/unit/murmurhash3_spec.js",
  70. "pdfjs-test/unit/network_spec.js",
  71. "pdfjs-test/unit/network_utils_spec.js",
  72. "pdfjs-test/unit/parser_spec.js",
  73. "pdfjs-test/unit/pdf_find_controller_spec.js",
  74. "pdfjs-test/unit/pdf_find_utils_spec.js",
  75. "pdfjs-test/unit/pdf_history_spec.js",
  76. "pdfjs-test/unit/pdf_viewer_spec.js",
  77. "pdfjs-test/unit/primitives_spec.js",
  78. "pdfjs-test/unit/scripting_spec.js",
  79. "pdfjs-test/unit/stream_spec.js",
  80. "pdfjs-test/unit/struct_tree_spec.js",
  81. "pdfjs-test/unit/text_layer_spec.js",
  82. "pdfjs-test/unit/type1_parser_spec.js",
  83. "pdfjs-test/unit/ui_utils_spec.js",
  84. "pdfjs-test/unit/unicode_spec.js",
  85. "pdfjs-test/unit/util_spec.js",
  86. "pdfjs-test/unit/writer_spec.js",
  87. "pdfjs-test/unit/xfa_formcalc_spec.js",
  88. "pdfjs-test/unit/xfa_parser_spec.js",
  89. "pdfjs-test/unit/xfa_serialize_data_spec.js",
  90. "pdfjs-test/unit/xfa_tohtml_spec.js",
  91. "pdfjs-test/unit/xml_spec.js",
  92. ].map(function (moduleName) {
  93. // eslint-disable-next-line no-unsanitized/method
  94. return import(moduleName);
  95. })
  96. );
  97. if (isNodeJS) {
  98. throw new Error(
  99. "The `gulp unittest` command cannot be used in Node.js environments."
  100. );
  101. }
  102. // Set the network stream factory for the unit-tests.
  103. setPDFNetworkStreamFactory(params => {
  104. if (isValidFetchUrl(params.url)) {
  105. return new PDFFetchStream(params);
  106. }
  107. return new PDFNetworkStream(params);
  108. });
  109. // Configure the worker.
  110. GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";
  111. callback();
  112. }
  113. (function () {
  114. window.jasmine = jasmineRequire.core(jasmineRequire);
  115. jasmineRequire.html(jasmine);
  116. const env = jasmine.getEnv();
  117. const jasmineInterface = jasmineRequire.interface(jasmine, env);
  118. extend(window, jasmineInterface);
  119. // Runner Parameters
  120. const queryString = new jasmine.QueryString({
  121. getWindowLocation() {
  122. return window.location;
  123. },
  124. });
  125. const config = {
  126. failFast: queryString.getParam("failFast"),
  127. oneFailurePerSpec: queryString.getParam("oneFailurePerSpec"),
  128. hideDisabled: queryString.getParam("hideDisabled"),
  129. };
  130. const random = queryString.getParam("random");
  131. if (random !== undefined && random !== "") {
  132. config.random = random;
  133. }
  134. const seed = queryString.getParam("seed");
  135. if (seed) {
  136. config.seed = seed;
  137. }
  138. // Reporters
  139. const htmlReporter = new jasmine.HtmlReporter({
  140. env,
  141. navigateWithNewParam(key, value) {
  142. return queryString.navigateWithNewParam(key, value);
  143. },
  144. addToExistingQueryString(key, value) {
  145. return queryString.fullStringWithNewParam(key, value);
  146. },
  147. getContainer() {
  148. return document.body;
  149. },
  150. createElement() {
  151. return document.createElement(...arguments);
  152. },
  153. createTextNode() {
  154. return document.createTextNode(...arguments);
  155. },
  156. timer: new jasmine.Timer(),
  157. });
  158. env.addReporter(htmlReporter);
  159. if (queryString.getParam("browser")) {
  160. const testReporter = new TestReporter(queryString.getParam("browser"));
  161. env.addReporter(testReporter);
  162. }
  163. // Filter which specs will be run by matching the start of the full name
  164. // against the `spec` query param.
  165. const specFilter = new jasmine.HtmlSpecFilter({
  166. filterString() {
  167. return queryString.getParam("spec");
  168. },
  169. });
  170. config.specFilter = function (spec) {
  171. return specFilter.matches(spec.getFullName());
  172. };
  173. env.configure(config);
  174. // Sets longer timeout.
  175. jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
  176. function extend(destination, source) {
  177. for (const property in source) {
  178. destination[property] = source[property];
  179. }
  180. return destination;
  181. }
  182. function unitTestInit() {
  183. initializePDFJS(function () {
  184. htmlReporter.initialize();
  185. env.execute();
  186. });
  187. }
  188. if (
  189. document.readyState === "interactive" ||
  190. document.readyState === "complete"
  191. ) {
  192. unitTestInit();
  193. } else {
  194. document.addEventListener("DOMContentLoaded", unitTestInit, true);
  195. }
  196. })();