Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 5 | |
michael@0 | 6 | function waitForCondition(condition, nextTest, errorMsg) { |
michael@0 | 7 | var tries = 0; |
michael@0 | 8 | var interval = setInterval(function() { |
michael@0 | 9 | if (tries >= 30) { |
michael@0 | 10 | ok(false, errorMsg); |
michael@0 | 11 | moveOn(); |
michael@0 | 12 | } |
michael@0 | 13 | var conditionPassed; |
michael@0 | 14 | try { |
michael@0 | 15 | conditionPassed = condition(); |
michael@0 | 16 | } catch (e) { |
michael@0 | 17 | ok(false, e + "\n" + e.stack); |
michael@0 | 18 | conditionPassed = false; |
michael@0 | 19 | } |
michael@0 | 20 | if (conditionPassed) { |
michael@0 | 21 | moveOn(); |
michael@0 | 22 | } |
michael@0 | 23 | tries++; |
michael@0 | 24 | }, 100); |
michael@0 | 25 | var moveOn = function() { clearInterval(interval); nextTest(); }; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | function is_hidden(element) { |
michael@0 | 29 | var style = element.ownerDocument.defaultView.getComputedStyle(element, ""); |
michael@0 | 30 | if (style.display == "none") |
michael@0 | 31 | return true; |
michael@0 | 32 | if (style.visibility != "visible") |
michael@0 | 33 | return true; |
michael@0 | 34 | if (style.display == "-moz-popup") |
michael@0 | 35 | return ["hiding","closed"].indexOf(element.state) != -1; |
michael@0 | 36 | |
michael@0 | 37 | // Hiding a parent element will hide all its children |
michael@0 | 38 | if (element.parentNode != element.ownerDocument) |
michael@0 | 39 | return is_hidden(element.parentNode); |
michael@0 | 40 | |
michael@0 | 41 | return false; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function is_element_visible(element, msg) { |
michael@0 | 45 | isnot(element, null, "Element should not be null, when checking visibility"); |
michael@0 | 46 | ok(!is_hidden(element), msg); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function waitForElementToBeVisible(element, nextTest, msg) { |
michael@0 | 50 | waitForCondition(() => !is_hidden(element), |
michael@0 | 51 | () => { |
michael@0 | 52 | ok(true, msg); |
michael@0 | 53 | nextTest(); |
michael@0 | 54 | }, |
michael@0 | 55 | "Timeout waiting for visibility: " + msg); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function waitForElementToBeHidden(element, nextTest, msg) { |
michael@0 | 59 | waitForCondition(() => is_hidden(element), |
michael@0 | 60 | () => { |
michael@0 | 61 | ok(true, msg); |
michael@0 | 62 | nextTest(); |
michael@0 | 63 | }, |
michael@0 | 64 | "Timeout waiting for invisibility: " + msg); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function waitForPopupAtAnchor(popup, anchorNode, nextTest, msg) { |
michael@0 | 68 | waitForCondition(() => popup.popupBoxObject.anchorNode == anchorNode, |
michael@0 | 69 | () => { |
michael@0 | 70 | ok(true, msg); |
michael@0 | 71 | is_element_visible(popup, "Popup should be visible"); |
michael@0 | 72 | nextTest(); |
michael@0 | 73 | }, |
michael@0 | 74 | "Timeout waiting for popup at anchor: " + msg); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function promisePanelShown(win) { |
michael@0 | 78 | let panelEl = win.PanelUI.panel; |
michael@0 | 79 | return promisePanelElementShown(win, panelEl); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function promisePanelElementShown(win, aPanel) { |
michael@0 | 83 | let deferred = Promise.defer(); |
michael@0 | 84 | let timeoutId = win.setTimeout(() => { |
michael@0 | 85 | deferred.reject("Panel did not show within 5 seconds."); |
michael@0 | 86 | }, 5000); |
michael@0 | 87 | aPanel.addEventListener("popupshown", function onPanelOpen(e) { |
michael@0 | 88 | aPanel.removeEventListener("popupshown", onPanelOpen); |
michael@0 | 89 | win.clearTimeout(timeoutId); |
michael@0 | 90 | deferred.resolve(); |
michael@0 | 91 | }); |
michael@0 | 92 | return deferred.promise; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function is_element_hidden(element, msg) { |
michael@0 | 96 | isnot(element, null, "Element should not be null, when checking visibility"); |
michael@0 | 97 | ok(is_hidden(element), msg); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function loadUITourTestPage(callback, host = "https://example.com/") { |
michael@0 | 101 | if (gTestTab) |
michael@0 | 102 | gBrowser.removeTab(gTestTab); |
michael@0 | 103 | |
michael@0 | 104 | let url = getRootDirectory(gTestPath) + "uitour.html"; |
michael@0 | 105 | url = url.replace("chrome://mochitests/content/", host); |
michael@0 | 106 | |
michael@0 | 107 | gTestTab = gBrowser.addTab(url); |
michael@0 | 108 | gBrowser.selectedTab = gTestTab; |
michael@0 | 109 | |
michael@0 | 110 | gTestTab.linkedBrowser.addEventListener("load", function onLoad() { |
michael@0 | 111 | gTestTab.linkedBrowser.removeEventListener("load", onLoad, true); |
michael@0 | 112 | |
michael@0 | 113 | gContentWindow = Components.utils.waiveXrays(gTestTab.linkedBrowser.contentDocument.defaultView); |
michael@0 | 114 | gContentAPI = gContentWindow.Mozilla.UITour; |
michael@0 | 115 | |
michael@0 | 116 | waitForFocus(callback, gContentWindow); |
michael@0 | 117 | }, true); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | function UITourTest() { |
michael@0 | 121 | Services.prefs.setBoolPref("browser.uitour.enabled", true); |
michael@0 | 122 | let testUri = Services.io.newURI("http://example.com", null, null); |
michael@0 | 123 | Services.perms.add(testUri, "uitour", Services.perms.ALLOW_ACTION); |
michael@0 | 124 | |
michael@0 | 125 | waitForExplicitFinish(); |
michael@0 | 126 | |
michael@0 | 127 | registerCleanupFunction(function() { |
michael@0 | 128 | delete window.UITour; |
michael@0 | 129 | delete window.gContentWindow; |
michael@0 | 130 | delete window.gContentAPI; |
michael@0 | 131 | if (gTestTab) |
michael@0 | 132 | gBrowser.removeTab(gTestTab); |
michael@0 | 133 | delete window.gTestTab; |
michael@0 | 134 | Services.prefs.clearUserPref("browser.uitour.enabled", true); |
michael@0 | 135 | Services.perms.remove("example.com", "uitour"); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | function done() { |
michael@0 | 139 | executeSoon(() => { |
michael@0 | 140 | if (gTestTab) |
michael@0 | 141 | gBrowser.removeTab(gTestTab); |
michael@0 | 142 | gTestTab = null; |
michael@0 | 143 | |
michael@0 | 144 | let highlight = document.getElementById("UITourHighlightContainer"); |
michael@0 | 145 | is_element_hidden(highlight, "Highlight should be closed/hidden after UITour tab is closed"); |
michael@0 | 146 | |
michael@0 | 147 | let tooltip = document.getElementById("UITourTooltip"); |
michael@0 | 148 | is_element_hidden(tooltip, "Tooltip should be closed/hidden after UITour tab is closed"); |
michael@0 | 149 | |
michael@0 | 150 | ok(!PanelUI.panel.hasAttribute("noautohide"), "@noautohide on the menu panel should have been cleaned up"); |
michael@0 | 151 | ok(!PanelUI.panel.hasAttribute("panelopen"), "The panel shouldn't have @panelopen"); |
michael@0 | 152 | isnot(PanelUI.panel.state, "open", "The panel shouldn't be open"); |
michael@0 | 153 | is(document.getElementById("PanelUI-menu-button").hasAttribute("open"), false, "Menu button should know that the menu is closed"); |
michael@0 | 154 | |
michael@0 | 155 | is(UITour.pinnedTabs.get(window), null, "Any pinned tab should be closed after UITour tab is closed"); |
michael@0 | 156 | |
michael@0 | 157 | executeSoon(nextTest); |
michael@0 | 158 | }); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | function nextTest() { |
michael@0 | 162 | if (tests.length == 0) { |
michael@0 | 163 | finish(); |
michael@0 | 164 | return; |
michael@0 | 165 | } |
michael@0 | 166 | let test = tests.shift(); |
michael@0 | 167 | info("Starting " + test.name); |
michael@0 | 168 | waitForFocus(function() { |
michael@0 | 169 | loadUITourTestPage(function() { |
michael@0 | 170 | test(done); |
michael@0 | 171 | }); |
michael@0 | 172 | }); |
michael@0 | 173 | } |
michael@0 | 174 | nextTest(); |
michael@0 | 175 | } |