Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | // Avoid leaks by using tmp for imports... |
michael@0 | 8 | let tmp = {}; |
michael@0 | 9 | Cu.import("resource://gre/modules/Promise.jsm", tmp); |
michael@0 | 10 | Cu.import("resource:///modules/CustomizableUI.jsm", tmp); |
michael@0 | 11 | let {Promise, CustomizableUI} = tmp; |
michael@0 | 12 | |
michael@0 | 13 | let ChromeUtils = {}; |
michael@0 | 14 | Services.scriptloader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils); |
michael@0 | 15 | |
michael@0 | 16 | Services.prefs.setBoolPref("browser.uiCustomization.skipSourceNodeCheck", true); |
michael@0 | 17 | registerCleanupFunction(() => Services.prefs.clearUserPref("browser.uiCustomization.skipSourceNodeCheck")); |
michael@0 | 18 | |
michael@0 | 19 | let {synthesizeDragStart, synthesizeDrop} = ChromeUtils; |
michael@0 | 20 | |
michael@0 | 21 | const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 22 | const kTabEventFailureTimeoutInMs = 20000; |
michael@0 | 23 | |
michael@0 | 24 | function createDummyXULButton(id, label) { |
michael@0 | 25 | let btn = document.createElementNS(kNSXUL, "toolbarbutton"); |
michael@0 | 26 | btn.id = id; |
michael@0 | 27 | btn.setAttribute("label", label || id); |
michael@0 | 28 | btn.className = "toolbarbutton-1 chromeclass-toolbar-additional"; |
michael@0 | 29 | window.gNavToolbox.palette.appendChild(btn); |
michael@0 | 30 | return btn; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | let gAddedToolbars = new Set(); |
michael@0 | 34 | |
michael@0 | 35 | function createToolbarWithPlacements(id, placements = []) { |
michael@0 | 36 | gAddedToolbars.add(id); |
michael@0 | 37 | let tb = document.createElementNS(kNSXUL, "toolbar"); |
michael@0 | 38 | tb.id = id; |
michael@0 | 39 | tb.setAttribute("customizable", "true"); |
michael@0 | 40 | CustomizableUI.registerArea(id, { |
michael@0 | 41 | type: CustomizableUI.TYPE_TOOLBAR, |
michael@0 | 42 | defaultPlacements: placements |
michael@0 | 43 | }); |
michael@0 | 44 | gNavToolbox.appendChild(tb); |
michael@0 | 45 | return tb; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function createOverflowableToolbarWithPlacements(id, placements) { |
michael@0 | 49 | gAddedToolbars.add(id); |
michael@0 | 50 | |
michael@0 | 51 | let tb = document.createElementNS(kNSXUL, "toolbar"); |
michael@0 | 52 | tb.id = id; |
michael@0 | 53 | tb.setAttribute("customizationtarget", id + "-target"); |
michael@0 | 54 | |
michael@0 | 55 | let customizationtarget = document.createElementNS(kNSXUL, "hbox"); |
michael@0 | 56 | customizationtarget.id = id + "-target"; |
michael@0 | 57 | customizationtarget.setAttribute("flex", "1"); |
michael@0 | 58 | tb.appendChild(customizationtarget); |
michael@0 | 59 | |
michael@0 | 60 | let overflowPanel = document.createElementNS(kNSXUL, "panel"); |
michael@0 | 61 | overflowPanel.id = id + "-overflow"; |
michael@0 | 62 | document.getElementById("mainPopupSet").appendChild(overflowPanel); |
michael@0 | 63 | |
michael@0 | 64 | let overflowList = document.createElementNS(kNSXUL, "vbox"); |
michael@0 | 65 | overflowList.id = id + "-overflow-list"; |
michael@0 | 66 | overflowPanel.appendChild(overflowList); |
michael@0 | 67 | |
michael@0 | 68 | let chevron = document.createElementNS(kNSXUL, "toolbarbutton"); |
michael@0 | 69 | chevron.id = id + "-chevron"; |
michael@0 | 70 | tb.appendChild(chevron); |
michael@0 | 71 | |
michael@0 | 72 | CustomizableUI.registerArea(id, { |
michael@0 | 73 | type: CustomizableUI.TYPE_TOOLBAR, |
michael@0 | 74 | defaultPlacements: placements, |
michael@0 | 75 | overflowable: true, |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | tb.setAttribute("customizable", "true"); |
michael@0 | 79 | tb.setAttribute("overflowable", "true"); |
michael@0 | 80 | tb.setAttribute("overflowpanel", overflowPanel.id); |
michael@0 | 81 | tb.setAttribute("overflowtarget", overflowList.id); |
michael@0 | 82 | tb.setAttribute("overflowbutton", chevron.id); |
michael@0 | 83 | |
michael@0 | 84 | gNavToolbox.appendChild(tb); |
michael@0 | 85 | return tb; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | function removeCustomToolbars() { |
michael@0 | 89 | CustomizableUI.reset(); |
michael@0 | 90 | for (let toolbarId of gAddedToolbars) { |
michael@0 | 91 | CustomizableUI.unregisterArea(toolbarId, true); |
michael@0 | 92 | let tb = document.getElementById(toolbarId); |
michael@0 | 93 | if (tb.hasAttribute("overflowpanel")) { |
michael@0 | 94 | let panel = document.getElementById(tb.getAttribute("overflowpanel")); |
michael@0 | 95 | if (panel) |
michael@0 | 96 | panel.remove(); |
michael@0 | 97 | } |
michael@0 | 98 | tb.remove(); |
michael@0 | 99 | } |
michael@0 | 100 | gAddedToolbars.clear(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function getToolboxCustomToolbarId(toolbarName) { |
michael@0 | 104 | return "__customToolbar_" + toolbarName.replace(" ", "_"); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | function resetCustomization() { |
michael@0 | 108 | return CustomizableUI.reset(); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function isInWin8() { |
michael@0 | 112 | if (!Services.metro) |
michael@0 | 113 | return false; |
michael@0 | 114 | return Services.metro.supported; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | function addSwitchToMetroButtonInWindows8(areaPanelPlacements) { |
michael@0 | 118 | if (isInWin8()) { |
michael@0 | 119 | areaPanelPlacements.push("switch-to-metro-button"); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | function assertAreaPlacements(areaId, expectedPlacements) { |
michael@0 | 124 | let actualPlacements = getAreaWidgetIds(areaId); |
michael@0 | 125 | placementArraysEqual(areaId, actualPlacements, expectedPlacements); |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | function placementArraysEqual(areaId, actualPlacements, expectedPlacements) { |
michael@0 | 129 | is(actualPlacements.length, expectedPlacements.length, |
michael@0 | 130 | "Area " + areaId + " should have " + expectedPlacements.length + " items."); |
michael@0 | 131 | let minItems = Math.min(expectedPlacements.length, actualPlacements.length); |
michael@0 | 132 | for (let i = 0; i < minItems; i++) { |
michael@0 | 133 | if (typeof expectedPlacements[i] == "string") { |
michael@0 | 134 | is(actualPlacements[i], expectedPlacements[i], |
michael@0 | 135 | "Item " + i + " in " + areaId + " should match expectations."); |
michael@0 | 136 | } else if (expectedPlacements[i] instanceof RegExp) { |
michael@0 | 137 | ok(expectedPlacements[i].test(actualPlacements[i]), |
michael@0 | 138 | "Item " + i + " (" + actualPlacements[i] + ") in " + |
michael@0 | 139 | areaId + " should match " + expectedPlacements[i]); |
michael@0 | 140 | } else { |
michael@0 | 141 | ok(false, "Unknown type of expected placement passed to " + |
michael@0 | 142 | " assertAreaPlacements. Is your test broken?"); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | function todoAssertAreaPlacements(areaId, expectedPlacements) { |
michael@0 | 148 | let actualPlacements = getAreaWidgetIds(areaId); |
michael@0 | 149 | let isPassing = actualPlacements.length == expectedPlacements.length; |
michael@0 | 150 | let minItems = Math.min(expectedPlacements.length, actualPlacements.length); |
michael@0 | 151 | for (let i = 0; i < minItems; i++) { |
michael@0 | 152 | if (typeof expectedPlacements[i] == "string") { |
michael@0 | 153 | isPassing = isPassing && actualPlacements[i] == expectedPlacements[i]; |
michael@0 | 154 | } else if (expectedPlacements[i] instanceof RegExp) { |
michael@0 | 155 | isPassing = isPassing && expectedPlacements[i].test(actualPlacements[i]); |
michael@0 | 156 | } else { |
michael@0 | 157 | ok(false, "Unknown type of expected placement passed to " + |
michael@0 | 158 | " assertAreaPlacements. Is your test broken?"); |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | todo(isPassing, "The area placements for " + areaId + |
michael@0 | 162 | " should equal the expected placements."); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | function getAreaWidgetIds(areaId) { |
michael@0 | 166 | return CustomizableUI.getWidgetIdsInArea(areaId); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | function simulateItemDrag(toDrag, target) { |
michael@0 | 170 | let docId = toDrag.ownerDocument.documentElement.id; |
michael@0 | 171 | let dragData = [[{type: 'text/toolbarwrapper-id/' + docId, |
michael@0 | 172 | data: toDrag.id}]]; |
michael@0 | 173 | synthesizeDragStart(toDrag.parentNode, dragData); |
michael@0 | 174 | synthesizeDrop(target, target, dragData); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | function endCustomizing(aWindow=window) { |
michael@0 | 178 | if (aWindow.document.documentElement.getAttribute("customizing") != "true") { |
michael@0 | 179 | return true; |
michael@0 | 180 | } |
michael@0 | 181 | Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", true); |
michael@0 | 182 | let deferredEndCustomizing = Promise.defer(); |
michael@0 | 183 | function onCustomizationEnds() { |
michael@0 | 184 | Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", false); |
michael@0 | 185 | aWindow.gNavToolbox.removeEventListener("aftercustomization", onCustomizationEnds); |
michael@0 | 186 | deferredEndCustomizing.resolve(); |
michael@0 | 187 | } |
michael@0 | 188 | aWindow.gNavToolbox.addEventListener("aftercustomization", onCustomizationEnds); |
michael@0 | 189 | aWindow.gCustomizeMode.exit(); |
michael@0 | 190 | |
michael@0 | 191 | return deferredEndCustomizing.promise.then(function() { |
michael@0 | 192 | let deferredLoadNewTab = Promise.defer(); |
michael@0 | 193 | |
michael@0 | 194 | //XXXgijs so some tests depend on this tab being about:blank. Make it so. |
michael@0 | 195 | let newTabBrowser = aWindow.gBrowser.selectedBrowser; |
michael@0 | 196 | newTabBrowser.stop(); |
michael@0 | 197 | |
michael@0 | 198 | // If we stop early enough, this might actually be about:blank. |
michael@0 | 199 | if (newTabBrowser.contentDocument.location.href == "about:blank") { |
michael@0 | 200 | return; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | // Otherwise, make it be about:blank, and wait for that to be done. |
michael@0 | 204 | function onNewTabLoaded(e) { |
michael@0 | 205 | newTabBrowser.removeEventListener("load", onNewTabLoaded, true); |
michael@0 | 206 | deferredLoadNewTab.resolve(); |
michael@0 | 207 | } |
michael@0 | 208 | newTabBrowser.addEventListener("load", onNewTabLoaded, true); |
michael@0 | 209 | newTabBrowser.contentDocument.location.replace("about:blank"); |
michael@0 | 210 | return deferredLoadNewTab.promise; |
michael@0 | 211 | }); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | function startCustomizing(aWindow=window) { |
michael@0 | 215 | if (aWindow.document.documentElement.getAttribute("customizing") == "true") { |
michael@0 | 216 | return; |
michael@0 | 217 | } |
michael@0 | 218 | Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", true); |
michael@0 | 219 | let deferred = Promise.defer(); |
michael@0 | 220 | function onCustomizing() { |
michael@0 | 221 | aWindow.gNavToolbox.removeEventListener("customizationready", onCustomizing); |
michael@0 | 222 | Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", false); |
michael@0 | 223 | deferred.resolve(); |
michael@0 | 224 | } |
michael@0 | 225 | aWindow.gNavToolbox.addEventListener("customizationready", onCustomizing); |
michael@0 | 226 | aWindow.gCustomizeMode.enter(); |
michael@0 | 227 | return deferred.promise; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | function openAndLoadWindow(aOptions, aWaitForDelayedStartup=false) { |
michael@0 | 231 | let deferred = Promise.defer(); |
michael@0 | 232 | let win = OpenBrowserWindow(aOptions); |
michael@0 | 233 | if (aWaitForDelayedStartup) { |
michael@0 | 234 | Services.obs.addObserver(function onDS(aSubject, aTopic, aData) { |
michael@0 | 235 | if (aSubject != win) { |
michael@0 | 236 | return; |
michael@0 | 237 | } |
michael@0 | 238 | Services.obs.removeObserver(onDS, "browser-delayed-startup-finished"); |
michael@0 | 239 | deferred.resolve(win); |
michael@0 | 240 | }, "browser-delayed-startup-finished", false); |
michael@0 | 241 | |
michael@0 | 242 | } else { |
michael@0 | 243 | win.addEventListener("load", function onLoad() { |
michael@0 | 244 | win.removeEventListener("load", onLoad); |
michael@0 | 245 | deferred.resolve(win); |
michael@0 | 246 | }); |
michael@0 | 247 | } |
michael@0 | 248 | return deferred.promise; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | function promiseWindowClosed(win) { |
michael@0 | 252 | let deferred = Promise.defer(); |
michael@0 | 253 | win.addEventListener("unload", function onunload() { |
michael@0 | 254 | win.removeEventListener("unload", onunload); |
michael@0 | 255 | deferred.resolve(); |
michael@0 | 256 | }); |
michael@0 | 257 | win.close(); |
michael@0 | 258 | return deferred.promise; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | function promisePanelShown(win) { |
michael@0 | 262 | let panelEl = win.PanelUI.panel; |
michael@0 | 263 | return promisePanelElementShown(win, panelEl); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | function promiseOverflowShown(win) { |
michael@0 | 267 | let panelEl = win.document.getElementById("widget-overflow"); |
michael@0 | 268 | return promisePanelElementShown(win, panelEl); |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | function promisePanelElementShown(win, aPanel) { |
michael@0 | 272 | let deferred = Promise.defer(); |
michael@0 | 273 | let timeoutId = win.setTimeout(() => { |
michael@0 | 274 | deferred.reject("Panel did not show within 20 seconds."); |
michael@0 | 275 | }, 20000); |
michael@0 | 276 | function onPanelOpen(e) { |
michael@0 | 277 | aPanel.removeEventListener("popupshown", onPanelOpen); |
michael@0 | 278 | win.clearTimeout(timeoutId); |
michael@0 | 279 | deferred.resolve(); |
michael@0 | 280 | }; |
michael@0 | 281 | aPanel.addEventListener("popupshown", onPanelOpen); |
michael@0 | 282 | return deferred.promise; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | function promisePanelHidden(win) { |
michael@0 | 286 | let panelEl = win.PanelUI.panel; |
michael@0 | 287 | return promisePanelElementHidden(win, panelEl); |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | function promiseOverflowHidden(win) { |
michael@0 | 291 | let panelEl = document.getElementById("widget-overflow"); |
michael@0 | 292 | return promisePanelElementHidden(win, panelEl); |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | function promisePanelElementHidden(win, aPanel) { |
michael@0 | 296 | let deferred = Promise.defer(); |
michael@0 | 297 | let timeoutId = win.setTimeout(() => { |
michael@0 | 298 | deferred.reject("Panel did not hide within 20 seconds."); |
michael@0 | 299 | }, 20000); |
michael@0 | 300 | function onPanelClose(e) { |
michael@0 | 301 | aPanel.removeEventListener("popuphidden", onPanelClose); |
michael@0 | 302 | win.clearTimeout(timeoutId); |
michael@0 | 303 | deferred.resolve(); |
michael@0 | 304 | } |
michael@0 | 305 | aPanel.addEventListener("popuphidden", onPanelClose); |
michael@0 | 306 | return deferred.promise; |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | function isPanelUIOpen() { |
michael@0 | 310 | return PanelUI.panel.state == "open" || PanelUI.panel.state == "showing"; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | function subviewShown(aSubview) { |
michael@0 | 314 | let deferred = Promise.defer(); |
michael@0 | 315 | let win = aSubview.ownerDocument.defaultView; |
michael@0 | 316 | let timeoutId = win.setTimeout(() => { |
michael@0 | 317 | deferred.reject("Subview (" + aSubview.id + ") did not show within 20 seconds."); |
michael@0 | 318 | }, 20000); |
michael@0 | 319 | function onViewShowing(e) { |
michael@0 | 320 | aSubview.removeEventListener("ViewShowing", onViewShowing); |
michael@0 | 321 | win.clearTimeout(timeoutId); |
michael@0 | 322 | deferred.resolve(); |
michael@0 | 323 | }; |
michael@0 | 324 | aSubview.addEventListener("ViewShowing", onViewShowing); |
michael@0 | 325 | return deferred.promise; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | function subviewHidden(aSubview) { |
michael@0 | 329 | let deferred = Promise.defer(); |
michael@0 | 330 | let win = aSubview.ownerDocument.defaultView; |
michael@0 | 331 | let timeoutId = win.setTimeout(() => { |
michael@0 | 332 | deferred.reject("Subview (" + aSubview.id + ") did not hide within 20 seconds."); |
michael@0 | 333 | }, 20000); |
michael@0 | 334 | function onViewHiding(e) { |
michael@0 | 335 | aSubview.removeEventListener("ViewHiding", onViewHiding); |
michael@0 | 336 | win.clearTimeout(timeoutId); |
michael@0 | 337 | deferred.resolve(); |
michael@0 | 338 | }; |
michael@0 | 339 | aSubview.addEventListener("ViewHiding", onViewHiding); |
michael@0 | 340 | return deferred.promise; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | function waitForCondition(aConditionFn, aMaxTries=50, aCheckInterval=100) { |
michael@0 | 344 | function tryNow() { |
michael@0 | 345 | tries++; |
michael@0 | 346 | if (aConditionFn()) { |
michael@0 | 347 | deferred.resolve(); |
michael@0 | 348 | } else if (tries < aMaxTries) { |
michael@0 | 349 | tryAgain(); |
michael@0 | 350 | } else { |
michael@0 | 351 | deferred.reject("Condition timed out: " + aConditionFn.toSource()); |
michael@0 | 352 | } |
michael@0 | 353 | } |
michael@0 | 354 | function tryAgain() { |
michael@0 | 355 | setTimeout(tryNow, aCheckInterval); |
michael@0 | 356 | } |
michael@0 | 357 | let deferred = Promise.defer(); |
michael@0 | 358 | let tries = 0; |
michael@0 | 359 | tryAgain(); |
michael@0 | 360 | return deferred.promise; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | function waitFor(aTimeout=100) { |
michael@0 | 364 | let deferred = Promise.defer(); |
michael@0 | 365 | setTimeout(function() deferred.resolve(), aTimeout); |
michael@0 | 366 | return deferred.promise; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | /** |
michael@0 | 370 | * Starts a load in an existing tab and waits for it to finish (via some event). |
michael@0 | 371 | * |
michael@0 | 372 | * @param aTab The tab to load into. |
michael@0 | 373 | * @param aUrl The url to load. |
michael@0 | 374 | * @param aEventType The load event type to wait for. Defaults to "load". |
michael@0 | 375 | * @return {Promise} resolved when the event is handled. |
michael@0 | 376 | */ |
michael@0 | 377 | function promiseTabLoadEvent(aTab, aURL, aEventType="load") { |
michael@0 | 378 | let deferred = Promise.defer(); |
michael@0 | 379 | info("Wait for tab event: " + aEventType); |
michael@0 | 380 | |
michael@0 | 381 | let timeoutId = setTimeout(() => { |
michael@0 | 382 | aTab.linkedBrowser.removeEventListener(aEventType, onTabLoad, true); |
michael@0 | 383 | deferred.reject("TabSelect did not happen within " + kTabEventFailureTimeoutInMs + "ms"); |
michael@0 | 384 | }, kTabEventFailureTimeoutInMs); |
michael@0 | 385 | |
michael@0 | 386 | function onTabLoad(event) { |
michael@0 | 387 | if (event.originalTarget != aTab.linkedBrowser.contentDocument || |
michael@0 | 388 | event.target.location.href == "about:blank") { |
michael@0 | 389 | info("skipping spurious load event"); |
michael@0 | 390 | return; |
michael@0 | 391 | } |
michael@0 | 392 | clearTimeout(timeoutId); |
michael@0 | 393 | aTab.linkedBrowser.removeEventListener(aEventType, onTabLoad, true); |
michael@0 | 394 | info("Tab event received: " + aEventType); |
michael@0 | 395 | deferred.resolve(); |
michael@0 | 396 | } |
michael@0 | 397 | aTab.linkedBrowser.addEventListener(aEventType, onTabLoad, true, true); |
michael@0 | 398 | aTab.linkedBrowser.loadURI(aURL); |
michael@0 | 399 | return deferred.promise; |
michael@0 | 400 | } |
michael@0 | 401 | |
michael@0 | 402 | /** |
michael@0 | 403 | * Navigate back or forward in tab history and wait for it to finish. |
michael@0 | 404 | * |
michael@0 | 405 | * @param aDirection Number to indicate to move backward or forward in history. |
michael@0 | 406 | * @param aConditionFn Function that returns the result of an evaluated condition |
michael@0 | 407 | * that needs to be `true` to resolve the promise. |
michael@0 | 408 | * @return {Promise} resolved when navigation has finished. |
michael@0 | 409 | */ |
michael@0 | 410 | function promiseTabHistoryNavigation(aDirection = -1, aConditionFn) { |
michael@0 | 411 | let deferred = Promise.defer(); |
michael@0 | 412 | |
michael@0 | 413 | let timeoutId = setTimeout(() => { |
michael@0 | 414 | gBrowser.removeEventListener("pageshow", listener, true); |
michael@0 | 415 | deferred.reject("Pageshow did not happen within " + kTabEventFailureTimeoutInMs + "ms"); |
michael@0 | 416 | }, kTabEventFailureTimeoutInMs); |
michael@0 | 417 | |
michael@0 | 418 | function listener(event) { |
michael@0 | 419 | gBrowser.removeEventListener("pageshow", listener, true); |
michael@0 | 420 | clearTimeout(timeoutId); |
michael@0 | 421 | |
michael@0 | 422 | if (aConditionFn) { |
michael@0 | 423 | waitForCondition(aConditionFn).then(() => deferred.resolve(), |
michael@0 | 424 | aReason => deferred.reject(aReason)); |
michael@0 | 425 | } else { |
michael@0 | 426 | deferred.resolve(); |
michael@0 | 427 | } |
michael@0 | 428 | } |
michael@0 | 429 | gBrowser.addEventListener("pageshow", listener, true); |
michael@0 | 430 | |
michael@0 | 431 | content.history.go(aDirection); |
michael@0 | 432 | |
michael@0 | 433 | return deferred.promise; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | function popupShown(aPopup) { |
michael@0 | 437 | return promisePopupEvent(aPopup, "shown"); |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | function popupHidden(aPopup) { |
michael@0 | 441 | return promisePopupEvent(aPopup, "hidden"); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | /** |
michael@0 | 445 | * Returns a Promise that resolves when aPopup fires an event of type |
michael@0 | 446 | * aEventType. Times out and rejects after 20 seconds. |
michael@0 | 447 | * |
michael@0 | 448 | * @param aPopup the popup to monitor for events. |
michael@0 | 449 | * @param aEventSuffix the _suffix_ for the popup event type to watch for. |
michael@0 | 450 | * |
michael@0 | 451 | * Example usage: |
michael@0 | 452 | * let popupShownPromise = promisePopupEvent(somePopup, "shown"); |
michael@0 | 453 | * // ... something that opens a popup |
michael@0 | 454 | * yield popupShownPromise; |
michael@0 | 455 | * |
michael@0 | 456 | * let popupHiddenPromise = promisePopupEvent(somePopup, "hidden"); |
michael@0 | 457 | * // ... something that hides a popup |
michael@0 | 458 | * yield popupHiddenPromise; |
michael@0 | 459 | */ |
michael@0 | 460 | function promisePopupEvent(aPopup, aEventSuffix) { |
michael@0 | 461 | let deferred = Promise.defer(); |
michael@0 | 462 | let win = aPopup.ownerDocument.defaultView; |
michael@0 | 463 | let eventType = "popup" + aEventSuffix; |
michael@0 | 464 | |
michael@0 | 465 | let timeoutId = win.setTimeout(() => { |
michael@0 | 466 | deferred.reject("Context menu (" + aPopup.id + ") did not fire " |
michael@0 | 467 | + eventType + " within 20 seconds."); |
michael@0 | 468 | }, 20000); |
michael@0 | 469 | |
michael@0 | 470 | function onPopupEvent(e) { |
michael@0 | 471 | win.clearTimeout(timeoutId); |
michael@0 | 472 | aPopup.removeEventListener(eventType, onPopupEvent); |
michael@0 | 473 | deferred.resolve(); |
michael@0 | 474 | }; |
michael@0 | 475 | |
michael@0 | 476 | aPopup.addEventListener(eventType, onPopupEvent); |
michael@0 | 477 | return deferred.promise; |
michael@0 | 478 | } |
michael@0 | 479 | |
michael@0 | 480 | // This is a simpler version of the context menu check that |
michael@0 | 481 | // exists in contextmenu_common.js. |
michael@0 | 482 | function checkContextMenu(aContextMenu, aExpectedEntries, aWindow=window) { |
michael@0 | 483 | let childNodes = aContextMenu.childNodes; |
michael@0 | 484 | for (let i = 0; i < childNodes.length; i++) { |
michael@0 | 485 | let menuitem = childNodes[i]; |
michael@0 | 486 | try { |
michael@0 | 487 | if (aExpectedEntries[i][0] == "---") { |
michael@0 | 488 | is(menuitem.localName, "menuseparator", "menuseparator expected"); |
michael@0 | 489 | continue; |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | let selector = aExpectedEntries[i][0]; |
michael@0 | 493 | ok(menuitem.mozMatchesSelector(selector), "menuitem should match " + selector + " selector"); |
michael@0 | 494 | let commandValue = menuitem.getAttribute("command"); |
michael@0 | 495 | let relatedCommand = commandValue ? aWindow.document.getElementById(commandValue) : null; |
michael@0 | 496 | let menuItemDisabled = relatedCommand ? |
michael@0 | 497 | relatedCommand.getAttribute("disabled") == "true" : |
michael@0 | 498 | menuitem.getAttribute("disabled") == "true"; |
michael@0 | 499 | is(menuItemDisabled, !aExpectedEntries[i][1], "disabled state for " + selector); |
michael@0 | 500 | } catch (e) { |
michael@0 | 501 | ok(false, "Exception when checking context menu: " + e); |
michael@0 | 502 | } |
michael@0 | 503 | } |
michael@0 | 504 | } |