OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // downloads api test | 5 // downloads api test |
6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads | 6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads |
7 | 7 |
8 var downloads = chrome.experimental.downloads; | 8 var downloads = chrome.experimental.downloads; |
9 | 9 |
10 chrome.test.getConfig(function(testConfig) { | 10 chrome.test.getConfig(function(testConfig) { |
(...skipping 30 matching lines...) Expand all Loading... | |
41 } | 41 } |
42 | 42 |
43 // The "/slow" handler waits a specified amount of time before returning a | 43 // The "/slow" handler waits a specified amount of time before returning a |
44 // safe file. Specify zero seconds to return quickly. | 44 // safe file. Specify zero seconds to return quickly. |
45 var SAFE_FAST_URL = getURL('slow?0'); | 45 var SAFE_FAST_URL = getURL('slow?0'); |
46 var NEVER_FINISH_URL = getURL('download-known-size'); | 46 var NEVER_FINISH_URL = getURL('download-known-size'); |
47 var ERROR_GENERIC = downloads.ERROR_GENERIC; | 47 var ERROR_GENERIC = downloads.ERROR_GENERIC; |
48 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; | 48 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
49 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; | 49 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; |
50 | 50 |
51 console.log("I know all these console.logs stink up the place, but please " + | |
52 "leave them in: they will help you check that all the " + | |
53 "callbacks for a particular test run before the test harness " + | |
54 "thinks that that test is finished and that that test doesn't " + | |
55 "'finish' twice."); | |
56 console.log("All log lines that contain a download_id should appear " + | |
57 "before that test finishes. If a callback for test N runs " + | |
58 "after test N+1 begins, then that indicates a race condition " + | |
59 "in test N."); | |
60 | |
51 chrome.test.runTests([ | 61 chrome.test.runTests([ |
52 // TODO(benjhayden): Test onErased using remove(). | 62 // TODO(benjhayden): Test onErased using remove(). |
63 // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443 | |
64 // function downloadSubDirectoryFilename() { | |
65 // var download_id = getNextId(); | |
66 // console.log("id: " + download_id); | |
67 // var callbackCompleted = chrome.test.callbackAdded(); | |
68 // function myListener(delta) { | |
69 // console.log("id: " + download_id); | |
70 // if (delta.filename) console.log("filename: " + delta.filename.new); | |
71 // if (delta.filename && | |
72 // delta.filename.new.indexOf('/foo/slow') !== -1) { | |
73 // downloads.onChanged.removeListener(myListener); | |
74 // callbackCompleted(); | |
75 // } | |
76 // } | |
77 // downloads.onChanged.addListener(myListener); | |
78 // downloads.download( | |
79 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | |
80 // chrome.test.callback(function(id) { | |
81 // console.log("id: " + download_id); | |
82 // chrome.test.assertEq(download_id, id); | |
83 // })); | |
84 // }, | |
85 function downloadSimple() { | |
86 // Test that we can begin a download. | |
87 var download_id = getNextId(); | |
88 console.log("id: " + download_id); | |
89 downloads.download( | |
90 {'url': SAFE_FAST_URL}, | |
91 chrome.test.callbackPass(function(id) { | |
92 console.log("id: " + download_id); | |
93 chrome.test.assertEq(download_id, id); | |
94 })); | |
95 }, | |
96 function downloadPost() { | |
97 // Test the |method| download option. | |
98 var download_id = getNextId(); | |
99 console.log("id: " + download_id); | |
100 downloads.download( | |
101 {'url': getURL('files/post/downloads/a_zip_file.js'), | |
102 'method': 'POST', | |
103 'body': 'WOOHOO'}, | |
104 chrome.test.callbackPass(function(id) { | |
105 console.log("id: " + download_id); | |
106 chrome.test.assertEq(download_id, id); | |
Randy Smith (Not in Mondays)
2012/02/03 20:02:00
Do we have a test confirming that we get the corre
benjhayden
2012/02/10 18:53:07
Done.
| |
107 })); | |
108 }, | |
109 function downloadHeader() { | |
110 // Test the |headers| download option. | |
111 var download_id = getNextId(); | |
112 console.log("id: " + download_id); | |
113 downloads.download( | |
114 {'url': SAFE_FAST_URL, | |
115 'headers': [{'name': 'Foo', 'value': 'bar'}] | |
116 }, | |
117 chrome.test.callbackPass(function(id) { | |
118 console.log("id: " + download_id); | |
119 chrome.test.assertEq(download_id, id); | |
120 })); | |
121 }, | |
122 function downloadInterrupted() { | |
123 // Test that cancel()ing an in-progress download causes its state to | |
124 // transition to interrupted, and test that that state transition is | |
125 // detectable by an onChanged event listener. | |
126 var download_id = getNextId(); | |
127 console.log("id: " + download_id); | |
128 | |
129 var createdCompleted = chrome.test.callbackAdded(); | |
130 function createdListener(created_item) { | |
131 console.log("created_id: " + created_item.id); | |
132 // Ignore onCreated events for any download besides our own. | |
133 if (created_item.id != download_id) | |
134 return; | |
135 // TODO(benjhayden) Move this cancel() into the download() callback | |
136 // after ensuring that DownloadItems are created before that callback | |
137 // is fired. | |
138 downloads.cancel(download_id, chrome.test.callback(function() { | |
139 console.log("id: " + download_id); | |
140 })); | |
141 downloads.onCreated.removeListener(createdListener); | |
142 createdCompleted(); | |
143 } | |
144 downloads.onCreated.addListener(createdListener); | |
145 | |
146 var changedCompleted = chrome.test.callbackAdded(); | |
147 function changedListener(delta) { | |
148 console.log("id: " + delta.id); | |
149 // Ignore onChanged events for downloads besides our own, or events that | |
150 // signal any change besides completion. | |
151 if ((delta.id == download_id) && | |
152 delta.state && | |
153 (delta.state.new == downloads.STATE_INTERRUPTED)) { | |
154 console.log("id: " + delta.id); | |
155 downloads.onChanged.removeListener(changedListener); | |
156 changedCompleted(); | |
157 } | |
158 } | |
159 downloads.onChanged.addListener(changedListener); | |
160 | |
161 downloads.download( | |
162 {'url': NEVER_FINISH_URL}, | |
163 chrome.test.callback(function(id) { | |
164 console.log("id: " + download_id); | |
165 chrome.test.assertEq(download_id, id); | |
166 })); | |
167 }, | |
168 function downloadOnChanged() { | |
169 // Test that download completion is detectable by an onChanged event | |
170 // listener. | |
171 var download_id = getNextId(); | |
172 console.log("id: " + download_id); | |
173 var callbackCompleted = chrome.test.callbackAdded(); | |
174 function myListener(delta) { | |
175 console.log("id: " + delta.id); | |
176 if (delta.state) console.log("state: " + delta.state.new); | |
177 if (delta.state && delta.state.new == downloads.STATE_COMPLETE) { | |
178 downloads.onChanged.removeListener(myListener); | |
179 callbackCompleted(); | |
180 } | |
181 } | |
182 downloads.onChanged.addListener(myListener); | |
183 downloads.download( | |
184 {"url": getURL("slow?0")}, | |
185 chrome.test.callback(function(id) { | |
186 console.log("id: " + download_id); | |
187 chrome.test.assertEq(download_id, id); | |
188 })); | |
189 }, | |
53 function downloadFilename() { | 190 function downloadFilename() { |
191 // Test that we can suggest a filename for a new download, and test that | |
192 // we can detect filename changes with an onChanged event listener. | |
193 const kFilename = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi'; | |
194 var download_id = getNextId(); | |
195 console.log("id: " + download_id); | |
196 var callbackCompleted = chrome.test.callbackAdded(); | |
197 function myListener(delta) { | |
198 console.log("id: " + download_id); | |
199 if (delta.filename) console.log("filename: " + delta.filename.new); | |
200 if (delta.filename && delta.filename.new.indexOf(kFilename) !== -1) { | |
201 downloads.onChanged.removeListener(myListener); | |
202 callbackCompleted(); | |
203 } | |
204 } | |
205 downloads.onChanged.addListener(myListener); | |
54 downloads.download( | 206 downloads.download( |
55 {'url': SAFE_FAST_URL, 'filename': 'foo'}, | 207 {'url': SAFE_FAST_URL, 'filename': kFilename}, |
56 chrome.test.callbackPass(function(id) { | 208 chrome.test.callback(function(id) { |
57 chrome.test.assertEq(getNextId(), id); | 209 console.log("id: " + download_id); |
210 chrome.test.assertEq(download_id, id); | |
58 })); | 211 })); |
59 // TODO(benjhayden): Test the filename using onChanged. | |
60 }, | 212 }, |
61 function downloadOnCreated() { | 213 function downloadOnCreated() { |
62 chrome.test.listenOnce(downloads.onCreated, | 214 // Test that the onCreated event fires when we start a download. |
63 chrome.test.callbackPass(function(item) {})); | 215 var download_id = getNextId(); |
216 console.log("id: " + download_id); | |
217 var createdCompleted = chrome.test.callbackAdded(); | |
218 function createdListener(item) { | |
219 if (item.id == download_id) { | |
220 createdCompleted(); | |
221 downloads.onCreated.removeListener(createdListener); | |
222 } | |
223 }; | |
224 downloads.onCreated.addListener(createdListener); | |
64 downloads.download( | 225 downloads.download( |
65 {'url': SAFE_FAST_URL}, | 226 {'url': SAFE_FAST_URL}, |
66 function(id) { | 227 chrome.test.callback(function(id) { |
67 chrome.test.assertEq(getNextId(), id); | 228 chrome.test.assertEq(download_id, id); |
68 }); | |
69 }, | |
70 function downloadSubDirectoryFilename() { | |
71 downloads.download( | |
72 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | |
73 chrome.test.callbackPass(function(id) { | |
74 chrome.test.assertEq(getNextId(), id); | |
75 })); | 229 })); |
76 // TODO(benjhayden): Test the filename using onChanged. | |
77 }, | 230 }, |
78 function downloadInvalidFilename() { | 231 function downloadInvalidFilename() { |
232 // Test that we disallow invalid filenames for new downloads. | |
79 downloads.download( | 233 downloads.download( |
80 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, | 234 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
81 chrome.test.callbackFail(ERROR_GENERIC)); | 235 chrome.test.callbackFail(ERROR_GENERIC)); |
82 // TODO(benjhayden): Give a better error message. | |
83 }, | 236 }, |
84 function downloadEmpty() { | 237 function downloadEmpty() { |
85 assertThrows(('Invalid value for argument 1. Property \'url\': ' + | 238 assertThrows(('Invalid value for argument 1. Property \'url\': ' + |
86 'Property is required.'), | 239 'Property is required.'), |
87 downloads.download, {}); | 240 downloads.download, {}); |
88 }, | 241 }, |
89 function downloadInvalidSaveAs() { | 242 function downloadInvalidSaveAs() { |
90 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + | 243 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + |
91 'Expected \'boolean\' but got \'string\'.'), | 244 'Expected \'boolean\' but got \'string\'.'), |
92 downloads.download, | 245 downloads.download, |
93 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); | 246 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); |
94 }, | 247 }, |
95 function downloadInvalidHeadersOption() { | 248 function downloadInvalidHeadersOption() { |
96 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + | 249 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + |
97 'Expected \'array\' but got \'string\'.'), | 250 'Expected \'array\' but got \'string\'.'), |
98 downloads.download, | 251 downloads.download, |
99 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); | 252 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
100 }, | 253 }, |
101 function downloadInvalidURL() { | 254 function downloadInvalidURL() { |
255 // Test that download() requires a valid url. | |
102 downloads.download( | 256 downloads.download( |
103 {'url': 'foo bar'}, | 257 {'url': 'foo bar'}, |
104 chrome.test.callbackFail(ERROR_INVALID_URL)); | 258 chrome.test.callbackFail(ERROR_INVALID_URL)); |
105 }, | 259 }, |
106 function downloadInvalidMethod() { | 260 function downloadInvalidMethod() { |
107 assertThrows(('Invalid value for argument 1. Property \'method\': ' + | 261 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
108 'Value must be one of: [GET, POST].'), | 262 'Value must be one of: [GET, POST].'), |
109 downloads.download, | 263 downloads.download, |
110 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); | 264 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
111 }, | 265 }, |
112 function downloadSimple() { | |
113 downloads.download( | |
114 {'url': SAFE_FAST_URL}, | |
115 chrome.test.callbackPass(function(id) { | |
116 chrome.test.assertEq(getNextId(), id); | |
117 })); | |
118 }, | |
119 function downloadPost() { | |
120 downloads.download( | |
121 {'url': getURL('files/post/downloads/a_zip_file.js'), | |
122 'method': 'POST', | |
123 'body': 'WOOHOO'}, | |
124 chrome.test.callbackPass(function(id) { | |
125 chrome.test.assertEq(getNextId(), id); | |
126 })); | |
127 }, | |
128 function downloadHeader() { | |
129 downloads.download( | |
130 {'url': SAFE_FAST_URL, | |
131 'headers': [{'name': 'Foo', 'value': 'bar'}] | |
132 }, | |
133 chrome.test.callbackPass(function(id) { | |
134 chrome.test.assertEq(getNextId(), id); | |
135 })); | |
136 }, | |
137 function downloadInterrupted() { | |
138 // TODO(benjhayden): Find a suitable URL and test that this id is | |
139 // eventually interrupted using onChanged. | |
140 downloads.download( | |
141 {'url': SAFE_FAST_URL}, | |
142 chrome.test.callbackPass(function(id) { | |
143 chrome.test.assertEq(getNextId(), id); | |
144 })); | |
145 }, | |
146 function downloadInvalidHeader() { | 266 function downloadInvalidHeader() { |
267 // Test that download() disallows setting the Cookie header. | |
147 downloads.download( | 268 downloads.download( |
148 {'url': SAFE_FAST_URL, | 269 {'url': SAFE_FAST_URL, |
149 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] | 270 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] |
150 }, | 271 }, |
151 chrome.test.callbackFail(ERROR_GENERIC)); | 272 chrome.test.callbackFail(ERROR_GENERIC)); |
152 // TODO(benjhayden): Give a better error message. | |
153 }, | 273 }, |
154 function downloadGetFileIconInvalidOptions() { | 274 function downloadGetFileIconInvalidOptions() { |
155 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + | 275 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + |
156 'Unexpected property.'), | 276 'Unexpected property.'), |
157 downloads.getFileIcon, | 277 downloads.getFileIcon, |
158 -1, {cat: 'mouse'}); | 278 -1, {cat: 'mouse'}); |
159 }, | 279 }, |
160 function downloadGetFileIconInvalidSize() { | 280 function downloadGetFileIconInvalidSize() { |
161 assertThrows(('Invalid value for argument 2. Property \'size\': ' + | 281 assertThrows(('Invalid value for argument 2. Property \'size\': ' + |
162 'Value must be one of: [16, 32].'), | 282 'Value must be one of: [16, 32].'), |
163 downloads.getFileIcon, -1, {size: 31}); | 283 downloads.getFileIcon, -1, {size: 31}); |
164 }, | 284 }, |
165 function downloadGetFileIconInvalidId() { | 285 function downloadGetFileIconInvalidId() { |
166 downloads.getFileIcon(-42, {size: 32}, | 286 downloads.getFileIcon(-42, {size: 32}, |
167 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 287 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
168 }, | 288 }, |
169 function downloadNoComplete() { | |
170 // This is used partly to test cleanUp. | |
171 downloads.download( | |
172 {'url': NEVER_FINISH_URL}, | |
173 chrome.test.callbackPass(function(id) { | |
174 chrome.test.assertEq(getNextId(), id); | |
175 })); | |
176 }, | |
177 function downloadPauseInvalidId() { | 289 function downloadPauseInvalidId() { |
178 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 290 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
179 }, | 291 }, |
180 function downloadPauseInvalidType() { | 292 function downloadPauseInvalidType() { |
181 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 293 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
182 'but got \'string\'.'), | 294 'but got \'string\'.'), |
183 downloads.pause, | 295 downloads.pause, |
184 'foo'); | 296 'foo'); |
185 }, | 297 }, |
186 function downloadResumeInvalidId() { | 298 function downloadResumeInvalidId() { |
187 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 299 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
188 }, | 300 }, |
189 function downloadResumeInvalidType() { | 301 function downloadResumeInvalidType() { |
190 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 302 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
191 'but got \'string\'.'), | 303 'but got \'string\'.'), |
192 downloads.resume, | 304 downloads.resume, |
193 'foo'); | 305 'foo'); |
194 }, | 306 }, |
195 function downloadCancelInvalidId() { | 307 function downloadCancelInvalidId() { |
196 // Canceling a non-existent download is not considered an error. | 308 // Canceling a non-existent download is not considered an error. |
197 downloads.cancel(-42, chrome.test.callbackPass(function() {})); | 309 downloads.cancel(-42, chrome.test.callbackPass(function() {})); |
198 }, | 310 }, |
199 function downloadCancelInvalidType() { | 311 function downloadCancelInvalidType() { |
200 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 312 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
201 'but got \'string\'.'), | 313 'but got \'string\'.'), |
202 downloads.cancel, 'foo'); | 314 downloads.cancel, 'foo'); |
203 }, | 315 }, |
316 function downloadNoComplete() { | |
317 // This is used partly to test cleanUp. | |
318 var download_id = getNextId(); | |
319 console.log("id: " + download_id); | |
320 downloads.download( | |
321 {'url': NEVER_FINISH_URL}, | |
322 chrome.test.callbackPass(function(id) { | |
323 console.log("id: " + download_id); | |
324 chrome.test.assertEq(download_id, id); | |
325 })); | |
326 }, | |
204 function cleanUp() { | 327 function cleanUp() { |
205 // cleanUp must come last. It clears out all in-progress downloads | 328 // cleanUp must come last. It clears out all in-progress downloads |
206 // so the browser can shutdown cleanly. | 329 // so the browser can shutdown cleanly. |
207 for (var id = 0; id < nextId; ++id) { | 330 for (var id = 0; id < nextId; ++id) { |
208 downloads.cancel(id, chrome.test.callbackPass(function() {})); | 331 downloads.cancel(id, chrome.test.callbackPass(function() {})); |
209 } | 332 } |
210 } | 333 } |
211 ]); | 334 ]); |
212 }); | 335 }); |
OLD | NEW |