michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Promise", michael@0: "resource://gre/modules/Promise.jsm"); michael@0: michael@0: function whenNewWindowLoaded(aOptions, aCallback) { michael@0: let win = OpenBrowserWindow(aOptions); michael@0: let gotLoad = false; michael@0: let gotActivate = (Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager).activeWindow == win); michael@0: michael@0: function maybeRunCallback() { michael@0: if (gotLoad && gotActivate) { michael@0: win.BrowserChromeTest.runWhenReady(function() { michael@0: executeSoon(function() { aCallback(win); }); michael@0: }); michael@0: } michael@0: } michael@0: michael@0: if (!gotActivate) { michael@0: win.addEventListener("activate", function onActivate() { michael@0: info("Got activate."); michael@0: win.removeEventListener("activate", onActivate, false); michael@0: gotActivate = true; michael@0: maybeRunCallback(); michael@0: }, false); michael@0: } else { michael@0: info("Was activated."); michael@0: } michael@0: michael@0: win.addEventListener("load", function onLoad() { michael@0: info("Got load"); michael@0: win.removeEventListener("load", onLoad, false); michael@0: gotLoad = true; michael@0: maybeRunCallback(); michael@0: }, false); michael@0: return win; michael@0: } michael@0: michael@0: /** michael@0: * Recursively compare two objects and check that every property of expectedObj has the same value michael@0: * on actualObj. michael@0: */ michael@0: function isSubObjectOf(expectedObj, actualObj, name) { michael@0: for (let prop in expectedObj) { michael@0: if (typeof expectedObj[prop] == 'function') michael@0: continue; michael@0: if (expectedObj[prop] instanceof Object) { michael@0: is(actualObj[prop].length, expectedObj[prop].length, name + "[" + prop + "]"); michael@0: isSubObjectOf(expectedObj[prop], actualObj[prop], name + "[" + prop + "]"); michael@0: } else { michael@0: is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function getLocale() { michael@0: const localePref = "general.useragent.locale"; michael@0: return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref)); michael@0: } michael@0: michael@0: /** michael@0: * Wrapper for nsIPrefBranch::getComplexValue. michael@0: * @param aPrefName michael@0: * The name of the pref to get. michael@0: * @returns aDefault if the requested pref doesn't exist. michael@0: */ michael@0: function getLocalizedPref(aPrefName, aDefault) { michael@0: try { michael@0: return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data; michael@0: } catch (ex) { michael@0: return aDefault; michael@0: } michael@0: michael@0: return aDefault; michael@0: } michael@0: michael@0: function waitForPopupShown(aPopupId, aCallback) { michael@0: let popup = document.getElementById(aPopupId); michael@0: info("waitForPopupShown: got popup: " + popup.id); michael@0: function onPopupShown() { michael@0: info("onPopupShown"); michael@0: removePopupShownListener(); michael@0: SimpleTest.executeSoon(aCallback); michael@0: } michael@0: function removePopupShownListener() { michael@0: popup.removeEventListener("popupshown", onPopupShown); michael@0: } michael@0: popup.addEventListener("popupshown", onPopupShown); michael@0: registerCleanupFunction(removePopupShownListener); michael@0: } michael@0: michael@0: function* promiseEvent(aTarget, aEventName, aPreventDefault) { michael@0: let deferred = Promise.defer(); michael@0: aTarget.addEventListener(aEventName, function onEvent(aEvent) { michael@0: aTarget.removeEventListener(aEventName, onEvent, true); michael@0: if (aPreventDefault) { michael@0: aEvent.preventDefault(); michael@0: } michael@0: deferred.resolve(); michael@0: }, true); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function waitForBrowserContextMenu(aCallback) { michael@0: waitForPopupShown(gBrowser.selectedBrowser.contextMenu, aCallback); michael@0: } michael@0: michael@0: function doOnloadOnce(aCallback) { michael@0: function doOnloadOnceListener(aEvent) { michael@0: info("doOnloadOnce: " + aEvent.originalTarget.location); michael@0: removeDoOnloadOnceListener(); michael@0: SimpleTest.executeSoon(function doOnloadOnceCallback() { michael@0: aCallback(aEvent); michael@0: }); michael@0: } michael@0: function removeDoOnloadOnceListener() { michael@0: gBrowser.removeEventListener("load", doOnloadOnceListener, true); michael@0: } michael@0: gBrowser.addEventListener("load", doOnloadOnceListener, true); michael@0: registerCleanupFunction(removeDoOnloadOnceListener); michael@0: } michael@0: michael@0: function* promiseOnLoad() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: gBrowser.addEventListener("load", function onLoadListener(aEvent) { michael@0: info("onLoadListener: " + aEvent.originalTarget.location); michael@0: gBrowser.removeEventListener("load", onLoadListener, true); michael@0: deferred.resolve(aEvent); michael@0: }, true); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: