Merge pull request from timvandermeij/test-driver

Modernize the test driver
This commit is contained in:
Jonas Jenwald 2021-12-05 23:52:46 +01:00 committed by GitHub
commit 034b870c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* eslint-disable no-var */
/* globals pdfjsLib, pdfjsViewer */ /* globals pdfjsLib, pdfjsViewer */
"use strict"; "use strict";
@ -24,6 +23,7 @@ const {
GlobalWorkerOptions, GlobalWorkerOptions,
PixelsPerInch, PixelsPerInch,
renderTextLayer, renderTextLayer,
shadow,
XfaLayer, XfaLayer,
} = pdfjsLib; } = pdfjsLib;
const { SimpleLinkService } = pdfjsViewer; const { SimpleLinkService } = pdfjsViewer;
@ -38,33 +38,33 @@ const RENDER_TASK_ON_CONTINUE_DELAY = 5; // ms
const SVG_NS = "http://www.w3.org/2000/svg"; const SVG_NS = "http://www.w3.org/2000/svg";
function loadStyles(styles) { function loadStyles(styles) {
styles = Object.values(styles); const promises = [];
if (styles.every(style => style.promise)) {
return Promise.all(styles.map(style => style.promise));
}
for (const style of styles) { for (const file of styles) {
style.promise = new Promise(function (resolve, reject) { promises.push(
new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("GET", style.file); xhr.open("GET", file);
xhr.onload = function () { xhr.onload = function () {
resolve(xhr.responseText); resolve(xhr.responseText);
}; };
xhr.onerror = function (e) { xhr.onerror = function (e) {
reject(new Error(`Error fetching style (${style.file}): ${e}`)); reject(new Error(`Error fetching style (${file}): ${e}`));
}; };
xhr.send(null); xhr.send(null);
}); })
);
} }
return Promise.all(styles.map(style => style.promise)); return Promise.all(promises);
} }
function writeSVG(svgElement, ctx, resolve, reject) { function writeSVG(svgElement, ctx) {
// We need to have UTF-8 encoded XML. // We need to have UTF-8 encoded XML.
const svg_xml = unescape( const svg_xml = unescape(
encodeURIComponent(new XMLSerializer().serializeToString(svgElement)) encodeURIComponent(new XMLSerializer().serializeToString(svgElement))
); );
return new Promise((resolve, reject) => {
const img = new Image(); const img = new Image();
img.src = "data:image/svg+xml;base64," + btoa(svg_xml); img.src = "data:image/svg+xml;base64," + btoa(svg_xml);
img.onload = function () { img.onload = function () {
@ -72,8 +72,9 @@ function writeSVG(svgElement, ctx, resolve, reject) {
resolve(); resolve();
}; };
img.onerror = function (e) { img.onerror = function (e) {
reject(new Error("Error rasterizing text layer " + e)); reject(new Error(`Error rasterizing SVG: ${e}`));
}; };
});
} }
function inlineImages(images) { function inlineImages(images) {
@ -143,106 +144,58 @@ async function resolveImages(node, silentErrors = false) {
await Promise.all(loadedPromises); await Promise.all(loadedPromises);
} }
class Rasterize {
/** /**
* @class * For the reference tests, the full content of the various layers must be
*/ * visible. To achieve this, we load the common styles as used by the viewer
var rasterizeTextLayer = (function rasterizeTextLayerClosure() { * and extend them with a set of overrides to make all elements visible.
const styles = {
common: {
file: "./text_layer_test.css",
promise: null,
},
};
function getTextLayerStyle() {
return loadStyles(styles);
}
// eslint-disable-next-line no-shadow
function rasterizeTextLayer(
ctx,
viewport,
textContent,
enhanceTextSelection
) {
return new Promise(function (resolve, reject) {
// Building SVG with size of the viewport.
var svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
// items are transformed to have 1px font size
svg.setAttribute("font-size", 1);
// Adding element to host our HTML (style + text layer div).
var foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
var style = document.createElement("style");
var stylePromise = getTextLayerStyle();
foreignObject.appendChild(style);
var div = document.createElement("div");
div.className = "textLayer";
foreignObject.appendChild(div);
stylePromise
.then(async ([cssRules]) => {
style.textContent = cssRules;
// Rendering text layer as HTML.
var task = renderTextLayer({
textContent,
container: div,
viewport,
enhanceTextSelection,
});
await task.promise;
task.expandTextDivs(true);
svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject);
})
.catch(reason => {
reject(new Error(`rasterizeTextLayer: "${reason?.message}".`));
});
});
}
return rasterizeTextLayer;
})();
/**
* @class
*/
var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
/**
* For the reference tests, the entire annotation layer must be visible. To
* achieve this, we load the common styles as used by the viewer and extend
* them with a set of overrides to make all elements visible.
* *
* Note that we cannot simply use `@import` to import the common styles in * Note that we cannot simply use `@import` to import the common styles in
* the overrides file because the browser does not resolve that when the * the overrides file because the browser does not resolve that when the
* styles are inserted via XHR. Therefore, we load and combine them here. * styles are inserted via XHR. Therefore, we load and combine them here.
*/ */
const styles = { static get annotationStylePromise() {
common: { const styles = [
file: "../web/annotation_layer_builder.css", "../web/annotation_layer_builder.css",
promise: null, "./annotation_layer_builder_overrides.css",
}, ];
overrides: { return shadow(this, "annotationStylePromise", loadStyles(styles));
file: "./annotation_layer_builder_overrides.css",
promise: null,
},
};
function getAnnotationLayerStyle() {
return loadStyles(styles);
} }
// eslint-disable-next-line no-shadow static get textStylePromise() {
function rasterizeAnnotationLayer( const styles = ["./text_layer_test.css"];
return shadow(this, "textStylePromise", loadStyles(styles));
}
static get xfaStylePromise() {
const styles = [
"../web/xfa_layer_builder.css",
"./xfa_layer_builder_overrides.css",
];
return shadow(this, "xfaStylePromise", loadStyles(styles));
}
static createContainer(viewport) {
const svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", `${viewport.width}px`);
svg.setAttribute("height", `${viewport.height}px`);
const foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", `${viewport.width}px`);
foreignObject.setAttribute("height", `${viewport.height}px`);
const style = document.createElement("style");
foreignObject.appendChild(style);
const div = document.createElement("div");
foreignObject.appendChild(div);
return { svg, foreignObject, style, div };
}
static async annotationLayer(
ctx, ctx,
viewport, viewport,
annotations, annotations,
@ -251,36 +204,21 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
imageResourcesPath, imageResourcesPath,
renderForms = false renderForms = false
) { ) {
return new Promise(function (resolve, reject) { try {
// Building SVG with size of the viewport. const { svg, foreignObject, style, div } = this.createContainer(viewport);
var svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
// Adding element to host our HTML (style + annotation layer div).
var foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
var style = document.createElement("style");
var stylePromise = getAnnotationLayerStyle();
foreignObject.appendChild(style);
var div = document.createElement("div");
div.className = "annotationLayer"; div.className = "annotationLayer";
// Rendering annotation layer as HTML. const [common, overrides] = await this.annotationStylePromise;
stylePromise style.textContent = `${common}\n${overrides}`;
.then(async ([common, overrides]) => {
style.textContent = common + "\n" + overrides;
var annotation_viewport = viewport.clone({ dontFlip: true }); const annotationViewport = viewport.clone({ dontFlip: true });
const annotationImageMap = await convertCanvasesToImages( const annotationImageMap = await convertCanvasesToImages(
annotationCanvasMap annotationCanvasMap
); );
var parameters = { // Rendering annotation layer as HTML.
viewport: annotation_viewport, const parameters = {
viewport: annotationViewport,
div, div,
annotations, annotations,
page, page,
@ -296,38 +234,42 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
foreignObject.appendChild(div); foreignObject.appendChild(div);
svg.appendChild(foreignObject); svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject); await writeSVG(svg, ctx);
}) } catch (reason) {
.catch(reason => { throw new Error(`Rasterize.annotationLayer: "${reason?.message}".`);
reject(new Error(`rasterizeAnnotationLayer: "${reason?.message}".`)); }
});
});
} }
return rasterizeAnnotationLayer; static async textLayer(ctx, viewport, textContent, enhanceTextSelection) {
})(); try {
const { svg, foreignObject, style, div } = this.createContainer(viewport);
div.className = "textLayer";
/** // Items are transformed to have 1px font size.
* @class svg.setAttribute("font-size", 1);
*/
var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
const styles = {
common: {
file: "../web/xfa_layer_builder.css",
promise: null,
},
overrides: {
file: "./xfa_layer_builder_overrides.css",
promise: null,
},
};
function getXfaLayerStyle() { const [cssRules] = await this.textStylePromise;
return loadStyles(styles); style.textContent = cssRules;
// Rendering text layer as HTML.
const task = renderTextLayer({
textContent,
container: div,
viewport,
enhanceTextSelection,
});
await task.promise;
task.expandTextDivs(true);
svg.appendChild(foreignObject);
await writeSVG(svg, ctx);
} catch (reason) {
throw new Error(`Rasterize.textLayer: "${reason?.message}".`);
}
} }
// eslint-disable-next-line no-shadow static async xfaLayer(
function rasterizeXfaLayer(
ctx, ctx,
viewport, viewport,
xfa, xfa,
@ -335,29 +277,13 @@ var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
annotationStorage, annotationStorage,
isPrint isPrint
) { ) {
return new Promise(function (resolve, reject) { try {
// Building SVG with size of the viewport. const { svg, foreignObject, style, div } = this.createContainer(viewport);
const svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
const foreignObject = document.createElementNS(
SVG_NS,
"svg:foreignObject"
);
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
const style = document.createElement("style");
const stylePromise = getXfaLayerStyle();
foreignObject.appendChild(style);
const div = document.createElement("div");
foreignObject.appendChild(div);
stylePromise const [common, overrides] = await this.xfaStylePromise;
.then(async ([common, overrides]) => { style.textContent = `${fontRules}\n${common}\n${overrides}`;
style.textContent = fontRules + "\n" + common + "\n" + overrides;
// Rendering XFA layer as HTML.
XfaLayer.render({ XfaLayer.render({
xfa, xfa,
div, div,
@ -367,21 +293,16 @@ var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
intent: isPrint ? "print" : "display", intent: isPrint ? "print" : "display",
}); });
// Some unsupported type of images (e.g. tiff) // Some unsupported type of images (e.g. tiff) lead to errors.
// lead to errors.
await resolveImages(div, /* silentErrors = */ true); await resolveImages(div, /* silentErrors = */ true);
svg.appendChild(foreignObject); svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject); await writeSVG(svg, ctx);
}) } catch (reason) {
.catch(reason => { throw new Error(`Rasterize.xfaLayer: "${reason?.message}".`);
reject(new Error(`rasterizeXfaLayer: "${reason?.message}".`)); }
}); }
});
} }
return rasterizeXfaLayer;
})();
/** /**
* @typedef {Object} DriverOptions * @typedef {Object} DriverOptions
@ -393,17 +314,12 @@ var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
* @property {HTMLDivElement} end - Container for a completion message. * @property {HTMLDivElement} end - Container for a completion message.
*/ */
/**
* @class
*/
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
var Driver = (function DriverClosure() { class Driver {
/** /**
* @constructs Driver
* @param {DriverOptions} options * @param {DriverOptions} options
*/ */
// eslint-disable-next-line no-shadow constructor(options) {
function Driver(options) {
// Configure the global worker options. // Configure the global worker options.
GlobalWorkerOptions.workerSrc = WORKER_SRC; GlobalWorkerOptions.workerSrc = WORKER_SRC;
@ -414,7 +330,7 @@ var Driver = (function DriverClosure() {
this.end = options.end; this.end = options.end;
// Set parameters from the query string // Set parameters from the query string
var parameters = this._getQueryStringParameters(); const parameters = this._getQueryStringParameters();
this.browser = parameters.browser; this.browser = parameters.browser;
this.manifestFile = parameters.manifestFile; this.manifestFile = parameters.manifestFile;
this.delay = parameters.delay | 0 || 0; this.delay = parameters.delay | 0 || 0;
@ -428,16 +344,14 @@ var Driver = (function DriverClosure() {
this.canvas = document.createElement("canvas"); this.canvas = document.createElement("canvas");
} }
Driver.prototype = { _getQueryStringParameters() {
_getQueryStringParameters: function Driver_getQueryStringParameters() {
const queryString = window.location.search.substring(1); const queryString = window.location.search.substring(1);
return Object.fromEntries(new URLSearchParams(queryString).entries()); return Object.fromEntries(new URLSearchParams(queryString).entries());
}, }
run: function Driver_run() { run() {
var self = this; window.onerror = (message, source, line, column, error) => {
window.onerror = function (message, source, line, column, error) { this._info(
self._info(
"Error: " + "Error: " +
message + message +
" Script: " + " Script: " +
@ -454,25 +368,25 @@ var Driver = (function DriverClosure() {
this._log(`Harness thinks this browser is ${this.browser}\n`); this._log(`Harness thinks this browser is ${this.browser}\n`);
this._log('Fetching manifest "' + this.manifestFile + '"... '); this._log('Fetching manifest "' + this.manifestFile + '"... ');
var r = new XMLHttpRequest(); const r = new XMLHttpRequest();
r.open("GET", this.manifestFile, false); r.open("GET", this.manifestFile, false);
r.onreadystatechange = function () { r.onreadystatechange = () => {
if (r.readyState === 4) { if (r.readyState === 4) {
self._log("done\n"); this._log("done\n");
self.manifest = JSON.parse(r.responseText); this.manifest = JSON.parse(r.responseText);
if (self.testFilter?.length || self.xfaOnly) { if (this.testFilter?.length || this.xfaOnly) {
self.manifest = self.manifest.filter(function (item) { this.manifest = this.manifest.filter(item => {
if (self.testFilter.includes(item.id)) { if (this.testFilter.includes(item.id)) {
return true; return true;
} }
if (self.xfaOnly && item.enableXfa) { if (this.xfaOnly && item.enableXfa) {
return true; return true;
} }
return false; return false;
}); });
} }
self.currentTask = 0; this.currentTask = 0;
self._nextTask(); this._nextTask();
} }
}; };
if (this.delay > 0) { if (this.delay > 0) {
@ -483,7 +397,7 @@ var Driver = (function DriverClosure() {
setTimeout(function () { setTimeout(function () {
r.send(null); r.send(null);
}, this.delay); }, this.delay);
}, }
/** /**
* A debugging tool to log to the terminal while tests are running. * A debugging tool to log to the terminal while tests are running.
@ -501,7 +415,7 @@ var Driver = (function DriverClosure() {
} }
this._info(`${id}: ${msg}`); this._info(`${id}: ${msg}`);
}, }
_nextTask() { _nextTask() {
let failure = ""; let failure = "";
@ -570,13 +484,11 @@ var Driver = (function DriverClosure() {
} }
task.pdfDoc = doc; task.pdfDoc = doc;
task.optionalContentConfigPromise = task.optionalContentConfigPromise = doc.getOptionalContentConfig();
doc.getOptionalContentConfig();
if (task.optionalContent) { if (task.optionalContent) {
const entries = Object.entries(task.optionalContent), const entries = Object.entries(task.optionalContent),
optionalContentConfig = optionalContentConfig = await task.optionalContentConfigPromise;
await task.optionalContentConfigPromise;
for (const [id, visible] of entries) { for (const [id, visible] of entries) {
optionalContentConfig.setVisibility(id, visible); optionalContentConfig.setVisibility(id, visible);
} }
@ -595,7 +507,7 @@ var Driver = (function DriverClosure() {
} }
this._nextPage(task, failure); this._nextPage(task, failure);
}); });
}, }
_cleanup() { _cleanup() {
// Clear out all the stylesheets since a new one is created for each font. // Clear out all the stylesheets since a new one is created for each font.
@ -620,9 +532,9 @@ var Driver = (function DriverClosure() {
} }
} }
return Promise.all(destroyedPromises); return Promise.all(destroyedPromises);
}, }
_exceptionToString: function Driver_exceptionToString(e) { _exceptionToString(e) {
if (typeof e !== "object") { if (typeof e !== "object") {
return String(e); return String(e);
} }
@ -630,32 +542,31 @@ var Driver = (function DriverClosure() {
return JSON.stringify(e); return JSON.stringify(e);
} }
return e.message + ("stack" in e ? " at " + e.stack.split("\n")[0] : ""); return e.message + ("stack" in e ? " at " + e.stack.split("\n")[0] : "");
}, }
_getLastPageNumber: function Driver_getLastPageNumber(task) { _getLastPageNumber(task) {
if (!task.pdfDoc) { if (!task.pdfDoc) {
return task.firstPage || 1; return task.firstPage || 1;
} }
var lastPageNumber = task.lastPage || 0; let lastPageNumber = task.lastPage || 0;
if (!lastPageNumber || lastPageNumber > task.pdfDoc.numPages) { if (!lastPageNumber || lastPageNumber > task.pdfDoc.numPages) {
lastPageNumber = task.pdfDoc.numPages; lastPageNumber = task.pdfDoc.numPages;
} }
return lastPageNumber; return lastPageNumber;
}, }
_nextPage: function Driver_nextPage(task, loadError) { _nextPage(task, loadError) {
var self = this; let failure = loadError || "";
var failure = loadError || ""; let ctx;
var ctx;
if (!task.pdfDoc) { if (!task.pdfDoc) {
var dataUrl = this.canvas.toDataURL("image/png"); const dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, function () { this._sendResult(dataUrl, task, failure, () => {
self._log( this._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n" "done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
); );
self.currentTask++; this.currentTask++;
self._nextTask(); this._nextTask();
}); });
return; return;
} }
@ -673,11 +584,7 @@ var Driver = (function DriverClosure() {
if (task.skipPages && task.skipPages.includes(task.pageNum)) { if (task.skipPages && task.skipPages.includes(task.pageNum)) {
this._log( this._log(
" Skipping page " + " Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n"
task.pageNum +
"/" +
task.pdfDoc.numPages +
"...\n"
); );
task.pageNum++; task.pageNum++;
this._nextPage(task); this._nextPage(task);
@ -687,25 +594,21 @@ var Driver = (function DriverClosure() {
if (!failure) { if (!failure) {
try { try {
this._log( this._log(
" Loading page " + " Loading page " + task.pageNum + "/" + task.pdfDoc.numPages + "... "
task.pageNum +
"/" +
task.pdfDoc.numPages +
"... "
); );
this.canvas.mozOpaque = true; this.canvas.mozOpaque = true;
ctx = this.canvas.getContext("2d", { alpha: false }); ctx = this.canvas.getContext("2d", { alpha: false });
task.pdfDoc.getPage(task.pageNum).then( task.pdfDoc.getPage(task.pageNum).then(
function (page) { page => {
var viewport = page.getViewport({ const viewport = page.getViewport({
scale: PixelsPerInch.PDF_TO_CSS_UNITS, scale: PixelsPerInch.PDF_TO_CSS_UNITS,
}); });
self.canvas.width = viewport.width; this.canvas.width = viewport.width;
self.canvas.height = viewport.height; this.canvas.height = viewport.height;
self._clearCanvas(); this._clearCanvas();
// Initialize various `eq` test subtypes, see comment below. // Initialize various `eq` test subtypes, see comment below.
var renderAnnotations = false, let renderAnnotations = false,
renderForms = false, renderForms = false,
renderPrint = false, renderPrint = false,
renderXfa = false, renderXfa = false,
@ -719,25 +622,25 @@ var Driver = (function DriverClosure() {
} }
} }
var textLayerCanvas, annotationLayerCanvas; let textLayerCanvas, annotationLayerCanvas, annotationLayerContext;
var initPromise; let initPromise;
if (task.type === "text") { if (task.type === "text") {
// Using a dummy canvas for PDF context drawing operations // Using a dummy canvas for PDF context drawing operations
textLayerCanvas = self.textLayerCanvas; textLayerCanvas = this.textLayerCanvas;
if (!textLayerCanvas) { if (!textLayerCanvas) {
textLayerCanvas = document.createElement("canvas"); textLayerCanvas = document.createElement("canvas");
self.textLayerCanvas = textLayerCanvas; this.textLayerCanvas = textLayerCanvas;
} }
textLayerCanvas.width = viewport.width; textLayerCanvas.width = viewport.width;
textLayerCanvas.height = viewport.height; textLayerCanvas.height = viewport.height;
var textLayerContext = textLayerCanvas.getContext("2d"); const textLayerContext = textLayerCanvas.getContext("2d");
textLayerContext.clearRect( textLayerContext.clearRect(
0, 0,
0, 0,
textLayerCanvas.width, textLayerCanvas.width,
textLayerCanvas.height textLayerCanvas.height
); );
var enhanceText = !!task.enhance; const enhanceText = !!task.enhance;
// The text builder will draw its content on the test canvas // The text builder will draw its content on the test canvas
initPromise = page initPromise = page
.getTextContent({ .getTextContent({
@ -745,7 +648,7 @@ var Driver = (function DriverClosure() {
includeMarkedContent: true, includeMarkedContent: true,
}) })
.then(function (textContent) { .then(function (textContent) {
return rasterizeTextLayer( return Rasterize.textLayer(
textLayerContext, textLayerContext,
viewport, viewport,
textContent, textContent,
@ -764,15 +667,14 @@ var Driver = (function DriverClosure() {
// Render the annotation layer if necessary. // Render the annotation layer if necessary.
if (renderAnnotations || renderForms || renderXfa) { if (renderAnnotations || renderForms || renderXfa) {
// Create a dummy canvas for the drawing operations. // Create a dummy canvas for the drawing operations.
annotationLayerCanvas = self.annotationLayerCanvas; annotationLayerCanvas = this.annotationLayerCanvas;
if (!annotationLayerCanvas) { if (!annotationLayerCanvas) {
annotationLayerCanvas = document.createElement("canvas"); annotationLayerCanvas = document.createElement("canvas");
self.annotationLayerCanvas = annotationLayerCanvas; this.annotationLayerCanvas = annotationLayerCanvas;
} }
annotationLayerCanvas.width = viewport.width; annotationLayerCanvas.width = viewport.width;
annotationLayerCanvas.height = viewport.height; annotationLayerCanvas.height = viewport.height;
var annotationLayerContext = annotationLayerContext = annotationLayerCanvas.getContext("2d");
annotationLayerCanvas.getContext("2d");
annotationLayerContext.clearRect( annotationLayerContext.clearRect(
0, 0,
0, 0,
@ -787,7 +689,7 @@ var Driver = (function DriverClosure() {
annotationCanvasMap = new Map(); annotationCanvasMap = new Map();
} else { } else {
initPromise = page.getXfa().then(function (xfa) { initPromise = page.getXfa().then(function (xfa) {
return rasterizeXfaLayer( return Rasterize.xfaLayer(
annotationLayerContext, annotationLayerContext,
viewport, viewport,
xfa, xfa,
@ -802,7 +704,7 @@ var Driver = (function DriverClosure() {
initPromise = Promise.resolve(); initPromise = Promise.resolve();
} }
} }
var renderContext = { const renderContext = {
canvasContext: ctx, canvasContext: ctx,
viewport, viewport,
optionalContentConfigPromise: task.optionalContentConfigPromise, optionalContentConfigPromise: task.optionalContentConfigPromise,
@ -817,7 +719,7 @@ var Driver = (function DriverClosure() {
renderContext.intent = "print"; renderContext.intent = "print";
} }
var completeRender = function (error) { const completeRender = error => {
// if text layer is present, compose it on top of the page // if text layer is present, compose it on top of the page
if (textLayerCanvas) { if (textLayerCanvas) {
ctx.save(); ctx.save();
@ -836,7 +738,7 @@ var Driver = (function DriverClosure() {
task.stats = page.stats; task.stats = page.stats;
} }
page.cleanup(/* resetStats = */ true); page.cleanup(/* resetStats = */ true);
self._snapshot(task, error); this._snapshot(task, error);
}; };
initPromise initPromise
.then(function (data) { .then(function (data) {
@ -850,7 +752,7 @@ var Driver = (function DriverClosure() {
} }
return renderTask.promise.then(function () { return renderTask.promise.then(function () {
if (annotationCanvasMap) { if (annotationCanvasMap) {
rasterizeAnnotationLayer( Rasterize.annotationLayer(
annotationLayerContext, annotationLayerContext,
viewport, viewport,
data, data,
@ -870,8 +772,8 @@ var Driver = (function DriverClosure() {
completeRender("render : " + error); completeRender("render : " + error);
}); });
}, },
function (error) { error => {
self._snapshot(task, "render : " + error); this._snapshot(task, "render : " + error);
} }
); );
} catch (e) { } catch (e) {
@ -879,34 +781,33 @@ var Driver = (function DriverClosure() {
this._snapshot(task, failure); this._snapshot(task, failure);
} }
} }
}, }
_clearCanvas: function Driver_clearCanvas() { _clearCanvas() {
var ctx = this.canvas.getContext("2d", { alpha: false }); const ctx = this.canvas.getContext("2d", { alpha: false });
ctx.beginPath(); ctx.beginPath();
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}, }
_snapshot: function Driver_snapshot(task, failure) { _snapshot(task, failure) {
var self = this;
this._log("Snapshotting... "); this._log("Snapshotting... ");
var dataUrl = this.canvas.toDataURL("image/png"); const dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, function () { this._sendResult(dataUrl, task, failure, () => {
self._log( this._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n" "done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
); );
task.pageNum++; task.pageNum++;
self._nextPage(task); this._nextPage(task);
}); });
}, }
_quit: function Driver_quit() { _quit() {
this._log("Done !"); this._log("Done !");
this.end.textContent = "Tests finished. Close this window!"; this.end.textContent = "Tests finished. Close this window!";
// Send the quit request // Send the quit request
var r = new XMLHttpRequest(); const r = new XMLHttpRequest();
r.open("POST", `/tellMeToQuit?browser=${escape(this.browser)}`, false); r.open("POST", `/tellMeToQuit?browser=${escape(this.browser)}`, false);
r.onreadystatechange = function (e) { r.onreadystatechange = function (e) {
if (r.readyState === 4) { if (r.readyState === 4) {
@ -914,9 +815,9 @@ var Driver = (function DriverClosure() {
} }
}; };
r.send(null); r.send(null);
}, }
_info: function Driver_info(message) { _info(message) {
this._send( this._send(
"/info", "/info",
JSON.stringify({ JSON.stringify({
@ -924,9 +825,9 @@ var Driver = (function DriverClosure() {
message, message,
}) })
); );
}, }
_log: function Driver_log(message) { _log(message) {
// Using insertAdjacentHTML yields a large performance gain and // Using insertAdjacentHTML yields a large performance gain and
// reduces runtime significantly. // reduces runtime significantly.
if (this.output.insertAdjacentHTML) { if (this.output.insertAdjacentHTML) {
@ -940,19 +841,19 @@ var Driver = (function DriverClosure() {
// Scroll to the bottom of the page // Scroll to the bottom of the page
this.output.scrollTop = this.output.scrollHeight; this.output.scrollTop = this.output.scrollHeight;
} }
}, }
_done: function Driver_done() { _done() {
if (this.inFlightRequests > 0) { if (this.inFlightRequests > 0) {
this.inflight.textContent = this.inFlightRequests; this.inflight.textContent = this.inFlightRequests;
setTimeout(this._done.bind(this), WAITING_TIME); setTimeout(this._done.bind(this), WAITING_TIME);
} else { } else {
setTimeout(this._quit.bind(this), WAITING_TIME); setTimeout(this._quit.bind(this), WAITING_TIME);
} }
}, }
_sendResult: function Driver_sendResult(snapshot, task, failure, callback) { _sendResult(snapshot, task, failure, callback) {
var result = JSON.stringify({ const result = JSON.stringify({
browser: this.browser, browser: this.browser,
id: task.id, id: task.id,
numPages: task.pdfDoc ? task.lastPage || task.pdfDoc.numPages : 0, numPages: task.pdfDoc ? task.lastPage || task.pdfDoc.numPages : 0,
@ -965,21 +866,20 @@ var Driver = (function DriverClosure() {
stats: task.stats.times, stats: task.stats.times,
}); });
this._send("/submit_task_results", result, callback); this._send("/submit_task_results", result, callback);
}, }
_send: function Driver_send(url, message, callback) { _send(url, message, callback) {
var self = this; const r = new XMLHttpRequest();
var r = new XMLHttpRequest();
r.open("POST", url, true); r.open("POST", url, true);
r.setRequestHeader("Content-Type", "application/json"); r.setRequestHeader("Content-Type", "application/json");
r.onreadystatechange = function (e) { r.onreadystatechange = e => {
if (r.readyState === 4) { if (r.readyState === 4) {
self.inFlightRequests--; this.inFlightRequests--;
// Retry until successful // Retry until successful
if (r.status !== 200) { if (r.status !== 200) {
setTimeout(function () { setTimeout(() => {
self._send(url, message); this._send(url, message);
}); });
} }
if (callback) { if (callback) {
@ -989,8 +889,5 @@ var Driver = (function DriverClosure() {
}; };
this.inflight.textContent = this.inFlightRequests++; this.inflight.textContent = this.inFlightRequests++;
r.send(message); r.send(message);
}, }
}; }
return Driver;
})();