|
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 * Test bug 228842 to make sure multiple selections work in the download |
|
7 * manager by making sure commands work as expected for both single and doubly |
|
8 * selected items. |
|
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="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
|
19 <script type="application/javascript" |
|
20 src="utils.js"/> |
|
21 |
|
22 <script type="application/javascript"> |
|
23 <![CDATA[ |
|
24 |
|
25 function test() |
|
26 { |
|
27 var dmui = getDMUI(); |
|
28 if (!dmui) { |
|
29 todo(false, "skip test for toolkit download manager UI"); |
|
30 return; |
|
31 } |
|
32 |
|
33 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
34 getService(Ci.nsIDownloadManager); |
|
35 let db = dm.DBConnection; |
|
36 |
|
37 // Empty any old downloads |
|
38 db.executeSimpleSQL("DELETE FROM moz_downloads"); |
|
39 |
|
40 let stmt = db.createStatement( |
|
41 "INSERT INTO moz_downloads (source, state, target, referrer) " + |
|
42 "VALUES (?1, ?2, ?3, ?4)"); |
|
43 |
|
44 try { |
|
45 for each (let site in ["ed.agadak.net", "mozilla.org", "mozilla.com", "mozilla.net"]) { |
|
46 let file = Cc["@mozilla.org/file/directory_service;1"]. |
|
47 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); |
|
48 file.append(site); |
|
49 let fileSpec = Cc["@mozilla.org/network/io-service;1"]. |
|
50 getService(Ci.nsIIOService).newFileURI(file).spec; |
|
51 |
|
52 stmt.bindStringParameter(0, "http://" + site + "/file"); |
|
53 stmt.bindInt32Parameter(1, dm.DOWNLOAD_FINISHED); |
|
54 stmt.bindStringParameter(2, fileSpec); |
|
55 stmt.bindStringParameter(3, "http://referrer/"); |
|
56 |
|
57 // Add it! |
|
58 stmt.execute(); |
|
59 } |
|
60 } |
|
61 finally { |
|
62 stmt.reset(); |
|
63 stmt.finalize(); |
|
64 } |
|
65 |
|
66 // Close the UI if necessary |
|
67 let wm = Cc["@mozilla.org/appshell/window-mediator;1"]. |
|
68 getService(Ci.nsIWindowMediator); |
|
69 let win = wm.getMostRecentWindow("Download:Manager"); |
|
70 if (win) win.close(); |
|
71 |
|
72 let obs = Cc["@mozilla.org/observer-service;1"]. |
|
73 getService(Ci.nsIObserverService); |
|
74 const DLMGR_UI_DONE = "download-manager-ui-done"; |
|
75 |
|
76 let testPhase = 0; |
|
77 let testObs = { |
|
78 observe: function(aSubject, aTopic, aData) { |
|
79 if (aTopic != DLMGR_UI_DONE) |
|
80 return; |
|
81 |
|
82 SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject); |
|
83 } |
|
84 }; |
|
85 |
|
86 function getLoadContext() { |
|
87 return window.QueryInterface(Ci.nsIInterfaceRequestor) |
|
88 .getInterface(Ci.nsIWebNavigation) |
|
89 .QueryInterface(Ci.nsILoadContext); |
|
90 } |
|
91 |
|
92 function continueTest(win) { |
|
93 let $ = function(aId) win.document.getElementById(aId); |
|
94 let downloadView = $("downloadView"); |
|
95 |
|
96 // Default test/check for invocations |
|
97 let invokeCount = 0; |
|
98 let counter = function() invokeCount++; |
|
99 |
|
100 // Accessors for returning a value for various properties |
|
101 let getItems = function() downloadView.itemCount; |
|
102 let getSelected = function() downloadView.selectedCount; |
|
103 let getClipboard = function() { |
|
104 let clip = Cc["@mozilla.org/widget/clipboard;1"]. |
|
105 getService(Ci.nsIClipboard); |
|
106 let trans = Cc["@mozilla.org/widget/transferable;1"]. |
|
107 createInstance(Ci.nsITransferable); |
|
108 trans.init(getLoadContext()); |
|
109 trans.addDataFlavor("text/unicode"); |
|
110 clip.getData(trans, clip.kGlobalClipboard); |
|
111 let str = {}; |
|
112 let strLen = {}; |
|
113 trans.getTransferData("text/unicode", str, strLen); |
|
114 return str.value.QueryInterface(Ci.nsISupportsString).data. |
|
115 substring(0, strLen.value / 2); |
|
116 }; |
|
117 |
|
118 // Array of tests that consist of the command name, download manager |
|
119 // function to temporarily replace, method to use in its place, value to |
|
120 // use when checking correctness |
|
121 let commandTests = [ |
|
122 ["pause", "pauseDownload", counter, counter], |
|
123 ["resume", "resumeDownload", counter, counter], |
|
124 ["cancel", "cancelDownload", counter, counter], |
|
125 ["open", "openDownload", counter, counter], |
|
126 ["show", "showDownload", counter, counter], |
|
127 ["retry", "retryDownload", counter, counter], |
|
128 ["openReferrer", "openReferrer", counter, counter], |
|
129 ["copyLocation", null, null, getClipboard], |
|
130 ["removeFromList", null, null, getItems], |
|
131 ["selectAll", null, null, getSelected], |
|
132 ]; |
|
133 |
|
134 // All the expected results for both single and double selections |
|
135 let allExpected = { |
|
136 single: { |
|
137 pause: [0, "Paused no downloads"], |
|
138 resume: [0, "Resumed no downloads"], |
|
139 cancel: [0, "Canceled no downloads"], |
|
140 open: [0, "Opened no downloads"], |
|
141 show: [0, "Showed no downloads"], |
|
142 retry: [1, "Retried one download"], |
|
143 openReferrer: [1, "Opened one referrer"], |
|
144 copyLocation: ["http://ed.agadak.net/file", "Copied one location"], |
|
145 removeFromList: [3, "Removed one downloads, remaining 3"], |
|
146 selectAll: [3, "Selected all 3 remaining downloads"], |
|
147 }, |
|
148 double: { |
|
149 pause: [0, "Paused neither download"], |
|
150 resume: [0, "Resumed neither download"], |
|
151 cancel: [0, "Canceled neither download"], |
|
152 open: [0, "Opened neither download"], |
|
153 show: [0, "Showed neither download"], |
|
154 retry: [2, "Retried both downloads"], |
|
155 openReferrer: [2, "Opened both referrers"], |
|
156 copyLocation: ["http://mozilla.org/file\nhttp://mozilla.com/file", "Copied both locations"], |
|
157 removeFromList: [1, "Removed both downloads, remaining 1"], |
|
158 selectAll: [1, "Selected the 1 remaining download"], |
|
159 }, |
|
160 }; |
|
161 |
|
162 // Run two tests: single selected, double selected |
|
163 for each (let whichTest in ["single", "double"]) { |
|
164 let expected = allExpected[whichTest]; |
|
165 |
|
166 // Select the first download |
|
167 downloadView.selectedIndex = 0; |
|
168 |
|
169 // Select 2 downloads for double |
|
170 if (whichTest == "double") |
|
171 synthesizeKey("VK_DOWN", { shiftKey: true }, win); |
|
172 |
|
173 for each (let [command, func, test, value] in commandTests) { |
|
174 // Make a copy of the original function and replace it with a test |
|
175 let copy; |
|
176 [copy, win[func]] = [win[func], test]; |
|
177 |
|
178 // Run the command from the menu |
|
179 $("menuitem_" + command).doCommand(); |
|
180 |
|
181 // Make sure the value is as expected |
|
182 let [correct, message] = expected[command]; |
|
183 ok(value() == correct, message); |
|
184 |
|
185 // Restore original values |
|
186 invokeCount = 0; |
|
187 win[func] = copy; |
|
188 } |
|
189 } |
|
190 |
|
191 // We're done! |
|
192 win.close(); |
|
193 obs.removeObserver(testObs, DLMGR_UI_DONE); |
|
194 SimpleTest.finish(); |
|
195 }; |
|
196 obs.addObserver(testObs, DLMGR_UI_DONE, false); |
|
197 |
|
198 // Show the Download Manager UI |
|
199 dmui.show(); |
|
200 |
|
201 SimpleTest.waitForExplicitFinish(); |
|
202 } |
|
203 |
|
204 ]]> |
|
205 </script> |
|
206 |
|
207 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
208 <p id="display"></p> |
|
209 <div id="content" style="display:none;"></div> |
|
210 <pre id="test"></pre> |
|
211 </body> |
|
212 </window> |