Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-major] Rework the find controller for unit testing #10099

Merged
merged 7 commits into from
Sep 30, 2018
15 changes: 8 additions & 7 deletions examples/components/simpleviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ var container = document.getElementById('viewerContainer');
// (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService();

// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
linkService: pdfLinkService,
});

var pdfViewer = new pdfjsViewer.PDFViewer({
container: container,
linkService: pdfLinkService,
findController: pdfFindController,
});
pdfLinkService.setViewer(pdfViewer);

// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
pdfViewer: pdfViewer,
});
pdfViewer.setFindController(pdfFindController);

container.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = 'page-width';

if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
}
});

Expand All @@ -70,4 +70,5 @@ pdfjsLib.getDocument({
pdfViewer.setDocument(pdfDocument);

pdfLinkService.setDocument(pdfDocument, null);
pdfFindController.setDocument(pdfDocument);
});
15 changes: 8 additions & 7 deletions examples/components/singlepageviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ var container = document.getElementById('viewerContainer');
// (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService();

// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
linkService: pdfLinkService,
});

var pdfSinglePageViewer = new pdfjsViewer.PDFSinglePageViewer({
container: container,
linkService: pdfLinkService,
findController: pdfFindController,
});
pdfLinkService.setViewer(pdfSinglePageViewer);

// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
pdfViewer: pdfSinglePageViewer,
});
pdfSinglePageViewer.setFindController(pdfFindController);

container.addEventListener('pagesinit', function () {
// We can use pdfSinglePageViewer now, e.g. let's change default scale.
pdfSinglePageViewer.currentScaleValue = 'page-width';

if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
}
});

Expand All @@ -70,4 +70,5 @@ pdfjsLib.getDocument({
pdfSinglePageViewer.setDocument(pdfDocument);

pdfLinkService.setDocument(pdfDocument, null);
pdfFindController.setDocument(pdfDocument);
});
11 changes: 0 additions & 11 deletions examples/svgviewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;

var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var SEARCH_FOR = ''; // try 'Mozilla';

var container = document.getElementById('viewerContainer');

Expand All @@ -46,19 +45,9 @@ var pdfViewer = new pdfjsViewer.PDFViewer({
});
pdfLinkService.setViewer(pdfViewer);

// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
pdfViewer: pdfViewer,
});
pdfViewer.setFindController(pdfFindController);

container.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = 'page-width';

if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
}
});

// Loading document.
Expand Down
5 changes: 3 additions & 2 deletions test/unit/clitests.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"network_utils_spec.js",
"node_stream_spec.js",
"parser_spec.js",
"pdf_find_utils.js",
"pdf_history.js",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, thanks for fixing this :-)

