diff --git a/browser/devtools/framework/test/browser_toolbox_options.js b/browser/devtools/framework/test/browser_toolbox_options.js index 299e19c8cd01..b60e9d168420 100644 --- a/browser/devtools/framework/test/browser_toolbox_options.js +++ b/browser/devtools/framework/test/browser_toolbox_options.js @@ -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* () { - let tab = yield addTab(URL); - let target = TargetFactory.forTab(tab); - let toolbox = yield gDevTools.showToolbox(target); - yield testSelectTool(toolbox); - yield testOptionsShortcut(); - yield testOptions(); - yield testToggleTools(); - }).then(cleanup, errorHandler); -} - -function testSelectTool(aToolbox) { - let deferred = promise.defer(); - - toolbox = aToolbox; + registerNewTool(); + let tab = yield addTab(URL); + let target = TargetFactory.forTab(tab); + toolbox = yield gDevTools.showToolbox(target); doc = toolbox.doc; - toolbox.once("options-selected", () => { - ok(true, "Toolbox selected via selectTool method"); - deferred.resolve(); - }); - toolbox.selectTool("options"); + yield testSelectTool(); + yield testOptionsShortcut(); + yield testOptions(); + yield testToggleTools(); + yield cleanup(); +}); - 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() { + gDevTools.unregisterTool("test-tool"); + yield toolbox.destroy(); + gBrowser.removeCurrentTab(); + for (let pref of modifiedPrefs) { + Services.prefs.clearUserPref(pref); } -} - -function cleanup() { - toolbox.destroy().then(function() { - 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(); + toolbox = doc = panelWin = modifiedPrefs = null; } diff --git a/browser/devtools/framework/toolbox-options.js b/browser/devtools/framework/toolbox-options.js index fdcc81e55b8f..3d028eda9740 100644 --- a/browser/devtools/framework/toolbox-options.js +++ b/browser/devtools/framework/toolbox-options.js @@ -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); }