|
1 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
2 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
3 |
|
4 let Cc = Components.classes; |
|
5 let Ci = Components.interfaces; |
|
6 |
|
7 const PREF_DISABLE_OPEN_NEW_WINDOW = "browser.link.open_newwindow.disabled_in_fullscreen"; |
|
8 const isOSX = (Services.appinfo.OS === "Darwin"); |
|
9 |
|
10 const TEST_FILE = "file_fullscreen-window-open.html"; |
|
11 const gHttpTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/", |
|
12 "http://127.0.0.1:8888/"); |
|
13 |
|
14 function test () { |
|
15 waitForExplicitFinish(); |
|
16 |
|
17 Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true); |
|
18 |
|
19 let newTab = gBrowser.addTab(); |
|
20 gBrowser.selectedTab = newTab; |
|
21 |
|
22 let gTestBrowser = gBrowser.selectedBrowser; |
|
23 gTestBrowser.addEventListener("load", function onLoad(){ |
|
24 gTestBrowser.removeEventListener("load", onLoad, true, true); |
|
25 |
|
26 // Enter browser fullscreen mode. |
|
27 BrowserFullScreen(); |
|
28 |
|
29 runNextTest(); |
|
30 }, true, true); |
|
31 gTestBrowser.contentWindow.location.href = gHttpTestRoot + TEST_FILE; |
|
32 } |
|
33 |
|
34 registerCleanupFunction(function(){ |
|
35 // Exit browser fullscreen mode. |
|
36 BrowserFullScreen(); |
|
37 |
|
38 gBrowser.removeCurrentTab(); |
|
39 |
|
40 Services.prefs.clearUserPref(PREF_DISABLE_OPEN_NEW_WINDOW); |
|
41 }); |
|
42 |
|
43 let gTests = [ |
|
44 test_open, |
|
45 test_open_with_size, |
|
46 test_open_with_pos, |
|
47 test_open_with_outerSize, |
|
48 test_open_with_innerSize, |
|
49 test_open_with_dialog, |
|
50 test_open_when_open_new_window_by_pref, |
|
51 test_open_with_pref_to_disable_in_fullscreen, |
|
52 test_open_from_chrome, |
|
53 ]; |
|
54 |
|
55 function runNextTest () { |
|
56 let test = gTests.shift(); |
|
57 if (test) { |
|
58 executeSoon(test); |
|
59 } |
|
60 else { |
|
61 finish(); |
|
62 } |
|
63 } |
|
64 |
|
65 |
|
66 // Test for window.open() with no feature. |
|
67 function test_open() { |
|
68 waitForTabOpen({ |
|
69 message: { |
|
70 title: "test_open", |
|
71 param: "", |
|
72 }, |
|
73 finalizeFn: function () {}, |
|
74 }); |
|
75 } |
|
76 |
|
77 // Test for window.open() with width/height. |
|
78 function test_open_with_size() { |
|
79 waitForTabOpen({ |
|
80 message: { |
|
81 title: "test_open_with_size", |
|
82 param: "width=400,height=400", |
|
83 }, |
|
84 finalizeFn: function () {}, |
|
85 }); |
|
86 } |
|
87 |
|
88 // Test for window.open() with top/left. |
|
89 function test_open_with_pos() { |
|
90 waitForTabOpen({ |
|
91 message: { |
|
92 title: "test_open_with_pos", |
|
93 param: "top=200,left=200", |
|
94 }, |
|
95 finalizeFn: function () {}, |
|
96 }); |
|
97 } |
|
98 |
|
99 // Test for window.open() with outerWidth/Height. |
|
100 function test_open_with_outerSize() { |
|
101 let [outerWidth, outerHeight] = [window.outerWidth, window.outerHeight]; |
|
102 waitForTabOpen({ |
|
103 message: { |
|
104 title: "test_open_with_outerSize", |
|
105 param: "outerWidth=200,outerHeight=200", |
|
106 }, |
|
107 successFn: function () { |
|
108 is(window.outerWidth, outerWidth, "Don't change window.outerWidth."); |
|
109 is(window.outerHeight, outerHeight, "Don't change window.outerHeight."); |
|
110 }, |
|
111 finalizeFn: function () {}, |
|
112 }); |
|
113 } |
|
114 |
|
115 // Test for window.open() with innerWidth/Height. |
|
116 function test_open_with_innerSize() { |
|
117 let [innerWidth, innerHeight] = [window.innerWidth, window.innerHeight]; |
|
118 waitForTabOpen({ |
|
119 message: { |
|
120 title: "test_open_with_innerSize", |
|
121 param: "innerWidth=200,innerHeight=200", |
|
122 }, |
|
123 successFn: function () { |
|
124 is(window.innerWidth, innerWidth, "Don't change window.innerWidth."); |
|
125 is(window.innerHeight, innerHeight, "Don't change window.innerHeight."); |
|
126 }, |
|
127 finalizeFn: function () {}, |
|
128 }); |
|
129 } |
|
130 |
|
131 // Test for window.open() with dialog. |
|
132 function test_open_with_dialog() { |
|
133 waitForTabOpen({ |
|
134 message: { |
|
135 title: "test_open_with_dialog", |
|
136 param: "dialog=yes", |
|
137 }, |
|
138 finalizeFn: function () {}, |
|
139 }); |
|
140 } |
|
141 |
|
142 // Test for window.open() |
|
143 // when "browser.link.open_newwindow" is nsIBrowserDOMWindow.OPEN_NEWWINDOW |
|
144 function test_open_when_open_new_window_by_pref() { |
|
145 const PREF_NAME = "browser.link.open_newwindow"; |
|
146 Services.prefs.setIntPref(PREF_NAME, Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW); |
|
147 is(Services.prefs.getIntPref(PREF_NAME), Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW, |
|
148 PREF_NAME + " is nsIBrowserDOMWindow.OPEN_NEWWINDOW at this time"); |
|
149 |
|
150 waitForTabOpen({ |
|
151 message: { |
|
152 title: "test_open_when_open_new_window_by_pref", |
|
153 param: "width=400,height=400", |
|
154 }, |
|
155 finalizeFn: function () { |
|
156 Services.prefs.clearUserPref(PREF_NAME); |
|
157 }, |
|
158 }); |
|
159 } |
|
160 |
|
161 // Test for the pref, "browser.link.open_newwindow.disabled_in_fullscreen" |
|
162 function test_open_with_pref_to_disable_in_fullscreen() { |
|
163 Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, false); |
|
164 |
|
165 waitForWindowOpen({ |
|
166 message: { |
|
167 title: "test_open_with_pref_disabled_in_fullscreen", |
|
168 param: "width=400,height=400", |
|
169 }, |
|
170 finalizeFn: function () { |
|
171 Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true); |
|
172 }, |
|
173 }); |
|
174 } |
|
175 |
|
176 |
|
177 // Test for window.open() called from chrome context. |
|
178 function test_open_from_chrome() { |
|
179 waitForWindowOpenFromChrome({ |
|
180 message: { |
|
181 title: "test_open_from_chrome", |
|
182 param: "", |
|
183 }, |
|
184 finalizeFn: function () {}, |
|
185 timeout: 10000, |
|
186 }); |
|
187 } |
|
188 |
|
189 function waitForTabOpen(aOptions) { |
|
190 let start = Date.now(); |
|
191 let timeout = aOptions.timeout || 5000; |
|
192 let message = aOptions.message; |
|
193 |
|
194 if (!message.title) { |
|
195 ok(false, "Can't get message.title."); |
|
196 aOptions.finalizeFn(); |
|
197 runNextTest(); |
|
198 return; |
|
199 } |
|
200 |
|
201 info("Running test: " + message.title); |
|
202 |
|
203 let onTabOpen = function onTabOpen(aEvent) { |
|
204 gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true); |
|
205 |
|
206 let tab = aEvent.target; |
|
207 tab.linkedBrowser.addEventListener("load", function onLoad(ev){ |
|
208 let browser = ev.currentTarget; |
|
209 browser.removeEventListener("load", onLoad, true, true); |
|
210 clearTimeout(onTimeout); |
|
211 |
|
212 is(browser.contentWindow.document.title, message.title, |
|
213 "Opened Tab is expected: " + message.title); |
|
214 |
|
215 if (aOptions.successFn) { |
|
216 aOptions.successFn(); |
|
217 } |
|
218 |
|
219 gBrowser.removeTab(tab); |
|
220 finalize(); |
|
221 }, true, true); |
|
222 } |
|
223 gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, true); |
|
224 |
|
225 let finalize = function () { |
|
226 aOptions.finalizeFn(); |
|
227 info("Finished: " + message.title); |
|
228 runNextTest(); |
|
229 }; |
|
230 |
|
231 let onTimeout = setTimeout(function(){ |
|
232 gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true); |
|
233 |
|
234 ok(false, "Timeout: '"+message.title + "'."); |
|
235 finalize(); |
|
236 }, timeout); |
|
237 |
|
238 |
|
239 const URI = "data:text/html;charset=utf-8,<!DOCTYPE html><html><head><title>"+ |
|
240 message.title + |
|
241 "<%2Ftitle><%2Fhead><body><%2Fbody><%2Fhtml>"; |
|
242 |
|
243 executeWindowOpenInContent({ |
|
244 uri: URI, |
|
245 title: message.title, |
|
246 option: message.param, |
|
247 }); |
|
248 } |
|
249 |
|
250 |
|
251 function waitForWindowOpen(aOptions) { |
|
252 let start = Date.now(); |
|
253 let timeout = aOptions.timeout || 10000; |
|
254 let message = aOptions.message; |
|
255 let url = aOptions.url || getBrowserURL(); |
|
256 |
|
257 if (!message.title) { |
|
258 ok(false, "Can't get message.title"); |
|
259 aOptions.finalizeFn(); |
|
260 runNextTest(); |
|
261 return; |
|
262 } |
|
263 |
|
264 info("Running test: " + message.title); |
|
265 |
|
266 let onFinalize = function () { |
|
267 aOptions.finalizeFn(); |
|
268 |
|
269 info("Finished: " + message.title); |
|
270 runNextTest(); |
|
271 }; |
|
272 |
|
273 let onTimeout = setTimeout(function(){ |
|
274 Services.wm.removeListener(listener); |
|
275 ok(false, "Fail: '"+message.title + "'."); |
|
276 |
|
277 onFinalize(); |
|
278 }, timeout); |
|
279 |
|
280 let listener = new WindowListener(message.title, url, { |
|
281 onSuccess: aOptions.successFn, |
|
282 onTimeout: onTimeout, |
|
283 onFinalize: onFinalize, |
|
284 }); |
|
285 Services.wm.addListener(listener); |
|
286 |
|
287 const URI = aOptions.url || "about:blank"; |
|
288 |
|
289 executeWindowOpenInContent({ |
|
290 uri: URI, |
|
291 title: message.title, |
|
292 option: message.param, |
|
293 }); |
|
294 } |
|
295 |
|
296 function executeWindowOpenInContent(aParam) { |
|
297 var testWindow = gBrowser.selectedBrowser.contentWindow; |
|
298 var testElm = testWindow.document.getElementById("test"); |
|
299 |
|
300 testElm.setAttribute("data-test-param", JSON.stringify(aParam)); |
|
301 EventUtils.synthesizeMouseAtCenter(testElm, {}, testWindow); |
|
302 } |
|
303 |
|
304 function waitForWindowOpenFromChrome(aOptions) { |
|
305 let start = Date.now(); |
|
306 let timeout = aOptions.timeout || 10000; |
|
307 let message = aOptions.message; |
|
308 let url = aOptions.url || getBrowserURL(); |
|
309 |
|
310 if (!message.title) { |
|
311 ok(false, "Can't get message.title"); |
|
312 aOptions.finalizeFn(); |
|
313 runNextTest(); |
|
314 return; |
|
315 } |
|
316 |
|
317 info("Running test: " + message.title); |
|
318 |
|
319 let onFinalize = function () { |
|
320 aOptions.finalizeFn(); |
|
321 |
|
322 info("Finished: " + message.title); |
|
323 runNextTest(); |
|
324 }; |
|
325 |
|
326 let onTimeout = setTimeout(function(){ |
|
327 Services.wm.removeListener(listener); |
|
328 ok(false, "Fail: '"+message.title + "'."); |
|
329 |
|
330 testWindow.close(); |
|
331 onFinalize(); |
|
332 }, timeout); |
|
333 |
|
334 let listener = new WindowListener(message.title, url, { |
|
335 onSuccess: aOptions.successFn, |
|
336 onTimeout: onTimeout, |
|
337 onFinalize: onFinalize, |
|
338 }); |
|
339 Services.wm.addListener(listener); |
|
340 |
|
341 |
|
342 const URI = aOptions.url || "about:blank"; |
|
343 |
|
344 let testWindow = window.open(URI, message.title, message.option); |
|
345 } |
|
346 |
|
347 function WindowListener(aTitle, aUrl, aCallBackObj) { |
|
348 this.test_title = aTitle; |
|
349 this.test_url = aUrl; |
|
350 this.callback_onSuccess = aCallBackObj.onSuccess; |
|
351 this.callBack_onTimeout = aCallBackObj.onTimeout; |
|
352 this.callBack_onFinalize = aCallBackObj.onFinalize; |
|
353 } |
|
354 WindowListener.prototype = { |
|
355 |
|
356 test_title: null, |
|
357 test_url: null, |
|
358 callback_onSuccess: null, |
|
359 callBack_onTimeout: null, |
|
360 callBack_onFinalize: null, |
|
361 |
|
362 onOpenWindow: function(aXULWindow) { |
|
363 Services.wm.removeListener(this); |
|
364 |
|
365 let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
|
366 .getInterface(Ci.nsIDOMWindow); |
|
367 domwindow.addEventListener("load", function onLoad(aEvent) { |
|
368 is(domwindow.document.location.href, this.test_url, |
|
369 "Opened Window is expected: "+ this.test_title); |
|
370 if (this.callback_onSuccess) { |
|
371 this.callback_onSuccess(); |
|
372 } |
|
373 |
|
374 domwindow.removeEventListener("load", onLoad, true); |
|
375 clearTimeout(this.callBack_onTimeout); |
|
376 |
|
377 // wait for trasition to fullscreen on OSX Lion later |
|
378 if (isOSX) { |
|
379 setTimeout(function(){ |
|
380 domwindow.close(); |
|
381 executeSoon(this.callBack_onFinalize); |
|
382 }.bind(this), 3000); |
|
383 } |
|
384 else { |
|
385 domwindow.close(); |
|
386 executeSoon(this.callBack_onFinalize); |
|
387 } |
|
388 }.bind(this), true); |
|
389 }, |
|
390 onCloseWindow: function(aXULWindow) {}, |
|
391 onWindowTitleChange: function(aXULWindow, aNewTitle) {}, |
|
392 QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowMediatorListener, |
|
393 Ci.nsISupports]), |
|
394 }; |