pdf_find_controller_spec.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _test_utils = require("./test_utils.js");
  24. var _event_utils = require("../../web/event_utils.js");
  25. var _api = require("../../display/api.js");
  26. var _pdf_find_controller = require("../../web/pdf_find_controller.js");
  27. var _pdf_link_service = require("../../web/pdf_link_service.js");
  28. const tracemonkeyFileName = "tracemonkey.pdf";
  29. class MockLinkService extends _pdf_link_service.SimpleLinkService {
  30. constructor() {
  31. super();
  32. this._page = 1;
  33. this._pdfDocument = null;
  34. }
  35. setDocument(pdfDocument) {
  36. this._pdfDocument = pdfDocument;
  37. }
  38. get pagesCount() {
  39. return this._pdfDocument.numPages;
  40. }
  41. get page() {
  42. return this._page;
  43. }
  44. set page(value) {
  45. this._page = value;
  46. }
  47. }
  48. async function initPdfFindController(filename) {
  49. const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename || tracemonkeyFileName));
  50. const pdfDocument = await loadingTask.promise;
  51. const eventBus = new _event_utils.EventBus();
  52. const linkService = new MockLinkService();
  53. linkService.setDocument(pdfDocument);
  54. const pdfFindController = new _pdf_find_controller.PDFFindController({
  55. linkService,
  56. eventBus
  57. });
  58. pdfFindController.setDocument(pdfDocument);
  59. return {
  60. eventBus,
  61. pdfFindController
  62. };
  63. }
  64. function testSearch({
  65. eventBus,
  66. pdfFindController,
  67. state,
  68. matchesPerPage,
  69. selectedMatch,
  70. pageMatches = null,
  71. pageMatchesLength = null
  72. }) {
  73. return new Promise(function (resolve) {
  74. const eventState = Object.assign(Object.create(null), {
  75. source: this,
  76. type: "",
  77. query: null,
  78. caseSensitive: false,
  79. entireWord: false,
  80. phraseSearch: true,
  81. findPrevious: false
  82. }, state);
  83. eventBus.dispatch("find", eventState);
  84. let totalPages = matchesPerPage.length;
  85. for (let i = totalPages - 1; i >= 0; i--) {
  86. if (matchesPerPage[i] > 0) {
  87. totalPages = i + 1;
  88. break;
  89. }
  90. }
  91. const totalMatches = matchesPerPage.reduce((a, b) => {
  92. return a + b;
  93. });
  94. eventBus.on("updatefindmatchescount", function onUpdateFindMatchesCount(evt) {
  95. if (pdfFindController.pageMatches.length !== totalPages) {
  96. return;
  97. }
  98. eventBus.off("updatefindmatchescount", onUpdateFindMatchesCount);
  99. expect(evt.matchesCount.total).toBe(totalMatches);
  100. for (let i = 0; i < totalPages; i++) {
  101. expect(pdfFindController.pageMatches[i].length).toEqual(matchesPerPage[i]);
  102. }
  103. expect(pdfFindController.selected.pageIdx).toEqual(selectedMatch.pageIndex);
  104. expect(pdfFindController.selected.matchIdx).toEqual(selectedMatch.matchIndex);
  105. if (pageMatches) {
  106. expect(pdfFindController.pageMatches).toEqual(pageMatches);
  107. expect(pdfFindController.pageMatchesLength).toEqual(pageMatchesLength);
  108. }
  109. resolve();
  110. });
  111. });
  112. }
  113. describe("pdf_find_controller", function () {
  114. it("performs a normal search", async function () {
  115. const {
  116. eventBus,
  117. pdfFindController
  118. } = await initPdfFindController();
  119. await testSearch({
  120. eventBus,
  121. pdfFindController,
  122. state: {
  123. query: "Dynamic"
  124. },
  125. matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
  126. selectedMatch: {
  127. pageIndex: 0,
  128. matchIndex: 0
  129. }
  130. });
  131. });
  132. it("performs a normal search and finds the previous result", async function () {
  133. const {
  134. eventBus,
  135. pdfFindController
  136. } = await initPdfFindController();
  137. await testSearch({
  138. eventBus,
  139. pdfFindController,
  140. state: {
  141. query: "conference",
  142. findPrevious: true
  143. },
  144. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
  145. selectedMatch: {
  146. pageIndex: 13,
  147. matchIndex: 4
  148. }
  149. });
  150. });
  151. it("performs a case sensitive search", async function () {
  152. const {
  153. eventBus,
  154. pdfFindController
  155. } = await initPdfFindController();
  156. await testSearch({
  157. eventBus,
  158. pdfFindController,
  159. state: {
  160. query: "Dynamic",
  161. caseSensitive: true
  162. },
  163. matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
  164. selectedMatch: {
  165. pageIndex: 0,
  166. matchIndex: 0
  167. }
  168. });
  169. });
  170. it("performs an entire word search", async function () {
  171. const {
  172. eventBus,
  173. pdfFindController
  174. } = await initPdfFindController();
  175. await testSearch({
  176. eventBus,
  177. pdfFindController,
  178. state: {
  179. query: "Government",
  180. entireWord: true
  181. },
  182. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  183. selectedMatch: {
  184. pageIndex: 12,
  185. matchIndex: 0
  186. }
  187. });
  188. });
  189. it("performs a multiple term (no phrase) search", async function () {
  190. const {
  191. eventBus,
  192. pdfFindController
  193. } = await initPdfFindController();
  194. await testSearch({
  195. eventBus,
  196. pdfFindController,
  197. state: {
  198. query: "alternate solution",
  199. phraseSearch: false
  200. },
  201. matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
  202. selectedMatch: {
  203. pageIndex: 5,
  204. matchIndex: 0
  205. }
  206. });
  207. });
  208. it("performs a normal search, where the text is normalized", async function () {
  209. const {
  210. eventBus,
  211. pdfFindController
  212. } = await initPdfFindController("fraction-highlight.pdf");
  213. await testSearch({
  214. eventBus,
  215. pdfFindController,
  216. state: {
  217. query: "fraction"
  218. },
  219. matchesPerPage: [3],
  220. selectedMatch: {
  221. pageIndex: 0,
  222. matchIndex: 0
  223. },
  224. pageMatches: [[19, 46, 62]],
  225. pageMatchesLength: [[8, 8, 8]]
  226. });
  227. });
  228. });