mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Merge pull request #10537 from timvandermeij/unittest
Improve unit test coverage
This commit is contained in:
commit
1d90c76097
6 changed files with 374 additions and 30 deletions
|
@ -14,7 +14,8 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
buildGetDocumentParams, NodeFileReaderFactory, TEST_PDFS_PATH
|
||||
buildGetDocumentParams, NodeCanvasFactory, NodeFileReaderFactory,
|
||||
TEST_PDFS_PATH
|
||||
} from './test_utils';
|
||||
import {
|
||||
createPromiseCapability, FontType, InvalidPDFException, MissingPDFException,
|
||||
|
@ -40,8 +41,7 @@ describe('api', function() {
|
|||
|
||||
beforeAll(function(done) {
|
||||
if (isNodeJS()) {
|
||||
// NOTE: To support running the canvas-related tests in Node.js,
|
||||
// a `NodeCanvasFactory` would need to be added (in test_utils.js).
|
||||
CanvasFactory = new NodeCanvasFactory();
|
||||
} else {
|
||||
CanvasFactory = new DOMCanvasFactory();
|
||||
}
|
||||
|
@ -1275,9 +1275,6 @@ describe('api', function() {
|
|||
});
|
||||
it('gets page stats after rendering page, with `pdfBug` set',
|
||||
function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
let loadingTask = getDocument(
|
||||
buildGetDocumentParams(basicApiFileName, { pdfBug: true, }));
|
||||
let canvasAndCtx;
|
||||
|
@ -1289,6 +1286,7 @@ describe('api', function() {
|
|||
|
||||
let renderTask = pdfPage.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
return renderTask.promise.then(() => {
|
||||
|
@ -1315,14 +1313,12 @@ describe('api', function() {
|
|||
});
|
||||
|
||||
it('cancels rendering of page', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
var viewport = page.getViewport({ scale: 1, });
|
||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||
|
||||
var renderTask = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
renderTask.cancel();
|
||||
|
@ -1339,14 +1335,12 @@ describe('api', function() {
|
|||
|
||||
it('re-render page, using the same canvas, after cancelling rendering',
|
||||
function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
let viewport = page.getViewport({ scale: 1, });
|
||||
let canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||
|
||||
let renderTask = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
renderTask.cancel();
|
||||
|
@ -1358,6 +1352,7 @@ describe('api', function() {
|
|||
}).then(() => {
|
||||
let reRenderTask = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
return reRenderTask.promise;
|
||||
|
@ -1368,18 +1363,17 @@ describe('api', function() {
|
|||
});
|
||||
|
||||
it('multiple render() on the same canvas', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
var viewport = page.getViewport({ scale: 1, });
|
||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||
|
||||
var renderTask1 = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
var renderTask2 = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
|
||||
|
@ -1418,6 +1412,7 @@ describe('api', function() {
|
|||
viewport.height);
|
||||
const renderTask = page.render({
|
||||
canvasContext: canvasAndCtx.context,
|
||||
canvasFactory: CanvasFactory,
|
||||
viewport,
|
||||
});
|
||||
await renderTask.promise;
|
||||
|
@ -1445,10 +1440,6 @@ describe('api', function() {
|
|||
});
|
||||
|
||||
it('should correctly render PDFs in parallel', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
|
||||
var baseline1, baseline2, baseline3;
|
||||
var promiseDone = renderPDF(pdf1).then(function(data1) {
|
||||
baseline1 = data1;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { buildGetDocumentParams } from './test_utils';
|
||||
import { buildGetDocumentParams, NodeCanvasFactory } from './test_utils';
|
||||
import { DOMCanvasFactory } from '../../src/display/dom_utils';
|
||||
import { getDocument } from '../../src/display/api';
|
||||
import isNodeJS from '../../src/shared/is_node';
|
||||
|
@ -37,8 +37,7 @@ describe('custom canvas rendering', function() {
|
|||
|
||||
beforeAll(function(done) {
|
||||
if (isNodeJS()) {
|
||||
// NOTE: To support running the canvas-related tests in Node.js,
|
||||
// a `NodeCanvasFactory` would need to be added (in test_utils.js).
|
||||
CanvasFactory = new NodeCanvasFactory();
|
||||
} else {
|
||||
CanvasFactory = new DOMCanvasFactory();
|
||||
}
|
||||
|
@ -58,9 +57,6 @@ describe('custom canvas rendering', function() {
|
|||
});
|
||||
|
||||
it('renders to canvas with a default white background', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
var viewport = page.getViewport({ scale: 1, });
|
||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||
|
||||
|
@ -77,9 +73,6 @@ describe('custom canvas rendering', function() {
|
|||
});
|
||||
|
||||
it('renders to canvas with a custom background', function(done) {
|
||||
if (isNodeJS()) {
|
||||
pending('TODO: Support Canvas testing in Node.js.');
|
||||
}
|
||||
var viewport = page.getViewport({ scale: 1, });
|
||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { CMapCompressionType } from '../../src/shared/util';
|
||||
import { assert, CMapCompressionType } from '../../src/shared/util';
|
||||
import isNodeJS from '../../src/shared/is_node';
|
||||
import { isRef } from '../../src/core/primitives';
|
||||
|
||||
|
@ -43,6 +43,38 @@ function buildGetDocumentParams(filename, options) {
|
|||
return params;
|
||||
}
|
||||
|
||||
class NodeCanvasFactory {
|
||||
create(width, height) {
|
||||
assert(width > 0 && height > 0, 'Invalid canvas size');
|
||||
|
||||
const Canvas = require('canvas');
|
||||
const canvas = Canvas.createCanvas(width, height);
|
||||
return {
|
||||
canvas,
|
||||
context: canvas.getContext('2d'),
|
||||
};
|
||||
}
|
||||
|
||||
reset(canvasAndContext, width, height) {
|
||||
assert(canvasAndContext.canvas, 'Canvas is not specified');
|
||||
assert(width > 0 && height > 0, 'Invalid canvas size');
|
||||
|
||||
canvasAndContext.canvas.width = width;
|
||||
canvasAndContext.canvas.height = height;
|
||||
}
|
||||
|
||||
destroy(canvasAndContext) {
|
||||
assert(canvasAndContext.canvas, 'Canvas is not specified');
|
||||
|
||||
// Zeroing the width and height cause Firefox to release graphics
|
||||
// resources immediately, which can greatly reduce memory consumption.
|
||||
canvasAndContext.canvas.width = 0;
|
||||
canvasAndContext.canvas.height = 0;
|
||||
canvasAndContext.canvas = null;
|
||||
canvasAndContext.context = null;
|
||||
}
|
||||
}
|
||||
|
||||
class NodeCMapReaderFactory {
|
||||
constructor({ baseUrl = null, isCompressed = false, }) {
|
||||
this.baseUrl = baseUrl;
|
||||
|
@ -111,6 +143,7 @@ class XRefMock {
|
|||
|
||||
export {
|
||||
NodeFileReaderFactory,
|
||||
NodeCanvasFactory,
|
||||
NodeCMapReaderFactory,
|
||||
XRefMock,
|
||||
buildGetDocumentParams,
|
||||
|
|
|
@ -17,7 +17,7 @@ import {
|
|||
bytesToString, createPromiseCapability, createValidAbsoluteUrl,
|
||||
getInheritableProperty, isArrayBuffer, isBool, isEmptyObj, isNum,
|
||||
isSameOrigin, isSpace, isString, log2, ReadableStream, removeNullCharacters,
|
||||
stringToBytes, stringToPDFString, URL
|
||||
string32, stringToBytes, stringToPDFString, toRomanNumerals, URL
|
||||
} from '../../src/shared/util';
|
||||
import { Dict, Ref } from '../../src/core/primitives';
|
||||
import { XRefMock } from './test_utils';
|
||||
|
@ -254,6 +254,14 @@ describe('util', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('string32', function() {
|
||||
it('converts unsigned 32-bit integers to strings', function() {
|
||||
expect(string32(0x74727565)).toEqual('true');
|
||||
expect(string32(0x74797031)).toEqual('typ1');
|
||||
expect(string32(0x4F54544F)).toEqual('OTTO');
|
||||
});
|
||||
});
|
||||
|
||||
describe('stringToBytes', function() {
|
||||
it('handles non-string arguments', function() {
|
||||
expect(function() {
|
||||
|
@ -313,6 +321,42 @@ describe('util', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toRomanNumerals', function() {
|
||||
it('handles invalid arguments', function() {
|
||||
for (const input of ['foo', -1, 0]) {
|
||||
expect(function() {
|
||||
toRomanNumerals(input);
|
||||
}).toThrow(new Error('The number should be a positive integer.'));
|
||||
}
|
||||
});
|
||||
|
||||
it('converts numbers to uppercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1)).toEqual('I');
|
||||
expect(toRomanNumerals(6)).toEqual('VI');
|
||||
expect(toRomanNumerals(7)).toEqual('VII');
|
||||
expect(toRomanNumerals(8)).toEqual('VIII');
|
||||
expect(toRomanNumerals(10)).toEqual('X');
|
||||
expect(toRomanNumerals(40)).toEqual('XL');
|
||||
expect(toRomanNumerals(100)).toEqual('C');
|
||||
expect(toRomanNumerals(500)).toEqual('D');
|
||||
expect(toRomanNumerals(1000)).toEqual('M');
|
||||
expect(toRomanNumerals(2019)).toEqual('MMXIX');
|
||||
});
|
||||
|
||||
it('converts numbers to lowercase Roman numerals', function() {
|
||||
expect(toRomanNumerals(1, /* lowercase = */ true)).toEqual('i');
|
||||
expect(toRomanNumerals(6, /* lowercase = */ true)).toEqual('vi');
|
||||
expect(toRomanNumerals(7, /* lowercase = */ true)).toEqual('vii');
|
||||
expect(toRomanNumerals(8, /* lowercase = */ true)).toEqual('viii');
|
||||
expect(toRomanNumerals(10, /* lowercase = */ true)).toEqual('x');
|
||||
expect(toRomanNumerals(40, /* lowercase = */ true)).toEqual('xl');
|
||||
expect(toRomanNumerals(100, /* lowercase = */ true)).toEqual('c');
|
||||
expect(toRomanNumerals(500, /* lowercase = */ true)).toEqual('d');
|
||||
expect(toRomanNumerals(1000, /* lowercase = */ true)).toEqual('m');
|
||||
expect(toRomanNumerals(2019, /* lowercase = */ true)).toEqual('mmxix');
|
||||
});
|
||||
});
|
||||
|
||||
describe('URL', function() {
|
||||
it('should return an Object', function() {
|
||||
const url = new URL('https://example.com');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue