browser/components/search/test/head.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 5 "resource://gre/modules/Promise.jsm");
michael@0 6
michael@0 7 function whenNewWindowLoaded(aOptions, aCallback) {
michael@0 8 let win = OpenBrowserWindow(aOptions);
michael@0 9 let gotLoad = false;
michael@0 10 let gotActivate = (Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager).activeWindow == win);
michael@0 11
michael@0 12 function maybeRunCallback() {
michael@0 13 if (gotLoad && gotActivate) {
michael@0 14 win.BrowserChromeTest.runWhenReady(function() {
michael@0 15 executeSoon(function() { aCallback(win); });
michael@0 16 });
michael@0 17 }
michael@0 18 }
michael@0 19
michael@0 20 if (!gotActivate) {
michael@0 21 win.addEventListener("activate", function onActivate() {
michael@0 22 info("Got activate.");
michael@0 23 win.removeEventListener("activate", onActivate, false);
michael@0 24 gotActivate = true;
michael@0 25 maybeRunCallback();
michael@0 26 }, false);
michael@0 27 } else {
michael@0 28 info("Was activated.");
michael@0 29 }
michael@0 30
michael@0 31 win.addEventListener("load", function onLoad() {
michael@0 32 info("Got load");
michael@0 33 win.removeEventListener("load", onLoad, false);
michael@0 34 gotLoad = true;
michael@0 35 maybeRunCallback();
michael@0 36 }, false);
michael@0 37 return win;
michael@0 38 }
michael@0 39
michael@0 40 /**
michael@0 41 * Recursively compare two objects and check that every property of expectedObj has the same value
michael@0 42 * on actualObj.
michael@0 43 */
michael@0 44 function isSubObjectOf(expectedObj, actualObj, name) {
michael@0 45 for (let prop in expectedObj) {
michael@0 46 if (typeof expectedObj[prop] == 'function')
michael@0 47 continue;
michael@0 48 if (expectedObj[prop] instanceof Object) {
michael@0 49 is(actualObj[prop].length, expectedObj[prop].length, name + "[" + prop + "]");
michael@0 50 isSubObjectOf(expectedObj[prop], actualObj[prop], name + "[" + prop + "]");
michael@0 51 } else {
michael@0 52 is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]");
michael@0 53 }
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 function getLocale() {
michael@0 58 const localePref = "general.useragent.locale";
michael@0 59 return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref));
michael@0 60 }
michael@0 61
michael@0 62 /**
michael@0 63 * Wrapper for nsIPrefBranch::getComplexValue.
michael@0 64 * @param aPrefName
michael@0 65 * The name of the pref to get.
michael@0 66 * @returns aDefault if the requested pref doesn't exist.
michael@0 67 */
michael@0 68 function getLocalizedPref(aPrefName, aDefault) {
michael@0 69 try {
michael@0 70 return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
michael@0 71 } catch (ex) {
michael@0 72 return aDefault;
michael@0 73 }
michael@0 74
michael@0 75 return aDefault;
michael@0 76 }
michael@0 77
michael@0 78 function waitForPopupShown(aPopupId, aCallback) {
michael@0 79 let popup = document.getElementById(aPopupId);
michael@0 80 info("waitForPopupShown: got popup: " + popup.id);
michael@0 81 function onPopupShown() {
michael@0 82 info("onPopupShown");
michael@0 83 removePopupShownListener();
michael@0 84 SimpleTest.executeSoon(aCallback);
michael@0 85 }
michael@0 86 function removePopupShownListener() {
michael@0 87 popup.removeEventListener("popupshown", onPopupShown);
michael@0 88 }
michael@0 89 popup.addEventListener("popupshown", onPopupShown);
michael@0 90 registerCleanupFunction(removePopupShownListener);
michael@0 91 }
michael@0 92
michael@0 93 function* promiseEvent(aTarget, aEventName, aPreventDefault) {
michael@0 94 let deferred = Promise.defer();
michael@0 95 aTarget.addEventListener(aEventName, function onEvent(aEvent) {
michael@0 96 aTarget.removeEventListener(aEventName, onEvent, true);
michael@0 97 if (aPreventDefault) {
michael@0 98 aEvent.preventDefault();
michael@0 99 }
michael@0 100 deferred.resolve();
michael@0 101 }, true);
michael@0 102 return deferred.promise;
michael@0 103 }
michael@0 104
michael@0 105 function waitForBrowserContextMenu(aCallback) {
michael@0 106 waitForPopupShown(gBrowser.selectedBrowser.contextMenu, aCallback);
michael@0 107 }
michael@0 108
michael@0 109 function doOnloadOnce(aCallback) {
michael@0 110 function doOnloadOnceListener(aEvent) {
michael@0 111 info("doOnloadOnce: " + aEvent.originalTarget.location);
michael@0 112 removeDoOnloadOnceListener();
michael@0 113 SimpleTest.executeSoon(function doOnloadOnceCallback() {
michael@0 114 aCallback(aEvent);
michael@0 115 });
michael@0 116 }
michael@0 117 function removeDoOnloadOnceListener() {
michael@0 118 gBrowser.removeEventListener("load", doOnloadOnceListener, true);
michael@0 119 }
michael@0 120 gBrowser.addEventListener("load", doOnloadOnceListener, true);
michael@0 121 registerCleanupFunction(removeDoOnloadOnceListener);
michael@0 122 }
michael@0 123
michael@0 124 function* promiseOnLoad() {
michael@0 125 let deferred = Promise.defer();
michael@0 126
michael@0 127 gBrowser.addEventListener("load", function onLoadListener(aEvent) {
michael@0 128 info("onLoadListener: " + aEvent.originalTarget.location);
michael@0 129 gBrowser.removeEventListener("load", onLoadListener, true);
michael@0 130 deferred.resolve(aEvent);
michael@0 131 }, true);
michael@0 132
michael@0 133 return deferred.promise;
michael@0 134 }
michael@0 135

mercurial