2013-05-30 14:09:04 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
|
|
// Avoid leaks by using tmp for imports...
|
|
|
|
let tmp = {};
|
2013-06-17 07:37:41 -07:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm", tmp);
|
2013-05-30 14:09:04 -07:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm", tmp);
|
|
|
|
Cu.import("resource:///modules/CustomizableUI.jsm", tmp);
|
2013-06-03 13:13:34 -07:00
|
|
|
let {Promise, Task, CustomizableUI} = tmp;
|
|
|
|
|
|
|
|
let ChromeUtils = {};
|
|
|
|
let scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader);
|
|
|
|
scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils);
|
|
|
|
|
|
|
|
let {synthesizeDragStart, synthesizeDrop} = ChromeUtils;
|
2013-05-30 14:09:04 -07:00
|
|
|
|
|
|
|
const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
|
2013-06-03 13:12:00 -07:00
|
|
|
function createDummyXULButton(id, label) {
|
|
|
|
let btn = document.createElementNS(kNSXUL, "toolbarbutton");
|
|
|
|
btn.id = id;
|
|
|
|
btn.setAttribute("label", label || id);
|
|
|
|
btn.className = "toolbarbutton-1 chromeclass-toolbar-additional";
|
|
|
|
window.gNavToolbox.palette.appendChild(btn);
|
|
|
|
return btn;
|
|
|
|
}
|
|
|
|
|
2013-10-16 11:52:00 -07:00
|
|
|
let gAddedToolbars = new Set();
|
|
|
|
|
2013-05-30 14:09:04 -07:00
|
|
|
function createToolbarWithPlacements(id, placements) {
|
2013-10-16 11:52:00 -07:00
|
|
|
gAddedToolbars.add(id);
|
2013-05-30 14:09:04 -07:00
|
|
|
let tb = document.createElementNS(kNSXUL, "toolbar");
|
|
|
|
tb.id = id;
|
|
|
|
tb.setAttribute("customizable", "true");
|
|
|
|
CustomizableUI.registerArea(id, {
|
2013-06-11 08:10:08 -07:00
|
|
|
type: CustomizableUI.TYPE_TOOLBAR,
|
2013-06-03 13:12:00 -07:00
|
|
|
defaultPlacements: placements
|
2013-05-30 14:09:04 -07:00
|
|
|
});
|
2013-10-16 11:52:00 -07:00
|
|
|
gNavToolbox.appendChild(tb);
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeCustomToolbars() {
|
2013-10-16 11:52:00 -07:00
|
|
|
CustomizableUI.reset();
|
|
|
|
for (let toolbarId of gAddedToolbars) {
|
|
|
|
CustomizableUI.unregisterArea(toolbarId);
|
|
|
|
document.getElementById(toolbarId).remove();
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
2013-10-16 11:52:00 -07:00
|
|
|
gAddedToolbars.clear();
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetCustomization() {
|
2013-06-17 07:37:41 -07:00
|
|
|
return CustomizableUI.reset();
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertAreaPlacements(areaId, expectedPlacements) {
|
|
|
|
let actualPlacements = getAreaWidgetIds(areaId);
|
|
|
|
is(actualPlacements.length, expectedPlacements.length,
|
2013-06-03 13:13:34 -07:00
|
|
|
"Area " + areaId + " should have " + expectedPlacements.length + " items.");
|
2013-05-30 14:09:04 -07:00
|
|
|
let minItems = Math.min(expectedPlacements.length, actualPlacements.length);
|
|
|
|
for (let i = 0; i < minItems; i++) {
|
|
|
|
if (typeof expectedPlacements[i] == "string") {
|
|
|
|
is(actualPlacements[i], expectedPlacements[i],
|
|
|
|
"Item " + i + " in " + areaId + " should match expectations.");
|
|
|
|
} else if (expectedPlacements[i] instanceof RegExp) {
|
|
|
|
ok(expectedPlacements[i].test(actualPlacements[i]),
|
|
|
|
"Item " + i + " (" + actualPlacements[i] + ") in " +
|
|
|
|
areaId + " should match " + expectedPlacements[i]);
|
|
|
|
} else {
|
|
|
|
ok(false, "Unknown type of expected placement passed to " +
|
|
|
|
" assertAreaPlacements. Is your test broken?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 16:13:01 -07:00
|
|
|
function todoAssertAreaPlacements(areaId, expectedPlacements) {
|
|
|
|
let actualPlacements = getAreaWidgetIds(areaId);
|
|
|
|
let isPassing = actualPlacements.length == expectedPlacements.length;
|
|
|
|
let minItems = Math.min(expectedPlacements.length, actualPlacements.length);
|
|
|
|
for (let i = 0; i < minItems; i++) {
|
|
|
|
if (typeof expectedPlacements[i] == "string") {
|
|
|
|
isPassing = isPassing && actualPlacements[i] == expectedPlacements[i];
|
|
|
|
} else if (expectedPlacements[i] instanceof RegExp) {
|
|
|
|
isPassing = isPassing && expectedPlacements[i].test(actualPlacements[i]);
|
|
|
|
} else {
|
|
|
|
ok(false, "Unknown type of expected placement passed to " +
|
|
|
|
" assertAreaPlacements. Is your test broken?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
todo(isPassing, "The area placements for " + areaId +
|
|
|
|
" should equal the expected placements.")
|
|
|
|
}
|
|
|
|
|
2013-05-30 14:09:04 -07:00
|
|
|
function getAreaWidgetIds(areaId) {
|
2013-07-05 06:36:00 -07:00
|
|
|
return CustomizableUI.getWidgetIdsInArea(areaId);
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
2013-06-03 13:13:34 -07:00
|
|
|
function simulateItemDrag(toDrag, target) {
|
|
|
|
let docId = toDrag.ownerDocument.documentElement.id;
|
|
|
|
let dragData = [[{type: 'text/toolbarwrapper-id/' + docId,
|
2013-08-27 02:17:25 -07:00
|
|
|
data: toDrag.id}]];
|
2013-06-03 13:13:34 -07:00
|
|
|
synthesizeDragStart(toDrag.parentNode, dragData);
|
|
|
|
synthesizeDrop(target, target, dragData);
|
|
|
|
}
|
|
|
|
|
|
|
|
function endCustomizing() {
|
2013-10-16 11:52:00 -07:00
|
|
|
if (document.documentElement.getAttribute("customizing") != "true") {
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-04 12:07:14 -07:00
|
|
|
let deferredEndCustomizing = Promise.defer();
|
|
|
|
function onCustomizationEnds() {
|
2013-06-03 13:13:34 -07:00
|
|
|
window.gNavToolbox.removeEventListener("aftercustomization", onCustomizationEnds);
|
2013-06-04 12:07:14 -07:00
|
|
|
deferredEndCustomizing.resolve();
|
2013-06-03 13:13:34 -07:00
|
|
|
}
|
|
|
|
window.gNavToolbox.addEventListener("aftercustomization", onCustomizationEnds);
|
|
|
|
window.gCustomizeMode.exit();
|
2013-06-04 12:07:14 -07:00
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
return deferredEndCustomizing.promise.then(function() {
|
|
|
|
let deferredLoadNewTab = Promise.defer();
|
2013-06-04 12:07:14 -07:00
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
//XXXgijs so some tests depend on this tab being about:blank. Make it so.
|
|
|
|
let newTabBrowser = window.gBrowser.selectedBrowser;
|
|
|
|
newTabBrowser.stop();
|
2013-06-04 12:07:14 -07:00
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
// If we stop early enough, this might actually be about:blank.
|
|
|
|
if (newTabBrowser.contentDocument.location.href == "about:blank") {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-04 12:07:14 -07:00
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
// Otherwise, make it be about:blank, and wait for that to be done.
|
|
|
|
function onNewTabLoaded(e) {
|
|
|
|
newTabBrowser.removeEventListener("load", onNewTabLoaded, true);
|
|
|
|
deferredLoadNewTab.resolve();
|
|
|
|
}
|
|
|
|
newTabBrowser.addEventListener("load", onNewTabLoaded, true);
|
|
|
|
newTabBrowser.contentDocument.location.replace("about:blank");
|
|
|
|
return deferredLoadNewTab.promise;
|
|
|
|
});
|
2013-06-03 13:13:34 -07:00
|
|
|
}
|
2013-06-04 12:07:14 -07:00
|
|
|
|
2013-06-03 13:13:34 -07:00
|
|
|
function startCustomizing() {
|
2013-09-26 01:06:55 -07:00
|
|
|
if (document.documentElement.getAttribute("customizing") == "true") {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-03 13:13:34 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
function onCustomizing() {
|
|
|
|
window.gNavToolbox.removeEventListener("customizationready", onCustomizing);
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
window.gNavToolbox.addEventListener("customizationready", onCustomizing);
|
|
|
|
window.gCustomizeMode.enter();
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2013-09-26 01:04:21 -07:00
|
|
|
function openAndLoadWindow(aOptions, aWaitForDelayedStartup=false) {
|
2013-07-10 09:56:10 -07:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
let win = OpenBrowserWindow(aOptions);
|
2013-09-26 01:04:21 -07:00
|
|
|
if (aWaitForDelayedStartup) {
|
|
|
|
Services.obs.addObserver(function onDS(aSubject, aTopic, aData) {
|
|
|
|
if (aSubject != win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Services.obs.removeObserver(onDS, "browser-delayed-startup-finished");
|
|
|
|
deferred.resolve(win);
|
|
|
|
}, "browser-delayed-startup-finished", false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
win.addEventListener("load", function onLoad() {
|
|
|
|
win.removeEventListener("load", onLoad);
|
|
|
|
deferred.resolve(win);
|
|
|
|
});
|
|
|
|
}
|
2013-07-10 09:56:10 -07:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2013-09-09 15:37:24 -07:00
|
|
|
function waitForCondition(aConditionFn, aMaxTries=50, aCheckInterval=100) {
|
|
|
|
function tryNow() {
|
|
|
|
tries++;
|
|
|
|
if (aConditionFn()) {
|
|
|
|
deferred.resolve();
|
|
|
|
} else if (tries < aMaxTries) {
|
|
|
|
tryAgain();
|
|
|
|
} else {
|
|
|
|
deferred.reject("Condition timed out: " + aConditionFn.toSource());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function tryAgain() {
|
|
|
|
setTimeout(tryNow, aCheckInterval);
|
|
|
|
}
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
let tries = 0;
|
|
|
|
tryAgain();
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2013-10-10 13:41:36 -07:00
|
|
|
function waitFor(aTimeout=100) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
setTimeout(function() deferred.resolve(), aTimeout);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
function testRunner(testAry, asyncCleanup) {
|
2013-09-20 05:16:33 -07:00
|
|
|
Services.prefs.setBoolPref("browser.uiCustomization.disableAnimation", true);
|
2013-05-30 14:09:04 -07:00
|
|
|
for (let test of testAry) {
|
|
|
|
info(test.desc);
|
|
|
|
|
|
|
|
if (test.setup)
|
|
|
|
yield test.setup();
|
|
|
|
|
|
|
|
info("Running test");
|
|
|
|
yield test.run();
|
|
|
|
info("Cleanup");
|
|
|
|
if (test.teardown)
|
|
|
|
yield test.teardown();
|
2013-10-10 13:41:36 -07:00
|
|
|
ok(!document.getElementById(CustomizableUI.AREA_NAVBAR).hasAttribute("overflowing"), "Shouldn't overflow");
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
2013-06-17 07:37:41 -07:00
|
|
|
if (asyncCleanup) {
|
|
|
|
yield asyncCleanup();
|
|
|
|
}
|
2013-09-09 15:37:38 -07:00
|
|
|
ok(CustomizableUI.inDefaultState, "Should remain in default state");
|
2013-09-20 05:16:33 -07:00
|
|
|
Services.prefs.clearUserPref("browser.uiCustomization.disableAnimation");
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|
|
|
|
|
2013-06-17 07:37:41 -07:00
|
|
|
function runTests(testAry, asyncCleanup) {
|
|
|
|
Task.spawn(testRunner(gTests, asyncCleanup)).then(finish, ex => {
|
2013-06-20 16:13:01 -07:00
|
|
|
// The stack of ok() here is misleading due to Promises. The stack of the
|
|
|
|
// actual exception is likely much more valuable, hence concatentating it.
|
|
|
|
ok(false, "Unexpected exception: " + ex + " With stack: " + ex.stack);
|
2013-05-30 14:09:04 -07:00
|
|
|
finish();
|
2013-08-21 12:38:00 -07:00
|
|
|
}).then(null, Cu.reportError);
|
2013-05-30 14:09:04 -07:00
|
|
|
}
|