Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/downloads/test.js |
| diff --git a/chrome/test/data/extensions/api_test/downloads/test.js b/chrome/test/data/extensions/api_test/downloads/test.js |
| index 0a1368c87aaebfcd15db05b3747c253b60c14074..d802fe1101af6277770422199e07cdd379046191 100644 |
| --- a/chrome/test/data/extensions/api_test/downloads/test.js |
| +++ b/chrome/test/data/extensions/api_test/downloads/test.js |
| @@ -42,165 +42,429 @@ chrome.test.getConfig(function(testConfig) { |
| // The "/slow" handler waits a specified amount of time before returning a |
| // safe file. Specify zero seconds to return quickly. |
| - var SAFE_FAST_URL = getURL('slow?0'); |
| - var NEVER_FINISH_URL = getURL('download-known-size'); |
| - var ERROR_GENERIC = downloads.ERROR_GENERIC; |
| - var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
| - var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; |
| + const SAFE_FAST_URL = getURL('slow?0'); |
| + const NEVER_FINISH_URL = getURL('download-known-size'); |
| + const POST_URL = 'files/post/downloads/a_zip_file.zip?expected_body=BODY' |
| + const ERROR_GENERIC = downloads.ERROR_GENERIC; |
| + const ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
| + const ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; |
| + |
| + console.log("I know all these console.logs stink up the place, but please " + |
| + "leave them in: they will help you check that all the " + |
| + "callbacks for a particular test run before the test harness " + |
| + "thinks that that test is finished and that that test doesn't " + |
| + "'finish' twice."); |
| + console.log("All log lines that contain a download_id should appear " + |
| + "before that test finishes. If a callback for test N runs " + |
| + "after test N+1 begins, then that indicates a race condition " + |
| + "in test N."); |
| chrome.test.runTests([ |
| // TODO(benjhayden): Test onErased using remove(). |
| - function downloadFilename() { |
| + |
| + // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443 |
| + // function downloadSubDirectoryFilename() { |
| + // var download_id = getNextId(); |
| + // console.log("id: " + download_id); |
| + // var callbackCompleted = chrome.test.callbackAdded(); |
| + // function myListener(delta) { |
| + // console.log("id: " + download_id); |
| + // if (delta.filename) console.log("filename: " + delta.filename.new); |
| + // if (delta.filename && |
| + // delta.filename.new.indexOf('/foo/slow') !== -1) { |
| + // downloads.onChanged.removeListener(myListener); |
| + // callbackCompleted(); |
| + // } |
| + // } |
| + // downloads.onChanged.addListener(myListener); |
| + // downloads.download( |
| + // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, |
| + // chrome.test.callback(function(id) { |
| + // console.log("id: " + download_id); |
| + // chrome.test.assertEq(download_id, id); |
| + // })); |
| + // }, |
| + |
| + function downloadSimple() { |
| + // Test that we can begin a download. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| downloads.download( |
| - {'url': SAFE_FAST_URL, 'filename': 'foo'}, |
| + {'url': SAFE_FAST_URL}, |
| chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| })); |
| - // TODO(benjhayden): Test the filename using onChanged. |
| }, |
| - function downloadOnCreated() { |
| - chrome.test.listenOnce(downloads.onCreated, |
| - chrome.test.callbackPass(function(item) {})); |
| + |
| + function downloadPostSuccess() { |
| + // Test the |method| download option. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + |
| + var changedCompleted = chrome.test.callbackAdded(); |
| + function changedListener(delta) { |
| + console.log("id: " + delta.id); |
| + if ((delta.id == download_id) && |
| + delta.state) |
| + console.log("state: " + delta.state.new); |
| + // Ignore onChanged events for downloads besides our own, or events that |
| + // signal any change besides completion. |
| + if ((delta.id == download_id) && |
| + delta.state && |
| + (delta.state.new == downloads.STATE_COMPLETE)) { |
| + console.log("id: " + delta.id); |
| + chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new); |
| + downloads.search({id: download_id}, |
| + chrome.test.callback(function(items) { |
| + chrome.test.assertEq(1, items.length); |
| + chrome.test.assertEq(download_id, items[0].id); |
| + const kExpectedSize = 164; |
| + chrome.test.assertEq(kExpectedSize, items[0].totalBytes); |
| + chrome.test.assertEq(kExpectedSize, items[0].fileSize); |
| + chrome.test.assertEq(kExpectedSize, items[0].bytesReceived); |
| + })); |
| + downloads.onChanged.removeListener(changedListener); |
| + changedCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(changedListener); |
| + |
| downloads.download( |
| - {'url': SAFE_FAST_URL}, |
| - function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - }); |
| + {'url': getURL(POST_URL), |
| + 'method': 'POST', |
| + 'filename': download_id + '.txt', |
| + 'body': 'BODY'}, |
| + chrome.test.callbackPass(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + function downloadPostWouldFailWithoutMethod() { |
| + // Test that downloadPostSuccess would fail if the resource requires the |
| + // POST method, and chrome fails to propagate the |method| parameter back |
| + // to the server. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + |
| + var changedCompleted = chrome.test.callbackAdded(); |
| + function changedListener(delta) { |
| + console.log("id: " + delta.id); |
| + if (delta.state) console.log("state: " + delta.state.new); |
| + // Ignore onChanged events for downloads besides our own, or events that |
| + // signal any change besides interruption. |
| + if ((delta.id == download_id) && |
| + delta.state && |
| + ((delta.state.new == downloads.STATE_INTERRUPTED) || |
| + (delta.state.new == downloads.STATE_COMPLETE))) { |
| + console.log("id: " + delta.id); |
| + // TODO(benjhayden): Figure out why this download isn't interrupted, |
| + // why 4XX HTTP errors are not considered interruptions. |
| + downloads.search({id: download_id}, |
| + chrome.test.callback(function(items) { |
| + chrome.test.assertEq(1, items.length); |
| + chrome.test.assertEq(download_id, items[0].id); |
| + chrome.test.assertEq(0, items[0].totalBytes); |
| + })); |
| + downloads.onChanged.removeListener(changedListener); |
| + changedCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(changedListener); |
| + |
| + downloads.download( |
| + {'url': getURL(POST_URL), |
| + 'filename': download_id + '.txt', |
| + 'body': 'BODY'}, |
| + chrome.test.callbackPass(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + function downloadPostWouldFailWithoutBody() { |
| + // Test that downloadPostSuccess would fail if the resource requires the |
| + // POST method and a request body, and chrome fails to propagate the |
| + // |body| parameter back to the server. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + |
| + var changedCompleted = chrome.test.callbackAdded(); |
| + function changedListener(delta) { |
| + console.log("id: " + delta.id); |
| + if (delta.state) console.log("state: " + delta.state.new); |
| + // Ignore onChanged events for downloads besides our own, or events that |
| + // signal any change besides interruption. |
| + if ((delta.id == download_id) && |
| + delta.state && |
| + ((delta.state.new == downloads.STATE_INTERRUPTED) || |
| + (delta.state.new == downloads.STATE_COMPLETE))) { |
| + console.log("id: " + delta.id); |
| + // TODO(benjhayden): Figure out why this download isn't interrupted. |
| + downloads.search({id: download_id}, |
| + chrome.test.callback(function(items) { |
| + chrome.test.assertEq(1, items.length); |
| + chrome.test.assertEq(download_id, items[0].id); |
| + chrome.test.assertEq(0, items[0].totalBytes); |
| + })); |
| + downloads.onChanged.removeListener(changedListener); |
| + changedCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(changedListener); |
| + |
| + downloads.download( |
| + {'url': getURL(POST_URL), |
| + 'filename': download_id + '.txt', |
| + 'method': 'POST'}, |
| + chrome.test.callbackPass(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| }, |
| - function downloadSubDirectoryFilename() { |
| + |
| + function downloadHeader() { |
| + // Test the |headers| download option. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| downloads.download( |
| - {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, |
| + {'url': SAFE_FAST_URL, |
| + 'headers': [{'name': 'Foo', 'value': 'bar'}] |
| + }, |
| chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + function downloadInterrupted() { |
| + // Test that cancel()ing an in-progress download causes its state to |
| + // transition to interrupted, and test that that state transition is |
| + // detectable by an onChanged event listener. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + |
| + var createdCompleted = chrome.test.callbackAdded(); |
| + function createdListener(created_item) { |
| + console.log("created_id: " + created_item.id); |
| + // Ignore onCreated events for any download besides our own. |
| + if (created_item.id != download_id) |
| + return; |
| + // TODO(benjhayden) Move this cancel() into the download() callback |
| + // after ensuring that DownloadItems are created before that callback |
| + // is fired. |
| + downloads.cancel(download_id, chrome.test.callback(function() { |
| + console.log("id: " + download_id); |
| + })); |
| + downloads.onCreated.removeListener(createdListener); |
| + createdCompleted(); |
| + } |
| + downloads.onCreated.addListener(createdListener); |
| + |
| + var changedCompleted = chrome.test.callbackAdded(); |
| + function changedListener(delta) { |
| + console.log("id: " + delta.id); |
| + // Ignore onChanged events for downloads besides our own, or events that |
| + // signal any change besides interruption. |
| + if ((delta.id == download_id) && |
| + delta.state && |
| + (delta.state.new == downloads.STATE_INTERRUPTED)) { |
| + console.log("id: " + delta.id); |
| + downloads.onChanged.removeListener(changedListener); |
| + changedCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(changedListener); |
| + |
| + downloads.download( |
| + {'url': NEVER_FINISH_URL}, |
| + chrome.test.callback(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + |
| + function downloadOnChanged() { |
| + // Test that download completion is detectable by an onChanged event |
| + // listener. |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + var callbackCompleted = chrome.test.callbackAdded(); |
| + function myListener(delta) { |
| + console.log("id: " + delta.id); |
| + if (delta.state) console.log("state: " + delta.state.new); |
| + if (delta.state && delta.state.new == downloads.STATE_COMPLETE) { |
| + downloads.onChanged.removeListener(myListener); |
| + callbackCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(myListener); |
| + downloads.download( |
| + {"url": getURL("slow?0")}, |
| + chrome.test.callback(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + function downloadFilename() { |
| + // Test that we can suggest a filename for a new download, and test that |
| + // we can detect filename changes with an onChanged event listener. |
| + const kFilename = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi'; |
| + const download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + var callbackCompleted = chrome.test.callbackAdded(); |
| + function myListener(delta) { |
| + console.log("id: " + download_id); |
| + if (delta.filename) console.log("filename: " + delta.filename.new); |
| + if (delta.filename && delta.filename.new.indexOf(kFilename) !== -1) { |
| + downloads.onChanged.removeListener(myListener); |
| + callbackCompleted(); |
| + } |
| + } |
| + downloads.onChanged.addListener(myListener); |
| + downloads.download( |
| + {'url': SAFE_FAST_URL, 'filename': kFilename}, |
| + chrome.test.callback(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| + function downloadOnCreated() { |
| + // Test that the onCreated event fires when we start a download. |
| + var download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + var createdCompleted = chrome.test.callbackAdded(); |
| + function createdListener(item) { |
| + if (item.id == download_id) { |
| + createdCompleted(); |
| + downloads.onCreated.removeListener(createdListener); |
| + } |
| + }; |
| + downloads.onCreated.addListener(createdListener); |
| + downloads.download( |
| + {'url': SAFE_FAST_URL}, |
| + chrome.test.callback(function(id) { |
| + chrome.test.assertEq(download_id, id); |
| })); |
| - // TODO(benjhayden): Test the filename using onChanged. |
| }, |
| + |
| function downloadInvalidFilename() { |
| + // Test that we disallow invalid filenames for new downloads. |
| downloads.download( |
| {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, |
| chrome.test.callbackFail(ERROR_GENERIC)); |
| - // TODO(benjhayden): Give a better error message. |
| }, |
| + |
| function downloadEmpty() { |
| assertThrows(('Invalid value for argument 1. Property \'url\': ' + |
| 'Property is required.'), |
| downloads.download, {}); |
| }, |
| + |
| function downloadInvalidSaveAs() { |
| assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + |
| 'Expected \'boolean\' but got \'string\'.'), |
| downloads.download, |
| {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); |
| }, |
| + |
| function downloadInvalidHeadersOption() { |
| assertThrows(('Invalid value for argument 1. Property \'headers\': ' + |
| 'Expected \'array\' but got \'string\'.'), |
| downloads.download, |
| {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
| }, |
| + |
| function downloadInvalidURL() { |
| + // Test that download() requires a valid url. |
|
cbentzel
2012/02/10 22:32:52
This comment isn't really needed.
|
| downloads.download( |
| {'url': 'foo bar'}, |
| chrome.test.callbackFail(ERROR_INVALID_URL)); |
| }, |
| + |
| function downloadInvalidMethod() { |
| assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
| 'Value must be one of: [GET, POST].'), |
| downloads.download, |
| {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
| }, |
| - function downloadSimple() { |
| - downloads.download( |
| - {'url': SAFE_FAST_URL}, |
| - chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - })); |
| - }, |
| - function downloadPost() { |
| - downloads.download( |
| - {'url': getURL('files/post/downloads/a_zip_file.js'), |
| - 'method': 'POST', |
| - 'body': 'WOOHOO'}, |
| - chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - })); |
| - }, |
| - function downloadHeader() { |
| - downloads.download( |
| - {'url': SAFE_FAST_URL, |
| - 'headers': [{'name': 'Foo', 'value': 'bar'}] |
| - }, |
| - chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - })); |
| - }, |
| - function downloadInterrupted() { |
| - // TODO(benjhayden): Find a suitable URL and test that this id is |
| - // eventually interrupted using onChanged. |
| - downloads.download( |
| - {'url': SAFE_FAST_URL}, |
| - chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - })); |
| - }, |
| + |
| function downloadInvalidHeader() { |
| + // Test that download() disallows setting the Cookie header. |
| downloads.download( |
| {'url': SAFE_FAST_URL, |
| 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] |
| }, |
| chrome.test.callbackFail(ERROR_GENERIC)); |
| - // TODO(benjhayden): Give a better error message. |
| }, |
| + |
| function downloadGetFileIconInvalidOptions() { |
| assertThrows(('Invalid value for argument 2. Property \'cat\': ' + |
| 'Unexpected property.'), |
| downloads.getFileIcon, |
| -1, {cat: 'mouse'}); |
| }, |
| + |
| function downloadGetFileIconInvalidSize() { |
| assertThrows(('Invalid value for argument 2. Property \'size\': ' + |
| 'Value must be one of: [16, 32].'), |
| downloads.getFileIcon, -1, {size: 31}); |
| }, |
| + |
| function downloadGetFileIconInvalidId() { |
| downloads.getFileIcon(-42, {size: 32}, |
| chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| }, |
| - function downloadNoComplete() { |
| - // This is used partly to test cleanUp. |
| - downloads.download( |
| - {'url': NEVER_FINISH_URL}, |
| - chrome.test.callbackPass(function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - })); |
| - }, |
| + |
| function downloadPauseInvalidId() { |
| downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| }, |
| + |
| function downloadPauseInvalidType() { |
| assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 'but got \'string\'.'), |
| downloads.pause, |
| 'foo'); |
| }, |
| + |
| function downloadResumeInvalidId() { |
| downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); |
| }, |
| + |
| function downloadResumeInvalidType() { |
| assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 'but got \'string\'.'), |
| downloads.resume, |
| 'foo'); |
| }, |
| + |
| function downloadCancelInvalidId() { |
| // Canceling a non-existent download is not considered an error. |
| downloads.cancel(-42, chrome.test.callbackPass(function() {})); |
| }, |
| + |
| function downloadCancelInvalidType() { |
| assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + |
| 'but got \'string\'.'), |
| downloads.cancel, 'foo'); |
| }, |
| + |
| + function downloadNoComplete() { |
| + // This is used partly to test cleanUp. |
| + var download_id = getNextId(); |
| + console.log("id: " + download_id); |
| + downloads.download( |
| + {'url': NEVER_FINISH_URL}, |
| + chrome.test.callbackPass(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
| + })); |
| + }, |
| + |
| function cleanUp() { |
| // cleanUp must come last. It clears out all in-progress downloads |
| // so the browser can shutdown cleanly. |