Bug 1060945 - Make it possible to enable/disable a devtools panel even if its visibilityswitch pref doesn't exist. r=pbrosset

This commit is contained in:
Brian Grinstead 2014-09-08 11:06:00 -04:00
Родитель b662f8d8db
Коммит cb6b50ca50
2 изменённых файлов: 73 добавлений и 78 удалений

Просмотреть файл

@ -1,46 +1,60 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that changing preferences in the options panel updates the prefs
// and toggles appropriate things in the toolbox.
let doc = null, toolbox = null, panelWin = null, modifiedPrefs = [];
function test() {
let test = asyncTest(function*() {
const URL = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools";
Task.spawn(function* () {
registerNewTool();
let tab = yield addTab(URL);
let target = TargetFactory.forTab(tab);
let toolbox = yield gDevTools.showToolbox(target);
yield testSelectTool(toolbox);
toolbox = yield gDevTools.showToolbox(target);
doc = toolbox.doc;
yield testSelectTool();
yield testOptionsShortcut();
yield testOptions();
yield testToggleTools();
}).then(cleanup, errorHandler);
}
function testSelectTool(aToolbox) {
let deferred = promise.defer();
toolbox = aToolbox;
doc = toolbox.doc;
toolbox.once("options-selected", () => {
ok(true, "Toolbox selected via selectTool method");
deferred.resolve();
yield cleanup();
});
toolbox.selectTool("options");
return deferred.promise;
function registerNewTool() {
let toolDefinition = {
id: "test-tool",
isTargetSupported: function() true,
visibilityswitch: "devtools.test-tool.enabled",
url: "about:blank",
label: "someLabel"
};
ok(gDevTools, "gDevTools exists");
ok(!gDevTools.getToolDefinitionMap().has("test-tool"),
"The tool is not registered");
gDevTools.registerTool(toolDefinition);
ok(gDevTools.getToolDefinitionMap().has("test-tool"),
"The tool is registered");
}
function testOptionsShortcut() {
let deferred = promise.defer();
function* testSelectTool() {
info ("Checking to make sure that the options panel can be selected.");
toolbox.selectTool("webconsole")
let onceSelected = toolbox.once("options-selected");
toolbox.selectTool("options");
yield onceSelected;
ok(true, "Toolbox selected via selectTool method");
}
function* testOptionsShortcut() {
info ("Selecting another tool, then reselecting options panel with keyboard.");
yield toolbox.selectTool("webconsole")
.then(() => synthesizeKeyFromKeyTag("toolbox-options-key", doc))
.then(() => {
ok(true, "Toolbox selected via shortcut key");
deferred.resolve();
});
return deferred.promise;
}
function* testOptions() {
@ -103,7 +117,7 @@ function* testMenuList(menulist) {
}
}
function testMouseClick(node, prefValue) {
function* testMouseClick(node, prefValue) {
let deferred = promise.defer();
let pref = node.getAttribute("data-pref");
@ -127,14 +141,17 @@ function testMouseClick(node, prefValue) {
EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
});
return deferred.promise;
yield deferred.promise;
}
function testToggleTools() {
let toolNodes = panelWin.document.querySelectorAll("#default-tools-box > checkbox:not([unsupported])");
let enabledTools = Array.prototype.filter.call(toolNodes, node => node.checked);
function* testToggleTools() {
let toolNodes = panelWin.document.querySelectorAll("#default-tools-box > checkbox:not([unsupported]), #additional-tools-box > checkbox:not([unsupported])");
let enabledTools = [...toolNodes].filter(node => node.checked);
let toggleableTools = gDevTools.getDefaultTools().filter(tool => {
return tool.visibilityswitch;
}).concat(gDevTools.getAdditionalTools());
let toggleableTools = gDevTools.getDefaultTools().filter(tool => tool.visibilityswitch);
for (let node of toolNodes) {
let id = node.getAttribute("id");
ok (toggleableTools.some(tool => tool.id === id),
@ -148,23 +165,22 @@ function testToggleTools() {
}
// Toggle each tool
let p = promise.resolve();
for (let node of toolNodes) {
p = p.then(toggleTool.bind(null, node));
yield toggleTool(node);
}
// Toggle again to reset tool enablement state
for (let node of toolNodes) {
p = p.then(toggleTool.bind(null, node));
yield toggleTool(node);
}
// Test that a tool can still be added when no tabs are present:
// Disable all tools
for (let node of enabledTools) {
p = p.then(toggleTool.bind(null, node));
yield toggleTool(node);
}
// Re-enable the tools which are enabled by default
for (let node of enabledTools) {
p = p.then(toggleTool.bind(null, node));
yield toggleTool(node);
}
// Toggle first, middle, and last tools to ensure that toolbox tabs are
@ -173,20 +189,19 @@ function testToggleTools() {
middleTool = toolNodes[(toolNodes.length / 2) | 0],
lastTool = toolNodes[toolNodes.length - 1];
p = p.then(toggleTool.bind(null, firstTool))
.then(toggleTool.bind(null, firstTool))
.then(toggleTool.bind(null, middleTool))
.then(toggleTool.bind(null, middleTool))
.then(toggleTool.bind(null, lastTool))
.then(toggleTool.bind(null, lastTool));
return p;
yield toggleTool(firstTool);
yield toggleTool(firstTool);
yield toggleTool(middleTool);
yield toggleTool(middleTool);
yield toggleTool(lastTool);
yield toggleTool(lastTool);
}
function toggleTool(node) {
function* toggleTool(node) {
let deferred = promise.defer();
let toolId = node.getAttribute("id");
let onRegistrationChange;
if (node.checked) {
gDevTools.once("tool-unregistered", checkUnregistered.bind(null, toolId, deferred));
} else {
@ -195,7 +210,7 @@ function toggleTool(node) {
node.scrollIntoView();
EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
return deferred.promise;
yield deferred.promise;
}
function checkUnregistered(toolId, deferred, event, data) {
@ -249,32 +264,12 @@ function GetPref(name) {
}
}
function SetPref(name, value) {
let type = Services.prefs.getPrefType(name);
switch (type) {
case Services.prefs.PREF_STRING:
return Services.prefs.setCharPref(name, value);
case Services.prefs.PREF_INT:
return Services.prefs.setIntPref(name, value);
case Services.prefs.PREF_BOOL:
return Services.prefs.setBoolPref(name, value);
default:
throw new Error("Unknown type");
}
}
function cleanup() {
toolbox.destroy().then(function() {
function* cleanup() {
gDevTools.unregisterTool("test-tool");
yield toolbox.destroy();
gBrowser.removeCurrentTab();
for (let pref of modifiedPrefs) {
Services.prefs.clearUserPref(pref);
}
toolbox = doc = panelWin = modifiedPrefs = null;
finish();
});
}
function errorHandler(error) {
ok(false, "Unexpected error: " + error);
cleanup();
}

Просмотреть файл

@ -168,7 +168,7 @@ OptionsPanel.prototype = {
let onCheckboxClick = (checkbox) => {
let toolDefinition = toggleableButtons.filter(tool => tool.id === checkbox.id)[0];
SetPref(toolDefinition.visibilityswitch, checkbox.checked);
Services.prefs.setBoolPref(toolDefinition.visibilityswitch, checkbox.checked);
setToolboxButtonsVisibility();
};
@ -198,7 +198,7 @@ OptionsPanel.prototype = {
let onCheckboxClick = function(id) {
let toolDefinition = gDevTools._tools.get(id);
// Set the kill switch pref boolean to true
SetPref(toolDefinition.visibilityswitch, this.checked);
Services.prefs.setBoolPref(toolDefinition.visibilityswitch, this.checked);
if (this.checked) {
gDevTools.emit("tool-registered", id);
}