michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: let doc = null, toolbox = null, panelWin = null, modifiedPrefs = []; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: let target = TargetFactory.forTab(gBrowser.selectedTab); michael@0: michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) { michael@0: gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true); michael@0: gDevTools.showToolbox(target) michael@0: .then(testSelectTool) michael@0: .then(testToggleToolboxButtons) michael@0: .then(testPrefsAreRespectedWhenReopeningToolbox) michael@0: .then(cleanup, errorHandler); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools"; michael@0: } michael@0: michael@0: function testPrefsAreRespectedWhenReopeningToolbox() { michael@0: let deferred = promise.defer(); michael@0: let target = TargetFactory.forTab(gBrowser.selectedTab); michael@0: michael@0: info ("Closing toolbox to test after reopening"); michael@0: gDevTools.closeToolbox(target).then(() => { michael@0: let target = TargetFactory.forTab(gBrowser.selectedTab); michael@0: gDevTools.showToolbox(target) michael@0: .then(testSelectTool) michael@0: .then(() => { michael@0: info ("Toolbox has been reopened. Checking UI state."); michael@0: testPreferenceAndUIStateIsConsistent(); michael@0: deferred.resolve(); michael@0: }); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function testSelectTool(aToolbox) { michael@0: let deferred = promise.defer(); michael@0: info ("Selecting the options panel"); michael@0: michael@0: toolbox = aToolbox; michael@0: doc = toolbox.doc; michael@0: toolbox.once("options-selected", (event, tool) => { michael@0: ok(true, "Options panel selected via selectTool method"); michael@0: panelWin = tool.panelWin; michael@0: deferred.resolve(); michael@0: }); michael@0: toolbox.selectTool("options"); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function testPreferenceAndUIStateIsConsistent() { michael@0: let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")]; michael@0: let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")]; michael@0: let toggleableTools = toolbox.toolboxButtons; michael@0: michael@0: for (let tool of toggleableTools) { michael@0: let isVisible = getBoolPref(tool.visibilityswitch); michael@0: michael@0: let button = toolboxButtonNodes.filter(button=>button.id === tool.id)[0]; michael@0: is (!button.hasAttribute("hidden"), isVisible, "Button visibility matches pref for " + tool.id); michael@0: michael@0: let check = checkNodes.filter(node=>node.id === tool.id)[0]; michael@0: is (check.checked, isVisible, "Checkbox should be selected based on current pref for " + tool.id); michael@0: } michael@0: } michael@0: michael@0: function testToggleToolboxButtons() { michael@0: let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")]; michael@0: let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")]; michael@0: let visibleButtons = toolboxButtonNodes.filter(button=>!button.hasAttribute("hidden")); michael@0: let toggleableTools = toolbox.toolboxButtons; michael@0: michael@0: is (checkNodes.length, toggleableTools.length, "All of the buttons are toggleable." ); michael@0: is (checkNodes.length, toolboxButtonNodes.length, "All of the DOM buttons are toggleable." ); michael@0: michael@0: for (let tool of toggleableTools) { michael@0: let id = tool.id; michael@0: let matchedCheckboxes = checkNodes.filter(node=>node.id === id); michael@0: let matchedButtons = toolboxButtonNodes.filter(button=>button.id === id); michael@0: ok (matchedCheckboxes.length === 1, michael@0: "There should be a single toggle checkbox for: " + id); michael@0: ok (matchedButtons.length === 1, michael@0: "There should be a DOM button for: " + id); michael@0: is (matchedButtons[0], tool.button, michael@0: "DOM buttons should match for: " + id); michael@0: michael@0: is (matchedCheckboxes[0].getAttribute("label"), tool.label, michael@0: "The label for checkbox matches the tool definition.") michael@0: is (matchedButtons[0].getAttribute("tooltiptext"), tool.label, michael@0: "The tooltip for button matches the tool definition.") michael@0: } michael@0: michael@0: // Store modified pref names so that they can be cleared on error. michael@0: for (let tool of toggleableTools) { michael@0: let pref = tool.visibilityswitch; michael@0: modifiedPrefs.push(pref); michael@0: } michael@0: michael@0: // Try checking each checkbox, making sure that it changes the preference michael@0: for (let node of checkNodes) { michael@0: let tool = toggleableTools.filter(tool=>tool.id === node.id)[0]; michael@0: let isVisible = getBoolPref(tool.visibilityswitch); michael@0: michael@0: testPreferenceAndUIStateIsConsistent(); michael@0: toggleButton(node); michael@0: testPreferenceAndUIStateIsConsistent(); michael@0: michael@0: let isVisibleAfterClick = getBoolPref(tool.visibilityswitch); michael@0: michael@0: is (isVisible, !isVisibleAfterClick, michael@0: "Clicking on the node should have toggled visibility preference for " + tool.visibilityswitch); michael@0: } michael@0: michael@0: return promise.resolve(); michael@0: } michael@0: michael@0: function getBoolPref(key) { michael@0: return Services.prefs.getBoolPref(key); michael@0: } michael@0: michael@0: function toggleButton(node) { michael@0: node.scrollIntoView(); michael@0: EventUtils.synthesizeMouseAtCenter(node, {}, panelWin); michael@0: } michael@0: michael@0: function cleanup() { michael@0: toolbox.destroy().then(function() { michael@0: gBrowser.removeCurrentTab(); michael@0: for (let pref of modifiedPrefs) { michael@0: Services.prefs.clearUserPref(pref); michael@0: } michael@0: toolbox = doc = panelWin = modifiedPrefs = null; michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: function errorHandler(error) { michael@0: ok(false, "Unexpected error: " + error); michael@0: cleanup(); michael@0: }