"pdf_find_controller_spec.js",
"pdf_find_utils_spec.js",
"pdf_history_spec.js",
"primitives_spec.js",
"stream_spec.js",
"type1_parser_spec.js",
Expand Down
1 change: 1 addition & 0 deletions test/unit/jasmine-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function initializePDFJS(callback) {
'pdfjs-test/unit/network_spec',
'pdfjs-test/unit/network_utils_spec',
'pdfjs-test/unit/parser_spec',
'pdfjs-test/unit/pdf_find_controller_spec',
'pdfjs-test/unit/pdf_find_utils_spec',
'pdfjs-test/unit/pdf_history_spec',
'pdfjs-test/unit/primitives_spec',
Expand Down
99 changes: 99 additions & 0 deletions test/unit/pdf_find_controller_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* Copyright 2018 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { buildGetDocumentParams } from './test_utils';
import { EventBus } from '../../web/ui_utils';
import { getDocument } from '../../src/display/api';
import { PDFFindController } from '../../web/pdf_find_controller';
import { SimpleLinkService } from '../../web/pdf_link_service';

class MockLinkService extends SimpleLinkService {
constructor() {
super();

this._page = 1;
this._pdfDocument = null;
}

setDocument(pdfDocument) {
this._pdfDocument = pdfDocument;
}

get pagesCount() {
return this._pdfDocument.numPages;
}

get page() {
return this._page;
}

set page(value) {
this._page = value;
}
}

describe('pdf_find_controller', function() {
let eventBus;
let pdfFindController;

beforeEach(function(done) {
const loadingTask = getDocument(buildGetDocumentParams('tracemonkey.pdf'));
loadingTask.promise.then(function(pdfDocument) {
const linkService = new MockLinkService();
linkService.setDocument(pdfDocument);

eventBus = new EventBus();

pdfFindController = new PDFFindController({
linkService,
eventBus,
});
pdfFindController.setDocument(pdfDocument);

eventBus.dispatch('pagesinit');
done();
});
});

afterEach(function() {
eventBus = null;
pdfFindController = null;
});

it('performs a basic search', function(done) {
pdfFindController.executeCommand('find', { query: 'Dynamic', });

const matchesPerPage = [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4];
const totalPages = matchesPerPage.length;
const totalMatches = matchesPerPage.reduce((a, b) => {
return a + b;
});

eventBus.on('updatefindmatchescount',
function onUpdateFindMatchesCount(evt) {
if (pdfFindController.pageMatches.length !== totalPages) {
return;
}
eventBus.off('updatefindmatchescount', onUpdateFindMatchesCount);

expect(evt.matchesCount.total).toBe(totalMatches);
for (let i = 0; i < totalPages; i++) {
expect(pdfFindController.pageMatches[i].length)
.toEqual(matchesPerPage[i]);
}
done();
});
});
});
60 changes: 33 additions & 27 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ let PDFViewerApplication = {
});
this.downloadManager = downloadManager;

const findController = new PDFFindController({
linkService: pdfLinkService,
eventBus,
});
this.findController = findController;

let container = appConfig.mainContainer;
let viewer = appConfig.viewerContainer;
this.pdfViewer = new PDFViewer({
Expand All @@ -314,6 +320,7 @@ let PDFViewerApplication = {
renderingQueue: pdfRenderingQueue,
linkService: pdfLinkService,
downloadManager,
findController,
renderer: AppOptions.get('renderer'),
enableWebGL: AppOptions.get('enableWebGL'),
l10n: this.l10n,
Expand Down Expand Up @@ -342,34 +349,8 @@ let PDFViewerApplication = {
});
pdfLinkService.setHistory(this.pdfHistory);

this.findController = new PDFFindController({
pdfViewer: this.pdfViewer,
eventBus,
});
this.findController.onUpdateResultsCount = (matchesCount) => {
if (this.supportsIntegratedFind) {
this.externalServices.updateFindMatchesCount(matchesCount);
} else {
this.findBar.updateResultsCount(matchesCount);
}
};
this.findController.onUpdateState = (state, previous, matchesCount) => {
if (this.supportsIntegratedFind) {
this.externalServices.updateFindControlState({
result: state,
findPrevious: previous,
matchesCount,
});
} else {
this.findBar.updateUIState(state, previous, matchesCount);
}
};

this.pdfViewer.setFindController(this.findController);

// TODO: improve `PDFFindBar` constructor parameter passing
let findBarConfig = Object.create(appConfig.findBar);
findBarConfig.findController = this.findController;
findBarConfig.eventBus = eventBus;
this.findBar = new PDFFindBar(findBarConfig, this.l10n);

Expand Down Expand Up @@ -593,6 +574,7 @@ let PDFViewerApplication = {
if (this.pdfDocument) {
this.pdfDocument = null;

this.findController.setDocument(null);
this.pdfThumbnailViewer.setDocument(null);
this.pdfViewer.setDocument(null);
this.pdfLinkService.setDocument(null);
Expand All @@ -609,7 +591,6 @@ let PDFViewerApplication = {
this.pdfOutlineViewer.reset();
this.pdfAttachmentViewer.reset();

this.findController.reset();
this.findBar.reset();
this.toolbar.reset();
this.secondaryToolbar.reset();
Expand Down Expand Up @@ -917,6 +898,7 @@ let PDFViewerApplication = {
} else if (PDFJSDev.test('CHROME')) {
baseDocumentUrl = location.href.split('#')[0];
}
this.findController.setDocument(pdfDocument);
this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl);
this.pdfDocumentProperties.setDocument(pdfDocument, this.url);

Expand Down Expand Up @@ -1343,6 +1325,8 @@ let PDFViewerApplication = {
eventBus.on('documentproperties', webViewerDocumentProperties);
eventBus.on('find', webViewerFind);
eventBus.on('findfromurlhash', webViewerFindFromUrlHash);
eventBus.on('updatefindmatchescount', webViewerUpdateFindMatchesCount);
eventBus.on('updatefindcontrolstate', webViewerUpdateFindControlState);
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
eventBus.on('fileinputchange', webViewerFileInputChange);
}
Expand Down Expand Up @@ -1414,6 +1398,8 @@ let PDFViewerApplication = {
eventBus.off('documentproperties', webViewerDocumentProperties);
eventBus.off('find', webViewerFind);
eventBus.off('findfromurlhash', webViewerFindFromUrlHash);
eventBus.off('updatefindmatchescount', webViewerUpdateFindMatchesCount);
eventBus.off('updatefindcontrolstate', webViewerUpdateFindControlState);
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
eventBus.off('fileinputchange', webViewerFileInputChange);
}
Expand Down Expand Up @@ -1976,6 +1962,26 @@ function webViewerFindFromUrlHash(evt) {
});
}

function webViewerUpdateFindMatchesCount({ matchesCount, }) {
if (PDFViewerApplication.supportsIntegratedFind) {
PDFViewerApplication.externalServices.updateFindMatchesCount(matchesCount);
} else {
PDFViewerApplication.findBar.updateResultsCount(matchesCount);
}
}

function webViewerUpdateFindControlState({ state, previous, matchesCount, }) {
if (PDFViewerApplication.supportsIntegratedFind) {
PDFViewerApplication.externalServices.updateFindControlState({
result: state,
findPrevious: previous,
matchesCount,
});
} else {
PDFViewerApplication.findBar.updateUIState(state, previous, matchesCount);
}
}

function webViewerScaleChanging(evt) {
PDFViewerApplication.toolbar.setPageScale(evt.presetValue, evt.scale);

Expand Down
15 changes: 3 additions & 12 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const SpreadMode = {
* @property {IPDFLinkService} linkService - The navigation/linking service.
* @property {DownloadManager} downloadManager - (optional) The download
* manager component.
* @property {PDFFindController} findController - (optional) The find
* controller component.
* @property {PDFRenderingQueue} renderingQueue - (optional) The rendering
* queue object.
* @property {boolean} removePageBorders - (optional) Removes the border shadow
Expand Down Expand Up @@ -142,6 +144,7 @@ class BaseViewer {
this.eventBus = options.eventBus || getGlobalEventBus();
this.linkService = options.linkService || new SimpleLinkService();
this.downloadManager = options.downloadManager || null;
this.findController = options.findController || null;
this.removePageBorders = options.removePageBorders || false;
this.textLayerMode = Number.isInteger(options.textLayerMode) ?
options.textLayerMode : TextLayerMode.ENABLE;
Expand Down Expand Up @@ -913,14 +916,6 @@ class BaseViewer {
return false;
}

getPageTextContent(pageIndex) {
return this.pdfDocument.getPage(pageIndex + 1).then(function(page) {
return page.getTextContent({
normalizeWhitespace: true,
});
});
}

/**
* @param {HTMLDivElement} textLayerDiv
* @param {number} pageIndex
Expand Down Expand Up @@ -963,10 +958,6 @@ class BaseViewer {
});
}

setFindController(findController) {
this.findController = findController;
}

/**
* @returns {boolean} Whether all pages of the PDF document have identical
* widths and heights.
Expand Down
Loading