Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1152)

Unified Diff: chrome/browser/extensions/api/downloads/downloads_api_unittest.cc

Issue 11574006: Implement chrome.downloads.onDeterminingFilename() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r178299 Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
diff --git a/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc b/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
index 2c818f24cf02a079107ccd4501938da2d8841ba6..a795d3620ad3246a2ad89e46e90185bd5cb9010a 100644
--- a/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
+++ b/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
@@ -18,6 +18,7 @@
#include "chrome/browser/extensions/event_names.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
+#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/history/download_row.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/prefs/pref_service.h"
@@ -103,8 +104,11 @@ class DownloadsEventsListener : public content::NotificationObserver {
if ((profile_ != other.profile_) ||
(event_name_ != other.event_name_))
return false;
- if ((event_name_ == events::kOnDownloadCreated ||
- event_name_ == events::kOnDownloadChanged) &&
+ if (((event_name_ == events::kOnDownloadCreated) ||
+ (event_name_.substr(
+ 0, strlen(events::kOnDownloadDeterminingFilename)) ==
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Huh? (If you consider my confusion understandable
benjhayden 2013/01/25 16:21:36 Sorry, this is 'starts with', which I now see is a
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Confusion understanding fail :-}. Why is the file
benjhayden 2013/02/04 19:34:52 The actual name of the event includes the sub_even
+ events::kOnDownloadDeterminingFilename) ||
+ (event_name_ == events::kOnDownloadChanged)) &&
args_.get() &&
other.args_.get()) {
base::ListValue* left_list = NULL;
@@ -312,6 +316,17 @@ class DownloadExtensionTest : public ExtensionApiTest {
std::string GetExtensionURL() {
return extension_->url().spec();
}
+ std::string GetExtensionId() {
+ return extension_->id();
+ }
+ void DisableExtension() {
+ browser()->profile()->GetExtensionService()->DisableExtension(
+ GetExtensionId(), extensions::Extension::DISABLE_USER_ACTION);
+ }
+ void EnableExtension() {
+ browser()->profile()->GetExtensionService()->EnableExtension(
+ GetExtensionId());
+ }
std::string GetFilename(const char* path) {
std::string result =
@@ -1457,6 +1472,7 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
std::string download_url = test_server()->GetURL("slow?0").spec();
GoOnTheRecord();
+ // Start downloading a file.
scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
new DownloadsDownloadFunction(), base::StringPrintf(
"[{\"url\": \"%s\"}]", download_url.c_str())));
@@ -1497,6 +1513,7 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
GoOffTheRecord();
std::string download_url = test_server()->GetURL("slow?0").spec();
+ // Start downloading a file.
scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
new DownloadsDownloadFunction(), base::StringPrintf(
"[{\"url\": \"%s\"}]", download_url.c_str())));
@@ -1685,7 +1702,6 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_DataURL) {
LoadExtension("downloads_split");
- CHECK(StartTestServer());
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 What controls which tests this should be in and wh
benjhayden 2013/01/25 16:21:36 A test needs to start the test server if it plans
std::string download_url = "data:text/plain,hello";
GoOnTheRecord();
@@ -1725,7 +1741,6 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_File) {
GoOnTheRecord();
- CHECK(StartTestServer());
LoadExtension("downloads_split");
std::string download_url = "file:///";
#if defined(OS_WIN)
@@ -2132,3 +2147,823 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
EXPECT_STREQ(kPayloadData, disk_data.c_str());
}
+
+IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_NoChange) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ // Wait for the onCreated and onDeterminingFilename events.
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(),
+ false));
+
+ // The download should complete successfully.
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("overridden.swf")),
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"danger\": {"
+ " \"previous\":\"safe\","
+ " \"current\":\"file\"},"
+ " \"dangerAccepted\": {"
+ " \"current\":false}}]",
+ result_id)));
+
+ item->DangerousDownloadValidated();
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"dangerAccepted\": {"
+ " \"previous\":false,"
+ " \"current\":true}}]",
+ result_id)));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("overridden.swf.crdownload").c_str(),
+ GetFilename("overridden.swf").c_str())));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("sneaky/../../sneaky.txt")),
+ false));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL(".")),
+ false));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 I find myself wondering about the interface here.
benjhayden 2013/01/25 16:21:36 When an extension returns an invalid filename, a r
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Ok, that's fine.
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) {
+ CHECK(StartTestServer());
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("..")),
+ false));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename. Absolute paths should be rejected.
+ FilePath override;
+#if defined(OS_WIN)
+ override = FilePath(FILE_PATH_LITERAL("C:\\sneaky\\sneaky.dll"));
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Unless you do a mkdir, I wouldn't expect these dir
benjhayden 2013/01/25 16:21:36 These paths are invalid because they're absolute.
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Yes, but that's the behavior we're *testing*, so w
benjhayden 2013/02/04 19:34:52 Done.
+#else
+ override = FilePath(FILE_PATH_LITERAL("/sneaky/sneaky"));
+#endif
+ ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ override,
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_Override) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(),
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+
+ // Start downloading a file.
+ result.reset(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller2(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("slow.txt")),
+ true));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("slow.txt.crdownload").c_str(),
+ GetFilename("slow.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_LaterDeterminerPrecedence) {
+ CHECK(StartTestServer());
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "43"));
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/43",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("42.txt")),
+ false));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "43",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("43.txt")),
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("43.txt.crdownload").c_str(),
+ GetFilename("43.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+
+ // Start downloading a file.
+ result.reset(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller2(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/43",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "43",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("43b.txt")),
+ false));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "42",
+ result_id,
+ FilePath(FILE_PATH_LITERAL("42b.txt")),
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\": {"
+ " \"previous\": \"%s\","
+ " \"current\": \"%s\"},"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id,
+ GetFilename("43b.txt.crdownload").c_str(),
+ GetFilename("43b.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}
+
+// TODO test precedence rules: install_time
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) {
+ CHECK(StartTestServer());
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "43"));
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::RemoveFilenameDeterminer(
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Do you want to do this after filename determinatio
benjhayden 2013/01/25 16:21:36 Done.
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/43",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ "43",
+ result_id,
+ FilePath(),
+ false));
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}
+
+// TODO test incognito
+// TODO test incognito=spanning
+// TODO test interruption while extensions determining filename, re-run through
+// fan-out and fan-in
+
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ DownloadExtensionTest_OnDeterminingFilename_DisableExtension) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(),
+ GetExtensionId(),
+ "42"));
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ // Start downloading a file.
+ scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
+ new DownloadsDownloadFunction(), base::StringPrintf(
+ "[{\"url\": \"%s\"}]", download_url.c_str())));
+ ASSERT_TRUE(result.get());
+ int result_id = -1;
+ ASSERT_TRUE(result->GetAsInteger(&result_id));
+ DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
+ ASSERT_TRUE(item);
+ ScopedCancellingItem canceller(item);
+ ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
+ base::StringPrintf("[{\"danger\": \"safe\","
+ " \"incognito\": false,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ DisableExtension();
+
+ // The download should complete because it isn't waiting for any other
+ // determiners.
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}

Powered by Google App Engine
This is Rietveld 408576698