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 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."); | |
|
Randy Smith (Not in Mondays)
2012/02/02 18:10:07
Is there really no way for us to check for this pr
benjhayden
2012/02/02 20:40:38
chrome.test already tests it programmatically by a
Randy Smith (Not in Mondays)
2012/02/03 20:02:00
Would it satisfy your goals to change the above tw
benjhayden
2012/02/10 18:53:07
I think that if I saw test output as verbose as th
Randy Smith (Not in Mondays)
2012/02/13 16:13:08
It looks like you've nuked the logging; was that i
benjhayden
2012/02/13 19:01:31
Chris said that it was generally not acceptable.
| |
| 60 | |
| 51 chrome.test.runTests([ | 61 chrome.test.runTests([ |
|
Randy Smith (Not in Mondays)
2012/02/02 18:10:07
Could you put in a short comment about what these
benjhayden
2012/02/02 20:40:38
Um, you might want to have a look at download_exte
Randy Smith (Not in Mondays)
2012/02/03 20:02:00
I think I didn't read as closely as I should have.
benjhayden
2012/02/10 18:53:07
Done.
| |
| 62 function downloadSimple() { | |
| 63 var download_id = getNextId(); | |
| 64 console.log("id: " + download_id); | |
| 65 downloads.download( | |
| 66 {'url': SAFE_FAST_URL}, | |
| 67 chrome.test.callbackPass(function(id) { | |
| 68 console.log("id: " + download_id); | |
| 69 chrome.test.assertEq(download_id, id); | |
| 70 })); | |
| 71 }, | |
| 72 function downloadPost() { | |
| 73 var download_id = getNextId(); | |
| 74 console.log("id: " + download_id); | |
| 75 downloads.download( | |
| 76 {'url': getURL('files/post/downloads/a_zip_file.js'), | |
| 77 'method': 'POST', | |
| 78 'body': 'WOOHOO'}, | |
| 79 chrome.test.callbackPass(function(id) { | |
| 80 console.log("id: " + download_id); | |
| 81 chrome.test.assertEq(download_id, id); | |
| 82 })); | |
| 83 }, | |
| 84 function downloadHeader() { | |
| 85 var download_id = getNextId(); | |
| 86 console.log("id: " + download_id); | |
| 87 downloads.download( | |
| 88 {'url': SAFE_FAST_URL, | |
| 89 'headers': [{'name': 'Foo', 'value': 'bar'}] | |
| 90 }, | |
| 91 chrome.test.callbackPass(function(id) { | |
| 92 console.log("id: " + download_id); | |
| 93 chrome.test.assertEq(download_id, id); | |
| 94 })); | |
| 95 }, | |
| 96 function downloadInterrupted() { | |
| 97 var download_id = getNextId(); | |
| 98 console.log("id: " + download_id); | |
| 99 | |
| 100 var createdCompleted = chrome.test.callbackAdded(); | |
| 101 function createdListener(created_item) { | |
| 102 console.log("created_id: " + created_item.id); | |
| 103 if (created_item.id != download_id) | |
| 104 return; | |
| 105 // TODO(benjhayden) Move this cancel() into the download() callback | |
| 106 // after ensuring that DownloadItems are created before that callback | |
| 107 // is fired. | |
| 108 downloads.cancel(download_id, chrome.test.callback(function() { | |
| 109 console.log("id: " + download_id); | |
| 110 })); | |
| 111 downloads.onCreated.removeListener(createdListener); | |
| 112 createdCompleted(); | |
| 113 } | |
| 114 downloads.onCreated.addListener(createdListener); | |
| 115 | |
| 116 var changedCompleted = chrome.test.callbackAdded(); | |
| 117 function changedListener(delta) { | |
| 118 console.log("id: " + delta.id); | |
| 119 if ((delta.id == download_id) && | |
| 120 delta.state && | |
| 121 (delta.state.new == 'interrupted')) { | |
| 122 console.log("id: " + delta.id); | |
| 123 downloads.onChanged.removeListener(changedListener); | |
| 124 changedCompleted(); | |
| 125 } | |
| 126 } | |
| 127 downloads.onChanged.addListener(changedListener); | |
| 128 | |
| 129 downloads.download( | |
| 130 {'url': NEVER_FINISH_URL}, | |
| 131 chrome.test.callback(function(id) { | |
| 132 console.log("id: " + download_id); | |
| 133 chrome.test.assertEq(download_id, id); | |
| 134 })); | |
| 135 }, | |
| 52 // TODO(benjhayden): Test onErased using remove(). | 136 // TODO(benjhayden): Test onErased using remove(). |
| 137 function downloadOnChanged() { | |
| 138 var download_id = getNextId(); | |
| 139 console.log("id: " + download_id); | |
| 140 var callbackCompleted = chrome.test.callbackAdded(); | |
| 141 console.log("id: " + download_id); | |
| 142 function myListener(delta) { | |
| 143 console.log("id: " + delta.id); | |
| 144 if (delta.state) console.log("state: " + delta.state.new); | |
| 145 if (delta.state && delta.state.new == 'complete') { | |
| 146 downloads.onChanged.removeListener(myListener); | |
| 147 callbackCompleted(); | |
| 148 } | |
| 149 } | |
| 150 downloads.onChanged.addListener(myListener); | |
| 151 downloads.download( | |
| 152 {"url": getURL("slow?0")}, | |
| 153 chrome.test.callback(function(id) { | |
| 154 console.log("id: " + download_id); | |
| 155 chrome.test.assertEq(download_id, id); | |
| 156 })); | |
| 157 }, | |
| 53 function downloadFilename() { | 158 function downloadFilename() { |
| 159 const kFilename = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi'; | |
| 160 var download_id = getNextId(); | |
| 161 console.log("id: " + download_id); | |
| 162 var callbackCompleted = chrome.test.callbackAdded(); | |
| 163 function myListener(delta) { | |
| 164 console.log("id: " + download_id); | |
| 165 if (delta.filename) console.log("filename: " + delta.filename.new); | |
| 166 if (delta.filename && delta.filename.new.indexOf(kFilename) !== -1) { | |
| 167 downloads.onChanged.removeListener(myListener); | |
| 168 callbackCompleted(); | |
| 169 } | |
| 170 } | |
| 171 downloads.onChanged.addListener(myListener); | |
| 54 downloads.download( | 172 downloads.download( |
| 55 {'url': SAFE_FAST_URL, 'filename': 'foo'}, | 173 {'url': SAFE_FAST_URL, 'filename': kFilename}, |
| 56 chrome.test.callbackPass(function(id) { | 174 chrome.test.callback(function(id) { |
| 57 chrome.test.assertEq(getNextId(), id); | 175 console.log("id: " + download_id); |
| 176 chrome.test.assertEq(download_id, id); | |
| 58 })); | 177 })); |
| 59 // TODO(benjhayden): Test the filename using onChanged. | |
| 60 }, | 178 }, |
| 61 function downloadOnCreated() { | 179 function downloadOnCreated() { |
| 62 chrome.test.listenOnce(downloads.onCreated, | 180 var download_id = getNextId(); |
| 63 chrome.test.callbackPass(function(item) {})); | 181 console.log("id: " + download_id); |
| 182 chrome.test.listenOnce(downloads.onCreated, function(item) { | |
| 183 // This callback is implicitly wrapped by chrome.test.callbackPass(), | |
| 184 // so if onCreated never fires, then this test will timeout. | |
| 185 }); | |
| 64 downloads.download( | 186 downloads.download( |
| 65 {'url': SAFE_FAST_URL}, | 187 {'url': SAFE_FAST_URL}, |
| 66 function(id) { | 188 chrome.test.callback(function(id) { |
| 67 chrome.test.assertEq(getNextId(), id); | 189 chrome.test.assertEq(download_id, id); |
| 68 }); | 190 })); |
| 69 }, | 191 }, |
| 70 function downloadSubDirectoryFilename() { | 192 // TODO(benjhayden): Talk to Asanka about a way to make the filename |
| 71 downloads.download( | 193 // sanitizer respect filenames from the downloads api (instead of replace |
| 72 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | 194 // slashes with underscores). |
|
Randy Smith (Not in Mondays)
2012/02/02 18:10:07
I'd suggest you have a plan sketch (and maybe a bu
benjhayden
2012/02/02 20:40:38
Added the bug id, and Asanka has a vague plan if I
Randy Smith (Not in Mondays)
2012/02/03 20:02:00
Yep; if there's a bug id I agree the discussion sh
benjhayden
2012/02/10 18:53:07
Done.
| |
| 73 chrome.test.callbackPass(function(id) { | 195 // function downloadSubDirectoryFilename() { |
| 74 chrome.test.assertEq(getNextId(), id); | 196 // var download_id = getNextId(); |
| 75 })); | 197 // console.log("id: " + download_id); |
| 76 // TODO(benjhayden): Test the filename using onChanged. | 198 // var callbackCompleted = chrome.test.callbackAdded(); |
| 77 }, | 199 // function myListener(delta) { |
| 200 // console.log("id: " + download_id); | |
| 201 // if (delta.filename) console.log("filename: " + delta.filename.new); | |
| 202 // if (delta.filename && | |
| 203 // delta.filename.new.indexOf('/foo/slow') !== -1) { | |
| 204 // downloads.onChanged.removeListener(myListener); | |
| 205 // callbackCompleted(); | |
| 206 // } | |
| 207 // } | |
| 208 // downloads.onChanged.addListener(myListener); | |
| 209 // downloads.download( | |
| 210 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | |
| 211 // chrome.test.callback(function(id) { | |
| 212 // console.log("id: " + download_id); | |
| 213 // chrome.test.assertEq(download_id, id); | |
| 214 // })); | |
| 215 // }, | |
| 78 function downloadInvalidFilename() { | 216 function downloadInvalidFilename() { |
| 79 downloads.download( | 217 downloads.download( |
| 80 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, | 218 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
| 81 chrome.test.callbackFail(ERROR_GENERIC)); | 219 chrome.test.callbackFail(ERROR_GENERIC)); |
| 82 // TODO(benjhayden): Give a better error message. | |
| 83 }, | 220 }, |
| 84 function downloadEmpty() { | 221 function downloadEmpty() { |
| 85 assertThrows(('Invalid value for argument 1. Property \'url\': ' + | 222 assertThrows(('Invalid value for argument 1. Property \'url\': ' + |
| 86 'Property is required.'), | 223 'Property is required.'), |
| 87 downloads.download, {}); | 224 downloads.download, {}); |
| 88 }, | 225 }, |
| 89 function downloadInvalidSaveAs() { | 226 function downloadInvalidSaveAs() { |
| 90 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + | 227 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + |
| 91 'Expected \'boolean\' but got \'string\'.'), | 228 'Expected \'boolean\' but got \'string\'.'), |
| 92 downloads.download, | 229 downloads.download, |
| 93 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); | 230 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); |
| 94 }, | 231 }, |
| 95 function downloadInvalidHeadersOption() { | 232 function downloadInvalidHeadersOption() { |
| 96 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + | 233 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + |
| 97 'Expected \'array\' but got \'string\'.'), | 234 'Expected \'array\' but got \'string\'.'), |
| 98 downloads.download, | 235 downloads.download, |
| 99 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); | 236 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
| 100 }, | 237 }, |
| 101 function downloadInvalidURL() { | 238 function downloadInvalidURL() { |
| 102 downloads.download( | 239 downloads.download( |
| 103 {'url': 'foo bar'}, | 240 {'url': 'foo bar'}, |
| 104 chrome.test.callbackFail(ERROR_INVALID_URL)); | 241 chrome.test.callbackFail(ERROR_INVALID_URL)); |
| 105 }, | 242 }, |
| 106 function downloadInvalidMethod() { | 243 function downloadInvalidMethod() { |
| 107 assertThrows(('Invalid value for argument 1. Property \'method\': ' + | 244 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
| 108 'Value must be one of: [GET, POST].'), | 245 'Value must be one of: [GET, POST].'), |
| 109 downloads.download, | 246 downloads.download, |
| 110 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); | 247 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
| 111 }, | 248 }, |
| 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() { | 249 function downloadInvalidHeader() { |
| 147 downloads.download( | 250 downloads.download( |
| 148 {'url': SAFE_FAST_URL, | 251 {'url': SAFE_FAST_URL, |
| 149 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] | 252 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] |
| 150 }, | 253 }, |
| 151 chrome.test.callbackFail(ERROR_GENERIC)); | 254 chrome.test.callbackFail(ERROR_GENERIC)); |
| 152 // TODO(benjhayden): Give a better error message. | |
| 153 }, | 255 }, |
| 154 function downloadGetFileIconInvalidOptions() { | 256 function downloadGetFileIconInvalidOptions() { |
| 155 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + | 257 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + |
| 156 'Unexpected property.'), | 258 'Unexpected property.'), |
| 157 downloads.getFileIcon, | 259 downloads.getFileIcon, |
| 158 -1, {cat: 'mouse'}); | 260 -1, {cat: 'mouse'}); |
| 159 }, | 261 }, |
| 160 function downloadGetFileIconInvalidSize() { | 262 function downloadGetFileIconInvalidSize() { |
| 161 assertThrows(('Invalid value for argument 2. Property \'size\': ' + | 263 assertThrows(('Invalid value for argument 2. Property \'size\': ' + |
| 162 'Value must be one of: [16, 32].'), | 264 'Value must be one of: [16, 32].'), |
| 163 downloads.getFileIcon, -1, {size: 31}); | 265 downloads.getFileIcon, -1, {size: 31}); |
| 164 }, | 266 }, |
| 165 function downloadGetFileIconInvalidId() { | 267 function downloadGetFileIconInvalidId() { |
| 166 downloads.getFileIcon(-42, {size: 32}, | 268 downloads.getFileIcon(-42, {size: 32}, |
| 167 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 269 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 168 }, | 270 }, |
| 169 function downloadNoComplete() { | 271 function downloadNoComplete() { |
| 170 // This is used partly to test cleanUp. | 272 // This is used partly to test cleanUp. |
| 273 var download_id = getNextId(); | |
| 274 console.log("id: " + download_id); | |
| 171 downloads.download( | 275 downloads.download( |
| 172 {'url': NEVER_FINISH_URL}, | 276 {'url': NEVER_FINISH_URL}, |
| 173 chrome.test.callbackPass(function(id) { | 277 chrome.test.callbackPass(function(id) { |
| 174 chrome.test.assertEq(getNextId(), id); | 278 console.log("id: " + download_id); |
| 279 chrome.test.assertEq(download_id, id); | |
| 175 })); | 280 })); |
| 176 }, | 281 }, |
| 177 function downloadPauseInvalidId() { | 282 function downloadPauseInvalidId() { |
| 178 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 283 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 179 }, | 284 }, |
| 180 function downloadPauseInvalidType() { | 285 function downloadPauseInvalidType() { |
| 181 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + | 286 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 182 'but got \'string\'.'), | 287 'but got \'string\'.'), |
| 183 downloads.pause, | 288 downloads.pause, |
| 184 'foo'); | 289 'foo'); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 203 }, | 308 }, |
| 204 function cleanUp() { | 309 function cleanUp() { |
| 205 // cleanUp must come last. It clears out all in-progress downloads | 310 // cleanUp must come last. It clears out all in-progress downloads |
| 206 // so the browser can shutdown cleanly. | 311 // so the browser can shutdown cleanly. |
| 207 for (var id = 0; id < nextId; ++id) { | 312 for (var id = 0; id < nextId; ++id) { |
| 208 downloads.cancel(id, chrome.test.callbackPass(function() {})); | 313 downloads.cancel(id, chrome.test.callbackPass(function() {})); |
| 209 } | 314 } |
| 210 } | 315 } |
| 211 ]); | 316 ]); |
| 212 }); | 317 }); |
| OLD | NEW |