Chromium Code Reviews| 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 24 matching lines...) Expand all Loading... | |
| 35 }); | 35 }); |
| 36 func.apply(null, args); | 36 func.apply(null, args); |
| 37 } catch (exception) { | 37 } catch (exception) { |
| 38 chrome.test.assertEq(exceptionMessage, exception.message); | 38 chrome.test.assertEq(exceptionMessage, exception.message); |
| 39 chrome.test.succeed(); | 39 chrome.test.succeed(); |
| 40 } | 40 } |
| 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 const SAFE_FAST_URL = getURL('slow?0'); |
|
asanka
2012/02/13 19:45:47
Avoid 'const', here and elsewhere. See http://crbu
benjhayden
2012/02/13 21:05:31
Done.
The style guide suggested that the reason t
| |
| 46 var NEVER_FINISH_URL = getURL('download-known-size'); | 46 |
| 47 var ERROR_GENERIC = downloads.ERROR_GENERIC; | 47 const NEVER_FINISH_URL = getURL('download-known-size'); |
| 48 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; | 48 |
| 49 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; | 49 // This URL should only work with the POST method and a request body |
| 50 // containing 'BODY'. | |
| 51 const POST_URL = getURL('files/post/downloads/a_zip_file.zip?' + | |
| 52 'expected_body=BODY'); | |
| 53 | |
| 54 // This URL should only work with headers 'Foo: bar' and 'Qx: yo'. | |
| 55 const HEADERS_URL = getURL('files/downloads/a_zip_file.zip?' + | |
| 56 'expected_headers=Foo:bar&expected_headers=Qx:yo'); | |
| 50 | 57 |
| 51 chrome.test.runTests([ | 58 chrome.test.runTests([ |
| 52 // TODO(benjhayden): Test onErased using remove(). | 59 // TODO(benjhayden): Test onErased using remove(). |
| 60 | |
| 61 // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443 | |
| 62 // TODO(benjhayden): Windows slashes. | |
| 63 // function downloadSubDirectoryFilename() { | |
| 64 // const downloadId = getNextId(); | |
| 65 // var callbackCompleted = chrome.test.callbackAdded(); | |
| 66 // function myListener(delta) { | |
| 67 // if ((delta.id != downloadId) || | |
| 68 // !delta.filename || | |
| 69 // (delta.filename.new.indexOf('/foo/slow') == -1)) | |
| 70 // return; | |
| 71 // downloads.onChanged.removeListener(myListener); | |
| 72 // callbackCompleted(); | |
| 73 // } | |
| 74 // downloads.onChanged.addListener(myListener); | |
| 75 // downloads.download( | |
| 76 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | |
| 77 // chrome.test.callback(function(id) { | |
| 78 // chrome.test.assertEq(downloadId, id); | |
| 79 // })); | |
| 80 // }, | |
| 81 | |
| 82 function downloadSimple() { | |
| 83 // Test that we can begin a download. | |
| 84 const downloadId = getNextId(); | |
| 85 downloads.download( | |
| 86 {'url': SAFE_FAST_URL}, | |
| 87 chrome.test.callback(function(id) { | |
| 88 chrome.test.assertEq(downloadId, id); | |
| 89 })); | |
| 90 }, | |
| 91 | |
| 92 function downloadPostSuccess() { | |
| 93 // Test the |method| download option. | |
| 94 const downloadId = getNextId(); | |
| 95 var changedCompleted = chrome.test.callbackAdded(); | |
| 96 function changedListener(delta) { | |
| 97 // Ignore onChanged events for downloads besides our own, or events that | |
| 98 // signal any change besides completion. | |
| 99 if ((delta.id != downloadId) || | |
| 100 !delta.state || | |
| 101 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 102 return; | |
| 103 downloads.search({id: downloadId}, | |
| 104 chrome.test.callback(function(items) { | |
| 105 chrome.test.assertEq(1, items.length); | |
| 106 chrome.test.assertEq(downloadId, items[0].id); | |
| 107 const EXPECTED_SIZE = 164; | |
| 108 chrome.test.assertEq(EXPECTED_SIZE, items[0].totalBytes); | |
| 109 chrome.test.assertEq(EXPECTED_SIZE, items[0].fileSize); | |
| 110 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived); | |
| 111 })); | |
| 112 downloads.onChanged.removeListener(changedListener); | |
| 113 changedCompleted(); | |
| 114 } | |
| 115 downloads.onChanged.addListener(changedListener); | |
| 116 | |
| 117 downloads.download( | |
| 118 {'url': POST_URL, | |
| 119 'method': 'POST', | |
| 120 'filename': downloadId + '.txt', // Prevent 'file' danger. | |
| 121 'body': 'BODY'}, | |
| 122 chrome.test.callback(function(id) { | |
| 123 chrome.test.assertEq(downloadId, id); | |
| 124 })); | |
| 125 }, | |
| 126 | |
| 127 function downloadPostWouldFailWithoutMethod() { | |
| 128 // Test that downloadPostSuccess would fail if the resource requires the | |
| 129 // POST method, and chrome fails to propagate the |method| parameter back | |
| 130 // to the server. This tests both that testserver.py does not succeed when | |
| 131 // it should fail, and this tests how the downloads extension api exposes | |
| 132 // the failure to extensions. | |
| 133 const downloadId = getNextId(); | |
| 134 | |
| 135 var changedCompleted = chrome.test.callbackAdded(); | |
| 136 function changedListener(delta) { | |
| 137 // Ignore onChanged events for downloads besides our own, or events that | |
| 138 // signal any change besides interruption. | |
| 139 if ((delta.id != downloadId) || | |
| 140 !delta.state || | |
| 141 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 142 return; | |
| 143 // TODO(benjhayden): Change COMPLETE to INTERRUPTED after | |
| 144 // http://crbug.com/112342 | |
| 145 downloads.search({id: downloadId}, | |
| 146 chrome.test.callback(function(items) { | |
| 147 chrome.test.assertEq(1, items.length); | |
| 148 chrome.test.assertEq(downloadId, items[0].id); | |
| 149 chrome.test.assertEq(0, items[0].totalBytes); | |
| 150 })); | |
| 151 downloads.onChanged.removeListener(changedListener); | |
| 152 changedCompleted(); | |
| 153 } | |
| 154 downloads.onChanged.addListener(changedListener); | |
| 155 | |
| 156 downloads.download( | |
| 157 {'url': POST_URL, | |
| 158 'filename': downloadId + '.txt', // Prevent 'file' danger. | |
| 159 'body': 'BODY'}, | |
| 160 chrome.test.callback(function(id) { | |
| 161 chrome.test.assertEq(downloadId, id); | |
| 162 })); | |
| 163 }, | |
| 164 | |
| 165 function downloadPostWouldFailWithoutBody() { | |
| 166 // Test that downloadPostSuccess would fail if the resource requires the | |
| 167 // POST method and a request body, and chrome fails to propagate the | |
| 168 // |body| parameter back to the server. This tests both that testserver.py | |
| 169 // does not succeed when it should fail, and this tests how the downloads | |
| 170 // extension api exposes the failure to extensions. | |
| 171 const downloadId = getNextId(); | |
| 172 | |
| 173 var changedCompleted = chrome.test.callbackAdded(); | |
| 174 function changedListener(delta) { | |
| 175 // Ignore onChanged events for downloads besides our own, or events that | |
| 176 // signal any change besides interruption. | |
| 177 if ((delta.id != downloadId) || | |
| 178 !delta.state || | |
| 179 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 180 return; | |
| 181 // TODO(benjhayden): Change COMPLETE to INTERRUPTED after | |
| 182 // http://crbug.com/112342 | |
| 183 downloads.search({id: downloadId}, | |
| 184 chrome.test.callback(function(items) { | |
| 185 chrome.test.assertEq(1, items.length); | |
| 186 chrome.test.assertEq(downloadId, items[0].id); | |
| 187 chrome.test.assertEq(0, items[0].totalBytes); | |
| 188 })); | |
| 189 downloads.onChanged.removeListener(changedListener); | |
| 190 changedCompleted(); | |
| 191 } | |
| 192 downloads.onChanged.addListener(changedListener); | |
| 193 | |
| 194 downloads.download( | |
| 195 {'url': POST_URL, | |
| 196 'filename': downloadId + '.txt', // Prevent 'file' danger. | |
| 197 'method': 'POST'}, | |
| 198 chrome.test.callback(function(id) { | |
| 199 chrome.test.assertEq(downloadId, id); | |
| 200 })); | |
| 201 }, | |
| 202 | |
| 203 function downloadHeadersSuccess() { | |
| 204 // Test the |header| download option. | |
| 205 const downloadId = getNextId(); | |
| 206 var changedCompleted = chrome.test.callbackAdded(); | |
| 207 function changedListener(delta) { | |
| 208 // Ignore onChanged events for downloads besides our own, or events that | |
| 209 // signal any change besides completion. | |
| 210 if ((delta.id != downloadId) || | |
| 211 !delta.state || | |
| 212 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 213 return; | |
| 214 downloads.search({id: downloadId}, | |
| 215 chrome.test.callback(function(items) { | |
| 216 chrome.test.assertEq(1, items.length); | |
| 217 chrome.test.assertEq(downloadId, items[0].id); | |
| 218 const EXPECTED_SIZE = 164; | |
| 219 chrome.test.assertEq(EXPECTED_SIZE, items[0].totalBytes); | |
| 220 chrome.test.assertEq(EXPECTED_SIZE, items[0].fileSize); | |
| 221 chrome.test.assertEq(EXPECTED_SIZE, items[0].bytesReceived); | |
| 222 })); | |
| 223 downloads.onChanged.removeListener(changedListener); | |
| 224 changedCompleted(); | |
| 225 } | |
| 226 downloads.onChanged.addListener(changedListener); | |
| 227 | |
| 228 downloads.download( | |
| 229 {'url': HEADERS_URL, | |
| 230 'filename': downloadId + '.txt', // Prevent 'file' danger. | |
| 231 'headers': [{'name': 'Foo', 'value': 'bar'}, | |
| 232 {'name': 'Qx', 'value': 'yo'}]}, | |
| 233 chrome.test.callback(function(id) { | |
| 234 chrome.test.assertEq(downloadId, id); | |
| 235 })); | |
| 236 }, | |
| 237 | |
| 238 function downloadHeadersWouldFail() { | |
| 239 // Test that downloadHeadersSuccess() would fail if the resource requires | |
| 240 // the headers, and chrome fails to propagate them back to the server. | |
| 241 // This tests both that testserver.py does not succeed when it should | |
| 242 // fail as well as how the downloads extension api exposes the | |
| 243 // failure to extensions. | |
| 244 const downloadId = getNextId(); | |
| 245 | |
| 246 var changedCompleted = chrome.test.callbackAdded(); | |
| 247 function changedListener(delta) { | |
| 248 // Ignore onChanged events for downloads besides our own, or events that | |
| 249 // signal any change besides interruption. | |
| 250 if ((delta.id != downloadId) || | |
| 251 !delta.state || | |
| 252 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 253 return; | |
| 254 // TODO(benjhayden): Change COMPLETE to INTERRUPTED after | |
| 255 // http://crbug.com/112342 | |
| 256 downloads.search({id: downloadId}, | |
| 257 chrome.test.callback(function(items) { | |
| 258 chrome.test.assertEq(1, items.length); | |
| 259 chrome.test.assertEq(downloadId, items[0].id); | |
| 260 chrome.test.assertEq(0, items[0].totalBytes); | |
| 261 })); | |
| 262 downloads.onChanged.removeListener(changedListener); | |
| 263 changedCompleted(); | |
| 264 } | |
| 265 downloads.onChanged.addListener(changedListener); | |
| 266 | |
| 267 downloads.download( | |
| 268 {'url': HEADERS_URL}, | |
| 269 chrome.test.callback(function(id) { | |
| 270 chrome.test.assertEq(downloadId, id); | |
| 271 })); | |
| 272 }, | |
| 273 | |
| 274 function downloadInterrupted() { | |
| 275 // Test that cancel()ing an in-progress download causes its state to | |
| 276 // transition to interrupted, and test that that state transition is | |
| 277 // detectable by an onChanged event listener. | |
| 278 // TODO(benjhayden): Test other sources of interruptions such as server | |
| 279 // death. | |
| 280 const downloadId = getNextId(); | |
| 281 | |
| 282 var createdCompleted = chrome.test.callbackAdded(); | |
| 283 function createdListener(createdItem) { | |
| 284 // Ignore onCreated events for any download besides our own. | |
| 285 if (createdItem.id != downloadId) | |
| 286 return; | |
| 287 // TODO(benjhayden) Move this cancel() into the download() callback | |
| 288 // after ensuring that DownloadItems are created before that callback | |
| 289 // is fired. | |
| 290 downloads.cancel(downloadId, chrome.test.callback(function() { | |
| 291 })); | |
| 292 downloads.onCreated.removeListener(createdListener); | |
| 293 createdCompleted(); | |
| 294 } | |
| 295 downloads.onCreated.addListener(createdListener); | |
| 296 | |
| 297 var changedCompleted = chrome.test.callbackAdded(); | |
| 298 function changedListener(delta) { | |
| 299 // Ignore onChanged events for downloads besides our own, or events that | |
| 300 // signal any change besides interruption. | |
| 301 if ((delta.id != downloadId) || | |
| 302 !delta.state || | |
| 303 (delta.state.new != downloads.STATE_INTERRUPTED)) | |
| 304 return; | |
|
asanka
2012/02/13 19:45:47
Nit: Check delta.error.new?
benjhayden
2012/02/13 21:05:31
Done.
| |
| 305 downloads.onChanged.removeListener(changedListener); | |
| 306 changedCompleted(); | |
| 307 } | |
| 308 downloads.onChanged.addListener(changedListener); | |
| 309 | |
| 310 downloads.download( | |
| 311 {'url': NEVER_FINISH_URL}, | |
| 312 chrome.test.callback(function(id) { | |
| 313 chrome.test.assertEq(downloadId, id); | |
| 314 })); | |
| 315 }, | |
| 316 | |
| 317 function downloadOnChanged() { | |
| 318 // Test that download completion is detectable by an onChanged event | |
| 319 // listener. | |
| 320 const downloadId = getNextId(); | |
| 321 var callbackCompleted = chrome.test.callbackAdded(); | |
| 322 function myListener(delta) { | |
| 323 if ((delta.id != downloadId) || | |
| 324 !delta.state || | |
| 325 (delta.state.new != downloads.STATE_COMPLETE)) | |
| 326 return; | |
| 327 downloads.onChanged.removeListener(myListener); | |
| 328 callbackCompleted(); | |
| 329 } | |
| 330 downloads.onChanged.addListener(myListener); | |
| 331 downloads.download( | |
| 332 {"url": SAFE_FAST_URL}, | |
| 333 chrome.test.callback(function(id) { | |
| 334 chrome.test.assertEq(downloadId, id); | |
| 335 })); | |
| 336 }, | |
| 337 | |
| 53 function downloadFilename() { | 338 function downloadFilename() { |
| 54 downloads.download( | 339 // Test that we can suggest a filename for a new download, and test that |
| 55 {'url': SAFE_FAST_URL, 'filename': 'foo'}, | 340 // we can detect filename changes with an onChanged event listener. |
| 56 chrome.test.callbackPass(function(id) { | 341 const FILENAME = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi'; |
| 57 chrome.test.assertEq(getNextId(), id); | 342 const downloadId = getNextId(); |
| 58 })); | 343 var callbackCompleted = chrome.test.callbackAdded(); |
| 59 // TODO(benjhayden): Test the filename using onChanged. | 344 function myListener(delta) { |
| 60 }, | 345 if ((delta.id != downloadId) || |
| 346 !delta.filename || | |
| 347 (delta.filename.new.indexOf(FILENAME) == -1)) | |
|
asanka
2012/02/13 19:45:47
Is .filename intended to be the target (final) pat
benjhayden
2012/02/13 21:05:31
Yeah, there might be some more discussion to be ha
| |
| 348 return; | |
| 349 downloads.onChanged.removeListener(myListener); | |
| 350 callbackCompleted(); | |
| 351 } | |
| 352 downloads.onChanged.addListener(myListener); | |
| 353 downloads.download( | |
| 354 {'url': SAFE_FAST_URL, 'filename': FILENAME}, | |
| 355 chrome.test.callback(function(id) { | |
| 356 chrome.test.assertEq(downloadId, id); | |
| 357 })); | |
| 358 }, | |
| 359 | |
| 61 function downloadOnCreated() { | 360 function downloadOnCreated() { |
| 62 chrome.test.listenOnce(downloads.onCreated, | 361 // Test that the onCreated event fires when we start a download. |
| 63 chrome.test.callbackPass(function(item) {})); | 362 const downloadId = getNextId(); |
| 363 var createdCompleted = chrome.test.callbackAdded(); | |
| 364 function createdListener(item) { | |
| 365 if (item.id == downloadId) { | |
| 366 createdCompleted(); | |
| 367 downloads.onCreated.removeListener(createdListener); | |
| 368 } | |
| 369 }; | |
| 370 downloads.onCreated.addListener(createdListener); | |
| 64 downloads.download( | 371 downloads.download( |
| 65 {'url': SAFE_FAST_URL}, | 372 {'url': SAFE_FAST_URL}, |
| 66 function(id) { | 373 chrome.test.callback(function(id) { |
| 67 chrome.test.assertEq(getNextId(), id); | 374 chrome.test.assertEq(downloadId, id); |
| 68 }); | 375 })); |
| 69 }, | 376 }, |
| 70 function downloadSubDirectoryFilename() { | 377 |
| 71 downloads.download( | |
| 72 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | |
| 73 chrome.test.callbackPass(function(id) { | |
| 74 chrome.test.assertEq(getNextId(), id); | |
| 75 })); | |
| 76 // TODO(benjhayden): Test the filename using onChanged. | |
| 77 }, | |
| 78 function downloadInvalidFilename() { | 378 function downloadInvalidFilename() { |
| 379 // Test that we disallow invalid filenames for new downloads. | |
| 79 downloads.download( | 380 downloads.download( |
| 80 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, | 381 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
| 81 chrome.test.callbackFail(ERROR_GENERIC)); | 382 chrome.test.callbackFail(downloads.ERROR_GENERIC)); |
| 82 // TODO(benjhayden): Give a better error message. | 383 }, |
| 83 }, | 384 |
| 84 function downloadEmpty() { | 385 function downloadEmpty() { |
| 85 assertThrows(('Invalid value for argument 1. Property \'url\': ' + | 386 assertThrows(('Invalid value for argument 1. Property \'url\': ' + |
| 86 'Property is required.'), | 387 'Property is required.'), |
| 87 downloads.download, {}); | 388 downloads.download, {}); |
| 88 }, | 389 }, |
| 390 | |
| 89 function downloadInvalidSaveAs() { | 391 function downloadInvalidSaveAs() { |
| 90 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + | 392 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + |
| 91 'Expected \'boolean\' but got \'string\'.'), | 393 'Expected \'boolean\' but got \'string\'.'), |
| 92 downloads.download, | 394 downloads.download, |
| 93 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); | 395 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); |
| 94 }, | 396 }, |
| 397 | |
| 95 function downloadInvalidHeadersOption() { | 398 function downloadInvalidHeadersOption() { |
| 96 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + | 399 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + |
| 97 'Expected \'array\' but got \'string\'.'), | 400 'Expected \'array\' but got \'string\'.'), |
| 98 downloads.download, | 401 downloads.download, |
| 99 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); | 402 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
| 100 }, | 403 }, |
| 404 | |
| 101 function downloadInvalidURL() { | 405 function downloadInvalidURL() { |
| 406 // Test that download() requires a valid url. | |
| 102 downloads.download( | 407 downloads.download( |
| 103 {'url': 'foo bar'}, | 408 {'url': 'foo bar'}, |
| 104 chrome.test.callbackFail(ERROR_INVALID_URL)); | 409 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); |
| 105 }, | 410 }, |
| 411 | |
| 106 function downloadInvalidMethod() { | 412 function downloadInvalidMethod() { |
| 107 assertThrows(('Invalid value for argument 1. Property \'method\': ' + | 413 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
| 108 'Value must be one of: [GET, POST].'), | 414 'Value must be one of: [GET, POST].'), |
| 109 downloads.download, | 415 downloads.download, |
| 110 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); | 416 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
| 111 }, | 417 }, |
| 112 function downloadSimple() { | 418 |
| 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() { | 419 function downloadInvalidHeader() { |
| 420 // Test that download() disallows setting the Cookie header. | |
| 147 downloads.download( | 421 downloads.download( |
| 148 {'url': SAFE_FAST_URL, | 422 {'url': SAFE_FAST_URL, |
| 149 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] | 423 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] |
| 150 }, | 424 }, |
| 151 chrome.test.callbackFail(ERROR_GENERIC)); | 425 chrome.test.callbackFail(downloads.ERROR_GENERIC)); |
| 152 // TODO(benjhayden): Give a better error message. | 426 }, |
| 153 }, | 427 |
| 154 function downloadGetFileIconInvalidOptions() { | 428 function downloadGetFileIconInvalidOptions() { |
| 155 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + | 429 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + |
| 156 'Unexpected property.'), | 430 'Unexpected property.'), |
| 157 downloads.getFileIcon, | 431 downloads.getFileIcon, |
| 158 -1, {cat: 'mouse'}); | 432 -1, {cat: 'mouse'}); |
| 159 }, | 433 }, |
| 434 | |
| 160 function downloadGetFileIconInvalidSize() { | 435 function downloadGetFileIconInvalidSize() { |
| 161 assertThrows(('Invalid value for argument 2. Property \'size\': ' + | 436 assertThrows(('Invalid value for argument 2. Property \'size\': ' + |
| 162 'Value must be one of: [16, 32].'), | 437 'Value must be one of: [16, 32].'), |
| 163 downloads.getFileIcon, -1, {size: 31}); | 438 downloads.getFileIcon, -1, {size: 31}); |
| 164 }, | 439 }, |
| 440 | |
| 165 function downloadGetFileIconInvalidId() { | 441 function downloadGetFileIconInvalidId() { |
| 166 downloads.getFileIcon(-42, {size: 32}, | 442 downloads.getFileIcon(-42, {size: 32}, |
| 167 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 443 chrome.test.callbackFail(downloads.ERROR_INVALID_OPERATION)); |
| 168 }, | 444 }, |
| 169 function downloadNoComplete() { | 445 |
| 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() { | 446 function downloadPauseInvalidId() { |
| 178 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 447 downloads.pause(-42, chrome.test.callbackFail( |
| 179 }, | 448 downloads.ERROR_INVALID_OPERATION)); |
| 449 }, | |
| 450 | |
| 180 function downloadPauseInvalidType() { | 451 function downloadPauseInvalidType() { |
| 181 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 452 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 182 'but got \'string\'.'), | 453 'but got \'string\'.'), |
| 183 downloads.pause, | 454 downloads.pause, |
| 184 'foo'); | 455 'foo'); |
| 185 }, | 456 }, |
| 457 | |
| 186 function downloadResumeInvalidId() { | 458 function downloadResumeInvalidId() { |
| 187 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 459 downloads.resume(-42, chrome.test.callbackFail( |
| 188 }, | 460 downloads.ERROR_INVALID_OPERATION)); |
| 461 }, | |
| 462 | |
| 189 function downloadResumeInvalidType() { | 463 function downloadResumeInvalidType() { |
| 190 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 464 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 191 'but got \'string\'.'), | 465 'but got \'string\'.'), |
| 192 downloads.resume, | 466 downloads.resume, |
| 193 'foo'); | 467 'foo'); |
| 194 }, | 468 }, |
| 469 | |
| 195 function downloadCancelInvalidId() { | 470 function downloadCancelInvalidId() { |
| 196 // Canceling a non-existent download is not considered an error. | 471 // Canceling a non-existent download is not considered an error. |
| 197 downloads.cancel(-42, chrome.test.callbackPass(function() {})); | 472 downloads.cancel(-42, chrome.test.callback(function() {})); |
| 198 }, | 473 }, |
| 474 | |
| 199 function downloadCancelInvalidType() { | 475 function downloadCancelInvalidType() { |
| 200 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 476 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 201 'but got \'string\'.'), | 477 'but got \'string\'.'), |
| 202 downloads.cancel, 'foo'); | 478 downloads.cancel, 'foo'); |
| 203 }, | 479 }, |
| 480 | |
| 481 function downloadNoComplete() { | |
| 482 // This is used partly to test cleanUp. | |
| 483 const downloadId = getNextId(); | |
| 484 downloads.download( | |
| 485 {'url': NEVER_FINISH_URL}, | |
| 486 chrome.test.callback(function(id) { | |
| 487 chrome.test.assertEq(downloadId, id); | |
| 488 })); | |
| 489 }, | |
| 490 | |
| 204 function cleanUp() { | 491 function cleanUp() { |
| 205 // cleanUp must come last. It clears out all in-progress downloads | 492 // cleanUp must come last. It clears out all in-progress downloads |
| 206 // so the browser can shutdown cleanly. | 493 // so the browser can shutdown cleanly. |
| 207 for (var id = 0; id < nextId; ++id) { | 494 for (var id = 0; id < nextId; ++id) { |
| 208 downloads.cancel(id, chrome.test.callbackPass(function() {})); | 495 downloads.cancel(id, chrome.test.callback(function() {})); |
| 209 } | 496 } |
| 210 } | 497 } |
| 211 ]); | 498 ]); |
| 212 }); | 499 }); |
| OLD | NEW |