summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/test/helpers/event-utils.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /devtools/client/performance/test/helpers/event-utils.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/client/performance/test/helpers/event-utils.js')
-rw-r--r--devtools/client/performance/test/helpers/event-utils.js114
1 files changed, 114 insertions, 0 deletions
diff --git a/devtools/client/performance/test/helpers/event-utils.js b/devtools/client/performance/test/helpers/event-utils.js
new file mode 100644
index 000000000..aa184accc
--- /dev/null
+++ b/devtools/client/performance/test/helpers/event-utils.js
@@ -0,0 +1,114 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+/* globals dump */
+
+const Services = require("Services");
+
+const KNOWN_EE_APIS = [
+ ["on", "off"],
+ ["addEventListener", "removeEventListener"],
+ ["addListener", "removeListener"]
+];
+
+/**
+ * Listens for any event for a single time on a target, no matter what kind of
+ * event emitter it is, returning a promise resolved with the passed arguments
+ * once the event is fired.
+ */
+exports.once = function (target, eventName, options = {}) {
+ return exports.times(target, eventName, 1, options);
+};
+
+/**
+ * Waits for any event to be fired a specified amount of times on a target, no
+ * matter what kind of event emitter.
+ * Possible options: `useCapture`, `spreadArgs`, `expectedArgs`
+ */
+exports.times = function (target, eventName, receiveCount, options = {}) {
+ let msg = `Waiting for event: '${eventName}' on ${target} for ${receiveCount} time(s)`;
+ if ("expectedArgs" in options) {
+ dump(`${msg} with arguments: ${JSON.stringify(options.expectedArgs)}.\n`);
+ } else {
+ dump(`${msg}.\n`);
+ }
+
+ return new Promise((resolve, reject) => {
+ if (typeof eventName != "string") {
+ reject(new Error(`Unexpected event name: ${eventName}.`));
+ }
+
+ let API = KNOWN_EE_APIS.find(([a, r]) => (a in target) && (r in target));
+ if (!API) {
+ reject(new Error("Target is not a supported event listener."));
+ return;
+ }
+
+ let [add, remove] = API;
+
+ target[add](eventName, function onEvent(...args) {
+ if ("expectedArgs" in options) {
+ for (let index of Object.keys(options.expectedArgs)) {
+ if (
+ // Expected argument matches this regexp.
+ (options.expectedArgs[index] instanceof RegExp &&
+ !options.expectedArgs[index].exec(args[index])) ||
+ // Expected argument is not a regexp and equal to the received arg.
+ (!(options.expectedArgs[index] instanceof RegExp) &&
+ options.expectedArgs[index] != args[index])
+ ) {
+ dump(`Ignoring event '${eventName}' with unexpected argument at index ` +
+ `${index}: ${args[index]}\n`);
+ return;
+ }
+ }
+ }
+ if (--receiveCount > 0) {
+ dump(`Event: '${eventName}' on ${target} needs to be fired ${receiveCount} ` +
+ `more time(s).\n`);
+ } else if (!receiveCount) {
+ dump(`Event: '${eventName}' on ${target} received.\n`);
+ target[remove](eventName, onEvent, options.useCapture);
+ resolve(options.spreadArgs ? args : args[0]);
+ }
+ }, options.useCapture);
+ });
+};
+
+/**
+ * Like `once`, but for observer notifications.
+ */
+exports.observeOnce = function (notificationName, options = {}) {
+ return exports.observeTimes(notificationName, 1, options);
+};
+
+/**
+ * Like `times`, but for observer notifications.
+ * Possible options: `expectedSubject`
+ */
+exports.observeTimes = function (notificationName, receiveCount, options = {}) {
+ dump(`Waiting for notification: '${notificationName}' for ${receiveCount} time(s).\n`);
+
+ return new Promise((resolve, reject) => {
+ if (typeof notificationName != "string") {
+ reject(new Error(`Unexpected notification name: ${notificationName}.`));
+ }
+
+ Services.obs.addObserver(function onObserve(subject, topic, data) {
+ if ("expectedSubject" in options && options.expectedSubject != subject) {
+ dump(`Ignoring notification '${notificationName}' with unexpected subject: ` +
+ `${subject}\n`);
+ return;
+ }
+ if (--receiveCount > 0) {
+ dump(`Notification: '${notificationName}' needs to be fired ${receiveCount} ` +
+ `more time(s).\n`);
+ } else if (!receiveCount) {
+ dump(`Notification: '${notificationName}' received.\n`);
+ Services.obs.removeObserver(onObserve, topic);
+ resolve(data);
+ }
+ }, notificationName, false);
+ });
+};