diff options
Diffstat (limited to 'devtools/shared/worker/tests/browser')
5 files changed, 158 insertions, 0 deletions
diff --git a/devtools/shared/worker/tests/browser/.eslintrc.js b/devtools/shared/worker/tests/browser/.eslintrc.js new file mode 100644 index 000000000..698ae9181 --- /dev/null +++ b/devtools/shared/worker/tests/browser/.eslintrc.js @@ -0,0 +1,6 @@ +"use strict"; + +module.exports = { + // Extend from the shared list of defined globals for mochitests. + "extends": "../../../../.eslintrc.mochitests.js" +}; diff --git a/devtools/shared/worker/tests/browser/browser.ini b/devtools/shared/worker/tests/browser/browser.ini new file mode 100644 index 000000000..a64916dff --- /dev/null +++ b/devtools/shared/worker/tests/browser/browser.ini @@ -0,0 +1,9 @@ +[DEFAULT] +tags = devtools +subsuite = devtools +support-files = + ../../../../server/tests/browser/head.js + +[browser_worker-01.js] +[browser_worker-02.js] +[browser_worker-03.js] diff --git a/devtools/shared/worker/tests/browser/browser_worker-01.js b/devtools/shared/worker/tests/browser/browser_worker-01.js new file mode 100644 index 000000000..7679e4166 --- /dev/null +++ b/devtools/shared/worker/tests/browser/browser_worker-01.js @@ -0,0 +1,45 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// Tests that the devtools/shared/worker communicates properly +// as both CommonJS module and as a JSM. + +const WORKER_URL = + "resource://devtools/client/shared/widgets/GraphsWorker.js"; + +const count = 100000; +const WORKER_DATA = (function () { + let timestamps = []; + for (let i = 0; i < count; i++) { + timestamps.push(i); + } + return timestamps; +})(); +const INTERVAL = 100; +const DURATION = 1000; + +add_task(function* () { + // Test both CJS and JSM versions + + yield testWorker("JSM", () => Cu.import("resource://devtools/shared/worker/worker.js", {})); + yield testWorker("CommonJS", () => require("devtools/shared/worker/worker")); +}); + +function* testWorker(context, workerFactory) { + let { DevToolsWorker, workerify } = workerFactory(); + let worker = new DevToolsWorker(WORKER_URL); + let results = yield worker.performTask("plotTimestampsGraph", { + timestamps: WORKER_DATA, + interval: INTERVAL, + duration: DURATION + }); + + ok(results.plottedData.length, + `worker should have returned an object with array properties in ${context}`); + + let fn = workerify(function (x) { return x * x; }); + is((yield fn(5)), 25, `workerify works in ${context}`); + fn.destroy(); + + worker.destroy(); +} diff --git a/devtools/shared/worker/tests/browser/browser_worker-02.js b/devtools/shared/worker/tests/browser/browser_worker-02.js new file mode 100644 index 000000000..e6a9a54cf --- /dev/null +++ b/devtools/shared/worker/tests/browser/browser_worker-02.js @@ -0,0 +1,46 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// Tests errors are handled properly by the DevToolsWorker. + +const { DevToolsWorker } = require("devtools/shared/worker/worker"); +const WORKER_URL = + "resource://devtools/client/shared/widgets/GraphsWorker.js"; + +add_task(function* () { + try { + let workerNotFound = new DevToolsWorker("resource://i/dont/exist.js"); + ok(false, "Creating a DevToolsWorker with an invalid URL throws"); + } catch (e) { + ok(true, "Creating a DevToolsWorker with an invalid URL throws"); + } + + let worker = new DevToolsWorker(WORKER_URL); + try { + // plotTimestampsGraph requires timestamp, interval an duration props on the object + // passed in so there should be an error thrown in the worker + let results = yield worker.performTask("plotTimestampsGraph", {}); + ok(false, "DevToolsWorker returns a rejected promise when an error occurs in the worker"); + } catch (e) { + ok(true, "DevToolsWorker returns a rejected promise when an error occurs in the worker"); + } + + try { + let results = yield worker.performTask("not a real task"); + ok(false, "DevToolsWorker returns a rejected promise when task does not exist"); + } catch (e) { + ok(true, "DevToolsWorker returns a rejected promise when task does not exist"); + } + + worker.destroy(); + try { + let results = yield worker.performTask("plotTimestampsGraph", { + timestamps: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + interval: 1, + duration: 1 + }); + ok(false, "DevToolsWorker rejects when performing a task on a destroyed worker"); + } catch (e) { + ok(true, "DevToolsWorker rejects when performing a task on a destroyed worker"); + } +}); diff --git a/devtools/shared/worker/tests/browser/browser_worker-03.js b/devtools/shared/worker/tests/browser/browser_worker-03.js new file mode 100644 index 000000000..053e381b9 --- /dev/null +++ b/devtools/shared/worker/tests/browser/browser_worker-03.js @@ -0,0 +1,52 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// Tests that the devtools/shared/worker can handle: +// returned primitives (or promise or Error) +// +// And tests `workerify` by doing so. + +const { DevToolsWorker, workerify } = require("devtools/shared/worker/worker"); +function square(x) { + return x * x; +} + +function squarePromise(x) { + return new Promise((resolve) => resolve(x * x)); +} + +function squareError(x) { + return new Error("Nope"); +} + +function squarePromiseReject(x) { + return new Promise((_, reject) => reject("Nope")); +} + +add_task(function* () { + let fn = workerify(square); + is((yield fn(5)), 25, "return primitives successful"); + fn.destroy(); + + fn = workerify(squarePromise); + is((yield fn(5)), 25, "promise primitives successful"); + fn.destroy(); + + fn = workerify(squareError); + try { + yield fn(5); + ok(false, "return error should reject"); + } catch (e) { + ok(true, "return error should reject"); + } + fn.destroy(); + + fn = workerify(squarePromiseReject); + try { + yield fn(5); + ok(false, "returned rejected promise rejects"); + } catch (e) { + ok(true, "returned rejected promise rejects"); + } + fn.destroy(); +}); |