Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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. | 20 // There can be only one assertThrows per test function. |
| 19 function assertThrows(func, arg, expected_message) { | 21 function assertThrows(func, arg, expected_message) { |
| 20 try { | 22 try { |
| 21 func(arg, function() { | 23 func(arg, function() { |
| 22 // Don't use chrome.test.callbackFail because it requires the | 24 // Don't use chrome.test.callbackFail because it requires the |
| 23 // callback to be called. | 25 // callback to be called. |
| 24 chrome.test.fail("Failed to throw exception (" + | 26 chrome.test.fail('Failed to throw exception (' + |
| 25 expected_message + ")"); | 27 expected_message + ')'); |
| 26 }); | 28 }); |
| 27 } catch (exception) { | 29 } catch (exception) { |
| 28 chrome.test.assertEq(expected_message, exception.message); | 30 chrome.test.assertEq(expected_message, exception.message); |
| 29 chrome.test.succeed(); | 31 chrome.test.succeed(); |
| 30 } | 32 } |
| 31 } | 33 } |
| 32 | 34 |
| 33 // The "/slow" handler waits a specified amount of time before returning a | 35 // The "/slow" handler waits a specified amount of time before returning a |
| 34 // safe file. Specify zero seconds to return quickly. | 36 // safe file. Specify zero seconds to return quickly. |
| 35 var SAFE_FAST_URL = getURL("slow?0"); | 37 var SAFE_FAST_URL = getURL('slow?0'); |
| 36 | 38 var NEVER_FINISH_URL = getURL('download-known-size'); |
| 37 var ERROR_GENERIC = chrome.experimental.downloads.ERROR_GENERIC; | 39 var ERROR_GENERIC = downloads.ERROR_GENERIC; |
| 38 var ERROR_INVALID_URL = chrome.experimental.downloads.ERROR_INVALID_URL; | 40 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
| 39 var ERROR_INVALID_OPERATION = | 41 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; |
| 40 chrome.experimental.downloads.ERROR_INVALID_OPERATION; | |
| 41 | 42 |
| 42 chrome.test.runTests([ | 43 chrome.test.runTests([ |
| 43 // TODO(benjhayden): Test onErased using remove(). | 44 // TODO(benjhayden): Test onErased using remove(). |
| 44 function downloadFilename() { | 45 function downloadFilename() { |
| 45 chrome.experimental.downloads.download( | 46 downloads.download( |
| 46 {"url": SAFE_FAST_URL, "filename": "foo"}, | 47 {'url': SAFE_FAST_URL, 'filename': 'foo'}, |
| 47 chrome.test.callbackPass(function(id) { | 48 chrome.test.callbackPass(function(id) { |
| 48 chrome.test.assertEq(getNextId(), id); | 49 chrome.test.assertEq(getNextId(), id); |
| 49 })); | 50 })); |
| 50 // TODO(benjhayden): Test the filename using onChanged. | 51 // TODO(benjhayden): Test the filename using onChanged. |
| 51 }, | 52 }, |
| 52 function downloadOnCreated() { | 53 function downloadOnCreated() { |
| 53 chrome.test.listenOnce(chrome.experimental.downloads.onCreated, | 54 chrome.test.listenOnce(downloads.onCreated, |
| 54 chrome.test.callbackPass(function(item) { | 55 chrome.test.callbackPass(function(item) {})); |
| 55 })); | 56 downloads.download( |
| 56 chrome.experimental.downloads.download( | 57 {'url': SAFE_FAST_URL}, |
| 57 {"url": SAFE_FAST_URL}, | 58 function(id) { |
| 58 function(id) { | 59 chrome.test.assertEq(getNextId(), id); |
| 59 chrome.test.assertEq(getNextId(), id); | 60 }); |
| 60 }); | |
| 61 }, | 61 }, |
| 62 function downloadSubDirectoryFilename() { | 62 function downloadSubDirectoryFilename() { |
| 63 chrome.experimental.downloads.download( | 63 downloads.download( |
| 64 {"url": SAFE_FAST_URL, "filename": "foo/slow"}, | 64 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, |
| 65 chrome.test.callbackPass(function(id) { | 65 chrome.test.callbackPass(function(id) { |
| 66 chrome.test.assertEq(getNextId(), id); | 66 chrome.test.assertEq(getNextId(), id); |
| 67 })); | 67 })); |
| 68 // TODO(benjhayden): Test the filename using onChanged. | 68 // TODO(benjhayden): Test the filename using onChanged. |
| 69 }, | 69 }, |
| 70 function downloadInvalidFilename() { | 70 function downloadInvalidFilename() { |
| 71 chrome.experimental.downloads.download( | 71 downloads.download( |
| 72 {"url": SAFE_FAST_URL, "filename": "../../../../../etc/passwd"}, | 72 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
| 73 chrome.test.callbackFail("I'm afraid I can't do that.")); | 73 chrome.test.callbackFail('I\'m afraid I can\'t do that.')); |
|
asanka
2012/01/11 18:22:59
Nit: ERROR_GENERIC
cbentzel
2012/01/11 18:43:21
Done.
| |
| 74 // TODO(benjhayden): Give a better error message. | 74 // TODO(benjhayden): Give a better error message. |
| 75 }, | 75 }, |
| 76 function downloadEmpty() { | 76 function downloadEmpty() { |
| 77 assertThrows(chrome.experimental.downloads.download, {}, | 77 assertThrows(downloads.download, {}, |
| 78 ("Invalid value for argument 1. Property 'url': " + | 78 ('Invalid value for argument 1. Property \'url\': ' + |
| 79 "Property is required.")); | 79 'Property is required.')); |
| 80 }, | 80 }, |
| 81 function downloadInvalidSaveAs() { | 81 function downloadInvalidSaveAs() { |
| 82 assertThrows(chrome.experimental.downloads.download, | 82 assertThrows(downloads.download, |
| 83 {"url": SAFE_FAST_URL, "saveAs": "GOAT"}, | 83 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}, |
| 84 ("Invalid value for argument 1. Property 'saveAs': " + | 84 ('Invalid value for argument 1. Property \'saveAs\': ' + |
| 85 "Expected 'boolean' but got 'string'.")); | 85 'Expected \'boolean\' but got \'string\'.')); |
| 86 }, | 86 }, |
| 87 function downloadInvalidHeadersOption() { | 87 function downloadInvalidHeadersOption() { |
| 88 assertThrows(chrome.experimental.downloads.download, | 88 assertThrows(downloads.download, |
| 89 {"url": SAFE_FAST_URL, "headers": "GOAT"}, | 89 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}, |
| 90 ("Invalid value for argument 1. Property 'headers': " + | 90 ('Invalid value for argument 1. Property \'headers\': ' + |
| 91 "Expected 'array' but got 'string'.")); | 91 'Expected \'array\' but got \'string\'.')); |
| 92 }, | 92 }, |
| 93 function downloadInvalidURL() { | 93 function downloadInvalidURL() { |
| 94 chrome.experimental.downloads.download( | 94 downloads.download( |
| 95 {"url": "foo bar"}, | 95 {'url': 'foo bar'}, |
| 96 chrome.test.callbackFail(ERROR_INVALID_URL)); | 96 chrome.test.callbackFail(ERROR_INVALID_URL)); |
| 97 }, | 97 }, |
| 98 function downloadInvalidMethod() { | 98 function downloadInvalidMethod() { |
| 99 assertThrows(chrome.experimental.downloads.download, | 99 assertThrows(downloads.download, |
| 100 {"url": SAFE_FAST_URL, "method": "GOAT"}, | 100 {'url': SAFE_FAST_URL, 'method': 'GOAT'}, |
| 101 ("Invalid value for argument 1. Property 'method': " + | 101 ('Invalid value for argument 1. Property \'method\': ' + |
| 102 "Value must be one of: [GET, POST].")); | 102 'Value must be one of: [GET, POST].')); |
| 103 }, | 103 }, |
| 104 function downloadSimple() { | 104 function downloadSimple() { |
| 105 chrome.experimental.downloads.download( | 105 downloads.download( |
| 106 {"url": SAFE_FAST_URL}, | 106 {'url': SAFE_FAST_URL}, |
| 107 chrome.test.callbackPass(function(id) { | 107 chrome.test.callbackPass(function(id) { |
| 108 chrome.test.assertEq(getNextId(), id); | 108 chrome.test.assertEq(getNextId(), id); |
| 109 })); | 109 })); |
| 110 }, | 110 }, |
| 111 function downloadHeader() { | 111 function downloadHeader() { |
| 112 chrome.experimental.downloads.download( | 112 downloads.download( |
| 113 {"url": SAFE_FAST_URL, | 113 {'url': SAFE_FAST_URL, |
| 114 "headers": [{"name": "Foo", "value": "bar"}]}, | 114 'headers': [{'name': 'Foo', 'value': 'bar'}] |
| 115 chrome.test.callbackPass(function(id) { | 115 }, |
| 116 chrome.test.assertEq(getNextId(), id); | 116 chrome.test.callbackPass(function(id) { |
| 117 })); | 117 chrome.test.assertEq(getNextId(), id); |
| 118 })); | |
| 118 }, | 119 }, |
| 119 function downloadInterrupted() { | 120 function downloadInterrupted() { |
| 120 // TODO(benjhayden): Find a suitable URL and test that this id is | 121 // TODO(benjhayden): Find a suitable URL and test that this id is |
| 121 // eventually interrupted using onChanged. | 122 // eventually interrupted using onChanged. |
| 122 chrome.experimental.downloads.download( | 123 downloads.download( |
| 123 {"url": SAFE_FAST_URL}, | 124 {'url': SAFE_FAST_URL}, |
| 124 chrome.test.callbackPass(function(id) { | 125 chrome.test.callbackPass(function(id) { |
| 125 chrome.test.assertEq(getNextId(), id); | 126 chrome.test.assertEq(getNextId(), id); |
| 126 })); | 127 })); |
| 127 }, | 128 }, |
| 128 function downloadInvalidHeader() { | 129 function downloadInvalidHeader() { |
| 129 chrome.experimental.downloads.download( | 130 downloads.download( |
| 130 {"url": SAFE_FAST_URL, | 131 {'url': SAFE_FAST_URL, |
| 131 "headers": [{"name": "Cookie", "value": "fake"}]}, | 132 'headers': [{'name': 'Cookie', 'value': 'fake'}] |
| 132 chrome.test.callbackFail(ERROR_GENERIC)); | 133 }, |
| 134 chrome.test.callbackFail(ERROR_GENERIC)); | |
| 133 // TODO(benjhayden): Give a better error message. | 135 // TODO(benjhayden): Give a better error message. |
| 134 }, | 136 }, |
| 137 function downloadNoComplete() { | |
|
benjhayden
2012/01/11 17:39:44
Comment that this is to test cleanUp?
cbentzel
2012/01/11 18:43:21
Done.
| |
| 138 downloads.download( | |
| 139 {'url': NEVER_FINISH_URL}, | |
| 140 chrome.test.callbackPass(function(id) { | |
| 141 chrome.test.assertEq(getNextId(), id); | |
| 142 })); | |
| 143 }, | |
| 135 function downloadPauseInvalidId() { | 144 function downloadPauseInvalidId() { |
| 136 chrome.experimental.downloads.pause(-42, | 145 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 137 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | |
| 138 }, | 146 }, |
| 139 function downloadPauseInvalidType() { | 147 function downloadPauseInvalidType() { |
| 140 assertThrows(chrome.experimental.downloads.pause, | 148 assertThrows(downloads.pause, |
| 141 "foo", | 149 'foo', |
| 142 ("Invalid value for argument 1. Expected 'integer' " + | 150 ('Invalid value for argument 1. Expected \'integer\' ' + |
| 143 "but got 'string'.")); | 151 'but got \'string\'.')); |
| 144 }, | 152 }, |
| 145 function downloadResumeInvalidId() { | 153 function downloadResumeInvalidId() { |
| 146 chrome.experimental.downloads.resume(-42, | 154 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| 147 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); | |
| 148 }, | 155 }, |
| 149 function downloadResumeInvalidType() { | 156 function downloadResumeInvalidType() { |
| 150 assertThrows(chrome.experimental.downloads.resume, | 157 assertThrows(downloads.resume, |
| 151 "foo", | 158 'foo', |
| 152 ("Invalid value for argument 1. Expected 'integer' " + | 159 ('Invalid value for argument 1. Expected \'integer\' ' + |
| 153 "but got 'string'.")); | 160 'but got \'string\'.')); |
| 154 }, | 161 }, |
| 155 function downloadCancelInvalidId() { | 162 function downloadCancelInvalidId() { |
| 156 // Canceling a non-existent download is not considered an error. | 163 // Canceling a non-existent download is not considered an error. |
| 157 chrome.experimental.downloads.cancel(-42, | 164 downloads.cancel(-42, chrome.test.callbackPass(function() {})); |
| 158 chrome.test.callbackPass(function() {})); | |
| 159 }, | 165 }, |
| 160 function downloadCancelInvalidType() { | 166 function downloadCancelInvalidType() { |
| 161 assertThrows(chrome.experimental.downloads.cancel, | 167 assertThrows(downloads.cancel, |
| 162 "foo", | 168 'foo', |
| 163 ("Invalid value for argument 1. Expected 'integer' " + | 169 ('Invalid value for argument 1. Expected \'integer\' ' + |
| 164 "but got 'string'.")); | 170 'but got \'string\'.')); |
| 171 }, | |
| 172 function cleanUp() { | |
| 173 // cleanUp must come last. It clears out all in-progress downloads | |
| 174 // so the browser can shutdown cleanly. | |
| 175 for (var id = 0; id < nextId; ++id) { | |
| 176 downloads.cancel(id, chrome.test.callbackPass(function() {})); | |
| 177 } | |
| 165 } | 178 } |
| 166 ]); | 179 ]); |
| 167 }); | 180 }); |
| OLD | NEW |