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..c13f4d5a8a2eae3fbf9c34f1aeb804235bb7e695 100644 |
| --- a/chrome/test/data/extensions/api_test/downloads/test.js |
| +++ b/chrome/test/data/extensions/api_test/downloads/test.js |
| @@ -48,38 +48,191 @@ chrome.test.getConfig(function(testConfig) { |
| var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; |
| var 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. |
| + var 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 downloadPost() { |
| + // Test the |method| download option. |
| + var download_id = getNextId(); |
| + console.log("id: " + download_id); |
| downloads.download( |
| - {'url': SAFE_FAST_URL}, |
| - function(id) { |
| - chrome.test.assertEq(getNextId(), id); |
| - }); |
| + {'url': getURL('files/post/downloads/a_zip_file.js'), |
| + 'method': 'POST', |
| + 'body': 'WOOHOO'}, |
| + chrome.test.callbackPass(function(id) { |
| + console.log("id: " + download_id); |
| + chrome.test.assertEq(download_id, id); |
|
Randy Smith (Not in Mondays)
2012/02/03 20:02:00
Do we have a test confirming that we get the corre
benjhayden
2012/02/10 18:53:07
Done.
|
| + })); |
| }, |
| - function downloadSubDirectoryFilename() { |
| + function downloadHeader() { |
| + // Test the |headers| download option. |
| + var 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. |
| + var 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 completion. |
| + 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. |
| + var 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'; |
| + 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(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\': ' + |
| @@ -99,6 +252,7 @@ chrome.test.getConfig(function(testConfig) { |
| {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); |
| }, |
| function downloadInvalidURL() { |
| + // Test that download() requires a valid url. |
| downloads.download( |
| {'url': 'foo bar'}, |
| chrome.test.callbackFail(ERROR_INVALID_URL)); |
| @@ -109,47 +263,13 @@ chrome.test.getConfig(function(testConfig) { |
| 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\': ' + |
| @@ -166,14 +286,6 @@ chrome.test.getConfig(function(testConfig) { |
| 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)); |
| }, |
| @@ -201,6 +313,17 @@ chrome.test.getConfig(function(testConfig) { |
| '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. |