Wed, 31 Dec 2014 06:09:35 +0100
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 | const URL = "data:text/html;charset=utf8,test for dynamically registering and unregistering tools"; |
michael@0 | 10 | Task.spawn(function* () { |
michael@0 | 11 | let { target } = yield addTab(URL); |
michael@0 | 12 | let toolbox = yield gDevTools.showToolbox(target); |
michael@0 | 13 | yield testSelectTool(toolbox); |
michael@0 | 14 | yield testOptionsShortcut(); |
michael@0 | 15 | yield testOptions(); |
michael@0 | 16 | yield testToggleTools(); |
michael@0 | 17 | }).then(cleanup, errorHandler); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | function testSelectTool(aToolbox) { |
michael@0 | 21 | let deferred = promise.defer(); |
michael@0 | 22 | |
michael@0 | 23 | toolbox = aToolbox; |
michael@0 | 24 | doc = toolbox.doc; |
michael@0 | 25 | toolbox.once("options-selected", () => { |
michael@0 | 26 | ok(true, "Toolbox selected via selectTool method"); |
michael@0 | 27 | deferred.resolve(); |
michael@0 | 28 | }); |
michael@0 | 29 | toolbox.selectTool("options"); |
michael@0 | 30 | |
michael@0 | 31 | return deferred.promise; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function testOptionsShortcut() { |
michael@0 | 35 | let deferred = promise.defer(); |
michael@0 | 36 | |
michael@0 | 37 | toolbox.selectTool("webconsole") |
michael@0 | 38 | .then(() => synthesizeKeyFromKeyTag("toolbox-options-key", doc)) |
michael@0 | 39 | .then(() => { |
michael@0 | 40 | ok(true, "Toolbox selected via shortcut key"); |
michael@0 | 41 | deferred.resolve(); |
michael@0 | 42 | }); |
michael@0 | 43 | |
michael@0 | 44 | return deferred.promise; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function testOptions() { |
michael@0 | 48 | let tool = toolbox.getPanel("options"); |
michael@0 | 49 | panelWin = tool.panelWin; |
michael@0 | 50 | let prefNodes = tool.panelDoc.querySelectorAll("checkbox[data-pref]"); |
michael@0 | 51 | |
michael@0 | 52 | // Store modified pref names so that they can be cleared on error. |
michael@0 | 53 | for (let node of prefNodes) { |
michael@0 | 54 | let pref = node.getAttribute("data-pref"); |
michael@0 | 55 | modifiedPrefs.push(pref); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | // Test each options pref |
michael@0 | 59 | let p = promise.resolve(); |
michael@0 | 60 | for (let node of prefNodes) { |
michael@0 | 61 | let prefValue = Services.prefs.getBoolPref(node.getAttribute("data-pref")); |
michael@0 | 62 | p = p.then(testMouseClick.bind(null, node, prefValue)); |
michael@0 | 63 | } |
michael@0 | 64 | // Do again with opposite values to reset prefs |
michael@0 | 65 | for (let node of prefNodes) { |
michael@0 | 66 | let prefValue = !Services.prefs.getBoolPref(node.getAttribute("data-pref")); |
michael@0 | 67 | p = p.then(testMouseClick.bind(null, node, prefValue)); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | return p; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function testMouseClick(node, prefValue) { |
michael@0 | 74 | let deferred = promise.defer(); |
michael@0 | 75 | |
michael@0 | 76 | let pref = node.getAttribute("data-pref"); |
michael@0 | 77 | gDevTools.once("pref-changed", (event, data) => { |
michael@0 | 78 | if (data.pref == pref) { |
michael@0 | 79 | ok(true, "Correct pref was changed"); |
michael@0 | 80 | is(data.oldValue, prefValue, "Previous value is correct"); |
michael@0 | 81 | is(data.newValue, !prefValue, "New value is correct"); |
michael@0 | 82 | } else { |
michael@0 | 83 | ok(false, "Pref " + pref + " was not changed correctly"); |
michael@0 | 84 | } |
michael@0 | 85 | deferred.resolve(); |
michael@0 | 86 | }); |
michael@0 | 87 | |
michael@0 | 88 | node.scrollIntoView(); |
michael@0 | 89 | |
michael@0 | 90 | // We use executeSoon here to ensure that the element is in view and |
michael@0 | 91 | // clickable. |
michael@0 | 92 | executeSoon(function() { |
michael@0 | 93 | info("Click event synthesized for pref " + pref); |
michael@0 | 94 | EventUtils.synthesizeMouseAtCenter(node, {}, panelWin); |
michael@0 | 95 | }); |
michael@0 | 96 | |
michael@0 | 97 | return deferred.promise; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function testToggleTools() { |
michael@0 | 101 | let toolNodes = panelWin.document.querySelectorAll("#default-tools-box > checkbox:not([unsupported])"); |
michael@0 | 102 | let enabledTools = Array.prototype.filter.call(toolNodes, node => node.checked); |
michael@0 | 103 | |
michael@0 | 104 | let toggleableTools = gDevTools.getDefaultTools().filter(tool => tool.visibilityswitch); |
michael@0 | 105 | for (let node of toolNodes) { |
michael@0 | 106 | let id = node.getAttribute("id"); |
michael@0 | 107 | ok (toggleableTools.some(tool => tool.id === id), |
michael@0 | 108 | "There should be a toggle checkbox for: " + id); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // Store modified pref names so that they can be cleared on error. |
michael@0 | 112 | for (let tool of toggleableTools) { |
michael@0 | 113 | let pref = tool.visibilityswitch; |
michael@0 | 114 | modifiedPrefs.push(pref); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Toggle each tool |
michael@0 | 118 | let p = promise.resolve(); |
michael@0 | 119 | for (let node of toolNodes) { |
michael@0 | 120 | p = p.then(toggleTool.bind(null, node)); |
michael@0 | 121 | } |
michael@0 | 122 | // Toggle again to reset tool enablement state |
michael@0 | 123 | for (let node of toolNodes) { |
michael@0 | 124 | p = p.then(toggleTool.bind(null, node)); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | // Test that a tool can still be added when no tabs are present: |
michael@0 | 128 | // Disable all tools |
michael@0 | 129 | for (let node of enabledTools) { |
michael@0 | 130 | p = p.then(toggleTool.bind(null, node)); |
michael@0 | 131 | } |
michael@0 | 132 | // Re-enable the tools which are enabled by default |
michael@0 | 133 | for (let node of enabledTools) { |
michael@0 | 134 | p = p.then(toggleTool.bind(null, node)); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // Toggle first, middle, and last tools to ensure that toolbox tabs are |
michael@0 | 138 | // inserted in order |
michael@0 | 139 | let firstTool = toolNodes[0], |
michael@0 | 140 | middleTool = toolNodes[(toolNodes.length / 2) | 0], |
michael@0 | 141 | lastTool = toolNodes[toolNodes.length - 1]; |
michael@0 | 142 | |
michael@0 | 143 | p = p.then(toggleTool.bind(null, firstTool)) |
michael@0 | 144 | .then(toggleTool.bind(null, firstTool)) |
michael@0 | 145 | .then(toggleTool.bind(null, middleTool)) |
michael@0 | 146 | .then(toggleTool.bind(null, middleTool)) |
michael@0 | 147 | .then(toggleTool.bind(null, lastTool)) |
michael@0 | 148 | .then(toggleTool.bind(null, lastTool)); |
michael@0 | 149 | |
michael@0 | 150 | return p; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | function toggleTool(node) { |
michael@0 | 154 | let deferred = promise.defer(); |
michael@0 | 155 | |
michael@0 | 156 | let toolId = node.getAttribute("id"); |
michael@0 | 157 | if (node.checked) { |
michael@0 | 158 | gDevTools.once("tool-unregistered", checkUnregistered.bind(null, toolId, deferred)); |
michael@0 | 159 | } else { |
michael@0 | 160 | gDevTools.once("tool-registered", checkRegistered.bind(null, toolId, deferred)); |
michael@0 | 161 | } |
michael@0 | 162 | node.scrollIntoView(); |
michael@0 | 163 | EventUtils.synthesizeMouseAtCenter(node, {}, panelWin); |
michael@0 | 164 | |
michael@0 | 165 | return deferred.promise; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | function checkUnregistered(toolId, deferred, event, data) { |
michael@0 | 169 | if (data.id == toolId) { |
michael@0 | 170 | ok(true, "Correct tool removed"); |
michael@0 | 171 | // checking tab on the toolbox |
michael@0 | 172 | ok(!doc.getElementById("toolbox-tab-" + toolId), "Tab removed for " + toolId); |
michael@0 | 173 | } else { |
michael@0 | 174 | ok(false, "Something went wrong, " + toolId + " was not unregistered"); |
michael@0 | 175 | } |
michael@0 | 176 | deferred.resolve(); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | function checkRegistered(toolId, deferred, event, data) { |
michael@0 | 180 | if (data == toolId) { |
michael@0 | 181 | ok(true, "Correct tool added back"); |
michael@0 | 182 | // checking tab on the toolbox |
michael@0 | 183 | let radio = doc.getElementById("toolbox-tab-" + toolId); |
michael@0 | 184 | ok(radio, "Tab added back for " + toolId); |
michael@0 | 185 | if (radio.previousSibling) { |
michael@0 | 186 | ok(+radio.getAttribute("ordinal") >= |
michael@0 | 187 | +radio.previousSibling.getAttribute("ordinal"), |
michael@0 | 188 | "Inserted tab's ordinal is greater than equal to its previous tab." + |
michael@0 | 189 | "Expected " + radio.getAttribute("ordinal") + " >= " + |
michael@0 | 190 | radio.previousSibling.getAttribute("ordinal")); |
michael@0 | 191 | } |
michael@0 | 192 | if (radio.nextSibling) { |
michael@0 | 193 | ok(+radio.getAttribute("ordinal") < |
michael@0 | 194 | +radio.nextSibling.getAttribute("ordinal"), |
michael@0 | 195 | "Inserted tab's ordinal is less than its next tab. Expected " + |
michael@0 | 196 | radio.getAttribute("ordinal") + " < " + |
michael@0 | 197 | radio.nextSibling.getAttribute("ordinal")); |
michael@0 | 198 | } |
michael@0 | 199 | } else { |
michael@0 | 200 | ok(false, "Something went wrong, " + toolId + " was not registered"); |
michael@0 | 201 | } |
michael@0 | 202 | deferred.resolve(); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | function cleanup() { |
michael@0 | 206 | toolbox.destroy().then(function() { |
michael@0 | 207 | gBrowser.removeCurrentTab(); |
michael@0 | 208 | for (let pref of modifiedPrefs) { |
michael@0 | 209 | Services.prefs.clearUserPref(pref); |
michael@0 | 210 | } |
michael@0 | 211 | toolbox = doc = panelWin = modifiedPrefs = null; |
michael@0 | 212 | finish(); |
michael@0 | 213 | }); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | function errorHandler(error) { |
michael@0 | 217 | ok(false, "Unexpected error: " + error); |
michael@0 | 218 | cleanup(); |
michael@0 | 219 | } |