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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Test for bug 549340. |
michael@0 | 7 | * Test for browser.js::contentAreaClick() util. |
michael@0 | 8 | * |
michael@0 | 9 | * The test opens a new browser window, then replaces browser.js methods invoked |
michael@0 | 10 | * by contentAreaClick with a mock function that tracks which methods have been |
michael@0 | 11 | * called. |
michael@0 | 12 | * Each sub-test synthesizes a mouse click event on links injected in content, |
michael@0 | 13 | * the event is collected by a click handler that ensures that contentAreaClick |
michael@0 | 14 | * correctly prevent default events, and follows the correct code path. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | let gTests = [ |
michael@0 | 18 | |
michael@0 | 19 | { |
michael@0 | 20 | desc: "Simple left click", |
michael@0 | 21 | setup: function() {}, |
michael@0 | 22 | clean: function() {}, |
michael@0 | 23 | event: {}, |
michael@0 | 24 | targets: [ "commonlink", "mathxlink", "svgxlink", "maplink" ], |
michael@0 | 25 | expectedInvokedMethods: [], |
michael@0 | 26 | preventDefault: false, |
michael@0 | 27 | }, |
michael@0 | 28 | |
michael@0 | 29 | { |
michael@0 | 30 | desc: "Ctrl/Cmd left click", |
michael@0 | 31 | setup: function() {}, |
michael@0 | 32 | clean: function() {}, |
michael@0 | 33 | event: { ctrlKey: true, |
michael@0 | 34 | metaKey: true }, |
michael@0 | 35 | targets: [ "commonlink", "mathxlink", "svgxlink", "maplink" ], |
michael@0 | 36 | expectedInvokedMethods: [ "urlSecurityCheck", "openLinkIn" ], |
michael@0 | 37 | preventDefault: true, |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | // The next test was once handling feedService.forcePreview(). Now it should |
michael@0 | 41 | // just be like Alt click. |
michael@0 | 42 | { |
michael@0 | 43 | desc: "Shift+Alt left click", |
michael@0 | 44 | setup: function() { |
michael@0 | 45 | gPrefService.setBoolPref("browser.altClickSave", true); |
michael@0 | 46 | }, |
michael@0 | 47 | clean: function() { |
michael@0 | 48 | gPrefService.clearUserPref("browser.altClickSave"); |
michael@0 | 49 | }, |
michael@0 | 50 | event: { shiftKey: true, |
michael@0 | 51 | altKey: true }, |
michael@0 | 52 | targets: [ "commonlink", "maplink" ], |
michael@0 | 53 | expectedInvokedMethods: [ "gatherTextUnder", "saveURL" ], |
michael@0 | 54 | preventDefault: true, |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | { |
michael@0 | 58 | desc: "Shift+Alt left click on XLinks", |
michael@0 | 59 | setup: function() { |
michael@0 | 60 | gPrefService.setBoolPref("browser.altClickSave", true); |
michael@0 | 61 | }, |
michael@0 | 62 | clean: function() { |
michael@0 | 63 | gPrefService.clearUserPref("browser.altClickSave"); |
michael@0 | 64 | }, |
michael@0 | 65 | event: { shiftKey: true, |
michael@0 | 66 | altKey: true }, |
michael@0 | 67 | targets: [ "mathxlink", "svgxlink"], |
michael@0 | 68 | expectedInvokedMethods: [ "saveURL" ], |
michael@0 | 69 | preventDefault: true, |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | { |
michael@0 | 73 | desc: "Shift click", |
michael@0 | 74 | setup: function() {}, |
michael@0 | 75 | clean: function() {}, |
michael@0 | 76 | event: { shiftKey: true }, |
michael@0 | 77 | targets: [ "commonlink", "mathxlink", "svgxlink", "maplink" ], |
michael@0 | 78 | expectedInvokedMethods: [ "urlSecurityCheck", "openLinkIn" ], |
michael@0 | 79 | preventDefault: true, |
michael@0 | 80 | }, |
michael@0 | 81 | |
michael@0 | 82 | { |
michael@0 | 83 | desc: "Alt click", |
michael@0 | 84 | setup: function() { |
michael@0 | 85 | gPrefService.setBoolPref("browser.altClickSave", true); |
michael@0 | 86 | }, |
michael@0 | 87 | clean: function() { |
michael@0 | 88 | gPrefService.clearUserPref("browser.altClickSave"); |
michael@0 | 89 | }, |
michael@0 | 90 | event: { altKey: true }, |
michael@0 | 91 | targets: [ "commonlink", "maplink" ], |
michael@0 | 92 | expectedInvokedMethods: [ "gatherTextUnder", "saveURL" ], |
michael@0 | 93 | preventDefault: true, |
michael@0 | 94 | }, |
michael@0 | 95 | |
michael@0 | 96 | { |
michael@0 | 97 | desc: "Alt click on XLinks", |
michael@0 | 98 | setup: function() { |
michael@0 | 99 | gPrefService.setBoolPref("browser.altClickSave", true); |
michael@0 | 100 | }, |
michael@0 | 101 | clean: function() { |
michael@0 | 102 | gPrefService.clearUserPref("browser.altClickSave"); |
michael@0 | 103 | }, |
michael@0 | 104 | event: { altKey: true }, |
michael@0 | 105 | targets: [ "mathxlink", "svgxlink" ], |
michael@0 | 106 | expectedInvokedMethods: [ "saveURL" ], |
michael@0 | 107 | preventDefault: true, |
michael@0 | 108 | }, |
michael@0 | 109 | |
michael@0 | 110 | { |
michael@0 | 111 | desc: "Panel click", |
michael@0 | 112 | setup: function() {}, |
michael@0 | 113 | clean: function() {}, |
michael@0 | 114 | event: {}, |
michael@0 | 115 | targets: [ "panellink" ], |
michael@0 | 116 | expectedInvokedMethods: [ "urlSecurityCheck", "loadURI" ], |
michael@0 | 117 | preventDefault: true, |
michael@0 | 118 | }, |
michael@0 | 119 | |
michael@0 | 120 | { |
michael@0 | 121 | desc: "Simple middle click opentab", |
michael@0 | 122 | setup: function() {}, |
michael@0 | 123 | clean: function() {}, |
michael@0 | 124 | event: { button: 1 }, |
michael@0 | 125 | targets: [ "commonlink", "mathxlink", "svgxlink", "maplink" ], |
michael@0 | 126 | expectedInvokedMethods: [ "urlSecurityCheck", "openLinkIn" ], |
michael@0 | 127 | preventDefault: true, |
michael@0 | 128 | }, |
michael@0 | 129 | |
michael@0 | 130 | { |
michael@0 | 131 | desc: "Simple middle click openwin", |
michael@0 | 132 | setup: function() { |
michael@0 | 133 | gPrefService.setBoolPref("browser.tabs.opentabfor.middleclick", false); |
michael@0 | 134 | }, |
michael@0 | 135 | clean: function() { |
michael@0 | 136 | gPrefService.clearUserPref("browser.tabs.opentabfor.middleclick"); |
michael@0 | 137 | }, |
michael@0 | 138 | event: { button: 1 }, |
michael@0 | 139 | targets: [ "commonlink", "mathxlink", "svgxlink", "maplink" ], |
michael@0 | 140 | expectedInvokedMethods: [ "urlSecurityCheck", "openLinkIn" ], |
michael@0 | 141 | preventDefault: true, |
michael@0 | 142 | }, |
michael@0 | 143 | |
michael@0 | 144 | { |
michael@0 | 145 | desc: "Middle mouse paste", |
michael@0 | 146 | setup: function() { |
michael@0 | 147 | gPrefService.setBoolPref("middlemouse.contentLoadURL", true); |
michael@0 | 148 | gPrefService.setBoolPref("general.autoScroll", false); |
michael@0 | 149 | }, |
michael@0 | 150 | clean: function() { |
michael@0 | 151 | gPrefService.clearUserPref("middlemouse.contentLoadURL"); |
michael@0 | 152 | gPrefService.clearUserPref("general.autoScroll"); |
michael@0 | 153 | }, |
michael@0 | 154 | event: { button: 1 }, |
michael@0 | 155 | targets: [ "emptylink" ], |
michael@0 | 156 | expectedInvokedMethods: [ "middleMousePaste" ], |
michael@0 | 157 | preventDefault: true, |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | ]; |
michael@0 | 161 | |
michael@0 | 162 | // Array of method names that will be replaced in the new window. |
michael@0 | 163 | let gReplacedMethods = [ |
michael@0 | 164 | "middleMousePaste", |
michael@0 | 165 | "urlSecurityCheck", |
michael@0 | 166 | "loadURI", |
michael@0 | 167 | "gatherTextUnder", |
michael@0 | 168 | "saveURL", |
michael@0 | 169 | "openLinkIn", |
michael@0 | 170 | "getShortcutOrURIAndPostData", |
michael@0 | 171 | ]; |
michael@0 | 172 | |
michael@0 | 173 | // Reference to the new window. |
michael@0 | 174 | let gTestWin = null; |
michael@0 | 175 | |
michael@0 | 176 | // List of methods invoked by a specific call to contentAreaClick. |
michael@0 | 177 | let gInvokedMethods = []; |
michael@0 | 178 | |
michael@0 | 179 | // The test currently running. |
michael@0 | 180 | let gCurrentTest = null; |
michael@0 | 181 | |
michael@0 | 182 | function test() { |
michael@0 | 183 | waitForExplicitFinish(); |
michael@0 | 184 | |
michael@0 | 185 | gTestWin = openDialog(location, "", "chrome,all,dialog=no", "about:blank"); |
michael@0 | 186 | whenDelayedStartupFinished(gTestWin, function () { |
michael@0 | 187 | info("Browser window opened"); |
michael@0 | 188 | waitForFocus(function() { |
michael@0 | 189 | info("Browser window focused"); |
michael@0 | 190 | waitForFocus(function() { |
michael@0 | 191 | info("Setting up browser..."); |
michael@0 | 192 | setupTestBrowserWindow(); |
michael@0 | 193 | info("Running tests..."); |
michael@0 | 194 | executeSoon(runNextTest); |
michael@0 | 195 | }, gTestWin.content, true); |
michael@0 | 196 | }, gTestWin); |
michael@0 | 197 | }); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | // Click handler used to steal click events. |
michael@0 | 201 | let gClickHandler = { |
michael@0 | 202 | handleEvent: function (event) { |
michael@0 | 203 | let linkId = event.target.id || event.target.localName; |
michael@0 | 204 | is(event.type, "click", |
michael@0 | 205 | gCurrentTest.desc + ":Handler received a click event on " + linkId); |
michael@0 | 206 | |
michael@0 | 207 | let isPanelClick = linkId == "panellink"; |
michael@0 | 208 | gTestWin.contentAreaClick(event, isPanelClick); |
michael@0 | 209 | let prevent = event.defaultPrevented; |
michael@0 | 210 | is(prevent, gCurrentTest.preventDefault, |
michael@0 | 211 | gCurrentTest.desc + ": event.defaultPrevented is correct (" + prevent + ")") |
michael@0 | 212 | |
michael@0 | 213 | // Check that all required methods have been called. |
michael@0 | 214 | gCurrentTest.expectedInvokedMethods.forEach(function(aExpectedMethodName) { |
michael@0 | 215 | isnot(gInvokedMethods.indexOf(aExpectedMethodName), -1, |
michael@0 | 216 | gCurrentTest.desc + ":" + aExpectedMethodName + " was invoked"); |
michael@0 | 217 | }); |
michael@0 | 218 | |
michael@0 | 219 | if (gInvokedMethods.length != gCurrentTest.expectedInvokedMethods.length) { |
michael@0 | 220 | ok(false, "Wrong number of invoked methods"); |
michael@0 | 221 | gInvokedMethods.forEach(function (method) info(method + " was invoked")); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | event.preventDefault(); |
michael@0 | 225 | event.stopPropagation(); |
michael@0 | 226 | |
michael@0 | 227 | executeSoon(runNextTest); |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // Wraps around the methods' replacement mock function. |
michael@0 | 232 | function wrapperMethod(aInvokedMethods, aMethodName) { |
michael@0 | 233 | return function () { |
michael@0 | 234 | aInvokedMethods.push(aMethodName); |
michael@0 | 235 | // At least getShortcutOrURIAndPostData requires to return url |
michael@0 | 236 | return (aMethodName == "getShortcutOrURIAndPostData") ? arguments.url : arguments[0]; |
michael@0 | 237 | } |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | function setupTestBrowserWindow() { |
michael@0 | 241 | // Steal click events and don't propagate them. |
michael@0 | 242 | gTestWin.addEventListener("click", gClickHandler, true); |
michael@0 | 243 | |
michael@0 | 244 | // Replace methods. |
michael@0 | 245 | gReplacedMethods.forEach(function (aMethodName) { |
michael@0 | 246 | gTestWin["old_" + aMethodName] = gTestWin[aMethodName]; |
michael@0 | 247 | gTestWin[aMethodName] = wrapperMethod(gInvokedMethods, aMethodName); |
michael@0 | 248 | }); |
michael@0 | 249 | |
michael@0 | 250 | // Inject links in content. |
michael@0 | 251 | let doc = gTestWin.content.document; |
michael@0 | 252 | let mainDiv = doc.createElement("div"); |
michael@0 | 253 | mainDiv.innerHTML = |
michael@0 | 254 | '<p><a id="commonlink" href="http://mochi.test/moz/">Common link</a></p>' + |
michael@0 | 255 | '<p><a id="panellink" href="http://mochi.test/moz/">Panel link</a></p>' + |
michael@0 | 256 | '<p><a id="emptylink">Empty link</a></p>' + |
michael@0 | 257 | '<p><math id="mathxlink" xmlns="http://www.w3.org/1998/Math/MathML" xlink:type="simple" xlink:href="http://mochi.test/moz/"><mtext>MathML XLink</mtext></math></p>' + |
michael@0 | 258 | '<p><svg id="svgxlink" xmlns="http://www.w3.org/2000/svg" width="100px" height="50px" version="1.1"><a xlink:type="simple" xlink:href="http://mochi.test/moz/"><text transform="translate(10, 25)">SVG XLink</text></a></svg></p>' + |
michael@0 | 259 | '<p><map name="map" id="map"><area href="http://mochi.test/moz/" shape="rect" coords="0,0,128,128" /></map><img id="maplink" usemap="#map" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAABGdBTUEAALGPC%2FxhBQAAAOtJREFUeF7t0IEAAAAAgKD9qRcphAoDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGDAgAEDBgwYMGBgwIAAAT0N51AAAAAASUVORK5CYII%3D"/></p>' |
michael@0 | 260 | doc.body.appendChild(mainDiv); |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | function runNextTest() { |
michael@0 | 264 | if (!gCurrentTest) { |
michael@0 | 265 | gCurrentTest = gTests.shift(); |
michael@0 | 266 | gCurrentTest.setup(); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | if (gCurrentTest.targets.length == 0) { |
michael@0 | 270 | info(gCurrentTest.desc + ": cleaning up...") |
michael@0 | 271 | gCurrentTest.clean(); |
michael@0 | 272 | |
michael@0 | 273 | if (gTests.length > 0) { |
michael@0 | 274 | gCurrentTest = gTests.shift(); |
michael@0 | 275 | gCurrentTest.setup(); |
michael@0 | 276 | } |
michael@0 | 277 | else { |
michael@0 | 278 | finishTest(); |
michael@0 | 279 | return; |
michael@0 | 280 | } |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | // Move to next target. |
michael@0 | 284 | gInvokedMethods.length = 0; |
michael@0 | 285 | let target = gCurrentTest.targets.shift(); |
michael@0 | 286 | |
michael@0 | 287 | info(gCurrentTest.desc + ": testing " + target); |
michael@0 | 288 | |
michael@0 | 289 | // Fire click event. |
michael@0 | 290 | let targetElt = gTestWin.content.document.getElementById(target); |
michael@0 | 291 | ok(targetElt, gCurrentTest.desc + ": target is valid (" + targetElt.id + ")"); |
michael@0 | 292 | EventUtils.synthesizeMouseAtCenter(targetElt, gCurrentTest.event, gTestWin.content); |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | function finishTest() { |
michael@0 | 296 | info("Restoring browser..."); |
michael@0 | 297 | gTestWin.removeEventListener("click", gClickHandler, true); |
michael@0 | 298 | |
michael@0 | 299 | // Restore original methods. |
michael@0 | 300 | gReplacedMethods.forEach(function (aMethodName) { |
michael@0 | 301 | gTestWin[aMethodName] = gTestWin["old_" + aMethodName]; |
michael@0 | 302 | delete gTestWin["old_" + aMethodName]; |
michael@0 | 303 | }); |
michael@0 | 304 | |
michael@0 | 305 | gTestWin.close(); |
michael@0 | 306 | finish(); |
michael@0 | 307 | } |