pdf_find_controller_spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 _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. class MockLinkService extends _pdf_link_service.SimpleLinkService {
  29. constructor() {
  30. super();
  31. this._page = 1;
  32. this._pdfDocument = null;
  33. }
  34. setDocument(pdfDocument) {
  35. this._pdfDocument = pdfDocument;
  36. }
  37. get pagesCount() {
  38. return this._pdfDocument.numPages;
  39. }
  40. get page() {
  41. return this._page;
  42. }
  43. set page(value) {
  44. this._page = value;
  45. }
  46. }
  47. describe("pdf_find_controller", function () {
  48. let eventBus;
  49. let pdfFindController;
  50. beforeEach(function (done) {
  51. const loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("tracemonkey.pdf"));
  52. loadingTask.promise.then(function (pdfDocument) {
  53. eventBus = new _ui_utils.EventBus();
  54. const linkService = new MockLinkService();
  55. linkService.setDocument(pdfDocument);
  56. pdfFindController = new _pdf_find_controller.PDFFindController({
  57. linkService,
  58. eventBus
  59. });
  60. pdfFindController.setDocument(pdfDocument);
  61. done();
  62. });
  63. });
  64. afterEach(function () {
  65. eventBus = null;
  66. pdfFindController = null;
  67. });
  68. function testSearch({
  69. parameters,
  70. matchesPerPage,
  71. selectedMatch
  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. resolve();
  97. });
  98. });
  99. }
  100. it("performs a normal search", function (done) {
  101. testSearch({
  102. parameters: {
  103. query: "Dynamic",
  104. caseSensitive: false,
  105. entireWord: false,
  106. phraseSearch: true,
  107. findPrevious: false
  108. },
  109. matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
  110. selectedMatch: {
  111. pageIndex: 0,
  112. matchIndex: 0
  113. }
  114. }).then(done);
  115. });
  116. it("performs a normal search and finds the previous result", function (done) {
  117. testSearch({
  118. parameters: {
  119. query: "conference",
  120. caseSensitive: false,
  121. entireWord: false,
  122. phraseSearch: true,
  123. findPrevious: true
  124. },
  125. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
  126. selectedMatch: {
  127. pageIndex: 13,
  128. matchIndex: 4
  129. }
  130. }).then(done);
  131. });
  132. it("performs a case sensitive search", function (done) {
  133. testSearch({
  134. parameters: {
  135. query: "Dynamic",
  136. caseSensitive: true,
  137. entireWord: false,
  138. phraseSearch: true,
  139. findPrevious: false
  140. },
  141. matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
  142. selectedMatch: {
  143. pageIndex: 0,
  144. matchIndex: 0
  145. }
  146. }).then(done);
  147. });
  148. it("performs an entire word search", function (done) {
  149. testSearch({
  150. parameters: {
  151. query: "Government",
  152. caseSensitive: false,
  153. entireWord: true,
  154. phraseSearch: true,
  155. findPrevious: false
  156. },
  157. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  158. selectedMatch: {
  159. pageIndex: 12,
  160. matchIndex: 0
  161. }
  162. }).then(done);
  163. });
  164. it("performs a multiple term (no phrase) search", function (done) {
  165. testSearch({
  166. parameters: {
  167. query: "alternate solution",
  168. caseSensitive: false,
  169. entireWord: false,
  170. phraseSearch: false,
  171. findPrevious: false
  172. },
  173. matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
  174. selectedMatch: {
  175. pageIndex: 5,
  176. matchIndex: 0
  177. }
  178. }).then(done);
  179. });
  180. });