pdf_find_controller_spec.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  24. var _ui_utils = require("../../web/ui_utils");
  25. var _api = require("../../display/api");
  26. var _pdf_find_controller = require("../../web/pdf_find_controller");
  27. var _pdf_link_service = require("../../web/pdf_link_service");
  28. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  33. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  34. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  35. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  36. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  37. var MockLinkService =
  38. /*#__PURE__*/
  39. function (_SimpleLinkService) {
  40. _inherits(MockLinkService, _SimpleLinkService);
  41. function MockLinkService() {
  42. var _this;
  43. _classCallCheck(this, MockLinkService);
  44. _this = _possibleConstructorReturn(this, _getPrototypeOf(MockLinkService).call(this));
  45. _this._page = 1;
  46. _this._pdfDocument = null;
  47. return _this;
  48. }
  49. _createClass(MockLinkService, [{
  50. key: "setDocument",
  51. value: function setDocument(pdfDocument) {
  52. this._pdfDocument = pdfDocument;
  53. }
  54. }, {
  55. key: "pagesCount",
  56. get: function get() {
  57. return this._pdfDocument.numPages;
  58. }
  59. }, {
  60. key: "page",
  61. get: function get() {
  62. return this._page;
  63. },
  64. set: function set(value) {
  65. this._page = value;
  66. }
  67. }]);
  68. return MockLinkService;
  69. }(_pdf_link_service.SimpleLinkService);
  70. describe('pdf_find_controller', function () {
  71. var eventBus;
  72. var pdfFindController;
  73. beforeEach(function (done) {
  74. var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
  75. loadingTask.promise.then(function (pdfDocument) {
  76. eventBus = new _ui_utils.EventBus();
  77. var linkService = new MockLinkService();
  78. linkService.setDocument(pdfDocument);
  79. pdfFindController = new _pdf_find_controller.PDFFindController({
  80. linkService: linkService,
  81. eventBus: eventBus
  82. });
  83. pdfFindController.setDocument(pdfDocument);
  84. done();
  85. });
  86. });
  87. afterEach(function () {
  88. eventBus = null;
  89. pdfFindController = null;
  90. });
  91. function testSearch(_ref) {
  92. var parameters = _ref.parameters,
  93. matchesPerPage = _ref.matchesPerPage,
  94. selectedMatch = _ref.selectedMatch;
  95. return new Promise(function (resolve) {
  96. pdfFindController.executeCommand('find', parameters);
  97. var totalPages = matchesPerPage.length;
  98. for (var i = totalPages - 1; i >= 0; i--) {
  99. if (matchesPerPage[i] > 0) {
  100. totalPages = i + 1;
  101. break;
  102. }
  103. }
  104. var totalMatches = matchesPerPage.reduce(function (a, b) {
  105. return a + b;
  106. });
  107. eventBus.on('updatefindmatchescount', function onUpdateFindMatchesCount(evt) {
  108. if (pdfFindController.pageMatches.length !== totalPages) {
  109. return;
  110. }
  111. eventBus.off('updatefindmatchescount', onUpdateFindMatchesCount);
  112. expect(evt.matchesCount.total).toBe(totalMatches);
  113. for (var _i = 0; _i < totalPages; _i++) {
  114. expect(pdfFindController.pageMatches[_i].length).toEqual(matchesPerPage[_i]);
  115. }
  116. expect(pdfFindController.selected.pageIdx).toEqual(selectedMatch.pageIndex);
  117. expect(pdfFindController.selected.matchIdx).toEqual(selectedMatch.matchIndex);
  118. resolve();
  119. });
  120. });
  121. }
  122. it('performs a normal search', function (done) {
  123. testSearch({
  124. parameters: {
  125. query: 'Dynamic',
  126. caseSensitive: false,
  127. entireWord: false,
  128. phraseSearch: true,
  129. findPrevious: false
  130. },
  131. matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
  132. selectedMatch: {
  133. pageIndex: 0,
  134. matchIndex: 0
  135. }
  136. }).then(done);
  137. });
  138. it('performs a normal search and finds the previous result', function (done) {
  139. testSearch({
  140. parameters: {
  141. query: 'conference',
  142. caseSensitive: false,
  143. entireWord: false,
  144. phraseSearch: true,
  145. findPrevious: true
  146. },
  147. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
  148. selectedMatch: {
  149. pageIndex: 13,
  150. matchIndex: 4
  151. }
  152. }).then(done);
  153. });
  154. it('performs a case sensitive search', function (done) {
  155. testSearch({
  156. parameters: {
  157. query: 'Dynamic',
  158. caseSensitive: true,
  159. entireWord: false,
  160. phraseSearch: true,
  161. findPrevious: false
  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. }).then(done);
  169. });
  170. it('performs an entire word search', function (done) {
  171. testSearch({
  172. parameters: {
  173. query: 'Government',
  174. caseSensitive: false,
  175. entireWord: true,
  176. phraseSearch: true,
  177. findPrevious: false
  178. },
  179. matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
  180. selectedMatch: {
  181. pageIndex: 12,
  182. matchIndex: 0
  183. }
  184. }).then(done);
  185. });
  186. it('performs a multiple term (no phrase) search', function (done) {
  187. testSearch({
  188. parameters: {
  189. query: 'alternate solution',
  190. caseSensitive: false,
  191. entireWord: false,
  192. phraseSearch: false,
  193. findPrevious: false
  194. },
  195. matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
  196. selectedMatch: {
  197. pageIndex: 5,
  198. matchIndex: 0
  199. }
  200. }).then(done);
  201. });
  202. });