pdf_find_controller_spec.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 _ui_utils = require("../../web/ui_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 _ui_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. parameters,
  68. matchesPerPage,
  69. selectedMatch,
  70. pageMatches = null,
  71. pageMatchesLength = null
  72. }) {
  73. return new Promise(function (resolve) {
  74. pdfFindController.executeCommand("find", parameters);
  75. let totalPages = matchesPerPage.length;
  76. for (let i = totalPages - 1; i >= 0; i--) {
  77. if (matchesPerPage[i] > 0) {
  78. totalPages = i + 1;
  79. break;
  80. }
  81. }
  82. const totalMatches = matchesPerPage.reduce((a, b) => {
  83. return a + b;
  84. });
  85. eventBus.on("updatefindmatchescount", function onUpdateFindMatchesCount(evt) {
  86. if (pdfFindController.pageMatches.length !== totalPages) {
  87. return;
  88. }
  89. eventBus.off("updatefindmatchescount", onUpdateFindMatchesCount);
  90. expect(evt.matchesCount.total).toBe(totalMatches);
  91. for (let i = 0; i < totalPages; i++) {
  92. expect(pdfFindController.pageMatches[i].length).toEqual(matchesPerPage[i]);
  93. }
  94. expect(pdfFindController.selected.pageIdx).toEqual(selectedMatch.pageIndex);
  95. expect(pdfFindController.selected.matchIdx).toEqual(selectedMatch.matchIndex);
  96. if (pageMatches) {
  97. expect(pdfFindController.pageMatches).toEqual(pageMatches);
  98. expect(pdfFindController.pageMatchesLength).toEqual(pageMatchesLength);
  99. }
  100. resolve();
  101. });
  102. });
  103. }
  104. describe("pdf_find_controller", function () {
  105. it("performs a normal search", async function () {
  106. const {
  107. eventBus,
  108. pdfFindController
  109. } = await initPdfFindController();
  110. await testSearch({
  111. eventBus,
  112. pdfFindController,
  113. parameters: {
  114. query: "Dynamic",
  115. caseSensitive: false,
  116. entireWord: false,
  117. phraseSearch: true,
  118. findPrevious: false
  119. },
  120. matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
  121. selectedMatch: {
  122. pageIndex: 0,
  123. matchIndex: 0
  124. }
  125. });
  126. });
  127. it("performs a normal search and finds the previous result", async function () {
  128. const {
  129. eventBus,
  130. pdfFindController
  131. } = await initPdfFindController();
  132. await testSearch({
  133. eventBus,
  134. pdfFindController,
  135. parameters: {
  136. query: "conference",
  137. caseSensitive: false,
  138. entireWord: false,
  139. phraseSearch: true,
  140. findPrevious: true
  141. },
  142. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
  143. selectedMatch: {
  144. pageIndex: 13,
  145. matchIndex: 4
  146. }
  147. });
  148. });
  149. it("performs a case sensitive search", async function () {
  150. const {
  151. eventBus,
  152. pdfFindController
  153. } = await initPdfFindController();
  154. await testSearch({
  155. eventBus,
  156. pdfFindController,
  157. parameters: {
  158. query: "Dynamic",
  159. caseSensitive: true,
  160. entireWord: false,
  161. phraseSearch: true,
  162. findPrevious: false
  163. },
  164. matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
  165. selectedMatch: {
  166. pageIndex: 0,
  167. matchIndex: 0
  168. }
  169. });
  170. });
  171. it("performs an entire word search", async function () {
  172. const {
  173. eventBus,
  174. pdfFindController
  175. } = await initPdfFindController();
  176. await testSearch({
  177. eventBus,
  178. pdfFindController,
  179. parameters: {
  180. query: "Government",
  181. caseSensitive: false,
  182. entireWord: true,
  183. phraseSearch: true,
  184. findPrevious: false
  185. },
  186. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  187. selectedMatch: {
  188. pageIndex: 12,
  189. matchIndex: 0
  190. }
  191. });
  192. });
  193. it("performs a multiple term (no phrase) search", async function () {
  194. const {
  195. eventBus,
  196. pdfFindController
  197. } = await initPdfFindController();
  198. await testSearch({
  199. eventBus,
  200. pdfFindController,
  201. parameters: {
  202. query: "alternate solution",
  203. caseSensitive: false,
  204. entireWord: false,
  205. phraseSearch: false,
  206. findPrevious: false
  207. },
  208. matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
  209. selectedMatch: {
  210. pageIndex: 5,
  211. matchIndex: 0
  212. }
  213. });
  214. });
  215. it("performs a normal search, where the text is normalized", async function () {
  216. const {
  217. eventBus,
  218. pdfFindController
  219. } = await initPdfFindController("fraction-highlight.pdf");
  220. await testSearch({
  221. eventBus,
  222. pdfFindController,
  223. parameters: {
  224. query: "fraction",
  225. caseSensitive: false,
  226. entireWord: false,
  227. phraseSearch: true,
  228. findPrevious: false
  229. },
  230. matchesPerPage: [3],
  231. selectedMatch: {
  232. pageIndex: 0,
  233. matchIndex: 0
  234. },
  235. pageMatches: [[19, 48, 66]],
  236. pageMatchesLength: [[8, 8, 8]]
  237. });
  238. });
  239. });