|
1 <?xml version="1.0"?> |
|
2 <!-- |
|
3 /* Any copyright is dedicated to the Public Domain. |
|
4 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
5 */ |
|
6 |
|
7 /** |
|
8 * Bug 544356 - This tests that the Download Manager is closed |
|
9 * when the last browser window is closed, if there are no active |
|
10 * downloads. |
|
11 */ |
|
12 --> |
|
13 |
|
14 <window title="Download Manager Test" |
|
15 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
16 onload="test();"> |
|
17 |
|
18 <script type="application/javascript" |
|
19 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
|
20 <script type="application/javascript" |
|
21 src="utils.js"/> |
|
22 |
|
23 <script type="application/javascript"> |
|
24 <![CDATA[ |
|
25 |
|
26 const DLMGR_UI_DONE = "download-manager-ui-done"; |
|
27 |
|
28 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
29 |
|
30 let gGen, dm, dmui, chromeWindow, testCleanup; |
|
31 |
|
32 function test() |
|
33 { |
|
34 if (testSetup()) { |
|
35 SimpleTest.waitForExplicitFinish(); |
|
36 |
|
37 //Start test |
|
38 gGen = doTest(); |
|
39 gGen.next(); |
|
40 } |
|
41 } |
|
42 |
|
43 function doTest() |
|
44 { |
|
45 let browserWindow1 = openBrowserWindow(continueTest); |
|
46 yield; |
|
47 let browserWindow2 = openBrowserWindow(continueTest); |
|
48 yield; |
|
49 openDownloadManager(continueTest); |
|
50 yield; |
|
51 let DMWindow = Services.wm.getMostRecentWindow("Download:Manager"); |
|
52 |
|
53 addCloseListener([browserWindow1, browserWindow2]); |
|
54 browserWindow1.BrowserTryToCloseWindow(); |
|
55 |
|
56 yield; |
|
57 |
|
58 // Wait for a moment and make sure the Download Manager didn't close, |
|
59 // because there's still one browser window |
|
60 setTimeout(continueTest, 500); yield; |
|
61 ok(dmui.visible, "Download Manager wasn't closed"); |
|
62 |
|
63 let download = addDownload(); |
|
64 // The download listener will pause the download and continue the test |
|
65 yield; |
|
66 |
|
67 browserWindow2.BrowserTryToCloseWindow(); |
|
68 yield; |
|
69 |
|
70 // Wait for a moment and make sure the Download Manager didn't close, |
|
71 // because there's one active download |
|
72 setTimeout(continueTest, 500); yield; |
|
73 ok(dmui.visible, "Download Manager wasn't closed"); |
|
74 |
|
75 dm.cancelDownload(download.id); |
|
76 dm.removeDownload(download.id); |
|
77 |
|
78 let browserWindow3 = openBrowserWindow(continueTest); |
|
79 yield; |
|
80 |
|
81 // Prepare to close last window |
|
82 ok(dmui.visible, "Download Manager wasn't closed"); |
|
83 |
|
84 let isMac = /Mac/.test(navigator.platform); |
|
85 addCloseListener(isMac ? [browserWindow3] : [browserWindow3, DMWindow]); |
|
86 browserWindow3.BrowserTryToCloseWindow(); |
|
87 if (isMac) { |
|
88 // Manually continue the test after a moment to make sure the Download Manager |
|
89 // didn't close |
|
90 setTimeout(continueTest, 500); |
|
91 } |
|
92 yield; yield; |
|
93 |
|
94 // Finally we reach the case where the Download Manager should have been closed |
|
95 if (isMac) { |
|
96 ok(dmui.visible, "Download Manager wasn't closed"); |
|
97 DMWindow.close(); |
|
98 } else { |
|
99 ok (!dmui.visible, "Download Manager was closed"); |
|
100 } |
|
101 |
|
102 testCleanup(); |
|
103 SimpleTest.finish(); |
|
104 yield; |
|
105 } |
|
106 |
|
107 function continueTest() |
|
108 { |
|
109 SimpleTest.executeSoon(function() { |
|
110 gGen.next(); |
|
111 }); |
|
112 } |
|
113 |
|
114 function testSetup() |
|
115 { |
|
116 chromeWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor). |
|
117 getInterface(Components.interfaces.nsIWebNavigation). |
|
118 QueryInterface(Components.interfaces.nsIDocShellTreeItem). |
|
119 rootTreeItem. |
|
120 QueryInterface(Components.interfaces.nsIInterfaceRequestor). |
|
121 getInterface(Components.interfaces.nsIDOMWindow); |
|
122 |
|
123 dmui = getDMUI(); |
|
124 if (!dmui) { |
|
125 todo(false, "skip test for toolkit download manager UI"); |
|
126 return false; |
|
127 } |
|
128 |
|
129 dm = Cc["@mozilla.org/download-manager;1"]. |
|
130 getService(Ci.nsIDownloadManager); |
|
131 |
|
132 // Change the window type so this one doesn't count as a browser window |
|
133 let oldWinType = chromeWindow.document.documentElement.getAttribute("windowtype"); |
|
134 chromeWindow.document.documentElement.setAttribute("windowtype", "navigator:testrunner"); |
|
135 |
|
136 downloadListener = { |
|
137 |
|
138 onDownloadStateChange: function(aState, aDownload) |
|
139 { |
|
140 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) { |
|
141 continueTest(); |
|
142 } else if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) { |
|
143 dm.pauseDownload(aDownload.id); |
|
144 } |
|
145 }, |
|
146 |
|
147 onStateChange: function() { }, |
|
148 onProgressChange: function() { }, |
|
149 onSecurityChange: function() { } |
|
150 }; |
|
151 |
|
152 dm.addListener(downloadListener); |
|
153 |
|
154 testCleanup = function() { |
|
155 dm.removeListener(downloadListener); |
|
156 chromeWindow.document.documentElement.setAttribute("windowtype", oldWinType); |
|
157 }; |
|
158 |
|
159 return true; |
|
160 } |
|
161 |
|
162 function openBrowserWindow(callback) |
|
163 { |
|
164 let browserWindow = openDialog(chromeWindow.location, "_blank", |
|
165 "chrome,all,dialog=no", "about:blank"); |
|
166 |
|
167 let helperFunc = function() { |
|
168 callback(); |
|
169 browserWindow.removeEventListener("load", helperFunc, false); |
|
170 } |
|
171 |
|
172 browserWindow.addEventListener("load", helperFunc, false); |
|
173 |
|
174 return browserWindow; |
|
175 } |
|
176 |
|
177 function openDownloadManager(callback) |
|
178 { |
|
179 let testObs = { |
|
180 observe: function(aSubject, aTopic, aData) { |
|
181 if (aTopic != DLMGR_UI_DONE) { |
|
182 return; |
|
183 } |
|
184 |
|
185 callback(); |
|
186 Services.obs.removeObserver(testObs, DLMGR_UI_DONE); |
|
187 } |
|
188 }; |
|
189 |
|
190 Services.obs.addObserver(testObs, DLMGR_UI_DONE, false); |
|
191 |
|
192 dmui.show(); |
|
193 } |
|
194 |
|
195 function addCloseListener(aWins) |
|
196 { |
|
197 aWins.forEach(function(win) { |
|
198 let listener = function() { |
|
199 win.removeEventListener("unload", listener, false); |
|
200 continueTest(); |
|
201 }; |
|
202 win.addEventListener("unload", listener, false); |
|
203 }); |
|
204 } |
|
205 |
|
206 ]]> |
|
207 </script> |
|
208 |
|
209 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
210 <p id="display"></p> |
|
211 <div id="content" style="display:none;"></div> |
|
212 <pre id="test"></pre> |
|
213 </body> |
|
214 </window> |