1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/customizableui/test/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,504 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +// Avoid leaks by using tmp for imports... 1.11 +let tmp = {}; 1.12 +Cu.import("resource://gre/modules/Promise.jsm", tmp); 1.13 +Cu.import("resource:///modules/CustomizableUI.jsm", tmp); 1.14 +let {Promise, CustomizableUI} = tmp; 1.15 + 1.16 +let ChromeUtils = {}; 1.17 +Services.scriptloader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils); 1.18 + 1.19 +Services.prefs.setBoolPref("browser.uiCustomization.skipSourceNodeCheck", true); 1.20 +registerCleanupFunction(() => Services.prefs.clearUserPref("browser.uiCustomization.skipSourceNodeCheck")); 1.21 + 1.22 +let {synthesizeDragStart, synthesizeDrop} = ChromeUtils; 1.23 + 1.24 +const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; 1.25 +const kTabEventFailureTimeoutInMs = 20000; 1.26 + 1.27 +function createDummyXULButton(id, label) { 1.28 + let btn = document.createElementNS(kNSXUL, "toolbarbutton"); 1.29 + btn.id = id; 1.30 + btn.setAttribute("label", label || id); 1.31 + btn.className = "toolbarbutton-1 chromeclass-toolbar-additional"; 1.32 + window.gNavToolbox.palette.appendChild(btn); 1.33 + return btn; 1.34 +} 1.35 + 1.36 +let gAddedToolbars = new Set(); 1.37 + 1.38 +function createToolbarWithPlacements(id, placements = []) { 1.39 + gAddedToolbars.add(id); 1.40 + let tb = document.createElementNS(kNSXUL, "toolbar"); 1.41 + tb.id = id; 1.42 + tb.setAttribute("customizable", "true"); 1.43 + CustomizableUI.registerArea(id, { 1.44 + type: CustomizableUI.TYPE_TOOLBAR, 1.45 + defaultPlacements: placements 1.46 + }); 1.47 + gNavToolbox.appendChild(tb); 1.48 + return tb; 1.49 +} 1.50 + 1.51 +function createOverflowableToolbarWithPlacements(id, placements) { 1.52 + gAddedToolbars.add(id); 1.53 + 1.54 + let tb = document.createElementNS(kNSXUL, "toolbar"); 1.55 + tb.id = id; 1.56 + tb.setAttribute("customizationtarget", id + "-target"); 1.57 + 1.58 + let customizationtarget = document.createElementNS(kNSXUL, "hbox"); 1.59 + customizationtarget.id = id + "-target"; 1.60 + customizationtarget.setAttribute("flex", "1"); 1.61 + tb.appendChild(customizationtarget); 1.62 + 1.63 + let overflowPanel = document.createElementNS(kNSXUL, "panel"); 1.64 + overflowPanel.id = id + "-overflow"; 1.65 + document.getElementById("mainPopupSet").appendChild(overflowPanel); 1.66 + 1.67 + let overflowList = document.createElementNS(kNSXUL, "vbox"); 1.68 + overflowList.id = id + "-overflow-list"; 1.69 + overflowPanel.appendChild(overflowList); 1.70 + 1.71 + let chevron = document.createElementNS(kNSXUL, "toolbarbutton"); 1.72 + chevron.id = id + "-chevron"; 1.73 + tb.appendChild(chevron); 1.74 + 1.75 + CustomizableUI.registerArea(id, { 1.76 + type: CustomizableUI.TYPE_TOOLBAR, 1.77 + defaultPlacements: placements, 1.78 + overflowable: true, 1.79 + }); 1.80 + 1.81 + tb.setAttribute("customizable", "true"); 1.82 + tb.setAttribute("overflowable", "true"); 1.83 + tb.setAttribute("overflowpanel", overflowPanel.id); 1.84 + tb.setAttribute("overflowtarget", overflowList.id); 1.85 + tb.setAttribute("overflowbutton", chevron.id); 1.86 + 1.87 + gNavToolbox.appendChild(tb); 1.88 + return tb; 1.89 +} 1.90 + 1.91 +function removeCustomToolbars() { 1.92 + CustomizableUI.reset(); 1.93 + for (let toolbarId of gAddedToolbars) { 1.94 + CustomizableUI.unregisterArea(toolbarId, true); 1.95 + let tb = document.getElementById(toolbarId); 1.96 + if (tb.hasAttribute("overflowpanel")) { 1.97 + let panel = document.getElementById(tb.getAttribute("overflowpanel")); 1.98 + if (panel) 1.99 + panel.remove(); 1.100 + } 1.101 + tb.remove(); 1.102 + } 1.103 + gAddedToolbars.clear(); 1.104 +} 1.105 + 1.106 +function getToolboxCustomToolbarId(toolbarName) { 1.107 + return "__customToolbar_" + toolbarName.replace(" ", "_"); 1.108 +} 1.109 + 1.110 +function resetCustomization() { 1.111 + return CustomizableUI.reset(); 1.112 +} 1.113 + 1.114 +function isInWin8() { 1.115 + if (!Services.metro) 1.116 + return false; 1.117 + return Services.metro.supported; 1.118 +} 1.119 + 1.120 +function addSwitchToMetroButtonInWindows8(areaPanelPlacements) { 1.121 + if (isInWin8()) { 1.122 + areaPanelPlacements.push("switch-to-metro-button"); 1.123 + } 1.124 +} 1.125 + 1.126 +function assertAreaPlacements(areaId, expectedPlacements) { 1.127 + let actualPlacements = getAreaWidgetIds(areaId); 1.128 + placementArraysEqual(areaId, actualPlacements, expectedPlacements); 1.129 +} 1.130 + 1.131 +function placementArraysEqual(areaId, actualPlacements, expectedPlacements) { 1.132 + is(actualPlacements.length, expectedPlacements.length, 1.133 + "Area " + areaId + " should have " + expectedPlacements.length + " items."); 1.134 + let minItems = Math.min(expectedPlacements.length, actualPlacements.length); 1.135 + for (let i = 0; i < minItems; i++) { 1.136 + if (typeof expectedPlacements[i] == "string") { 1.137 + is(actualPlacements[i], expectedPlacements[i], 1.138 + "Item " + i + " in " + areaId + " should match expectations."); 1.139 + } else if (expectedPlacements[i] instanceof RegExp) { 1.140 + ok(expectedPlacements[i].test(actualPlacements[i]), 1.141 + "Item " + i + " (" + actualPlacements[i] + ") in " + 1.142 + areaId + " should match " + expectedPlacements[i]); 1.143 + } else { 1.144 + ok(false, "Unknown type of expected placement passed to " + 1.145 + " assertAreaPlacements. Is your test broken?"); 1.146 + } 1.147 + } 1.148 +} 1.149 + 1.150 +function todoAssertAreaPlacements(areaId, expectedPlacements) { 1.151 + let actualPlacements = getAreaWidgetIds(areaId); 1.152 + let isPassing = actualPlacements.length == expectedPlacements.length; 1.153 + let minItems = Math.min(expectedPlacements.length, actualPlacements.length); 1.154 + for (let i = 0; i < minItems; i++) { 1.155 + if (typeof expectedPlacements[i] == "string") { 1.156 + isPassing = isPassing && actualPlacements[i] == expectedPlacements[i]; 1.157 + } else if (expectedPlacements[i] instanceof RegExp) { 1.158 + isPassing = isPassing && expectedPlacements[i].test(actualPlacements[i]); 1.159 + } else { 1.160 + ok(false, "Unknown type of expected placement passed to " + 1.161 + " assertAreaPlacements. Is your test broken?"); 1.162 + } 1.163 + } 1.164 + todo(isPassing, "The area placements for " + areaId + 1.165 + " should equal the expected placements."); 1.166 +} 1.167 + 1.168 +function getAreaWidgetIds(areaId) { 1.169 + return CustomizableUI.getWidgetIdsInArea(areaId); 1.170 +} 1.171 + 1.172 +function simulateItemDrag(toDrag, target) { 1.173 + let docId = toDrag.ownerDocument.documentElement.id; 1.174 + let dragData = [[{type: 'text/toolbarwrapper-id/' + docId, 1.175 + data: toDrag.id}]]; 1.176 + synthesizeDragStart(toDrag.parentNode, dragData); 1.177 + synthesizeDrop(target, target, dragData); 1.178 +} 1.179 + 1.180 +function endCustomizing(aWindow=window) { 1.181 + if (aWindow.document.documentElement.getAttribute("customizing") != "true") { 1.182 + return true; 1.183 + } 1.184 + Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", true); 1.185 + let deferredEndCustomizing = Promise.defer(); 1.186 + function onCustomizationEnds() { 1.187 + Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", false); 1.188 + aWindow.gNavToolbox.removeEventListener("aftercustomization", onCustomizationEnds); 1.189 + deferredEndCustomizing.resolve(); 1.190 + } 1.191 + aWindow.gNavToolbox.addEventListener("aftercustomization", onCustomizationEnds); 1.192 + aWindow.gCustomizeMode.exit(); 1.193 + 1.194 + return deferredEndCustomizing.promise.then(function() { 1.195 + let deferredLoadNewTab = Promise.defer(); 1.196 + 1.197 + //XXXgijs so some tests depend on this tab being about:blank. Make it so. 1.198 + let newTabBrowser = aWindow.gBrowser.selectedBrowser; 1.199 + newTabBrowser.stop(); 1.200 + 1.201 + // If we stop early enough, this might actually be about:blank. 1.202 + if (newTabBrowser.contentDocument.location.href == "about:blank") { 1.203 + return; 1.204 + } 1.205 + 1.206 + // Otherwise, make it be about:blank, and wait for that to be done. 1.207 + function onNewTabLoaded(e) { 1.208 + newTabBrowser.removeEventListener("load", onNewTabLoaded, true); 1.209 + deferredLoadNewTab.resolve(); 1.210 + } 1.211 + newTabBrowser.addEventListener("load", onNewTabLoaded, true); 1.212 + newTabBrowser.contentDocument.location.replace("about:blank"); 1.213 + return deferredLoadNewTab.promise; 1.214 + }); 1.215 +} 1.216 + 1.217 +function startCustomizing(aWindow=window) { 1.218 + if (aWindow.document.documentElement.getAttribute("customizing") == "true") { 1.219 + return; 1.220 + } 1.221 + Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", true); 1.222 + let deferred = Promise.defer(); 1.223 + function onCustomizing() { 1.224 + aWindow.gNavToolbox.removeEventListener("customizationready", onCustomizing); 1.225 + Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", false); 1.226 + deferred.resolve(); 1.227 + } 1.228 + aWindow.gNavToolbox.addEventListener("customizationready", onCustomizing); 1.229 + aWindow.gCustomizeMode.enter(); 1.230 + return deferred.promise; 1.231 +} 1.232 + 1.233 +function openAndLoadWindow(aOptions, aWaitForDelayedStartup=false) { 1.234 + let deferred = Promise.defer(); 1.235 + let win = OpenBrowserWindow(aOptions); 1.236 + if (aWaitForDelayedStartup) { 1.237 + Services.obs.addObserver(function onDS(aSubject, aTopic, aData) { 1.238 + if (aSubject != win) { 1.239 + return; 1.240 + } 1.241 + Services.obs.removeObserver(onDS, "browser-delayed-startup-finished"); 1.242 + deferred.resolve(win); 1.243 + }, "browser-delayed-startup-finished", false); 1.244 + 1.245 + } else { 1.246 + win.addEventListener("load", function onLoad() { 1.247 + win.removeEventListener("load", onLoad); 1.248 + deferred.resolve(win); 1.249 + }); 1.250 + } 1.251 + return deferred.promise; 1.252 +} 1.253 + 1.254 +function promiseWindowClosed(win) { 1.255 + let deferred = Promise.defer(); 1.256 + win.addEventListener("unload", function onunload() { 1.257 + win.removeEventListener("unload", onunload); 1.258 + deferred.resolve(); 1.259 + }); 1.260 + win.close(); 1.261 + return deferred.promise; 1.262 +} 1.263 + 1.264 +function promisePanelShown(win) { 1.265 + let panelEl = win.PanelUI.panel; 1.266 + return promisePanelElementShown(win, panelEl); 1.267 +} 1.268 + 1.269 +function promiseOverflowShown(win) { 1.270 + let panelEl = win.document.getElementById("widget-overflow"); 1.271 + return promisePanelElementShown(win, panelEl); 1.272 +} 1.273 + 1.274 +function promisePanelElementShown(win, aPanel) { 1.275 + let deferred = Promise.defer(); 1.276 + let timeoutId = win.setTimeout(() => { 1.277 + deferred.reject("Panel did not show within 20 seconds."); 1.278 + }, 20000); 1.279 + function onPanelOpen(e) { 1.280 + aPanel.removeEventListener("popupshown", onPanelOpen); 1.281 + win.clearTimeout(timeoutId); 1.282 + deferred.resolve(); 1.283 + }; 1.284 + aPanel.addEventListener("popupshown", onPanelOpen); 1.285 + return deferred.promise; 1.286 +} 1.287 + 1.288 +function promisePanelHidden(win) { 1.289 + let panelEl = win.PanelUI.panel; 1.290 + return promisePanelElementHidden(win, panelEl); 1.291 +} 1.292 + 1.293 +function promiseOverflowHidden(win) { 1.294 + let panelEl = document.getElementById("widget-overflow"); 1.295 + return promisePanelElementHidden(win, panelEl); 1.296 +} 1.297 + 1.298 +function promisePanelElementHidden(win, aPanel) { 1.299 + let deferred = Promise.defer(); 1.300 + let timeoutId = win.setTimeout(() => { 1.301 + deferred.reject("Panel did not hide within 20 seconds."); 1.302 + }, 20000); 1.303 + function onPanelClose(e) { 1.304 + aPanel.removeEventListener("popuphidden", onPanelClose); 1.305 + win.clearTimeout(timeoutId); 1.306 + deferred.resolve(); 1.307 + } 1.308 + aPanel.addEventListener("popuphidden", onPanelClose); 1.309 + return deferred.promise; 1.310 +} 1.311 + 1.312 +function isPanelUIOpen() { 1.313 + return PanelUI.panel.state == "open" || PanelUI.panel.state == "showing"; 1.314 +} 1.315 + 1.316 +function subviewShown(aSubview) { 1.317 + let deferred = Promise.defer(); 1.318 + let win = aSubview.ownerDocument.defaultView; 1.319 + let timeoutId = win.setTimeout(() => { 1.320 + deferred.reject("Subview (" + aSubview.id + ") did not show within 20 seconds."); 1.321 + }, 20000); 1.322 + function onViewShowing(e) { 1.323 + aSubview.removeEventListener("ViewShowing", onViewShowing); 1.324 + win.clearTimeout(timeoutId); 1.325 + deferred.resolve(); 1.326 + }; 1.327 + aSubview.addEventListener("ViewShowing", onViewShowing); 1.328 + return deferred.promise; 1.329 +} 1.330 + 1.331 +function subviewHidden(aSubview) { 1.332 + let deferred = Promise.defer(); 1.333 + let win = aSubview.ownerDocument.defaultView; 1.334 + let timeoutId = win.setTimeout(() => { 1.335 + deferred.reject("Subview (" + aSubview.id + ") did not hide within 20 seconds."); 1.336 + }, 20000); 1.337 + function onViewHiding(e) { 1.338 + aSubview.removeEventListener("ViewHiding", onViewHiding); 1.339 + win.clearTimeout(timeoutId); 1.340 + deferred.resolve(); 1.341 + }; 1.342 + aSubview.addEventListener("ViewHiding", onViewHiding); 1.343 + return deferred.promise; 1.344 +} 1.345 + 1.346 +function waitForCondition(aConditionFn, aMaxTries=50, aCheckInterval=100) { 1.347 + function tryNow() { 1.348 + tries++; 1.349 + if (aConditionFn()) { 1.350 + deferred.resolve(); 1.351 + } else if (tries < aMaxTries) { 1.352 + tryAgain(); 1.353 + } else { 1.354 + deferred.reject("Condition timed out: " + aConditionFn.toSource()); 1.355 + } 1.356 + } 1.357 + function tryAgain() { 1.358 + setTimeout(tryNow, aCheckInterval); 1.359 + } 1.360 + let deferred = Promise.defer(); 1.361 + let tries = 0; 1.362 + tryAgain(); 1.363 + return deferred.promise; 1.364 +} 1.365 + 1.366 +function waitFor(aTimeout=100) { 1.367 + let deferred = Promise.defer(); 1.368 + setTimeout(function() deferred.resolve(), aTimeout); 1.369 + return deferred.promise; 1.370 +} 1.371 + 1.372 +/** 1.373 + * Starts a load in an existing tab and waits for it to finish (via some event). 1.374 + * 1.375 + * @param aTab The tab to load into. 1.376 + * @param aUrl The url to load. 1.377 + * @param aEventType The load event type to wait for. Defaults to "load". 1.378 + * @return {Promise} resolved when the event is handled. 1.379 + */ 1.380 +function promiseTabLoadEvent(aTab, aURL, aEventType="load") { 1.381 + let deferred = Promise.defer(); 1.382 + info("Wait for tab event: " + aEventType); 1.383 + 1.384 + let timeoutId = setTimeout(() => { 1.385 + aTab.linkedBrowser.removeEventListener(aEventType, onTabLoad, true); 1.386 + deferred.reject("TabSelect did not happen within " + kTabEventFailureTimeoutInMs + "ms"); 1.387 + }, kTabEventFailureTimeoutInMs); 1.388 + 1.389 + function onTabLoad(event) { 1.390 + if (event.originalTarget != aTab.linkedBrowser.contentDocument || 1.391 + event.target.location.href == "about:blank") { 1.392 + info("skipping spurious load event"); 1.393 + return; 1.394 + } 1.395 + clearTimeout(timeoutId); 1.396 + aTab.linkedBrowser.removeEventListener(aEventType, onTabLoad, true); 1.397 + info("Tab event received: " + aEventType); 1.398 + deferred.resolve(); 1.399 + } 1.400 + aTab.linkedBrowser.addEventListener(aEventType, onTabLoad, true, true); 1.401 + aTab.linkedBrowser.loadURI(aURL); 1.402 + return deferred.promise; 1.403 +} 1.404 + 1.405 +/** 1.406 + * Navigate back or forward in tab history and wait for it to finish. 1.407 + * 1.408 + * @param aDirection Number to indicate to move backward or forward in history. 1.409 + * @param aConditionFn Function that returns the result of an evaluated condition 1.410 + * that needs to be `true` to resolve the promise. 1.411 + * @return {Promise} resolved when navigation has finished. 1.412 + */ 1.413 +function promiseTabHistoryNavigation(aDirection = -1, aConditionFn) { 1.414 + let deferred = Promise.defer(); 1.415 + 1.416 + let timeoutId = setTimeout(() => { 1.417 + gBrowser.removeEventListener("pageshow", listener, true); 1.418 + deferred.reject("Pageshow did not happen within " + kTabEventFailureTimeoutInMs + "ms"); 1.419 + }, kTabEventFailureTimeoutInMs); 1.420 + 1.421 + function listener(event) { 1.422 + gBrowser.removeEventListener("pageshow", listener, true); 1.423 + clearTimeout(timeoutId); 1.424 + 1.425 + if (aConditionFn) { 1.426 + waitForCondition(aConditionFn).then(() => deferred.resolve(), 1.427 + aReason => deferred.reject(aReason)); 1.428 + } else { 1.429 + deferred.resolve(); 1.430 + } 1.431 + } 1.432 + gBrowser.addEventListener("pageshow", listener, true); 1.433 + 1.434 + content.history.go(aDirection); 1.435 + 1.436 + return deferred.promise; 1.437 +} 1.438 + 1.439 +function popupShown(aPopup) { 1.440 + return promisePopupEvent(aPopup, "shown"); 1.441 +} 1.442 + 1.443 +function popupHidden(aPopup) { 1.444 + return promisePopupEvent(aPopup, "hidden"); 1.445 +} 1.446 + 1.447 +/** 1.448 + * Returns a Promise that resolves when aPopup fires an event of type 1.449 + * aEventType. Times out and rejects after 20 seconds. 1.450 + * 1.451 + * @param aPopup the popup to monitor for events. 1.452 + * @param aEventSuffix the _suffix_ for the popup event type to watch for. 1.453 + * 1.454 + * Example usage: 1.455 + * let popupShownPromise = promisePopupEvent(somePopup, "shown"); 1.456 + * // ... something that opens a popup 1.457 + * yield popupShownPromise; 1.458 + * 1.459 + * let popupHiddenPromise = promisePopupEvent(somePopup, "hidden"); 1.460 + * // ... something that hides a popup 1.461 + * yield popupHiddenPromise; 1.462 + */ 1.463 +function promisePopupEvent(aPopup, aEventSuffix) { 1.464 + let deferred = Promise.defer(); 1.465 + let win = aPopup.ownerDocument.defaultView; 1.466 + let eventType = "popup" + aEventSuffix; 1.467 + 1.468 + let timeoutId = win.setTimeout(() => { 1.469 + deferred.reject("Context menu (" + aPopup.id + ") did not fire " 1.470 + + eventType + " within 20 seconds."); 1.471 + }, 20000); 1.472 + 1.473 + function onPopupEvent(e) { 1.474 + win.clearTimeout(timeoutId); 1.475 + aPopup.removeEventListener(eventType, onPopupEvent); 1.476 + deferred.resolve(); 1.477 + }; 1.478 + 1.479 + aPopup.addEventListener(eventType, onPopupEvent); 1.480 + return deferred.promise; 1.481 +} 1.482 + 1.483 +// This is a simpler version of the context menu check that 1.484 +// exists in contextmenu_common.js. 1.485 +function checkContextMenu(aContextMenu, aExpectedEntries, aWindow=window) { 1.486 + let childNodes = aContextMenu.childNodes; 1.487 + for (let i = 0; i < childNodes.length; i++) { 1.488 + let menuitem = childNodes[i]; 1.489 + try { 1.490 + if (aExpectedEntries[i][0] == "---") { 1.491 + is(menuitem.localName, "menuseparator", "menuseparator expected"); 1.492 + continue; 1.493 + } 1.494 + 1.495 + let selector = aExpectedEntries[i][0]; 1.496 + ok(menuitem.mozMatchesSelector(selector), "menuitem should match " + selector + " selector"); 1.497 + let commandValue = menuitem.getAttribute("command"); 1.498 + let relatedCommand = commandValue ? aWindow.document.getElementById(commandValue) : null; 1.499 + let menuItemDisabled = relatedCommand ? 1.500 + relatedCommand.getAttribute("disabled") == "true" : 1.501 + menuitem.getAttribute("disabled") == "true"; 1.502 + is(menuItemDisabled, !aExpectedEntries[i][1], "disabled state for " + selector); 1.503 + } catch (e) { 1.504 + ok(false, "Exception when checking context menu: " + e); 1.505 + } 1.506 + } 1.507 +}