browser/components/search/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/search/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +XPCOMUtils.defineLazyModuleGetter(this, "Promise",
     1.8 +  "resource://gre/modules/Promise.jsm");
     1.9 +
    1.10 +function whenNewWindowLoaded(aOptions, aCallback) {
    1.11 +  let win = OpenBrowserWindow(aOptions);
    1.12 +  let gotLoad = false;
    1.13 +  let gotActivate = (Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager).activeWindow == win);
    1.14 +
    1.15 +  function maybeRunCallback() {
    1.16 +    if (gotLoad && gotActivate) {
    1.17 +      win.BrowserChromeTest.runWhenReady(function() {
    1.18 +        executeSoon(function() { aCallback(win); });
    1.19 +      });
    1.20 +    }
    1.21 +  }
    1.22 +
    1.23 +  if (!gotActivate) {
    1.24 +    win.addEventListener("activate", function onActivate() {
    1.25 +      info("Got activate.");
    1.26 +      win.removeEventListener("activate", onActivate, false);
    1.27 +      gotActivate = true;
    1.28 +      maybeRunCallback();
    1.29 +    }, false);
    1.30 +  } else {
    1.31 +    info("Was activated.");
    1.32 +  }
    1.33 +
    1.34 +  win.addEventListener("load", function onLoad() {
    1.35 +    info("Got load");
    1.36 +    win.removeEventListener("load", onLoad, false);
    1.37 +    gotLoad = true;
    1.38 +    maybeRunCallback();
    1.39 +  }, false);
    1.40 +  return win;
    1.41 +}
    1.42 +
    1.43 +/**
    1.44 + * Recursively compare two objects and check that every property of expectedObj has the same value
    1.45 + * on actualObj.
    1.46 + */
    1.47 +function isSubObjectOf(expectedObj, actualObj, name) {
    1.48 +  for (let prop in expectedObj) {
    1.49 +    if (typeof expectedObj[prop] == 'function')
    1.50 +      continue;
    1.51 +    if (expectedObj[prop] instanceof Object) {
    1.52 +      is(actualObj[prop].length, expectedObj[prop].length, name + "[" + prop + "]");
    1.53 +      isSubObjectOf(expectedObj[prop], actualObj[prop], name + "[" + prop + "]");
    1.54 +    } else {
    1.55 +      is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]");
    1.56 +    }
    1.57 +  }
    1.58 +}
    1.59 +
    1.60 +function getLocale() {
    1.61 +  const localePref = "general.useragent.locale";
    1.62 +  return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref));
    1.63 +}
    1.64 +
    1.65 +/**
    1.66 + * Wrapper for nsIPrefBranch::getComplexValue.
    1.67 + * @param aPrefName
    1.68 + *        The name of the pref to get.
    1.69 + * @returns aDefault if the requested pref doesn't exist.
    1.70 + */
    1.71 +function getLocalizedPref(aPrefName, aDefault) {
    1.72 +  try {
    1.73 +    return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
    1.74 +  } catch (ex) {
    1.75 +    return aDefault;
    1.76 +  }
    1.77 +
    1.78 +  return aDefault;
    1.79 +}
    1.80 +
    1.81 +function waitForPopupShown(aPopupId, aCallback) {
    1.82 +  let popup = document.getElementById(aPopupId);
    1.83 +  info("waitForPopupShown: got popup: " + popup.id);
    1.84 +  function onPopupShown() {
    1.85 +    info("onPopupShown");
    1.86 +    removePopupShownListener();
    1.87 +    SimpleTest.executeSoon(aCallback);
    1.88 +  }
    1.89 +  function removePopupShownListener() {
    1.90 +    popup.removeEventListener("popupshown", onPopupShown);
    1.91 +  }
    1.92 +  popup.addEventListener("popupshown", onPopupShown);
    1.93 +  registerCleanupFunction(removePopupShownListener);
    1.94 +}
    1.95 +
    1.96 +function* promiseEvent(aTarget, aEventName, aPreventDefault) {
    1.97 +  let deferred = Promise.defer();
    1.98 +  aTarget.addEventListener(aEventName, function onEvent(aEvent) {
    1.99 +    aTarget.removeEventListener(aEventName, onEvent, true);
   1.100 +    if (aPreventDefault) {
   1.101 +      aEvent.preventDefault();
   1.102 +    }
   1.103 +    deferred.resolve();
   1.104 +  }, true);
   1.105 +  return deferred.promise;
   1.106 +}
   1.107 +
   1.108 +function waitForBrowserContextMenu(aCallback) {
   1.109 +  waitForPopupShown(gBrowser.selectedBrowser.contextMenu, aCallback);
   1.110 +}
   1.111 +
   1.112 +function doOnloadOnce(aCallback) {
   1.113 +  function doOnloadOnceListener(aEvent) {
   1.114 +    info("doOnloadOnce: " + aEvent.originalTarget.location);
   1.115 +    removeDoOnloadOnceListener();
   1.116 +    SimpleTest.executeSoon(function doOnloadOnceCallback() {
   1.117 +      aCallback(aEvent);
   1.118 +    });
   1.119 +  }
   1.120 +  function removeDoOnloadOnceListener() {
   1.121 +    gBrowser.removeEventListener("load", doOnloadOnceListener, true);
   1.122 +  }
   1.123 +  gBrowser.addEventListener("load", doOnloadOnceListener, true);
   1.124 +  registerCleanupFunction(removeDoOnloadOnceListener);
   1.125 +}
   1.126 +
   1.127 +function* promiseOnLoad() {
   1.128 +  let deferred = Promise.defer();
   1.129 +
   1.130 +  gBrowser.addEventListener("load", function onLoadListener(aEvent) {
   1.131 +    info("onLoadListener: " + aEvent.originalTarget.location);
   1.132 +    gBrowser.removeEventListener("load", onLoadListener, true);
   1.133 +    deferred.resolve(aEvent);
   1.134 +  }, true);
   1.135 +
   1.136 +  return deferred.promise;
   1.137 +}
   1.138 +

mercurial