1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_locationBarCommand.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const TEST_VALUE = "example.com"; 1.8 +const START_VALUE = "example.org"; 1.9 + 1.10 +let gFocusManager = Cc["@mozilla.org/focus-manager;1"]. 1.11 + getService(Ci.nsIFocusManager); 1.12 + 1.13 +function test() { 1.14 + waitForExplicitFinish(); 1.15 + 1.16 + registerCleanupFunction(function () { 1.17 + Services.prefs.clearUserPref("browser.altClickSave"); 1.18 + }); 1.19 + Services.prefs.setBoolPref("browser.altClickSave", true); 1.20 + 1.21 + runAltLeftClickTest(); 1.22 +} 1.23 + 1.24 +// Monkey patch saveURL to avoid dealing with file save code paths 1.25 +var oldSaveURL = saveURL; 1.26 +saveURL = function() { 1.27 + ok(true, "SaveURL was called"); 1.28 + is(gURLBar.value, "", "Urlbar reverted to original value"); 1.29 + saveURL = oldSaveURL; 1.30 + runShiftLeftClickTest(); 1.31 +} 1.32 +function runAltLeftClickTest() { 1.33 + info("Running test: Alt left click"); 1.34 + triggerCommand(true, { altKey: true }); 1.35 +} 1.36 + 1.37 +function runShiftLeftClickTest() { 1.38 + let listener = new BrowserWindowListener(getBrowserURL(), function(aWindow) { 1.39 + Services.wm.removeListener(listener); 1.40 + addPageShowListener(aWindow.gBrowser.selectedBrowser, function() { 1.41 + executeSoon(function () { 1.42 + info("URL should be loaded in a new window"); 1.43 + is(gURLBar.value, "", "Urlbar reverted to original value"); 1.44 + is(gFocusManager.focusedElement, null, "There should be no focused element"); 1.45 + is(gFocusManager.focusedWindow, aWindow.gBrowser.contentWindow, "Content window should be focused"); 1.46 + is(aWindow.gURLBar.value, TEST_VALUE, "New URL is loaded in new window"); 1.47 + 1.48 + aWindow.close(); 1.49 + 1.50 + // Continue testing when the original window has focus again. 1.51 + whenWindowActivated(window, runNextTest); 1.52 + }); 1.53 + }, "http://example.com/"); 1.54 + }); 1.55 + Services.wm.addListener(listener); 1.56 + 1.57 + info("Running test: Shift left click"); 1.58 + triggerCommand(true, { shiftKey: true }); 1.59 +} 1.60 + 1.61 +function runNextTest() { 1.62 + let test = gTests.shift(); 1.63 + if (!test) { 1.64 + finish(); 1.65 + return; 1.66 + } 1.67 + 1.68 + info("Running test: " + test.desc); 1.69 + // Tab will be blank if test.startValue is null 1.70 + let tab = gBrowser.selectedTab = gBrowser.addTab(test.startValue); 1.71 + addPageShowListener(gBrowser.selectedBrowser, function() { 1.72 + triggerCommand(test.click, test.event); 1.73 + test.check(tab); 1.74 + 1.75 + // Clean up 1.76 + while (gBrowser.tabs.length > 1) 1.77 + gBrowser.removeTab(gBrowser.selectedTab) 1.78 + runNextTest(); 1.79 + }); 1.80 +} 1.81 + 1.82 +let gTests = [ 1.83 + { desc: "Right click on go button", 1.84 + click: true, 1.85 + event: { button: 2 }, 1.86 + check: function(aTab) { 1.87 + // Right click should do nothing (context menu will be shown) 1.88 + is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); 1.89 + } 1.90 + }, 1.91 + 1.92 + { desc: "Left click on go button", 1.93 + click: true, 1.94 + event: {}, 1.95 + check: checkCurrent 1.96 + }, 1.97 + 1.98 + { desc: "Ctrl/Cmd left click on go button", 1.99 + click: true, 1.100 + event: { accelKey: true }, 1.101 + check: checkNewTab 1.102 + }, 1.103 + 1.104 + { desc: "Shift+Ctrl/Cmd left click on go button", 1.105 + click: true, 1.106 + event: { accelKey: true, shiftKey: true }, 1.107 + check: function(aTab) { 1.108 + info("URL should be loaded in a new background tab"); 1.109 + is(gURLBar.value, "", "Urlbar reverted to original value"); 1.110 + ok(!gURLBar.focused, "Urlbar is no longer focused after urlbar command"); 1.111 + is(gBrowser.selectedTab, aTab, "Focus did not change to the new tab"); 1.112 + 1.113 + // Select the new background tab 1.114 + gBrowser.selectedTab = gBrowser.selectedTab.nextSibling; 1.115 + is(gURLBar.value, TEST_VALUE, "New URL is loaded in new tab"); 1.116 + } 1.117 + }, 1.118 + 1.119 + { desc: "Simple return keypress", 1.120 + event: {}, 1.121 + check: checkCurrent 1.122 + }, 1.123 + 1.124 + { desc: "Alt+Return keypress in a blank tab", 1.125 + event: { altKey: true }, 1.126 + check: checkCurrent 1.127 + }, 1.128 + 1.129 + { desc: "Alt+Return keypress in a dirty tab", 1.130 + event: { altKey: true }, 1.131 + check: checkNewTab, 1.132 + startValue: START_VALUE 1.133 + }, 1.134 + 1.135 + { desc: "Ctrl/Cmd+Return keypress", 1.136 + event: { accelKey: true }, 1.137 + check: checkCurrent 1.138 + } 1.139 +] 1.140 + 1.141 +let gGoButton = document.getElementById("urlbar-go-button"); 1.142 +function triggerCommand(aClick, aEvent) { 1.143 + gURLBar.value = TEST_VALUE; 1.144 + gURLBar.focus(); 1.145 + 1.146 + if (aClick) { 1.147 + is(gURLBar.getAttribute("pageproxystate"), "invalid", 1.148 + "page proxy state must be invalid for go button to be visible"); 1.149 + EventUtils.synthesizeMouseAtCenter(gGoButton, aEvent); 1.150 + } 1.151 + else 1.152 + EventUtils.synthesizeKey("VK_RETURN", aEvent); 1.153 +} 1.154 + 1.155 +/* Checks that the URL was loaded in the current tab */ 1.156 +function checkCurrent(aTab) { 1.157 + info("URL should be loaded in the current tab"); 1.158 + is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); 1.159 + is(gFocusManager.focusedElement, null, "There should be no focused element"); 1.160 + is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused"); 1.161 + is(gBrowser.selectedTab, aTab, "New URL was loaded in the current tab"); 1.162 +} 1.163 + 1.164 +/* Checks that the URL was loaded in a new focused tab */ 1.165 +function checkNewTab(aTab) { 1.166 + info("URL should be loaded in a new focused tab"); 1.167 + is(gURLBar.value, TEST_VALUE, "Urlbar still has the value we entered"); 1.168 + is(gFocusManager.focusedElement, null, "There should be no focused element"); 1.169 + is(gFocusManager.focusedWindow, gBrowser.contentWindow, "Content window should be focused"); 1.170 + isnot(gBrowser.selectedTab, aTab, "New URL was loaded in a new tab"); 1.171 +} 1.172 + 1.173 +function addPageShowListener(browser, cb, expectedURL) { 1.174 + browser.addEventListener("pageshow", function pageShowListener() { 1.175 + info("pageshow: " + browser.currentURI.spec); 1.176 + if (expectedURL && browser.currentURI.spec != expectedURL) 1.177 + return; // ignore pageshows for non-expected URLs 1.178 + browser.removeEventListener("pageshow", pageShowListener, false); 1.179 + cb(); 1.180 + }); 1.181 +} 1.182 + 1.183 +function whenWindowActivated(win, cb) { 1.184 + if (Services.focus.activeWindow == win) { 1.185 + executeSoon(cb); 1.186 + return; 1.187 + } 1.188 + 1.189 + win.addEventListener("activate", function onActivate() { 1.190 + win.removeEventListener("activate", onActivate); 1.191 + executeSoon(cb); 1.192 + }); 1.193 +} 1.194 + 1.195 +function BrowserWindowListener(aURL, aCallback) { 1.196 + this.callback = aCallback; 1.197 + this.url = aURL; 1.198 +} 1.199 +BrowserWindowListener.prototype = { 1.200 + onOpenWindow: function(aXULWindow) { 1.201 + let cb = () => this.callback(domwindow); 1.202 + let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) 1.203 + .getInterface(Ci.nsIDOMWindow); 1.204 + 1.205 + let numWait = 2; 1.206 + function maybeRunCallback() { 1.207 + if (--numWait == 0) 1.208 + cb(); 1.209 + } 1.210 + 1.211 + whenWindowActivated(domwindow, maybeRunCallback); 1.212 + whenDelayedStartupFinished(domwindow, maybeRunCallback); 1.213 + }, 1.214 + onCloseWindow: function(aXULWindow) {}, 1.215 + onWindowTitleChange: function(aXULWindow, aNewTitle) {} 1.216 +}