Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/test/data/extensions/api_test/downloads/test.js

Issue 9617010: Move chrome.downloads out of experimental to dev (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/data/extensions/api_test/downloads/manifest.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Comment this out to enable debugging. 8 // Comment this out to enable debugging.
9 console.debug = function() {}; 9 console.debug = function() {};
10 10
11 function debugObject(obj) { 11 function debugObject(obj) {
12 for (var property in obj) { 12 for (var property in obj) {
13 console.debug(property + ': ' + obj[property]); 13 console.debug(property + ': ' + obj[property]);
14 } 14 }
15 } 15 }
16 16
17 var downloads = chrome.experimental.downloads;
18 window.requestFileSystem = (window.requestFileSystem || 17 window.requestFileSystem = (window.requestFileSystem ||
19 window.webkitRequestFileSystem); 18 window.webkitRequestFileSystem);
20 window.BlobBuilder = (window.BlobBuilder || 19 window.BlobBuilder = (window.BlobBuilder ||
21 window.WebKitBlobBuilder); 20 window.WebKitBlobBuilder);
22 21
22 var downloads = chrome.downloads;
23
24 // These strings may change. Do not rely on them in non-test extensions.
25 var ERROR_GENERIC = "I'm afraid I can't do that.";
26 var ERROR_INVALID_URL = 'Invalid URL.';
27 var ERROR_INVALID_OPERATION = 'Invalid operation.';
28
23 chrome.test.getConfig(function(testConfig) { 29 chrome.test.getConfig(function(testConfig) {
24 function getURL(path) { 30 function getURL(path) {
25 return 'http://localhost:' + testConfig.testServer.port + '/' + path; 31 return 'http://localhost:' + testConfig.testServer.port + '/' + path;
26 } 32 }
27 33
28 var nextId = 0; 34 var nextId = 0;
29 function getNextId() { 35 function getNextId() {
30 return nextId++; 36 return nextId++;
31 } 37 }
32 38
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // TODO(benjhayden): Test onErased using remove(). 84 // TODO(benjhayden): Test onErased using remove().
79 85
80 // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443 86 // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443
81 // TODO(benjhayden): Windows slashes. 87 // TODO(benjhayden): Windows slashes.
82 // function downloadSubDirectoryFilename() { 88 // function downloadSubDirectoryFilename() {
83 // var downloadId = getNextId(); 89 // var downloadId = getNextId();
84 // var callbackCompleted = chrome.test.callbackAdded(); 90 // var callbackCompleted = chrome.test.callbackAdded();
85 // function myListener(delta) { 91 // function myListener(delta) {
86 // if ((delta.id != downloadId) || 92 // if ((delta.id != downloadId) ||
87 // !delta.filename || 93 // !delta.filename ||
88 // (delta.filename.new.indexOf('/foo/slow') == -1)) 94 // (delta.filename.current.indexOf('/foo/slow') == -1))
89 // return; 95 // return;
90 // downloads.onChanged.removeListener(myListener); 96 // downloads.onChanged.removeListener(myListener);
91 // callbackCompleted(); 97 // callbackCompleted();
92 // } 98 // }
93 // downloads.onChanged.addListener(myListener); 99 // downloads.onChanged.addListener(myListener);
94 // downloads.download( 100 // downloads.download(
95 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, 101 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'},
96 // chrome.test.callback(function(id) { 102 // chrome.test.callback(function(id) {
97 // chrome.test.assertEq(downloadId, id); 103 // chrome.test.assertEq(downloadId, id);
98 // })); 104 // }));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 })); 163 }));
158 }, 164 },
159 165
160 function downloadOnChanged() { 166 function downloadOnChanged() {
161 // Test that download completion is detectable by an onChanged event 167 // Test that download completion is detectable by an onChanged event
162 // listener. 168 // listener.
163 var downloadId = getNextId(); 169 var downloadId = getNextId();
164 console.debug(downloadId); 170 console.debug(downloadId);
165 var callbackCompleted = chrome.test.callbackAdded(); 171 var callbackCompleted = chrome.test.callbackAdded();
166 function myListener(delta) { 172 function myListener(delta) {
167 console.debug(delta.id); 173 console.debug(JSON.stringify(delta));
168 if ((delta.id != downloadId) || 174 if ((delta.id != downloadId) ||
169 !delta.state) 175 !delta.state)
170 return; 176 return;
171 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new); 177 chrome.test.assertEq('complete', delta.state.current);
172 console.debug(downloadId); 178 console.debug(downloadId);
173 downloads.onChanged.removeListener(myListener); 179 downloads.onChanged.removeListener(myListener);
174 callbackCompleted(); 180 callbackCompleted();
175 } 181 }
176 downloads.onChanged.addListener(myListener); 182 downloads.onChanged.addListener(myListener);
177 downloads.download( 183 downloads.download(
178 {"url": SAFE_FAST_URL}, 184 {"url": SAFE_FAST_URL},
179 chrome.test.callback(function(id) { 185 chrome.test.callback(function(id) {
180 console.debug(downloadId); 186 console.debug(downloadId);
181 chrome.test.assertEq(downloadId, id); 187 chrome.test.assertEq(downloadId, id);
182 })); 188 }));
183 }, 189 },
184 190
185 function downloadAuthBasicFail() { 191 function downloadAuthBasicFail() {
186 var downloadId = getNextId(); 192 var downloadId = getNextId();
187 console.debug(downloadId); 193 console.debug(downloadId);
188 194
189 var changedCompleted = chrome.test.callbackAdded(); 195 var changedCompleted = chrome.test.callbackAdded();
190 function changedListener(delta) { 196 function changedListener(delta) {
191 console.debug(delta.id); 197 console.debug(delta.id);
192 // Ignore onChanged events for downloads besides our own, or events that 198 // Ignore onChanged events for downloads besides our own, or events that
193 // signal any change besides completion. 199 // signal any change besides completion.
194 if ((delta.id != downloadId) || 200 if ((delta.id != downloadId) ||
195 !delta.state || 201 !delta.state ||
196 !delta.error) 202 !delta.error)
197 return; 203 return;
198 console.debug(downloadId); 204 console.debug(downloadId);
199 chrome.test.assertEq(downloads.STATE_INTERRUPTED, delta.state.new); 205 chrome.test.assertEq('interrupted', delta.state.current);
200 chrome.test.assertEq(30, delta.error.new); 206 chrome.test.assertEq(30, delta.error.current);
201 downloads.onChanged.removeListener(changedListener); 207 downloads.onChanged.removeListener(changedListener);
202 if (changedCompleted) { 208 if (changedCompleted) {
203 changedCompleted(); 209 changedCompleted();
204 changedCompleted = null; 210 changedCompleted = null;
205 } 211 }
206 } 212 }
207 downloads.onChanged.addListener(changedListener); 213 downloads.onChanged.addListener(changedListener);
208 214
209 // Sometimes the DownloadsEventRouter detects the item for the first time 215 // Sometimes the DownloadsEventRouter detects the item for the first time
210 // after the item has already been interrupted. In this case, the 216 // after the item has already been interrupted. In this case, the
211 // onChanged event never fires, so run the changedListener manually. If 217 // onChanged event never fires, so run the changedListener manually. If
212 // the DownloadsEventRouter detects the item before it's interrupted, then 218 // the DownloadsEventRouter detects the item before it's interrupted, then
213 // the onChanged event should fire correctly. 219 // the onChanged event should fire correctly.
214 var createdCompleted = chrome.test.callbackAdded(); 220 var createdCompleted = chrome.test.callbackAdded();
215 function createdListener(createdItem) { 221 function createdListener(createdItem) {
216 console.debug(createdItem.id); 222 console.debug(createdItem.id);
217 // Ignore events for any download besides our own. 223 // Ignore events for any download besides our own.
218 if (createdItem.id != downloadId) 224 if (createdItem.id != downloadId)
219 return; 225 return;
220 console.debug(downloadId); 226 console.debug(downloadId);
221 downloads.onCreated.removeListener(createdListener); 227 downloads.onCreated.removeListener(createdListener);
222 createdCompleted(); 228 createdCompleted();
223 if (createdItem.state == downloads.STATE_INTERRUPTED) { 229 if (createdItem.state == 'interrupted') {
224 changedListener({id: downloadId, state: {new: createdItem.state}, 230 changedListener({id: downloadId,
225 error: {new: createdItem.error}}); 231 state: {current: createdItem.state},
232 error: {current: createdItem.error}});
226 } 233 }
227 } 234 }
228 downloads.onCreated.addListener(createdListener); 235 downloads.onCreated.addListener(createdListener);
229 236
230 downloads.download( 237 downloads.download(
231 {'url': AUTH_BASIC_URL, 238 {'url': AUTH_BASIC_URL,
232 'filename': downloadId + '.txt'}, 239 'filename': downloadId + '.txt'},
233 chrome.test.callback(function(id) { 240 chrome.test.callback(function(id) {
234 console.debug(downloadId); 241 console.debug(downloadId);
235 chrome.test.assertEq(downloadId, id); 242 chrome.test.assertEq(downloadId, id);
236 })); 243 }));
237 }, 244 },
238 245
239 function downloadAuthBasicSucceed() { 246 function downloadAuthBasicSucceed() {
240 var downloadId = getNextId(); 247 var downloadId = getNextId();
241 console.debug(downloadId); 248 console.debug(downloadId);
242 249
243 var changedCompleted = chrome.test.callbackAdded(); 250 var changedCompleted = chrome.test.callbackAdded();
244 function changedListener(delta) { 251 function changedListener(delta) {
245 console.debug(delta.id); 252 console.debug(delta.id);
246 // Ignore onChanged events for downloads besides our own, or events that 253 // Ignore onChanged events for downloads besides our own, or events that
247 // signal any change besides completion. 254 // signal any change besides completion.
248 if ((delta.id != downloadId) || 255 if ((delta.id != downloadId) ||
249 !delta.state) 256 !delta.state)
250 return; 257 return;
251 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new); 258 chrome.test.assertEq('complete', delta.state.current);
252 console.debug(downloadId); 259 console.debug(downloadId);
253 downloads.onChanged.removeListener(changedListener); 260 downloads.onChanged.removeListener(changedListener);
254 changedCompleted(); 261 changedCompleted();
255 } 262 }
256 downloads.onChanged.addListener(changedListener); 263 downloads.onChanged.addListener(changedListener);
257 264
258 downloads.download( 265 downloads.download(
259 {'url': AUTH_BASIC_URL, 266 {'url': AUTH_BASIC_URL,
260 'headers': [{'name': 'Authorization', 267 'headers': [{'name': 'Authorization',
261 'value': 'Basic ' + AUTHORIZATION}], 268 'value': 'Basic ' + AUTHORIZATION}],
262 'filename': downloadId + '.txt'}, 269 'filename': downloadId + '.txt'},
263 chrome.test.callback(function(id) { 270 chrome.test.callback(function(id) {
264 console.debug(downloadId); 271 console.debug(downloadId);
265 chrome.test.assertEq(downloadId, id); 272 chrome.test.assertEq(downloadId, id);
266 })); 273 }));
267 }, 274 },
268 275
269 function downloadPostSuccess() { 276 function downloadPostSuccess() {
270 // Test the |method| download option. 277 // Test the |method| download option.
271 var downloadId = getNextId(); 278 var downloadId = getNextId();
272 console.debug(downloadId); 279 console.debug(downloadId);
273 var changedCompleted = chrome.test.callbackAdded(); 280 var changedCompleted = chrome.test.callbackAdded();
274 function changedListener(delta) { 281 function changedListener(delta) {
275 console.debug(delta.id); 282 console.debug(delta.id);
276 // Ignore onChanged events for downloads besides our own, or events that 283 // Ignore onChanged events for downloads besides our own, or events that
277 // signal any change besides completion. 284 // signal any change besides completion.
278 if ((delta.id != downloadId) || 285 if ((delta.id != downloadId) ||
279 !delta.state) 286 !delta.state)
280 return; 287 return;
281 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new); 288 chrome.test.assertEq('complete', delta.state.current);
282 console.debug(downloadId); 289 console.debug(downloadId);
283 downloads.search({id: downloadId}, 290 downloads.search({id: downloadId},
284 chrome.test.callback(function(items) { 291 chrome.test.callback(function(items) {
285 console.debug(downloadId); 292 console.debug(downloadId);
286 chrome.test.assertEq(1, items.length); 293 chrome.test.assertEq(1, items.length);
287 chrome.test.assertEq(downloadId, items[0].id); 294 chrome.test.assertEq(downloadId, items[0].id);
288 debugObject(items[0]); 295 debugObject(items[0]);
289 var EXPECTED_SIZE = 164; 296 var EXPECTED_SIZE = 164;
290 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived); 297 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived);
291 })); 298 }));
(...skipping 24 matching lines...) Expand all
316 323
317 var changedCompleted = chrome.test.callbackAdded(); 324 var changedCompleted = chrome.test.callbackAdded();
318 function changedListener(delta) { 325 function changedListener(delta) {
319 console.debug(delta.id); 326 console.debug(delta.id);
320 // Ignore onChanged events for downloads besides our own, or events that 327 // Ignore onChanged events for downloads besides our own, or events that
321 // signal any change besides interruption. 328 // signal any change besides interruption.
322 if ((delta.id != downloadId) || 329 if ((delta.id != downloadId) ||
323 !delta.state || 330 !delta.state ||
324 !delta.error) 331 !delta.error)
325 return; 332 return;
326 chrome.test.assertEq(downloads.STATE_INTERRUPTED, delta.state.new); 333 chrome.test.assertEq('interrupted', delta.state.current);
327 chrome.test.assertEq(33, delta.error.new); 334 chrome.test.assertEq(33, delta.error.current);
328 console.debug(downloadId); 335 console.debug(downloadId);
329 downloads.onChanged.removeListener(changedListener); 336 downloads.onChanged.removeListener(changedListener);
330 if (changedCompleted) { 337 if (changedCompleted) {
331 changedCompleted(); 338 changedCompleted();
332 changedCompleted = null; 339 changedCompleted = null;
333 } 340 }
334 } 341 }
335 downloads.onChanged.addListener(changedListener); 342 downloads.onChanged.addListener(changedListener);
336 343
337 // Sometimes the DownloadsEventRouter detects the item for the first time 344 // Sometimes the DownloadsEventRouter detects the item for the first time
338 // after the item has already been interrupted. In this case, the 345 // after the item has already been interrupted. In this case, the
339 // onChanged event never fires, so run the changedListener manually. If 346 // onChanged event never fires, so run the changedListener manually. If
340 // the DownloadsEventRouter detects the item before it's interrupted, then 347 // the DownloadsEventRouter detects the item before it's interrupted, then
341 // the onChanged event should fire correctly. 348 // the onChanged event should fire correctly.
342 var createdCompleted = chrome.test.callbackAdded(); 349 var createdCompleted = chrome.test.callbackAdded();
343 function createdListener(createdItem) { 350 function createdListener(createdItem) {
344 console.debug(createdItem.id); 351 console.debug(createdItem.id);
345 // Ignore events for any download besides our own. 352 // Ignore events for any download besides our own.
346 if (createdItem.id != downloadId) 353 if (createdItem.id != downloadId)
347 return; 354 return;
348 console.debug(downloadId); 355 console.debug(downloadId);
349 downloads.onCreated.removeListener(createdListener); 356 downloads.onCreated.removeListener(createdListener);
350 createdCompleted(); 357 createdCompleted();
351 if (createdItem.state == downloads.STATE_INTERRUPTED) { 358 if (createdItem.state == 'interrupted') {
352 changedListener({id: downloadId, state: {new: createdItem.state}, 359 changedListener({id: downloadId,
353 error: {new: createdItem.error}}); 360 state: {current: createdItem.state},
361 error: {current: createdItem.error}});
354 } 362 }
355 } 363 }
356 downloads.onCreated.addListener(createdListener); 364 downloads.onCreated.addListener(createdListener);
357 365
358 downloads.download( 366 downloads.download(
359 {'url': POST_URL, 367 {'url': POST_URL,
360 'filename': downloadId + '.txt', // Prevent 'file' danger. 368 'filename': downloadId + '.txt', // Prevent 'file' danger.
361 'body': 'BODY'}, 369 'body': 'BODY'},
362 chrome.test.callback(function(id) { 370 chrome.test.callback(function(id) {
363 console.debug(downloadId); 371 console.debug(downloadId);
(...skipping 12 matching lines...) Expand all
376 384
377 var changedCompleted = chrome.test.callbackAdded(); 385 var changedCompleted = chrome.test.callbackAdded();
378 function changedListener(delta) { 386 function changedListener(delta) {
379 console.debug(delta.id); 387 console.debug(delta.id);
380 // Ignore onChanged events for downloads besides our own, or events that 388 // Ignore onChanged events for downloads besides our own, or events that
381 // signal any change besides interruption. 389 // signal any change besides interruption.
382 if ((delta.id != downloadId) || 390 if ((delta.id != downloadId) ||
383 !delta.state || 391 !delta.state ||
384 !delta.error) 392 !delta.error)
385 return; 393 return;
386 chrome.test.assertEq(downloads.STATE_INTERRUPTED, delta.state.new); 394 chrome.test.assertEq('interrupted', delta.state.current);
387 chrome.test.assertEq(33, delta.error.new); 395 chrome.test.assertEq(33, delta.error.current);
388 if (delta.error) console.debug(delta.error.new); 396 if (delta.error) console.debug(delta.error.current);
389 console.debug(downloadId); 397 console.debug(downloadId);
390 downloads.onChanged.removeListener(changedListener); 398 downloads.onChanged.removeListener(changedListener);
391 if (changedCompleted) { 399 if (changedCompleted) {
392 changedCompleted(); 400 changedCompleted();
393 changedCompleted = null; 401 changedCompleted = null;
394 } 402 }
395 } 403 }
396 downloads.onChanged.addListener(changedListener); 404 downloads.onChanged.addListener(changedListener);
397 405
398 // Sometimes the DownloadsEventRouter detects the item for the first time 406 // Sometimes the DownloadsEventRouter detects the item for the first time
399 // after the item has already been interrupted. In this case, the 407 // after the item has already been interrupted. In this case, the
400 // onChanged event never fires, so run the changedListener manually. If 408 // onChanged event never fires, so run the changedListener manually. If
401 // the DownloadsEventRouter detects the item before it's interrupted, then 409 // the DownloadsEventRouter detects the item before it's interrupted, then
402 // the onChanged event should fire correctly. 410 // the onChanged event should fire correctly.
403 var createdCompleted = chrome.test.callbackAdded(); 411 var createdCompleted = chrome.test.callbackAdded();
404 function createdListener(createdItem) { 412 function createdListener(createdItem) {
405 console.debug(createdItem.id); 413 console.debug(createdItem.id);
406 // Ignore events for any download besides our own. 414 // Ignore events for any download besides our own.
407 if (createdItem.id != downloadId) 415 if (createdItem.id != downloadId)
408 return; 416 return;
409 console.debug(downloadId); 417 console.debug(downloadId);
410 downloads.onCreated.removeListener(createdListener); 418 downloads.onCreated.removeListener(createdListener);
411 createdCompleted(); 419 createdCompleted();
412 if (createdItem.state == downloads.STATE_INTERRUPTED) { 420 if (createdItem.state == 'interrupted') {
413 changedListener({id: downloadId, state: {new: createdItem.state}, 421 changedListener({id: downloadId,
414 error: {new: createdItem.error}}); 422 state: {current: createdItem.state},
423 error: {current: createdItem.error}});
415 } 424 }
416 } 425 }
417 downloads.onCreated.addListener(createdListener); 426 downloads.onCreated.addListener(createdListener);
418 427
419 downloads.download( 428 downloads.download(
420 {'url': POST_URL, 429 {'url': POST_URL,
421 'filename': downloadId + '.txt', // Prevent 'file' danger. 430 'filename': downloadId + '.txt', // Prevent 'file' danger.
422 'method': 'POST'}, 431 'method': 'POST'},
423 chrome.test.callback(function(id) { 432 chrome.test.callback(function(id) {
424 console.debug(downloadId); 433 console.debug(downloadId);
425 chrome.test.assertEq(downloadId, id); 434 chrome.test.assertEq(downloadId, id);
426 })); 435 }));
427 }, 436 },
428 437
429 function downloadHeadersSuccess() { 438 function downloadHeadersSuccess() {
430 // Test the |header| download option. 439 // Test the |header| download option.
431 var downloadId = getNextId(); 440 var downloadId = getNextId();
432 console.debug(downloadId); 441 console.debug(downloadId);
433 var changedCompleted = chrome.test.callbackAdded(); 442 var changedCompleted = chrome.test.callbackAdded();
434 function changedListener(delta) { 443 function changedListener(delta) {
435 console.debug(delta.id); 444 console.debug(delta.id);
436 // Ignore onChanged events for downloads besides our own, or events that 445 // Ignore onChanged events for downloads besides our own, or events that
437 // signal any change besides completion. 446 // signal any change besides completion.
438 if ((delta.id != downloadId) || 447 if ((delta.id != downloadId) ||
439 !delta.state) 448 !delta.state)
440 return; 449 return;
441 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new); 450 chrome.test.assertEq('complete', delta.state.current);
442 console.debug(downloadId); 451 console.debug(downloadId);
443 downloads.search({id: downloadId}, 452 downloads.search({id: downloadId},
444 chrome.test.callback(function(items) { 453 chrome.test.callback(function(items) {
445 console.debug(downloadId); 454 console.debug(downloadId);
446 chrome.test.assertEq(1, items.length); 455 chrome.test.assertEq(1, items.length);
447 chrome.test.assertEq(downloadId, items[0].id); 456 chrome.test.assertEq(downloadId, items[0].id);
448 debugObject(items[0]); 457 debugObject(items[0]);
449 var EXPECTED_SIZE = 164; 458 var EXPECTED_SIZE = 164;
450 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived); 459 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived);
451 })); 460 }));
452 downloads.onChanged.removeListener(changedListener); 461 downloads.onChanged.removeListener(changedListener);
453 changedCompleted(); 462 changedCompleted();
454 } 463 }
455 downloads.onChanged.addListener(changedListener); 464 downloads.onChanged.addListener(changedListener);
456 465
457 downloads.download( 466 downloads.download(
458 {'url': HEADERS_URL, 467 {'url': HEADERS_URL,
459 'filename': downloadId + '.txt', // Prevent 'file' danger. 468 'filename': downloadId + '.txt', // Prevent 'file' danger.
460 'headers': [{'name': 'Foo', 'value': 'bar'}, 469 'headers': [{'name': 'Foo', 'value': 'bar'},
461 {'name': 'Qx', 'value': 'yo'}]}, 470 {'name': 'Qx', 'value': 'yo'}]},
462 chrome.test.callback(function(id) { 471 chrome.test.callback(function(id) {
463 console.debug(downloadId); 472 console.debug(downloadId);
464 chrome.test.assertEq(downloadId, id); 473 chrome.test.assertEq(downloadId, id);
465 })); 474 }));
466 }, 475 },
467 476
468 function downloadHeadersBinarySuccess() {
469 // Test the |header| download option.
470 var downloadId = getNextId();
471 console.debug(downloadId);
472 var changedCompleted = chrome.test.callbackAdded();
473 function changedListener(delta) {
474 console.debug(delta.id);
475 // Ignore onChanged events for downloads besides our own, or events that
476 // signal any change besides completion.
477 if ((delta.id != downloadId) ||
478 !delta.state)
479 return;
480 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new);
481 console.debug(downloadId);
482 downloads.search({id: downloadId},
483 chrome.test.callback(function(items) {
484 console.debug(downloadId);
485 chrome.test.assertEq(1, items.length);
486 chrome.test.assertEq(downloadId, items[0].id);
487 debugObject(items[0]);
488 var EXPECTED_SIZE = 164;
489 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived);
490 }));
491 downloads.onChanged.removeListener(changedListener);
492 changedCompleted();
493 }
494 downloads.onChanged.addListener(changedListener);
495
496 downloads.download(
497 {'url': HEADERS_URL,
498 'filename': downloadId + '.txt', // Prevent 'file' danger.
499 'headers': [{'name': 'Foo', 'binaryValue': [98, 97, 114]},
500 {'name': 'Qx', 'binaryValue': [121, 111]}]},
501 chrome.test.callback(function(id) {
502 console.debug(downloadId);
503 chrome.test.assertEq(downloadId, id);
504 }));
505 },
506
507 function downloadHeadersWouldFail() { 477 function downloadHeadersWouldFail() {
508 // Test that downloadHeadersSuccess() would fail if the resource requires 478 // Test that downloadHeadersSuccess() would fail if the resource requires
509 // the headers, and chrome fails to propagate them back to the server. 479 // the headers, and chrome fails to propagate them back to the server.
510 // This tests both that testserver.py does not succeed when it should 480 // This tests both that testserver.py does not succeed when it should
511 // fail as well as how the downloads extension api exposes the 481 // fail as well as how the downloads extension api exposes the
512 // failure to extensions. 482 // failure to extensions.
513 var downloadId = getNextId(); 483 var downloadId = getNextId();
514 console.debug(downloadId); 484 console.debug(downloadId);
515 485
516 var changedCompleted = chrome.test.callbackAdded(); 486 var changedCompleted = chrome.test.callbackAdded();
517 function changedListener(delta) { 487 function changedListener(delta) {
518 console.debug(delta.id); 488 console.debug(delta.id);
519 // Ignore onChanged events for downloads besides our own, or events that 489 // Ignore onChanged events for downloads besides our own, or events that
520 // signal any change besides interruption. 490 // signal any change besides interruption.
521 if ((delta.id != downloadId) || 491 if ((delta.id != downloadId) ||
522 !delta.state || 492 !delta.state ||
523 !delta.error) 493 !delta.error)
524 return; 494 return;
525 chrome.test.assertEq(downloads.STATE_INTERRUPTED, delta.state.new); 495 chrome.test.assertEq('interrupted', delta.state.current);
526 chrome.test.assertEq(33, delta.error.new); 496 chrome.test.assertEq(33, delta.error.current);
527 console.debug(downloadId); 497 console.debug(downloadId);
528 downloads.onChanged.removeListener(changedListener); 498 downloads.onChanged.removeListener(changedListener);
529 if (changedCompleted) { 499 if (changedCompleted) {
530 changedCompleted(); 500 changedCompleted();
531 changedCompleted = null; 501 changedCompleted = null;
532 } 502 }
533 } 503 }
534 downloads.onChanged.addListener(changedListener); 504 downloads.onChanged.addListener(changedListener);
535 505
536 // Sometimes the DownloadsEventRouter detects the item for the first time 506 // Sometimes the DownloadsEventRouter detects the item for the first time
537 // after the item has already been interrupted. In this case, the 507 // after the item has already been interrupted. In this case, the
538 // onChanged event never fires, so run the changedListener manually. If 508 // onChanged event never fires, so run the changedListener manually. If
539 // the DownloadsEventRouter detects the item before it's interrupted, then 509 // the DownloadsEventRouter detects the item before it's interrupted, then
540 // the onChanged event should fire correctly. 510 // the onChanged event should fire correctly.
541 var createdCompleted = chrome.test.callbackAdded(); 511 var createdCompleted = chrome.test.callbackAdded();
542 function createdListener(createdItem) { 512 function createdListener(createdItem) {
543 console.debug(createdItem.id); 513 console.debug(createdItem.id);
544 // Ignore events for any download besides our own. 514 // Ignore events for any download besides our own.
545 if (createdItem.id != downloadId) 515 if (createdItem.id != downloadId)
546 return; 516 return;
547 console.debug(downloadId); 517 console.debug(downloadId);
548 downloads.onCreated.removeListener(createdListener); 518 downloads.onCreated.removeListener(createdListener);
549 createdCompleted(); 519 createdCompleted();
550 if (createdItem.state == downloads.STATE_INTERRUPTED) { 520 if (createdItem.state == 'interrupted') {
551 changedListener({id: downloadId, state: {new: createdItem.state}, 521 changedListener({id: downloadId,
552 error: {new: createdItem.error}}); 522 state: {current: createdItem.state},
523 error: {current: createdItem.error}});
553 } 524 }
554 } 525 }
555 downloads.onCreated.addListener(createdListener); 526 downloads.onCreated.addListener(createdListener);
556 527
557 downloads.download( 528 downloads.download(
558 {'url': HEADERS_URL}, 529 {'url': HEADERS_URL},
559 chrome.test.callback(function(id) { 530 chrome.test.callback(function(id) {
560 console.debug(downloadId); 531 console.debug(downloadId);
561 chrome.test.assertEq(downloadId, id); 532 chrome.test.assertEq(downloadId, id);
562 })); 533 }));
563 }, 534 },
564 535
565 function downloadHeadersInvalid0() { 536 function downloadHeadersInvalid0() {
566 // Test that we disallow certain headers case-insensitive. 537 // Test that we disallow certain headers case-insensitive.
567 downloads.download( 538 downloads.download(
568 {'url': SAFE_FAST_URL, 539 {'url': SAFE_FAST_URL,
569 'headers': [{'name': 'Accept-chArsEt', 'value': 'evil'}]}, 540 'headers': [{'name': 'Accept-chArsEt', 'value': 'evil'}]},
570 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 541 chrome.test.callbackFail(ERROR_GENERIC));
571 }, 542 },
572 543
573 function downloadHeadersInvalid1() { 544 function downloadHeadersInvalid1() {
574 // Test that we disallow certain headers. 545 // Test that we disallow certain headers.
575 downloads.download( 546 downloads.download(
576 {'url': SAFE_FAST_URL, 547 {'url': SAFE_FAST_URL,
577 'headers': [{'name': 'accept-eNcoding', 'value': 'evil'}]}, 548 'headers': [{'name': 'accept-eNcoding', 'value': 'evil'}]},
578 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 549 chrome.test.callbackFail(ERROR_GENERIC));
579 }, 550 },
580 551
581 function downloadHeadersInvalid2() { 552 function downloadHeadersInvalid2() {
582 // Test that we disallow certain headers. 553 // Test that we disallow certain headers.
583 downloads.download( 554 downloads.download(
584 {'url': SAFE_FAST_URL, 555 {'url': SAFE_FAST_URL,
585 'headers': [{'name': 'coNNection', 'value': 'evil'}]}, 556 'headers': [{'name': 'coNNection', 'value': 'evil'}]},
586 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 557 chrome.test.callbackFail(ERROR_GENERIC));
587 }, 558 },
588 559
589 function downloadHeadersInvalid3() { 560 function downloadHeadersInvalid3() {
590 // Test that we disallow certain headers. 561 // Test that we disallow certain headers.
591 downloads.download( 562 downloads.download(
592 {'url': SAFE_FAST_URL, 563 {'url': SAFE_FAST_URL,
593 'headers': [{'name': 'coNteNt-leNgth', 'value': 'evil'}]}, 564 'headers': [{'name': 'coNteNt-leNgth', 'value': 'evil'}]},
594 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 565 chrome.test.callbackFail(ERROR_GENERIC));
595 }, 566 },
596 567
597 function downloadHeadersInvalid4() { 568 function downloadHeadersInvalid4() {
598 // Test that we disallow certain headers. 569 // Test that we disallow certain headers.
599 downloads.download( 570 downloads.download(
600 {'url': SAFE_FAST_URL, 571 {'url': SAFE_FAST_URL,
601 'headers': [{'name': 'cooKIE', 'value': 'evil'}]}, 572 'headers': [{'name': 'cooKIE', 'value': 'evil'}]},
602 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 573 chrome.test.callbackFail(ERROR_GENERIC));
603 }, 574 },
604 575
605 function downloadHeadersInvalid5() { 576 function downloadHeadersInvalid5() {
606 // Test that we disallow certain headers. 577 // Test that we disallow certain headers.
607 downloads.download( 578 downloads.download(
608 {'url': SAFE_FAST_URL, 579 {'url': SAFE_FAST_URL,
609 'headers': [{'name': 'cOOkie2', 'value': 'evil'}]}, 580 'headers': [{'name': 'cOOkie2', 'value': 'evil'}]},
610 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 581 chrome.test.callbackFail(ERROR_GENERIC));
611 }, 582 },
612 583
613 function downloadHeadersInvalid6() { 584 function downloadHeadersInvalid6() {
614 // Test that we disallow certain headers. 585 // Test that we disallow certain headers.
615 downloads.download( 586 downloads.download(
616 {'url': SAFE_FAST_URL, 587 {'url': SAFE_FAST_URL,
617 'headers': [{'name': 'coNteNt-traNsfer-eNcodiNg', 'value': 'evil'}]}, 588 'headers': [{'name': 'coNteNt-traNsfer-eNcodiNg', 'value': 'evil'}]},
618 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 589 chrome.test.callbackFail(ERROR_GENERIC));
619 }, 590 },
620 591
621 function downloadHeadersInvalid7() { 592 function downloadHeadersInvalid7() {
622 // Test that we disallow certain headers. 593 // Test that we disallow certain headers.
623 downloads.download( 594 downloads.download(
624 {'url': SAFE_FAST_URL, 595 {'url': SAFE_FAST_URL,
625 'headers': [{'name': 'dAtE', 'value': 'evil'}]}, 596 'headers': [{'name': 'dAtE', 'value': 'evil'}]},
626 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 597 chrome.test.callbackFail(ERROR_GENERIC));
627 }, 598 },
628 599
629 function downloadHeadersInvalid8() { 600 function downloadHeadersInvalid8() {
630 // Test that we disallow certain headers. 601 // Test that we disallow certain headers.
631 downloads.download( 602 downloads.download(
632 {'url': SAFE_FAST_URL, 603 {'url': SAFE_FAST_URL,
633 'headers': [{'name': 'ExpEcT', 'value': 'evil'}]}, 604 'headers': [{'name': 'ExpEcT', 'value': 'evil'}]},
634 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 605 chrome.test.callbackFail(ERROR_GENERIC));
635 }, 606 },
636 607
637 function downloadHeadersInvalid9() { 608 function downloadHeadersInvalid9() {
638 // Test that we disallow certain headers. 609 // Test that we disallow certain headers.
639 downloads.download( 610 downloads.download(
640 {'url': SAFE_FAST_URL, 611 {'url': SAFE_FAST_URL,
641 'headers': [{'name': 'hOsT', 'value': 'evil'}]}, 612 'headers': [{'name': 'hOsT', 'value': 'evil'}]},
642 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 613 chrome.test.callbackFail(ERROR_GENERIC));
643 }, 614 },
644 615
645 function downloadHeadersInvalid10() { 616 function downloadHeadersInvalid10() {
646 // Test that we disallow certain headers. 617 // Test that we disallow certain headers.
647 downloads.download( 618 downloads.download(
648 {'url': SAFE_FAST_URL, 619 {'url': SAFE_FAST_URL,
649 'headers': [{'name': 'kEEp-aLivE', 'value': 'evil'}]}, 620 'headers': [{'name': 'kEEp-aLivE', 'value': 'evil'}]},
650 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 621 chrome.test.callbackFail(ERROR_GENERIC));
651 }, 622 },
652 623
653 function downloadHeadersInvalid11() { 624 function downloadHeadersInvalid11() {
654 // Test that we disallow certain headers. 625 // Test that we disallow certain headers.
655 downloads.download( 626 downloads.download(
656 {'url': SAFE_FAST_URL, 627 {'url': SAFE_FAST_URL,
657 'headers': [{'name': 'rEfErEr', 'value': 'evil'}]}, 628 'headers': [{'name': 'rEfErEr', 'value': 'evil'}]},
658 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 629 chrome.test.callbackFail(ERROR_GENERIC));
659 }, 630 },
660 631
661 function downloadHeadersInvalid12() { 632 function downloadHeadersInvalid12() {
662 // Test that we disallow certain headers. 633 // Test that we disallow certain headers.
663 downloads.download( 634 downloads.download(
664 {'url': SAFE_FAST_URL, 635 {'url': SAFE_FAST_URL,
665 'headers': [{'name': 'tE', 'value': 'evil'}]}, 636 'headers': [{'name': 'tE', 'value': 'evil'}]},
666 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 637 chrome.test.callbackFail(ERROR_GENERIC));
667 }, 638 },
668 639
669 function downloadHeadersInvalid13() { 640 function downloadHeadersInvalid13() {
670 // Test that we disallow certain headers. 641 // Test that we disallow certain headers.
671 downloads.download( 642 downloads.download(
672 {'url': SAFE_FAST_URL, 643 {'url': SAFE_FAST_URL,
673 'headers': [{'name': 'trAilER', 'value': 'evil'}]}, 644 'headers': [{'name': 'trAilER', 'value': 'evil'}]},
674 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 645 chrome.test.callbackFail(ERROR_GENERIC));
675 }, 646 },
676 647
677 function downloadHeadersInvalid14() { 648 function downloadHeadersInvalid14() {
678 // Test that we disallow certain headers. 649 // Test that we disallow certain headers.
679 downloads.download( 650 downloads.download(
680 {'url': SAFE_FAST_URL, 651 {'url': SAFE_FAST_URL,
681 'headers': [{'name': 'trANsfer-eNcodiNg', 'value': 'evil'}]}, 652 'headers': [{'name': 'trANsfer-eNcodiNg', 'value': 'evil'}]},
682 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 653 chrome.test.callbackFail(ERROR_GENERIC));
683 }, 654 },
684 655
685 function downloadHeadersInvalid15() { 656 function downloadHeadersInvalid15() {
686 // Test that we disallow certain headers. 657 // Test that we disallow certain headers.
687 downloads.download( 658 downloads.download(
688 {'url': SAFE_FAST_URL, 659 {'url': SAFE_FAST_URL,
689 'headers': [{'name': 'upGRAde', 'value': 'evil'}]}, 660 'headers': [{'name': 'upGRAde', 'value': 'evil'}]},
690 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 661 chrome.test.callbackFail(ERROR_GENERIC));
691 }, 662 },
692 663
693 function downloadHeadersInvalid16() { 664 function downloadHeadersInvalid16() {
694 // Test that we disallow certain headers. 665 // Test that we disallow certain headers.
695 downloads.download( 666 downloads.download(
696 {'url': SAFE_FAST_URL, 667 {'url': SAFE_FAST_URL,
697 'headers': [{'name': 'usER-agENt', 'value': 'evil'}]}, 668 'headers': [{'name': 'usER-agENt', 'value': 'evil'}]},
698 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 669 chrome.test.callbackFail(ERROR_GENERIC));
699 }, 670 },
700 671
701 function downloadHeadersInvalid17() { 672 function downloadHeadersInvalid17() {
702 // Test that we disallow certain headers. 673 // Test that we disallow certain headers.
703 downloads.download( 674 downloads.download(
704 {'url': SAFE_FAST_URL, 675 {'url': SAFE_FAST_URL,
705 'headers': [{'name': 'viA', 'value': 'evil'}]}, 676 'headers': [{'name': 'viA', 'value': 'evil'}]},
706 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 677 chrome.test.callbackFail(ERROR_GENERIC));
707 }, 678 },
708 679
709 function downloadHeadersInvalid18() { 680 function downloadHeadersInvalid18() {
710 // Test that we disallow certain headers. 681 // Test that we disallow certain headers.
711 downloads.download( 682 downloads.download(
712 {'url': SAFE_FAST_URL, 683 {'url': SAFE_FAST_URL,
713 'headers': [{'name': 'pRoxY-', 'value': 'evil'}]}, 684 'headers': [{'name': 'pRoxY-', 'value': 'evil'}]},
714 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 685 chrome.test.callbackFail(ERROR_GENERIC));
715 }, 686 },
716 687
717 function downloadHeadersInvalid19() { 688 function downloadHeadersInvalid19() {
718 // Test that we disallow certain headers. 689 // Test that we disallow certain headers.
719 downloads.download( 690 downloads.download(
720 {'url': SAFE_FAST_URL, 691 {'url': SAFE_FAST_URL,
721 'headers': [{'name': 'sEc-', 'value': 'evil'}]}, 692 'headers': [{'name': 'sEc-', 'value': 'evil'}]},
722 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 693 chrome.test.callbackFail(ERROR_GENERIC));
723 }, 694 },
724 695
725 function downloadHeadersInvalid20() { 696 function downloadHeadersInvalid20() {
726 // Test that we disallow certain headers. 697 // Test that we disallow certain headers.
727 downloads.download( 698 downloads.download(
728 {'url': SAFE_FAST_URL, 699 {'url': SAFE_FAST_URL,
729 'headers': [{'name': 'pRoxY-probably-not-evil', 'value': 'evil'}]}, 700 'headers': [{'name': 'pRoxY-probably-not-evil', 'value': 'evil'}]},
730 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 701 chrome.test.callbackFail(ERROR_GENERIC));
731 }, 702 },
732 703
733 function downloadHeadersInvalid21() { 704 function downloadHeadersInvalid21() {
734 // Test that we disallow certain headers. 705 // Test that we disallow certain headers.
735 downloads.download( 706 downloads.download(
736 {'url': SAFE_FAST_URL, 707 {'url': SAFE_FAST_URL,
737 'headers': [{'name': 'sEc-probably-not-evil', 'value': 'evil'}]}, 708 'headers': [{'name': 'sEc-probably-not-evil', 'value': 'evil'}]},
738 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 709 chrome.test.callbackFail(ERROR_GENERIC));
739 }, 710 },
740 711
741 function downloadHeadersInvalid22() { 712 function downloadHeadersInvalid22() {
742 // Test that we disallow certain headers. 713 // Test that we disallow certain headers.
743 downloads.download( 714 downloads.download(
744 {'url': SAFE_FAST_URL, 715 {'url': SAFE_FAST_URL,
745 'headers': [{'name': 'oRiGiN', 'value': 'evil'}]}, 716 'headers': [{'name': 'oRiGiN', 'value': 'evil'}]},
746 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 717 chrome.test.callbackFail(ERROR_GENERIC));
747 }, 718 },
748 719
749 function downloadHeadersInvalid23() { 720 function downloadHeadersInvalid23() {
750 // Test that we disallow certain headers. 721 // Test that we disallow certain headers.
751 downloads.download( 722 downloads.download(
752 {'url': SAFE_FAST_URL, 723 {'url': SAFE_FAST_URL,
753 'headers': [{'name': 'Access-Control-Request-Headers', 724 'headers': [{'name': 'Access-Control-Request-Headers',
754 'value': 'evil'}]}, 725 'value': 'evil'}]},
755 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 726 chrome.test.callbackFail(ERROR_GENERIC));
756 }, 727 },
757 728
758 function downloadHeadersInvalid24() { 729 function downloadHeadersInvalid24() {
759 // Test that we disallow certain headers. 730 // Test that we disallow certain headers.
760 downloads.download( 731 downloads.download(
761 {'url': SAFE_FAST_URL, 732 {'url': SAFE_FAST_URL,
762 'headers': [{'name': 'Access-Control-Request-Method', 733 'headers': [{'name': 'Access-Control-Request-Method',
763 'value': 'evil'}]}, 734 'value': 'evil'}]},
764 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 735 chrome.test.callbackFail(ERROR_GENERIC));
765 }, 736 },
766 737
767 function downloadInterrupted() { 738 function downloadInterrupted() {
768 // Test that cancel()ing an in-progress download causes its state to 739 // Test that cancel()ing an in-progress download causes its state to
769 // transition to interrupted, and test that that state transition is 740 // transition to interrupted, and test that that state transition is
770 // detectable by an onChanged event listener. 741 // detectable by an onChanged event listener.
771 // TODO(benjhayden): Test other sources of interruptions such as server 742 // TODO(benjhayden): Test other sources of interruptions such as server
772 // death. 743 // death.
773 var downloadId = getNextId(); 744 var downloadId = getNextId();
774 console.debug(downloadId); 745 console.debug(downloadId);
(...skipping 18 matching lines...) Expand all
793 764
794 var changedCompleted = chrome.test.callbackAdded(); 765 var changedCompleted = chrome.test.callbackAdded();
795 function changedListener(delta) { 766 function changedListener(delta) {
796 console.debug(delta.id); 767 console.debug(delta.id);
797 // Ignore onChanged events for downloads besides our own, or events that 768 // Ignore onChanged events for downloads besides our own, or events that
798 // signal any change besides interruption. 769 // signal any change besides interruption.
799 if ((delta.id != downloadId) || 770 if ((delta.id != downloadId) ||
800 !delta.state || 771 !delta.state ||
801 !delta.error) 772 !delta.error)
802 return; 773 return;
803 chrome.test.assertEq(downloads.STATE_INTERRUPTED, delta.state.new); 774 chrome.test.assertEq('interrupted', delta.state.current);
804 chrome.test.assertEq(40, delta.error.new); 775 chrome.test.assertEq(40, delta.error.current);
805 console.debug(downloadId); 776 console.debug(downloadId);
806 downloads.onChanged.removeListener(changedListener); 777 downloads.onChanged.removeListener(changedListener);
807 changedCompleted(); 778 changedCompleted();
808 } 779 }
809 downloads.onChanged.addListener(changedListener); 780 downloads.onChanged.addListener(changedListener);
810 781
811 downloads.download( 782 downloads.download(
812 {'url': NEVER_FINISH_URL}, 783 {'url': NEVER_FINISH_URL},
813 chrome.test.callback(function(id) { 784 chrome.test.callback(function(id) {
814 console.debug(downloadId); 785 console.debug(downloadId);
815 chrome.test.assertEq(downloadId, id); 786 chrome.test.assertEq(downloadId, id);
816 })); 787 }));
817 }, 788 },
818 789
819 function downloadFilename() { 790 function downloadFilename() {
820 // Test that we can suggest a filename for a new download, and test that 791 // Test that we can suggest a filename for a new download, and test that
821 // we can detect filename changes with an onChanged event listener. 792 // we can detect filename changes with an onChanged event listener.
822 var FILENAME = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi'; 793 var FILENAME = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi';
823 var downloadId = getNextId(); 794 var downloadId = getNextId();
824 console.debug(downloadId); 795 console.debug(downloadId);
825 var callbackCompleted = chrome.test.callbackAdded(); 796 var callbackCompleted = chrome.test.callbackAdded();
826 function myListener(delta) { 797 function myListener(delta) {
827 console.debug(delta.id); 798 console.debug(delta.id);
828 if ((delta.id != downloadId) || 799 if ((delta.id != downloadId) ||
829 !delta.filename || 800 !delta.filename ||
830 (delta.filename.new.indexOf(FILENAME) == -1)) 801 (delta.filename.current.indexOf(FILENAME) == -1))
831 return; 802 return;
832 console.debug(downloadId); 803 console.debug(downloadId);
833 downloads.onChanged.removeListener(myListener); 804 downloads.onChanged.removeListener(myListener);
834 callbackCompleted(); 805 callbackCompleted();
835 } 806 }
836 downloads.onChanged.addListener(myListener); 807 downloads.onChanged.addListener(myListener);
837 downloads.download( 808 downloads.download(
838 {'url': SAFE_FAST_URL, 'filename': FILENAME}, 809 {'url': SAFE_FAST_URL, 'filename': FILENAME},
839 chrome.test.callback(function(id) { 810 chrome.test.callback(function(id) {
840 console.debug(downloadId); 811 console.debug(downloadId);
841 chrome.test.assertEq(downloadId, id); 812 chrome.test.assertEq(downloadId, id);
842 })); 813 }));
843 }, 814 },
844 815
845 // TODO(benjhayden): Update this test when downloading to sub-directories is 816 // TODO(benjhayden): Update this test when downloading to sub-directories is
846 // supported. 817 // supported.
847 function downloadFilenameDisallowSlashes() { 818 function downloadFilenameDisallowSlashes() {
848 downloads.download( 819 downloads.download(
849 {'url': SAFE_FAST_URL, 'filename': 'subdirectory/file.txt'}, 820 {'url': SAFE_FAST_URL, 'filename': 'subdirectory/file.txt'},
850 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 821 chrome.test.callbackFail(ERROR_GENERIC));
851 }, 822 },
852 823
853 function downloadOnCreated() { 824 function downloadOnCreated() {
854 // Test that the onCreated event fires when we start a download. 825 // Test that the onCreated event fires when we start a download.
855 var downloadId = getNextId(); 826 var downloadId = getNextId();
856 console.debug(downloadId); 827 console.debug(downloadId);
857 var createdCompleted = chrome.test.callbackAdded(); 828 var createdCompleted = chrome.test.callbackAdded();
858 function createdListener(item) { 829 function createdListener(item) {
859 console.debug(item.id); 830 console.debug(item.id);
860 if (item.id != downloadId) 831 if (item.id != downloadId)
861 return; 832 return;
862 console.debug(downloadId); 833 console.debug(downloadId);
863 createdCompleted(); 834 createdCompleted();
864 downloads.onCreated.removeListener(createdListener); 835 downloads.onCreated.removeListener(createdListener);
865 }; 836 };
866 downloads.onCreated.addListener(createdListener); 837 downloads.onCreated.addListener(createdListener);
867 downloads.download( 838 downloads.download(
868 {'url': SAFE_FAST_URL}, 839 {'url': SAFE_FAST_URL},
869 chrome.test.callback(function(id) { 840 chrome.test.callback(function(id) {
870 console.debug(downloadId); 841 console.debug(downloadId);
871 chrome.test.assertEq(downloadId, id); 842 chrome.test.assertEq(downloadId, id);
872 })); 843 }));
873 }, 844 },
874 845
875 function downloadInvalidFilename() { 846 function downloadInvalidFilename() {
876 // Test that we disallow invalid filenames for new downloads. 847 // Test that we disallow invalid filenames for new downloads.
877 downloads.download( 848 downloads.download(
878 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, 849 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'},
879 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 850 chrome.test.callbackFail(ERROR_GENERIC));
880 }, 851 },
881 852
882 function downloadEmpty() { 853 function downloadEmpty() {
883 assertThrows(('Invalid value for argument 1. Property \'url\': ' + 854 assertThrows(('Invalid value for argument 1. Property \'url\': ' +
884 'Property is required.'), 855 'Property is required.'),
885 downloads.download, {}); 856 downloads.download, {});
886 }, 857 },
887 858
888 function downloadInvalidSaveAs() { 859 function downloadInvalidSaveAs() {
889 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + 860 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' +
890 'Expected \'boolean\' but got \'string\'.'), 861 'Expected \'boolean\' but got \'string\'.'),
891 downloads.download, 862 downloads.download,
892 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); 863 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'});
893 }, 864 },
894 865
895 function downloadInvalidHeadersOption() { 866 function downloadInvalidHeadersOption() {
896 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + 867 assertThrows(('Invalid value for argument 1. Property \'headers\': ' +
897 'Expected \'array\' but got \'string\'.'), 868 'Expected \'array\' but got \'string\'.'),
898 downloads.download, 869 downloads.download,
899 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); 870 {'url': SAFE_FAST_URL, 'headers': 'GOAT'});
900 }, 871 },
901 872
902 function downloadInvalidURL0() { 873 function downloadInvalidURL0() {
903 // Test that download() requires a valid url. 874 // Test that download() requires a valid url.
904 downloads.download( 875 downloads.download(
905 {'url': 'foo bar'}, 876 {'url': 'foo bar'},
906 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 877 chrome.test.callbackFail(ERROR_INVALID_URL));
907 }, 878 },
908 879
909 function downloadInvalidURL1() { 880 function downloadInvalidURL1() {
910 // Test that download() requires a valid url, including protocol and 881 // Test that download() requires a valid url, including protocol and
911 // hostname. 882 // hostname.
912 downloads.download( 883 downloads.download(
913 {'url': '../hello'}, 884 {'url': '../hello'},
914 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 885 chrome.test.callbackFail(ERROR_INVALID_URL));
915 }, 886 },
916 887
917 function downloadInvalidURL2() { 888 function downloadInvalidURL2() {
918 // Test that download() requires a valid url, including protocol and 889 // Test that download() requires a valid url, including protocol and
919 // hostname. 890 // hostname.
920 downloads.download( 891 downloads.download(
921 {'url': '/hello'}, 892 {'url': '/hello'},
922 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 893 chrome.test.callbackFail(ERROR_INVALID_URL));
923 }, 894 },
924 895
925 function downloadInvalidURL3() { 896 function downloadInvalidURL3() {
926 // Test that download() requires a valid url, including protocol. 897 // Test that download() requires a valid url, including protocol.
927 downloads.download( 898 downloads.download(
928 {'url': 'google.com/'}, 899 {'url': 'google.com/'},
929 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 900 chrome.test.callbackFail(ERROR_INVALID_URL));
930 }, 901 },
931 902
932 function downloadInvalidURL4() { 903 function downloadInvalidURL4() {
933 // Test that download() requires a valid url, including protocol and 904 // Test that download() requires a valid url, including protocol and
934 // hostname. 905 // hostname.
935 downloads.download( 906 downloads.download(
936 {'url': 'http://'}, 907 {'url': 'http://'},
937 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 908 chrome.test.callbackFail(ERROR_INVALID_URL));
938 }, 909 },
939 910
940 function downloadInvalidURL5() { 911 function downloadInvalidURL5() {
941 // Test that download() requires a valid url, including protocol and 912 // Test that download() requires a valid url, including protocol and
942 // hostname. 913 // hostname.
943 downloads.download( 914 downloads.download(
944 {'url': '#frag'}, 915 {'url': '#frag'},
945 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 916 chrome.test.callbackFail(ERROR_INVALID_URL));
946 }, 917 },
947 918
948 function downloadInvalidURL6() { 919 function downloadInvalidURL6() {
949 // Test that download() requires a valid url, including protocol and 920 // Test that download() requires a valid url, including protocol and
950 // hostname. 921 // hostname.
951 downloads.download( 922 downloads.download(
952 {'url': 'foo/bar.html#frag'}, 923 {'url': 'foo/bar.html#frag'},
953 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 924 chrome.test.callbackFail(ERROR_INVALID_URL));
954 }, 925 },
955 926
956 function downloadAllowFragments() { 927 function downloadAllowFragments() {
957 // Valid URLs plus fragments are still valid URLs. 928 // Valid URLs plus fragments are still valid URLs.
958 var downloadId = getNextId(); 929 var downloadId = getNextId();
959 console.debug(downloadId); 930 console.debug(downloadId);
960 downloads.download( 931 downloads.download(
961 {'url': SAFE_FAST_URL + '#frag'}, 932 {'url': SAFE_FAST_URL + '#frag'},
962 chrome.test.callback(function(id) { 933 chrome.test.callback(function(id) {
963 chrome.test.assertEq(downloadId, id); 934 chrome.test.assertEq(downloadId, id);
(...skipping 17 matching lines...) Expand all
981 {'url': 'file:///'}, 952 {'url': 'file:///'},
982 chrome.test.callback(function(id) { 953 chrome.test.callback(function(id) {
983 chrome.test.assertEq(downloadId, id); 954 chrome.test.assertEq(downloadId, id);
984 })); 955 }));
985 }, 956 },
986 957
987 function downloadInvalidURL7() { 958 function downloadInvalidURL7() {
988 // Test that download() rejects javascript urls. 959 // Test that download() rejects javascript urls.
989 downloads.download( 960 downloads.download(
990 {'url': 'javascript:document.write("hello");'}, 961 {'url': 'javascript:document.write("hello");'},
991 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 962 chrome.test.callbackFail(ERROR_INVALID_URL));
992 }, 963 },
993 964
994 function downloadInvalidURL8() { 965 function downloadInvalidURL8() {
995 // Test that download() rejects javascript urls. 966 // Test that download() rejects javascript urls.
996 downloads.download( 967 downloads.download(
997 {'url': 'javascript:return false;'}, 968 {'url': 'javascript:return false;'},
998 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 969 chrome.test.callbackFail(ERROR_INVALID_URL));
999 }, 970 },
1000 971
1001 function downloadInvalidURL9() { 972 function downloadInvalidURL9() {
1002 // Test that download() rejects otherwise-valid URLs that fail the host 973 // Test that download() rejects otherwise-valid URLs that fail the host
1003 // permissions check. 974 // permissions check.
1004 downloads.download( 975 downloads.download(
1005 {'url': 'ftp://example.com/example.txt'}, 976 {'url': 'ftp://example.com/example.txt'},
1006 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); 977 chrome.test.callbackFail(ERROR_INVALID_URL));
1007 }, 978 },
1008 979
1009 // TODO(benjhayden): Set up a test ftp server, add ftp://localhost* to 980 // TODO(benjhayden): Set up a test ftp server, add ftp://localhost* to
1010 // permissions, maybe update downloadInvalidURL9. 981 // permissions, maybe update downloadInvalidURL9.
1011 // function downloadAllowFTPURLs() { 982 // function downloadAllowFTPURLs() {
1012 // // Valid ftp URLs are valid URLs. 983 // // Valid ftp URLs are valid URLs.
1013 // var downloadId = getNextId(); 984 // var downloadId = getNextId();
1014 // console.debug(downloadId); 985 // console.debug(downloadId);
1015 // downloads.download( 986 // downloads.download(
1016 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, 987 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'},
1017 // chrome.test.callback(function(id) { 988 // chrome.test.callback(function(id) {
1018 // chrome.test.assertEq(downloadId, id); 989 // chrome.test.assertEq(downloadId, id);
1019 // })); 990 // }));
1020 // }, 991 // },
1021 992
1022 function downloadInvalidMethod() { 993 function downloadInvalidMethod() {
1023 assertThrows(('Invalid value for argument 1. Property \'method\': ' + 994 assertThrows(('Invalid value for argument 1. Property \'method\': ' +
1024 'Value must be one of: [GET, POST].'), 995 'Value must be one of: [GET, POST].'),
1025 downloads.download, 996 downloads.download,
1026 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); 997 {'url': SAFE_FAST_URL, 'method': 'GOAT'});
1027 }, 998 },
1028 999
1029 function downloadInvalidHeader() { 1000 function downloadInvalidHeader() {
1030 // Test that download() disallows setting the Cookie header. 1001 // Test that download() disallows setting the Cookie header.
1031 downloads.download( 1002 downloads.download(
1032 {'url': SAFE_FAST_URL, 1003 {'url': SAFE_FAST_URL,
1033 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] 1004 'headers': [{ 'name': 'Cookie', 'value': 'fake'}]
1034 }, 1005 },
1035 chrome.test.callbackFail(downloads.ERROR_GENERIC)); 1006 chrome.test.callbackFail(ERROR_GENERIC));
1036 }, 1007 },
1037 1008
1038 function downloadGetFileIconInvalidOptions() { 1009 function downloadGetFileIconInvalidOptions() {
1039 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + 1010 assertThrows(('Invalid value for argument 2. Property \'cat\': ' +
1040 'Unexpected property.'), 1011 'Unexpected property.'),
1041 downloads.getFileIcon, 1012 downloads.getFileIcon,
1042 -1, {cat: 'mouse'}); 1013 -1, {cat: 'mouse'});
1043 }, 1014 },
1044 1015
1045 function downloadGetFileIconInvalidSize() { 1016 function downloadGetFileIconInvalidSize() {
1046 assertThrows(('Invalid value for argument 2. Property \'size\': ' + 1017 assertThrows(('Invalid value for argument 2. Property \'size\': ' +
1047 'Value must be one of: [16, 32].'), 1018 'Value must be one of: [16, 32].'),
1048 downloads.getFileIcon, -1, {size: 31}); 1019 downloads.getFileIcon, -1, {size: 31});
1049 }, 1020 },
1050 1021
1051 function downloadGetFileIconInvalidId() { 1022 function downloadGetFileIconInvalidId() {
1052 downloads.getFileIcon(-42, {size: 32}, 1023 downloads.getFileIcon(-42, {size: 32},
1053 chrome.test.callbackFail(downloads.ERROR_INVALID_OPERATION)); 1024 chrome.test.callbackFail(ERROR_INVALID_OPERATION));
1054 }, 1025 },
1055 1026
1056 function downloadPauseInvalidId() { 1027 function downloadPauseInvalidId() {
1057 downloads.pause(-42, chrome.test.callbackFail( 1028 downloads.pause(-42, chrome.test.callbackFail(
1058 downloads.ERROR_INVALID_OPERATION)); 1029 ERROR_INVALID_OPERATION));
1059 }, 1030 },
1060 1031
1061 function downloadPauseInvalidType() { 1032 function downloadPauseInvalidType() {
1062 assertThrows(('Invocation of form experimental.downloads.pause(string,' + 1033 assertThrows(('Invocation of form downloads.pause(string, function) ' +
1063 ' function) doesn\'t match definition experimental.' + 1034 'doesn\'t match definition downloads.pause(integer ' +
1064 'downloads.pause(integer id, optional function callback)'), 1035 'downloadId, optional function NullCallback)'),
1065 downloads.pause, 1036 downloads.pause,
1066 'foo'); 1037 'foo');
1067 }, 1038 },
1068 1039
1069 function downloadResumeInvalidId() { 1040 function downloadResumeInvalidId() {
1070 downloads.resume(-42, chrome.test.callbackFail( 1041 downloads.resume(-42, chrome.test.callbackFail(
1071 downloads.ERROR_INVALID_OPERATION)); 1042 ERROR_INVALID_OPERATION));
1072 }, 1043 },
1073 1044
1074 function downloadResumeInvalidType() { 1045 function downloadResumeInvalidType() {
1075 assertThrows(('Invocation of form experimental.downloads.resume(string,' + 1046 assertThrows(('Invocation of form downloads.resume(string, function) ' +
1076 ' function) doesn\'t match definition experimental.' + 1047 'doesn\'t match definition downloads.resume(integer ' +
1077 'downloads.resume(integer id, optional function callback)'), 1048 'downloadId, optional function NullCallback)'),
1078 downloads.resume, 1049 downloads.resume,
1079 'foo'); 1050 'foo');
1080 }, 1051 },
1081 1052
1082 function downloadCancelInvalidId() { 1053 function downloadCancelInvalidId() {
1083 // Canceling a non-existent download is not considered an error. 1054 // Canceling a non-existent download is not considered an error.
1084 downloads.cancel(-42, chrome.test.callback(function() { 1055 downloads.cancel(-42, chrome.test.callback(function() {
1085 console.debug(''); 1056 console.debug('');
1086 })); 1057 }));
1087 }, 1058 },
1088 1059
1089 function downloadCancelInvalidType() { 1060 function downloadCancelInvalidType() {
1090 assertThrows(('Invocation of form experimental.downloads.cancel(string,' + 1061 assertThrows(('Invocation of form downloads.cancel(string, function) ' +
1091 ' function) doesn\'t match definition experimental.' + 1062 'doesn\'t match definition downloads.cancel(integer ' +
1092 'downloads.cancel(integer id, optional function callback)'), 1063 'downloadId, optional function NullCallback)'),
1093 downloads.cancel, 'foo'); 1064 downloads.cancel, 'foo');
1094 }, 1065 },
1095 1066
1096 function downloadNoComplete() { 1067 function downloadNoComplete() {
1097 // This is used partly to test cleanUp. 1068 // This is used partly to test cleanUp.
1098 var downloadId = getNextId(); 1069 var downloadId = getNextId();
1099 console.debug(downloadId); 1070 console.debug(downloadId);
1100 downloads.download( 1071 downloads.download(
1101 {'url': NEVER_FINISH_URL}, 1072 {'url': NEVER_FINISH_URL},
1102 chrome.test.callback(function(id) { 1073 chrome.test.callback(function(id) {
(...skipping 17 matching lines...) Expand all
1120 }, 1091 },
1121 1092
1122 function callNotifyPass() { 1093 function callNotifyPass() {
1123 chrome.test.notifyPass(); 1094 chrome.test.notifyPass();
1124 setTimeout(chrome.test.callback(function() { 1095 setTimeout(chrome.test.callback(function() {
1125 console.debug(''); 1096 console.debug('');
1126 }), 0); 1097 }), 0);
1127 } 1098 }
1128 ]); 1099 ]);
1129 }); 1100 });
OLDNEW
« no previous file with comments | « chrome/test/data/extensions/api_test/downloads/manifest.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698