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