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

Implement the NodeCanvasFactory class to execute more unit tests in Node.js

This commit is contained in:
Tim van der Meij 2019-02-10 19:29:38 +01:00
parent b6eddc40b5
commit 7c91e94b19
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
5 changed files with 329 additions and 29 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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,