1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #8775 from Snuffleupagus/rewrite-PDFHistory-2

Re-write `PDFHistory` from scratch
This commit is contained in:
Tim van der Meij 2017-09-03 20:38:59 +02:00 committed by GitHub
commit 1c9af00bee
9 changed files with 817 additions and 461 deletions

View file

@ -17,6 +17,7 @@
"murmurhash3_spec.js",
"node_stream_spec.js",
"parser_spec.js",
"pdf_history.js",
"primitives_spec.js",
"stream_spec.js",
"type1_parser_spec.js",

View file

@ -64,6 +64,7 @@ function initializePDFJS(callback) {
'pdfjs-test/unit/murmurhash3_spec',
'pdfjs-test/unit/network_spec',
'pdfjs-test/unit/parser_spec',
'pdfjs-test/unit/pdf_history_spec',
'pdfjs-test/unit/primitives_spec',
'pdfjs-test/unit/stream_spec',
'pdfjs-test/unit/type1_parser_spec',

View file

@ -0,0 +1,45 @@
/* Copyright 2017 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 { isDestsEqual } from '../../web/pdf_history';
describe('pdf_history', function() {
describe('isDestsEqual', function() {
let firstDest = [{ num: 1, gen: 0, }, { name: 'XYZ', }, 0, 375, null];
let secondDest = [{ num: 5, gen: 0, }, { name: 'XYZ', }, 0, 375, null];
let thirdDest = [{ num: 1, gen: 0, }, { name: 'XYZ', }, 750, 0, null];
let fourthDest = [{ num: 1, gen: 0, }, { name: 'XYZ', }, 0, 375, 1.0];
let fifthDest = [{ gen: 0, num: 1, }, { name: 'XYZ', }, 0, 375, null];
it('should reject non-equal destination arrays', function() {
expect(isDestsEqual(firstDest, undefined)).toEqual(false);
expect(isDestsEqual(firstDest, [1, 2, 3, 4, 5])).toEqual(false);
expect(isDestsEqual(firstDest, secondDest)).toEqual(false);
expect(isDestsEqual(firstDest, thirdDest)).toEqual(false);
expect(isDestsEqual(firstDest, fourthDest)).toEqual(false);
});
it('should accept equal destination arrays', function() {
expect(isDestsEqual(firstDest, firstDest)).toEqual(true);
expect(isDestsEqual(firstDest, fifthDest)).toEqual(true);
let firstDestCopy = firstDest.slice();
expect(firstDest).not.toBe(firstDestCopy);
expect(isDestsEqual(firstDest, firstDestCopy)).toEqual(true);
});
});
});

View file

@ -14,7 +14,8 @@
*/
import {
binarySearchFirstItem, EventBus, getPDFFileNameFromURL
binarySearchFirstItem, EventBus, getPDFFileNameFromURL, waitOnEventOrTimeout,
WaitOnType
} from '../../web/ui_utils';
import { createObjectURL, isNodeJS } from '../../src/shared/util';
@ -259,4 +260,118 @@ describe('ui_utils', function() {
expect(count).toEqual(2);
});
});
describe('waitOnEventOrTimeout', function() {
let eventBus;
beforeAll(function(done) {
eventBus = new EventBus();
done();
});
afterAll(function() {
eventBus = null;
});
it('should reject invalid parameters', function(done) {
let invalidTarget = waitOnEventOrTimeout({
target: 'window',
name: 'DOMContentLoaded',
}).then(function() {
throw new Error('Should reject invalid parameters.');
}, function(reason) {
expect(reason instanceof Error).toEqual(true);
});
let invalidName = waitOnEventOrTimeout({
target: eventBus,
name: '',
}).then(function() {
throw new Error('Should reject invalid parameters.');
}, function(reason) {
expect(reason instanceof Error).toEqual(true);
});
let invalidDelay = waitOnEventOrTimeout({
target: eventBus,
name: 'pagerendered',
delay: -1000,
}).then(function() {
throw new Error('Should reject invalid parameters.');
}, function(reason) {
expect(reason instanceof Error).toEqual(true);
});
Promise.all([invalidTarget, invalidName, invalidDelay]).then(done,
done.fail);
});
it('should resolve on event, using the DOM', function(done) {
if (isNodeJS()) {
pending('Document in not supported in Node.js.');
}
let button = document.createElement('button');
let buttonClicked = waitOnEventOrTimeout({
target: button,
name: 'click',
delay: 10000,
});
// Immediately dispatch the expected event.
button.click();
buttonClicked.then(function(type) {
expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
});
it('should resolve on timeout, using the DOM', function(done) {
if (isNodeJS()) {
pending('Document in not supported in Node.js.');
}
let button = document.createElement('button');
let buttonClicked = waitOnEventOrTimeout({
target: button,
name: 'click',
delay: 10,
});
// Do *not* dispatch the event, and wait for the timeout.
buttonClicked.then(function(type) {
expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
});
it('should resolve on event, using the EventBus', function(done) {
let pageRendered = waitOnEventOrTimeout({
target: eventBus,
name: 'pagerendered',
delay: 10000,
});
// Immediately dispatch the expected event.
eventBus.dispatch('pagerendered');
pageRendered.then(function(type) {
expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
});
it('should resolve on timeout, using the EventBus', function(done) {
let pageRendered = waitOnEventOrTimeout({
target: eventBus,
name: 'pagerendered',
delay: 10,
});
// Do *not* dispatch the event, and wait for the timeout.
pageRendered.then(function(type) {
expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
});
});
});