browser/devtools/framework/test/browser_toolbox_options_disable_buttons.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 let doc = null, toolbox = null, panelWin = null, modifiedPrefs = [];
michael@0 5
michael@0 6 function test() {
michael@0 7 waitForExplicitFinish();
michael@0 8
michael@0 9 gBrowser.selectedTab = gBrowser.addTab();
michael@0 10 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 11
michael@0 12 gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) {
michael@0 13 gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true);
michael@0 14 gDevTools.showToolbox(target)
michael@0 15 .then(testSelectTool)
michael@0 16 .then(testToggleToolboxButtons)
michael@0 17 .then(testPrefsAreRespectedWhenReopeningToolbox)
michael@0 18 .then(cleanup, errorHandler);
michael@0 19 }, true);
michael@0 20
michael@0 21 content.location = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools";
michael@0 22 }
michael@0 23
michael@0 24 function testPrefsAreRespectedWhenReopeningToolbox() {
michael@0 25 let deferred = promise.defer();
michael@0 26 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 27
michael@0 28 info ("Closing toolbox to test after reopening");
michael@0 29 gDevTools.closeToolbox(target).then(() => {
michael@0 30 let target = TargetFactory.forTab(gBrowser.selectedTab);
michael@0 31 gDevTools.showToolbox(target)
michael@0 32 .then(testSelectTool)
michael@0 33 .then(() => {
michael@0 34 info ("Toolbox has been reopened. Checking UI state.");
michael@0 35 testPreferenceAndUIStateIsConsistent();
michael@0 36 deferred.resolve();
michael@0 37 });
michael@0 38 });
michael@0 39
michael@0 40 return deferred.promise;
michael@0 41 }
michael@0 42
michael@0 43 function testSelectTool(aToolbox) {
michael@0 44 let deferred = promise.defer();
michael@0 45 info ("Selecting the options panel");
michael@0 46
michael@0 47 toolbox = aToolbox;
michael@0 48 doc = toolbox.doc;
michael@0 49 toolbox.once("options-selected", (event, tool) => {
michael@0 50 ok(true, "Options panel selected via selectTool method");
michael@0 51 panelWin = tool.panelWin;
michael@0 52 deferred.resolve();
michael@0 53 });
michael@0 54 toolbox.selectTool("options");
michael@0 55
michael@0 56 return deferred.promise;
michael@0 57 }
michael@0 58
michael@0 59 function testPreferenceAndUIStateIsConsistent() {
michael@0 60 let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")];
michael@0 61 let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")];
michael@0 62 let toggleableTools = toolbox.toolboxButtons;
michael@0 63
michael@0 64 for (let tool of toggleableTools) {
michael@0 65 let isVisible = getBoolPref(tool.visibilityswitch);
michael@0 66
michael@0 67 let button = toolboxButtonNodes.filter(button=>button.id === tool.id)[0];
michael@0 68 is (!button.hasAttribute("hidden"), isVisible, "Button visibility matches pref for " + tool.id);
michael@0 69
michael@0 70 let check = checkNodes.filter(node=>node.id === tool.id)[0];
michael@0 71 is (check.checked, isVisible, "Checkbox should be selected based on current pref for " + tool.id);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 function testToggleToolboxButtons() {
michael@0 76 let checkNodes = [...panelWin.document.querySelectorAll("#enabled-toolbox-buttons-box > checkbox")];
michael@0 77 let toolboxButtonNodes = [...doc.querySelectorAll("#toolbox-buttons > toolbarbutton")];
michael@0 78 let visibleButtons = toolboxButtonNodes.filter(button=>!button.hasAttribute("hidden"));
michael@0 79 let toggleableTools = toolbox.toolboxButtons;
michael@0 80
michael@0 81 is (checkNodes.length, toggleableTools.length, "All of the buttons are toggleable." );
michael@0 82 is (checkNodes.length, toolboxButtonNodes.length, "All of the DOM buttons are toggleable." );
michael@0 83
michael@0 84 for (let tool of toggleableTools) {
michael@0 85 let id = tool.id;
michael@0 86 let matchedCheckboxes = checkNodes.filter(node=>node.id === id);
michael@0 87 let matchedButtons = toolboxButtonNodes.filter(button=>button.id === id);
michael@0 88 ok (matchedCheckboxes.length === 1,
michael@0 89 "There should be a single toggle checkbox for: " + id);
michael@0 90 ok (matchedButtons.length === 1,
michael@0 91 "There should be a DOM button for: " + id);
michael@0 92 is (matchedButtons[0], tool.button,
michael@0 93 "DOM buttons should match for: " + id);
michael@0 94
michael@0 95 is (matchedCheckboxes[0].getAttribute("label"), tool.label,
michael@0 96 "The label for checkbox matches the tool definition.")
michael@0 97 is (matchedButtons[0].getAttribute("tooltiptext"), tool.label,
michael@0 98 "The tooltip for button matches the tool definition.")
michael@0 99 }
michael@0 100
michael@0 101 // Store modified pref names so that they can be cleared on error.
michael@0 102 for (let tool of toggleableTools) {
michael@0 103 let pref = tool.visibilityswitch;
michael@0 104 modifiedPrefs.push(pref);
michael@0 105 }
michael@0 106
michael@0 107 // Try checking each checkbox, making sure that it changes the preference
michael@0 108 for (let node of checkNodes) {
michael@0 109 let tool = toggleableTools.filter(tool=>tool.id === node.id)[0];
michael@0 110 let isVisible = getBoolPref(tool.visibilityswitch);
michael@0 111
michael@0 112 testPreferenceAndUIStateIsConsistent();
michael@0 113 toggleButton(node);
michael@0 114 testPreferenceAndUIStateIsConsistent();
michael@0 115
michael@0 116 let isVisibleAfterClick = getBoolPref(tool.visibilityswitch);
michael@0 117
michael@0 118 is (isVisible, !isVisibleAfterClick,
michael@0 119 "Clicking on the node should have toggled visibility preference for " + tool.visibilityswitch);
michael@0 120 }
michael@0 121
michael@0 122 return promise.resolve();
michael@0 123 }
michael@0 124
michael@0 125 function getBoolPref(key) {
michael@0 126 return Services.prefs.getBoolPref(key);
michael@0 127 }
michael@0 128
michael@0 129 function toggleButton(node) {
michael@0 130 node.scrollIntoView();
michael@0 131 EventUtils.synthesizeMouseAtCenter(node, {}, panelWin);
michael@0 132 }
michael@0 133
michael@0 134 function cleanup() {
michael@0 135 toolbox.destroy().then(function() {
michael@0 136 gBrowser.removeCurrentTab();
michael@0 137 for (let pref of modifiedPrefs) {
michael@0 138 Services.prefs.clearUserPref(pref);
michael@0 139 }
michael@0 140 toolbox = doc = panelWin = modifiedPrefs = null;
michael@0 141 finish();
michael@0 142 });
michael@0 143 }
michael@0 144
michael@0 145 function errorHandler(error) {
michael@0 146 ok(false, "Unexpected error: " + error);
michael@0 147 cleanup();
michael@0 148 }

mercurial