|
1 <?xml version="1.0"?> |
|
2 <!-- This Source Code Form is subject to the terms of the Mozilla Public |
|
3 - License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> |
|
5 <!-- |
|
6 * This tests that the download manager UI closes upon download completion when |
|
7 * browser.download.manager.retention is set to 0. This test was added in bug |
|
8 * 413093. |
|
9 --> |
|
10 |
|
11 <window title="Download Manager Test" |
|
12 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
13 onload="test();"> |
|
14 |
|
15 <script type="application/javascript" |
|
16 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
|
17 <script type="application/javascript" |
|
18 src="utils.js"/> |
|
19 |
|
20 <script type="application/javascript"> |
|
21 <![CDATA[ |
|
22 |
|
23 function setPref(aDoTest) |
|
24 { |
|
25 let prefs = Cc["@mozilla.org/preferences-service;1"]. |
|
26 getService(Ci.nsIPrefBranch); |
|
27 |
|
28 // If we're testing, set retention to auto-remove and auto-close |
|
29 prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2); |
|
30 prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest); |
|
31 } |
|
32 |
|
33 function bug413093obs() |
|
34 { |
|
35 this.mDownload = null; |
|
36 this.wasPaused = false; |
|
37 } |
|
38 bug413093obs.prototype = { |
|
39 observe: function(aSubject, aTopic, aData) |
|
40 { |
|
41 if ("domwindowopened" == aTopic) { |
|
42 // If we opened before we were paused, we need to set up our proper state |
|
43 // We also should not try to resume (we weren't paused!) |
|
44 if (!this.wasPaused) { |
|
45 dump("domwindowopened callback - not pausing or resuming\n"); |
|
46 this.wasPaused = true; |
|
47 return; |
|
48 } |
|
49 |
|
50 dump("domwindowopened callback - resuming download\n"); |
|
51 |
|
52 // Resume the download now that UI is up and running |
|
53 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
54 getService(Ci.nsIDownloadManager); |
|
55 dm.resumeDownload(this.mDownload.id); |
|
56 } |
|
57 else if("domwindowclosed" == aTopic) { |
|
58 let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]. |
|
59 getService(Ci.nsIWindowWatcher); |
|
60 ww.unregisterNotification(this); |
|
61 |
|
62 // Let the UI finish doing whatever it needs to do |
|
63 SimpleTest.executeSoon(function() { |
|
64 setPref(false); |
|
65 |
|
66 let dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
|
67 getService(Ci.nsIDownloadManagerUI); |
|
68 ok(!dmui.visible, "Download Manager UI is not showing"); |
|
69 |
|
70 // reset the changed config values to default value |
|
71 let prefs = Cc["@mozilla.org/preferences-service;1"]. |
|
72 getService(Ci.nsIPrefService); |
|
73 prefs.clearUserPref("browser.download.manager.retention"); |
|
74 prefs.clearUserPref("browser.download.manager.closeWhenDone"); |
|
75 |
|
76 SimpleTest.finish(); |
|
77 }); |
|
78 } |
|
79 }, |
|
80 |
|
81 onDownloadStateChange: function(aOldState, aDownload) |
|
82 { |
|
83 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING && |
|
84 !this.wasPaused) { |
|
85 dump("onDownloadStateChange - pausing download\n"); |
|
86 this.wasPaused = true; |
|
87 |
|
88 // Pause the download until the UI shows up |
|
89 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
90 getService(Ci.nsIDownloadManager); |
|
91 dm.pauseDownload(aDownload.id); |
|
92 } |
|
93 |
|
94 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { |
|
95 aDownload.targetFile.remove(false); |
|
96 |
|
97 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
98 getService(Ci.nsIDownloadManager); |
|
99 dm.removeListener(this); |
|
100 |
|
101 // If this test is going to pass, we'll get a domwindowclosed notification |
|
102 } |
|
103 }, |
|
104 onStateChange: function(a, b, c, d, e) { }, |
|
105 onProgressChange: function(a, b, c, d, e, f, g) { }, |
|
106 onSecurityChange: function(a, b, c, d) { }, |
|
107 }; |
|
108 function test() |
|
109 { |
|
110 function addDownload() { |
|
111 function createURI(aObj) { |
|
112 let ios = Cc["@mozilla.org/network/io-service;1"]. |
|
113 getService(Ci.nsIIOService); |
|
114 return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) : |
|
115 ios.newURI(aObj, null, null); |
|
116 } |
|
117 |
|
118 const nsIWBP = Ci.nsIWebBrowserPersist; |
|
119 let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] |
|
120 .createInstance(Ci.nsIWebBrowserPersist); |
|
121 persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES | |
|
122 nsIWBP.PERSIST_FLAGS_BYPASS_CACHE | |
|
123 nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; |
|
124 |
|
125 let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. |
|
126 getService(Ci.nsIProperties); |
|
127 let destFile = dirSvc.get("ProfD", Ci.nsIFile); |
|
128 destFile.append("download.result"); |
|
129 if (destFile.exists()) |
|
130 destFile.remove(false); |
|
131 |
|
132 let src = createURI("http://example.com/httpd.js"); |
|
133 let target = createURI(destFile); |
|
134 let tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer); |
|
135 tr.init(src, target, "test download", null, Math.round(Date.now() * 1000), |
|
136 null, persist, false); |
|
137 persist.progressListener = tr; |
|
138 persist.saveURI(src, null, null, null, null, destFile); |
|
139 } |
|
140 |
|
141 // First, we clear out the database |
|
142 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
143 getService(Ci.nsIDownloadManager); |
|
144 dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads"); |
|
145 |
|
146 // See if the DM is already open, and if it is, close it! |
|
147 let wm = Cc["@mozilla.org/appshell/window-mediator;1"]. |
|
148 getService(Ci.nsIWindowMediator); |
|
149 let win = wm.getMostRecentWindow("Download:Manager"); |
|
150 if (win) |
|
151 win.close(); |
|
152 |
|
153 let obs = new bug413093obs(); |
|
154 dm.addListener(obs); |
|
155 setPref(true); |
|
156 |
|
157 // Start the test when the download manager window loads |
|
158 let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]. |
|
159 getService(Ci.nsIWindowWatcher); |
|
160 ww.registerNotification(obs); |
|
161 |
|
162 addDownload(); |
|
163 |
|
164 SimpleTest.waitForExplicitFinish(); |
|
165 } |
|
166 |
|
167 ]]> |
|
168 </script> |
|
169 |
|
170 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
171 <p id="display"></p> |
|
172 <div id="content" style="display:none;"></div> |
|
173 <pre id="test"></pre> |
|
174 </body> |
|
175 </window> |