browser/modules/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/modules/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,175 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/Promise.jsm");
     1.8 +
     1.9 +function waitForCondition(condition, nextTest, errorMsg) {
    1.10 +  var tries = 0;
    1.11 +  var interval = setInterval(function() {
    1.12 +    if (tries >= 30) {
    1.13 +      ok(false, errorMsg);
    1.14 +      moveOn();
    1.15 +    }
    1.16 +    var conditionPassed;
    1.17 +    try {
    1.18 +      conditionPassed = condition();
    1.19 +    } catch (e) {
    1.20 +      ok(false, e + "\n" + e.stack);
    1.21 +      conditionPassed = false;
    1.22 +    }
    1.23 +    if (conditionPassed) {
    1.24 +      moveOn();
    1.25 +    }
    1.26 +    tries++;
    1.27 +  }, 100);
    1.28 +  var moveOn = function() { clearInterval(interval); nextTest(); };
    1.29 +}
    1.30 +
    1.31 +function is_hidden(element) {
    1.32 +  var style = element.ownerDocument.defaultView.getComputedStyle(element, "");
    1.33 +  if (style.display == "none")
    1.34 +    return true;
    1.35 +  if (style.visibility != "visible")
    1.36 +    return true;
    1.37 +  if (style.display == "-moz-popup")
    1.38 +    return ["hiding","closed"].indexOf(element.state) != -1;
    1.39 +
    1.40 +  // Hiding a parent element will hide all its children
    1.41 +  if (element.parentNode != element.ownerDocument)
    1.42 +    return is_hidden(element.parentNode);
    1.43 +
    1.44 +  return false;
    1.45 +}
    1.46 +
    1.47 +function is_element_visible(element, msg) {
    1.48 +  isnot(element, null, "Element should not be null, when checking visibility");
    1.49 +  ok(!is_hidden(element), msg);
    1.50 +}
    1.51 +
    1.52 +function waitForElementToBeVisible(element, nextTest, msg) {
    1.53 +  waitForCondition(() => !is_hidden(element),
    1.54 +                   () => {
    1.55 +                     ok(true, msg);
    1.56 +                     nextTest();
    1.57 +                   },
    1.58 +                   "Timeout waiting for visibility: " + msg);
    1.59 +}
    1.60 +
    1.61 +function waitForElementToBeHidden(element, nextTest, msg) {
    1.62 +  waitForCondition(() => is_hidden(element),
    1.63 +                   () => {
    1.64 +                     ok(true, msg);
    1.65 +                     nextTest();
    1.66 +                   },
    1.67 +                   "Timeout waiting for invisibility: " + msg);
    1.68 +}
    1.69 +
    1.70 +function waitForPopupAtAnchor(popup, anchorNode, nextTest, msg) {
    1.71 +  waitForCondition(() => popup.popupBoxObject.anchorNode == anchorNode,
    1.72 +                   () => {
    1.73 +                     ok(true, msg);
    1.74 +                     is_element_visible(popup, "Popup should be visible");
    1.75 +                     nextTest();
    1.76 +                   },
    1.77 +                   "Timeout waiting for popup at anchor: " + msg);
    1.78 +}
    1.79 +
    1.80 +function promisePanelShown(win) {
    1.81 +  let panelEl = win.PanelUI.panel;
    1.82 +  return promisePanelElementShown(win, panelEl);
    1.83 +}
    1.84 +
    1.85 +function promisePanelElementShown(win, aPanel) {
    1.86 +  let deferred = Promise.defer();
    1.87 +  let timeoutId = win.setTimeout(() => {
    1.88 +    deferred.reject("Panel did not show within 5 seconds.");
    1.89 +  }, 5000);
    1.90 +  aPanel.addEventListener("popupshown", function onPanelOpen(e) {
    1.91 +    aPanel.removeEventListener("popupshown", onPanelOpen);
    1.92 +    win.clearTimeout(timeoutId);
    1.93 +    deferred.resolve();
    1.94 +  });
    1.95 +  return deferred.promise;
    1.96 +}
    1.97 +
    1.98 +function is_element_hidden(element, msg) {
    1.99 +  isnot(element, null, "Element should not be null, when checking visibility");
   1.100 +  ok(is_hidden(element), msg);
   1.101 +}
   1.102 +
   1.103 +function loadUITourTestPage(callback, host = "https://example.com/") {
   1.104 +  if (gTestTab)
   1.105 +    gBrowser.removeTab(gTestTab);
   1.106 +
   1.107 +  let url = getRootDirectory(gTestPath) + "uitour.html";
   1.108 +  url = url.replace("chrome://mochitests/content/", host);
   1.109 +
   1.110 +  gTestTab = gBrowser.addTab(url);
   1.111 +  gBrowser.selectedTab = gTestTab;
   1.112 +
   1.113 +  gTestTab.linkedBrowser.addEventListener("load", function onLoad() {
   1.114 +    gTestTab.linkedBrowser.removeEventListener("load", onLoad, true);
   1.115 +
   1.116 +    gContentWindow = Components.utils.waiveXrays(gTestTab.linkedBrowser.contentDocument.defaultView);
   1.117 +    gContentAPI = gContentWindow.Mozilla.UITour;
   1.118 +
   1.119 +    waitForFocus(callback, gContentWindow);
   1.120 +  }, true);
   1.121 +}
   1.122 +
   1.123 +function UITourTest() {
   1.124 +  Services.prefs.setBoolPref("browser.uitour.enabled", true);
   1.125 +  let testUri = Services.io.newURI("http://example.com", null, null);
   1.126 +  Services.perms.add(testUri, "uitour", Services.perms.ALLOW_ACTION);
   1.127 +
   1.128 +  waitForExplicitFinish();
   1.129 +
   1.130 +  registerCleanupFunction(function() {
   1.131 +    delete window.UITour;
   1.132 +    delete window.gContentWindow;
   1.133 +    delete window.gContentAPI;
   1.134 +    if (gTestTab)
   1.135 +      gBrowser.removeTab(gTestTab);
   1.136 +    delete window.gTestTab;
   1.137 +    Services.prefs.clearUserPref("browser.uitour.enabled", true);
   1.138 +    Services.perms.remove("example.com", "uitour");
   1.139 +  });
   1.140 +
   1.141 +  function done() {
   1.142 +    executeSoon(() => {
   1.143 +      if (gTestTab)
   1.144 +        gBrowser.removeTab(gTestTab);
   1.145 +      gTestTab = null;
   1.146 +
   1.147 +      let highlight = document.getElementById("UITourHighlightContainer");
   1.148 +      is_element_hidden(highlight, "Highlight should be closed/hidden after UITour tab is closed");
   1.149 +
   1.150 +      let tooltip = document.getElementById("UITourTooltip");
   1.151 +      is_element_hidden(tooltip, "Tooltip should be closed/hidden after UITour tab is closed");
   1.152 +
   1.153 +      ok(!PanelUI.panel.hasAttribute("noautohide"), "@noautohide on the menu panel should have been cleaned up");
   1.154 +      ok(!PanelUI.panel.hasAttribute("panelopen"), "The panel shouldn't have @panelopen");
   1.155 +      isnot(PanelUI.panel.state, "open", "The panel shouldn't be open");
   1.156 +      is(document.getElementById("PanelUI-menu-button").hasAttribute("open"), false, "Menu button should know that the menu is closed");
   1.157 +
   1.158 +      is(UITour.pinnedTabs.get(window), null, "Any pinned tab should be closed after UITour tab is closed");
   1.159 +
   1.160 +      executeSoon(nextTest);
   1.161 +    });
   1.162 +  }
   1.163 +
   1.164 +  function nextTest() {
   1.165 +    if (tests.length == 0) {
   1.166 +      finish();
   1.167 +      return;
   1.168 +    }
   1.169 +    let test = tests.shift();
   1.170 +    info("Starting " + test.name);
   1.171 +    waitForFocus(function() {
   1.172 +      loadUITourTestPage(function() {
   1.173 +        test(done);
   1.174 +      });
   1.175 +    });
   1.176 +  }
   1.177 +  nextTest();
   1.178 +}

mercurial