Convert the Driver class to ES6 syntax in test/driver.js

This commit is contained in:
Tim van der Meij 2021-12-04 15:21:04 +01:00
parent 3264d72e60
commit a58700b0dc
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -393,17 +393,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;
@ -428,13 +423,12 @@ 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; var self = this;
window.onerror = function (message, source, line, column, error) { window.onerror = function (message, source, line, column, error) {
self._info( self._info(
@ -483,7 +477,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 +495,7 @@ var Driver = (function DriverClosure() {
} }
this._info(`${id}: ${msg}`); this._info(`${id}: ${msg}`);
}, }
_nextTask() { _nextTask() {
let failure = ""; let failure = "";
@ -570,13 +564,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 +587,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 +612,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,9 +622,9 @@ 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;
} }
@ -641,9 +633,9 @@ var Driver = (function DriverClosure() {
lastPageNumber = task.pdfDoc.numPages; lastPageNumber = task.pdfDoc.numPages;
} }
return lastPageNumber; return lastPageNumber;
}, }
_nextPage: function Driver_nextPage(task, loadError) { _nextPage(task, loadError) {
var self = this; var self = this;
var failure = loadError || ""; var failure = loadError || "";
var ctx; var ctx;
@ -673,11 +665,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,11 +675,7 @@ 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 });
@ -879,15 +863,15 @@ 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 }); var 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; var self = this;
this._log("Snapshotting... "); this._log("Snapshotting... ");
@ -899,9 +883,9 @@ var Driver = (function DriverClosure() {
task.pageNum++; task.pageNum++;
self._nextPage(task); self._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!";
@ -914,9 +898,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 +908,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,18 +924,18 @@ 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({ var result = JSON.stringify({
browser: this.browser, browser: this.browser,
id: task.id, id: task.id,
@ -965,9 +949,9 @@ 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; var self = this;
var r = new XMLHttpRequest(); var r = new XMLHttpRequest();
r.open("POST", url, true); r.open("POST", url, true);
@ -989,8 +973,5 @@ var Driver = (function DriverClosure() {
}; };
this.inflight.textContent = this.inFlightRequests++; this.inflight.textContent = this.inFlightRequests++;
r.send(message); r.send(message);
}, }
}; }
return Driver;
})();