Skip to content

Instantly share code, notes, and snippets.

@bobsilverberg
Created November 6, 2012 16:07
Show Gist options
  • Save bobsilverberg/4025699 to your computer and use it in GitHub Desktop.
Save bobsilverberg/4025699 to your computer and use it in GitHub Desktop.
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* @fileoverview
* The EventsAPI The ToolbarAPI adds support for waiting for events to occur.
*
* @version 1.0.0
*/
// Include required modules
//var utils = require("utils");
/**
* Wait for a set of events to fire on a node
*
* @param {object} elem
* An element on which events are expected to fire.
* @param [string] events
* An array of event names.
* @param {Function} aCallback
* A callback function to execute while waiting for events.
* @returns {Boolean} Did all the events fire?
*/
function waitForEvents(elem, events, aCallback) {
if (elem.getNode != undefined)
elem = elem.getNode();
this.events = events;
this.elem = elem;
this.firedEvents = {};
this.registry = {};
dump(" *** this.elem: " + this.elem + " *** ");
dump(" *** this.events: " + this.events + " *** ");
dump(" *** this.firedEvents: " + this.firedEvents + " *** ");
dump(" *** this.registry: " + this.registry + " *** ");
for each(e in events) {
dump(" *** e: " + e + " *** ");
var listener = function(event) {
this.firedEvents[event.type] = true;;
}
this.registry[e] = listener;
dump(" *** this.registry[e]: " + this.registry[e] + " *** ");
//this.registry[e].result = false;
this.elem.addEventListener(e, this.registry[e], false);
}
try {
dump(" *** before callback *** ");
aCallback();
dump(" *** after callback *** ");
dump(" *** this.registry: " + this.registry + " *** ");
dump(" *** this.registry['TabOpen']: " + this.registry['TabOpen'] + " *** ");
mozmill.utils.waitFor(function () {
dump(" *** inside waitFor *** ");
dump(" *** this.registry: " + this.registry + " *** ");
for (var e in this.registry) {
dump(" *** e: " + e + " *** ");
dump(" *** this.elem.firedEvents[e]: " + this.elem.firedEvents[e] + " *** ");
if (!this.firedEvents[e])
return false;
}
return true;
}, "Expected all events to fire before timeout.");
return true;
} catch (e) {
return false;
} finally {
for (var e in this.registry) {
this.elem.removeEventListener(e, this.registry[e], false);
}
}
}
// Export of functions
exports.waitForEvents = waitForEvents;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment