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 | const TEST_VALUE = "example.com"; |
michael@0 | 5 | const START_VALUE = "example.org"; |
michael@0 | 6 | |
michael@0 | 7 | let gFocusManager = Cc["@mozilla.org/focus-manager;1"]. |
michael@0 | 8 | getService(Ci.nsIFocusManager); |
michael@0 | 9 | |
michael@0 | 10 | function test() { |
michael@0 | 11 | waitForExplicitFinish(); |
michael@0 | 12 | |
michael@0 | 13 | registerCleanupFunction(function () { |
michael@0 | 14 | Services.prefs.clearUserPref("browser.altClickSave"); |
michael@0 | 15 | }); |
michael@0 | 16 | Services.prefs.setBoolPref("browser.altClickSave", true); |
michael@0 | 17 | |
michael@0 | 18 | runAltLeftClickTest(); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | // Monkey patch saveURL to avoid dealing with file save code paths |
michael@0 | 22 | var oldSaveURL = saveURL; |
michael@0 | 23 | saveURL = function() { |
michael@0 | 24 | ok(true, "SaveURL was called"); |
michael@0 | 25 | is(gURLBar.value, "", "Urlbar reverted to original value"); |
michael@0 | 26 | saveURL = oldSaveURL; |
michael@0 | 27 | runShiftLeftClickTest(); |
michael@0 | 28 | } |
michael@0 | 29 | function runAltLeftClickTest() { |
michael@0 | 30 | info("Running test: Alt left click"); |
michael@0 | 31 | triggerCommand(true, { altKey: true }); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | function runShiftLeftClickTest() { |
michael@0 | 35 | let listener = new BrowserWindowListener(getBrowserURL(), function(aWindow) { |
michael@0 | 36 | Services.wm.removeListener(listener); |
michael@0 | 37 | addPageShowListener(aWindow.gBrowser.selectedBrowser, function() { |
michael@0 | 38 | executeSoon(function () { |
michael@0 | 39 | info("URL should be loaded in a new window"); |
michael@0 | 40 | is(gURLBar.value, "", "Urlbar reverted to original value"); |
michael@0 | 41 | is(gFocusManager.focusedElement, null, "There should be no focused element"); |
michael@0 | 42 | is(gFocusManager.focusedWindow, aWindow.gBrowser.contentWindow, "Content window should be focused"); |
michael@0 | 43 | is(aWindow.gURLBar.value, TEST_VALUE, "New URL is loaded in new window"); |
michael@0 | 44 | |
michael@0 | 45 | aWindow.close(); |
michael@0 | 46 | |
michael@0 | 47 | // Continue testing when the original window has focus again. |
michael@0 | 48 | whenWindowActivated(window, runNextTest); |
michael@0 | 49 | }); |
michael@0 | 50 | }, "http://example.com/"); |
michael@0 | 51 | }); |
michael@0 | 52 | Services.wm.addListener(listener); |
michael@0 | 53 | |
michael@0 | 54 | info("Running test: Shift left click"); |
michael@0 | 55 | triggerCommand(true, { shiftKey: true }); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | function runNextTest() { |
michael@0 | 59 | let test = gTests.shift(); |
michael@0 | 60 | if (!test) { |
michael@0 | 61 | finish(); |
michael@0 | 62 | return; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | info("Running test: " + test.desc); |
michael@0 | 66 | // Tab will be blank if test.startValue is null |
michael@0 | 67 | let tab = gBrowser.selectedTab = gBrowser.addTab(test.startValue); |
michael@0 | 68 | addPageShowListener(gBrowser.selectedBrowser, function() { |
michael@0 | 69 | triggerCommand(test.click, test.event); |
michael@0 | 70 | test.check(tab); |
michael@0 | 71 | |
michael@0 | 72 | // Clean up |
michael@0 | 73 | while (gBrowser.tabs.length > 1) |
michael@0 | 74 | gBrowser.removeTab(gBrowser.selectedTab) |
michael@0 | 75 | runNextTest(); |
michael@0 | 76 | }); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | let gTests = [ |
michael@0 | 80 | { desc: "Right click on go button", |
michael@0 | 81 | click: true, |
michael@0 | 82 | event: { button: 2 }, |
michael@0 | 83 | check: function(aTab) { |
michael@0 | 84 | // Right click should do nothing (context menu will be shown) |
michael@0 | 85 | is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); |
michael@0 | 86 | } |
michael@0 | 87 | }, |
michael@0 | 88 | |
michael@0 | 89 | { desc: "Left click on go button", |
michael@0 | 90 | click: true, |
michael@0 | 91 | event: {}, |
michael@0 | 92 | check: checkCurrent |
michael@0 | 93 | }, |
michael@0 | 94 | |
michael@0 | 95 | { desc: "Ctrl/Cmd left click on go button", |
michael@0 | 96 | click: true, |
michael@0 | 97 | event: { accelKey: true }, |
michael@0 | 98 | check: checkNewTab |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | { desc: "Shift+Ctrl/Cmd left click on go button", |
michael@0 | 102 | click: true, |
michael@0 | 103 | event: { accelKey: true, shiftKey: true }, |
michael@0 | 104 | check: function(aTab) { |
michael@0 | 105 | info("URL should be loaded in a new background tab"); |
michael@0 | 106 | is(gURLBar.value, "", "Urlbar reverted to original value"); |
michael@0 | 107 | ok(!gURLBar.focused, "Urlbar is no longer focused after urlbar command"); |
michael@0 | 108 | is(gBrowser.selectedTab, aTab, "Focus did not change to the new tab"); |
michael@0 | 109 | |
michael@0 | 110 | // Select the new background tab |
michael@0 | 111 | gBrowser.selectedTab = gBrowser.selectedTab.nextSibling; |
michael@0 | 112 | is(gURLBar.value, TEST_VALUE, "New URL is loaded in new tab"); |
michael@0 | 113 | } |
michael@0 | 114 | }, |
michael@0 | 115 | |
michael@0 | 116 | { desc: "Simple return keypress", |
michael@0 | 117 | event: {}, |
michael@0 | 118 | check: checkCurrent |
michael@0 | 119 | }, |
michael@0 | 120 | |
michael@0 | 121 | { desc: "Alt+Return keypress in a blank tab", |
michael@0 | 122 | event: { altKey: true }, |
michael@0 | 123 | check: checkCurrent |
michael@0 | 124 | }, |
michael@0 | 125 | |
michael@0 | 126 | { desc: "Alt+Return keypress in a dirty tab", |
michael@0 | 127 | event: { altKey: true }, |
michael@0 | 128 | check: checkNewTab, |
michael@0 | 129 | startValue: START_VALUE |
michael@0 | 130 | }, |
michael@0 | 131 | |
michael@0 | 132 | { desc: "Ctrl/Cmd+Return keypress", |
michael@0 | 133 | event: { accelKey: true }, |
michael@0 | 134 | check: checkCurrent |
michael@0 | 135 | } |
michael@0 | 136 | ] |
michael@0 | 137 | |
michael@0 | 138 | let gGoButton = document.getElementById("urlbar-go-button"); |
michael@0 | 139 | function triggerCommand(aClick, aEvent) { |
michael@0 | 140 | gURLBar.value = TEST_VALUE; |
michael@0 | 141 | gURLBar.focus(); |
michael@0 | 142 | |
michael@0 | 143 | if (aClick) { |
michael@0 | 144 | is(gURLBar.getAttribute("pageproxystate"), "invalid", |
michael@0 | 145 | "page proxy state must be invalid for go button to be visible"); |
michael@0 | 146 | EventUtils.synthesizeMouseAtCenter(gGoButton, aEvent); |
michael@0 | 147 | } |
michael@0 | 148 | else |
michael@0 | 149 | EventUtils.synthesizeKey("VK_RETURN", aEvent); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /* Checks that the URL was loaded in the current tab */ |
michael@0 | 153 | function checkCurrent(aTab) { |
michael@0 | 154 | info("URL should be loaded in the current tab"); |
michael@0 | 155 | is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); |
michael@0 | 156 | is(gFocusManager.focusedElement, null, "There should be no focused element"); |
michael@0 | 157 | is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused"); |
michael@0 | 158 | is(gBrowser.selectedTab, aTab, "New URL was loaded in the current tab"); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | /* Checks that the URL was loaded in a new focused tab */ |
michael@0 | 162 | function checkNewTab(aTab) { |
michael@0 | 163 | info("URL should be loaded in a new focused tab"); |
michael@0 | 164 | is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); |
michael@0 | 165 | is(gFocusManager.focusedElement, null, "There should be no focused element"); |
michael@0 | 166 | is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused"); |
michael@0 | 167 | isnot(gBrowser.selectedTab, aTab, "New URL was loaded in a new tab"); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | function addPageShowListener(browser, cb, expectedURL) { |
michael@0 | 171 | browser.addEventListener("pageshow", function pageShowListener() { |
michael@0 | 172 | info("pageshow: " + browser.currentURI.spec); |
michael@0 | 173 | if (expectedURL && browser.currentURI.spec != expectedURL) |
michael@0 | 174 | return; // ignore pageshows for non-expected URLs |
michael@0 | 175 | browser.removeEventListener("pageshow", pageShowListener, false); |
michael@0 | 176 | cb(); |
michael@0 | 177 | }); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | function whenWindowActivated(win, cb) { |
michael@0 | 181 | if (Services.focus.activeWindow == win) { |
michael@0 | 182 | executeSoon(cb); |
michael@0 | 183 | return; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | win.addEventListener("activate", function onActivate() { |
michael@0 | 187 | win.removeEventListener("activate", onActivate); |
michael@0 | 188 | executeSoon(cb); |
michael@0 | 189 | }); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | function BrowserWindowListener(aURL, aCallback) { |
michael@0 | 193 | this.callback = aCallback; |
michael@0 | 194 | this.url = aURL; |
michael@0 | 195 | } |
michael@0 | 196 | BrowserWindowListener.prototype = { |
michael@0 | 197 | onOpenWindow: function(aXULWindow) { |
michael@0 | 198 | let cb = () => this.callback(domwindow); |
michael@0 | 199 | let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 200 | .getInterface(Ci.nsIDOMWindow); |
michael@0 | 201 | |
michael@0 | 202 | let numWait = 2; |
michael@0 | 203 | function maybeRunCallback() { |
michael@0 | 204 | if (--numWait == 0) |
michael@0 | 205 | cb(); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | whenWindowActivated(domwindow, maybeRunCallback); |
michael@0 | 209 | whenDelayedStartupFinished(domwindow, maybeRunCallback); |
michael@0 | 210 | }, |
michael@0 | 211 | onCloseWindow: function(aXULWindow) {}, |
michael@0 | 212 | onWindowTitleChange: function(aXULWindow, aNewTitle) {} |
michael@0 | 213 | } |