| 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; |
| 9 |
| 8 chrome.test.getConfig(function(testConfig) { | 10 chrome.test.getConfig(function(testConfig) { |
| 9 function getURL(path) { | 11 function getURL(path) { |
| 10 return "http://localhost:" + testConfig.testServer.port + "/" + path; | 12 return 'http://localhost:' + testConfig.testServer.port + '/' + path; |
| 11 } | 13 } |
| 12 | 14 |
| 13 var nextId = 0; | 15 var nextId = 0; |
| 14 function getNextId() { | 16 function getNextId() { |
| 15 return nextId++; | 17 return nextId++; |
| 16 } | 18 } |
| 17 | 19 |
| 18 // There can be only one assertThrows per test function. This should be | 20 // There can be only one assertThrows per test function. This should be |
| 19 // called as follows: | 21 // called as follows: |
| 20 // | 22 // |
| 21 // assertThrows(exceptionMessage, function, [arg1, [arg2, ... ]]) | 23 // assertThrows(exceptionMessage, function, [arg1, [arg2, ... ]]) |
| 22 // | 24 // |
| 23 // ... where |exceptionMessage| is the expected exception message. | 25 // ... where |exceptionMessage| is the expected exception message. |
| 24 // |arg1|, |arg2|, etc.. are arguments to |function|. | 26 // |arg1|, |arg2|, etc.. are arguments to |function|. |
| 25 function assertThrows(exceptionMessage, func) { | 27 function assertThrows(exceptionMessage, func) { |
| 26 var args = Array.prototype.slice.call(arguments, 2); | 28 var args = Array.prototype.slice.call(arguments, 2); |
| 27 try { | 29 try { |
| 28 args.push(function() { | 30 args.push(function() { |
| 29 // Don't use chrome.test.callbackFail because it requires the | 31 // Don't use chrome.test.callbackFail because it requires the |
| 30 // callback to be called. | 32 // callback to be called. |
| 31 chrome.test.fail("Failed to throw exception (" + | 33 chrome.test.fail('Failed to throw exception (' + |
| 32 exceptionMessage + ")"); | 34 exceptionMessage + ')'); |
| 33 }); | 35 }); |
| 34 func.apply(null, args); | 36 func.apply(null, args); |
| 35 } catch (exception) { | 37 } catch (exception) { |
| 36 chrome.test.assertEq(exceptionMessage, exception.message); | 38 chrome.test.assertEq(exceptionMessage, exception.message); |
| 37 chrome.test.succeed(); | 39 chrome.test.succeed(); |
| 38 } | 40 } |
| 39 } | 41 } |
| 40 | 42 |
| 41 // 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 |
| 42 // safe file. Specify zero seconds to return quickly. | 44 // safe file. Specify zero seconds to return quickly. |
| 43 var SAFE_FAST_URL = getURL("slow?0"); | 45 var SAFE_FAST_URL = getURL('slow?0'); |
| 44 | 46 var NEVER_FINISH_URL = getURL('download-known-size'); |
| 45 var ERROR_GENERIC = chrome.experimental.downloads.ERROR_GENERIC; | 47 var ERROR_GENERIC = downloads.ERROR_GENERIC; |
| 46 var ERROR_INVALID_URL = chrome.experimental.downloads.ERROR_INVALID_URL; | 48 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
| 47 var ERROR_INVALID_OPERATION = | 49 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; |
| 48 chrome.experimental.downloads.ERROR_INVALID_OPERATION; | |
| 49 | 50 |
| 50 chrome.test.runTests([ | 51 chrome.test.runTests([ |
| 51 // TODO(benjhayden): Test onErased using remove(). | 52 // TODO(benjhayden): Test onErased using remove(). |
| 52 function downloadFilename() { | 53 function downloadFilename() { |
| 53 chrome.experimental.downloads.download( | 54 downloads.download( |
| 54 {"url": SAFE_FAST_URL, "filename": "foo"}, | 55 {'url': SAFE_FAST_URL, 'filename': 'foo'}, |
| 55 chrome.test.callbackPass(function(id) { | 56 chrome.test.callbackPass(function(id) { |
| 56 chrome.test.assertEq(getNextId(), id); | 57 chrome.test.assertEq(getNextId(), id); |
| 57 })); | 58 })); |
| 58 // TODO(benjhayden): Test the filename using onChanged. | 59 // TODO(benjhayden): Test the filename using onChanged. |
| 59 }, | 60 }, |
| 60 function downloadOnCreated() { | 61 function downloadOnCreated() { |
| 61 chrome.test.listenOnce(chrome.experimental.downloads.onCreated, | 62 chrome.test.listenOnce(downloads.onCreated, |
| 62 chrome.test.callbackPass(function(item) { | 63 chrome.test.callbackPass(function(item) {})); |
| 63 })); | 64 downloads.download( |
| 64 chrome.experimental.downloads.download( | 65 {'url': SAFE_FAST_URL}, |
| 65 {"url": SAFE_FAST_URL}, | 66 function(id) { |
| 66 function(id) { | 67 chrome.test.assertEq(getNextId(), id); |
| 67 chrome.test.assertEq(getNextId(), id); | 68 }); |
| 68 }); | |
| 69 }, | 69 }, |
| 70 function downloadSubDirectoryFilename() { | 70 function downloadSubDirectoryFilename() { |
| 71 chrome.experimental.downloads.download( | 71 downloads.download( |
| 72 {"url": SAFE_FAST_URL, "filename": "foo/slow"}, | 72 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, |
| 73 chrome.test.callbackPass(function(id) { | 73 chrome.test.callbackPass(function(id) { |
| 74 chrome.test.assertEq(getNextId(), id); | 74 chrome.test.assertEq(getNextId(), id); |
| 75 })); | 75 })); |
| 76 // TODO(benjhayden): Test the filename using onChanged. | 76 // TODO(benjhayden): Test the filename using onChanged. |
| 77 }, | 77 }, |
| 78 function downloadInvalidFilename() { | 78 function downloadInvalidFilename() { |
| 79 chrome.experimental.downloads.download( | 79 downloads.download( |
| 80 {"url": SAFE_FAST_URL, "filename": "../../../../../etc/passwd"}, | 80 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
| 81 chrome.test.callbackFail("I'm afraid I can't do that.")); | 81 chrome.test.callbackFail(ERROR_GENERIC)); |
| 82 // TODO(benjhayden): Give a better error message. | 82 // TODO(benjhayden): Give a better error message. |
| 83 }, | 83 }, |
| 84 function downloadEmpty() { | 84 function downloadEmpty() { |
| 85 assertThrows(("Invalid value for argument 1. Property 'url': " + | 85 assertThrows(('Invalid value for argument 1. Property \'url\': ' + |
| 86 "Property is required."), | 86 'Property is required.'), |
| 87 chrome.experimental.downloads.download, {}); | 87 downloads.download, {}); |
| 88 }, | 88 }, |
| 89 function downloadInvalidSaveAs() { | 89 function downloadInvalidSaveAs() { |
| 90 assertThrows(("Invalid value for argument 1. Property 'saveAs': " + | 90 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + |
| 91 "Expected 'boolean' but got 'string'."), | 91 'Expected \'boolean\' but got \'string\'.'), |
| 92 chrome.experimental.downloads.download, | 92 downloads.download, |
| 93 {"url": SAFE_FAST_URL, "saveAs": "GOAT"}); | 93 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); |
| 94 }, | 94 }, |
| 95 function downloadInvalidHeadersOption() { | 95 function downloadInvalidHeadersOption() { |
| 96 assertThrows(("Invalid value for argument 1. Property 'headers': " + | 96 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + |
| 97 "Expected 'array' but got 'string'."), | 97 'Expected \'array\' but got \'string\'.'), |
| 98 chrome.experimental.downloads.download, | 98 downloads.download, |
| 99 {"url": SAFE_FAST_URL, "headers": "GOAT"}); | 99 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
| 100 }, | 100 }, |
| 101 function downloadInvalidURL() { | 101 function downloadInvalidURL() { |
| 102 chrome.experimental.downloads.download( | 102 downloads.download( |
| 103 {"url": "foo bar"}, | 103 {'url': 'foo bar'}, |
| 104 chrome.test.callbackFail(ERROR_INVALID_URL)); | 104 chrome.test.callbackFail(ERROR_INVALID_URL)); |
| 105 }, | 105 }, |
| 106 function downloadInvalidMethod() { | 106 function downloadInvalidMethod() { |
| 107 assertThrows(("Invalid value for argument 1. Property 'method': " + | 107 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
| 108 "Value must be one of: [GET, POST]."), | 108 'Value must be one of: [GET, POST].'), |
| 109 chrome.experimental.downloads.download, | 109 downloads.download, |
| 110 {"url": SAFE_FAST_URL, "method": "GOAT"}); | 110 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
| 111 }, | 111 }, |
| 112 function downloadSimple() { | 112 function downloadSimple() { |
| 113 chrome.experimental.downloads.download( | 113 downloads.download( |
| 114 {"url": SAFE_FAST_URL}, | 114 {'url': SAFE_FAST_URL}, |
| 115 chrome.test.callbackPass(function(id) { | 115 chrome.test.callbackPass(function(id) { |
| 116 chrome.test.assertEq(getNextId(), id); | 116 chrome.test.assertEq(getNextId(), id); |
| 117 })); | 117 })); |
| 118 }, | 118 }, |
| 119 function downloadHeader() { | 119 function downloadHeader() { |
| 120 chrome.experimental.downloads.download( | 120 downloads.download( |
| 121 {"url": SAFE_FAST_URL, | 121 {'url': SAFE_FAST_URL, |
| 122 "headers": [{"name": "Foo", "value": "bar"}]}, | 122 'headers': [{'name': 'Foo', 'value': 'bar'}] |
| 123 chrome.test.callbackPass(function(id) { | 123 }, |
| 124 chrome.test.assertEq(getNextId(), id); | 124 chrome.test.callbackPass(function(id) { |
| 125 })); | 125 chrome.test.assertEq(getNextId(), id); |
| 126 })); |
| 126 }, | 127 }, |
| 127 function downloadInterrupted() { | 128 function downloadInterrupted() { |
| 128 // TODO(benjhayden): Find a suitable URL and test that this id is | 129 // TODO(benjhayden): Find a suitable URL and test that this id is |
| 129 // eventually interrupted using onChanged. | 130 // eventually interrupted using onChanged. |
| 130 chrome.experimental.downloads.download( | 131 downloads.download( |
| 131 {"url": SAFE_FAST_URL}, | 132 {'url': SAFE_FAST_URL}, |
| 132 chrome.test.callbackPass(function(id) { | 133 chrome.test.callbackPass(function(id) { |
| 133 chrome.test.assertEq(getNextId(), id); | 134 chrome.test.assertEq(getNextId(), id); |
| 134 })); | 135 })); |
| 135 }, | 136 }, |
| 136 function downloadInvalidHeader() { | 137 function downloadInvalidHeader() { |
| 137 chrome.experimental.downloads.download( | 138 downloads.download( |
| 138 {"url": SAFE_FAST_URL, | 139 {'url': SAFE_FAST_URL, |
| 139 "headers": [{"name": "Cookie", "value": "fake"}]}, | 140 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] |
| 141 }, |
| 140 chrome.test.callbackFail(ERROR_GENERIC)); | 142 chrome.test.callbackFail(ERROR_GENERIC)); |
| 141 // TODO(benjhayden): Give a better error message. | 143 // TODO(benjhayden): Give a better error message. |
| 142 }, | 144 }, |
| 143 function downloadGetFileIconInvalidOptions() { | 145 function downloadGetFileIconInvalidOptions() { |
| 144 assertThrows(("Invalid value for argument 2. Property 'cat': " + | 146 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + |
| 145 "Unexpected property."), | 147 'Unexpected property.'), |
| 146 chrome.experimental.downloads.getFileIcon, | 148 downloads.getFileIcon, |
| 147 -1, {cat: "mouse"}); | 149 -1, {cat: 'mouse'}); |
| 148 }, | 150 }, |
| 149 function downloadGetFileIconInvalidSize() { | 151 function downloadGetFileIconInvalidSize() { |
| 150 assertThrows(("Invalid value for argument 2. Property 'size': " + | 152 assertThrows(('Invalid value for argument 2. Property \'size\': ' + |
| 151 "Value must be one of: [16, 32]."), | 153 'Value must be one of: [16, 32].'), |
| 152 chrome.experimental.downloads.getFileIcon, -1, {size: 31}); | 154 downloads.getFileIcon, -1, {size: 31}); |
| 153 }, | 155 }, |
| 154 function downloadGetFileIconInvalidId() { | 156 function downloadGetFileIconInvalidId() { |
| 155 chrome.experimental.downloads.getFileIcon(-42, {size: 32}, | 157 downloads.getFileIcon(-42, {size: 32}, |
| 156 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | 158 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 157 }, | 159 }, |
| 160 function downloadNoComplete() { |
| 161 // This is used partly to test cleanUp. |
| 162 downloads.download( |
| 163 {'url': NEVER_FINISH_URL}, |
| 164 chrome.test.callbackPass(function(id) { |
| 165 chrome.test.assertEq(getNextId(), id); |
| 166 })); |
| 167 }, |
| 158 function downloadPauseInvalidId() { | 168 function downloadPauseInvalidId() { |
| 159 chrome.experimental.downloads.pause(-42, | 169 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 160 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | |
| 161 }, | 170 }, |
| 162 function downloadPauseInvalidType() { | 171 function downloadPauseInvalidType() { |
| 163 assertThrows(("Invalid value for argument 1. Expected 'integer' " + | 172 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 164 "but got 'string'."), | 173 'but got \'string\'.'), |
| 165 chrome.experimental.downloads.pause, | 174 downloads.pause, |
| 166 "foo"); | 175 'foo'); |
| 167 }, | 176 }, |
| 168 function downloadResumeInvalidId() { | 177 function downloadResumeInvalidId() { |
| 169 chrome.experimental.downloads.resume(-42, | 178 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 170 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | |
| 171 }, | 179 }, |
| 172 function downloadResumeInvalidType() { | 180 function downloadResumeInvalidType() { |
| 173 assertThrows(("Invalid value for argument 1. Expected 'integer' " + | 181 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 174 "but got 'string'."), | 182 'but got \'string\'.'), |
| 175 chrome.experimental.downloads.resume, | 183 downloads.resume, |
| 176 "foo"); | 184 'foo'); |
| 177 }, | 185 }, |
| 178 function downloadCancelInvalidId() { | 186 function downloadCancelInvalidId() { |
| 179 // Canceling a non-existent download is not considered an error. | 187 // Canceling a non-existent download is not considered an error. |
| 180 chrome.experimental.downloads.cancel(-42, | 188 downloads.cancel(-42, chrome.test.callbackPass(function() {})); |
| 181 chrome.test.callbackPass(function() {})); | |
| 182 }, | 189 }, |
| 183 function downloadCancelInvalidType() { | 190 function downloadCancelInvalidType() { |
| 184 assertThrows(("Invalid value for argument 1. Expected 'integer' " + | 191 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 185 "but got 'string'."), | 192 'but got \'string\'.'), |
| 186 chrome.experimental.downloads.cancel, | 193 downloads.cancel, 'foo'); |
| 187 "foo"); | 194 }, |
| 195 function cleanUp() { |
| 196 // cleanUp must come last. It clears out all in-progress downloads |
| 197 // so the browser can shutdown cleanly. |
| 198 for (var id = 0; id < nextId; ++id) { |
| 199 downloads.cancel(id, chrome.test.callbackPass(function() {})); |
| 200 } |
| 188 } | 201 } |
| 189 ]); | 202 ]); |
| 190 }); | 203 }); |
| OLD | NEW |