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

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: @r183850 Created 7 years, 10 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 d2d6cc91a0ab2f75ca4b5c29d055ef28a1b45d6a..ee7f004b49f877eadc7e2e050d7bc2537da0ade0 100644
--- a/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
+++ b/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
@@ -10,6 +10,7 @@
#include "base/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
+#include "base/string_util.h"
#include "base/stringprintf.h"
#include "chrome/browser/download/download_file_icon_extractor.h"
#include "chrome/browser/download/download_service.h"
@@ -19,6 +20,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/profiles/profile.h"
@@ -37,6 +39,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/common/page_transition_types.h"
#include "content/public/test/download_test_observer.h"
+#include "content/public/test/test_file_error_injector.h"
#include "content/test/net/url_request_slow_download_job.h"
#include "net/base/data_url.h"
#include "net/base/net_util.h"
@@ -103,8 +106,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 ((StartsWithASCII(event_name_,
+ events::kOnDownloadDeterminingFilename,
+ true) ||
+ (event_name_ == events::kOnDownloadCreated) ||
+ (event_name_ == events::kOnDownloadChanged)) &&
args_.get() &&
other.args_.get()) {
base::ListValue* left_list = NULL;
@@ -170,6 +176,7 @@ class DownloadsEventsListener : public content::NotificationObserver {
dns->profile,
dns->event_name,
*content::Details<std::string>(details).ptr(), base::Time::Now());
+ LOG(INFO) << "occam caught " << new_event->Debug();
asanka 2013/02/22 18:55:48 I'm guessing these LOG statements will be removed.
benjhayden 2013/02/22 20:43:23 Done.
events_.push_back(new_event);
if (waiting_ &&
waiting_for_.get() &&
@@ -193,6 +200,7 @@ class DownloadsEventsListener : public content::NotificationObserver {
if ((*iter)->Equals(*waiting_for_.get()))
return true;
}
+ LOG(INFO) << "occam waiting " << waiting_for_->Debug();
waiting_ = true;
content::RunMessageLoop();
bool success = !waiting_;
@@ -313,6 +321,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 =
@@ -675,7 +694,7 @@ class TestURLRequestContext : public net::URLRequestContext {
DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext);
};
-// TODO(benjhayden): Comment.
+// Writes an HTML5 file so that it can be downloaded.
class HTML5FileWriter {
public:
HTML5FileWriter(
@@ -1462,6 +1481,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())));
@@ -1502,6 +1522,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())));
@@ -1690,7 +1711,6 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
DownloadExtensionTest_Download_DataURL) {
LoadExtension("downloads_split");
- CHECK(StartTestServer());
std::string download_url = "data:text/plain,hello";
GoOnTheRecord();
@@ -1738,7 +1758,6 @@ IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
MAYBE_DownloadExtensionTest_Download_File) {
GoOnTheRecord();
- CHECK(StartTestServer());
LoadExtension("downloads_split");
std::string download_url = "file:///";
#if defined(OS_WIN)
@@ -2145,3 +2164,1219 @@ 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(), false, 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,
+ base::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(), false, 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,
+ base::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(), false, 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,
+ base::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(), false, 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,
+ base::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_ParentDirInvalid) {
+ CHECK(StartTestServer());
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(), false, 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,
+ base::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(), false, 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.
+ base::FilePath override;
+#if defined(OS_WIN)
+ override = base::FilePath(FILE_PATH_LITERAL("C:\\sneaky.dll"));
+#else
+ override = base::FilePath(FILE_PATH_LITERAL("/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(), false, 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,
+ base::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,
+ base::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(), false, GetExtensionId(), 5));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(), false, 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) + "/5",
+ 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(),
+ 5,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("5.txt")),
+ false));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ 43,
+ result_id,
+ base::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) + "/5",
+ 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,
+ base::FilePath(FILE_PATH_LITERAL("43b.txt")),
+ false));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ browser()->profile(),
+ false,
+ GetExtensionId(),
+ 5,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("5b.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(), false, GetExtensionId(), 42));
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(), false, 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) + "/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,
+ base::FilePath(),
+ false));
+
+ // Remove a determiner while waiting for it.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::RemoveFilenameDeterminer(
+ browser()->profile(), GetExtensionId(), 42));
+
+ 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) + "/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,
+ base::FilePath(),
+ false));
+ // Should not wait for the 42 determiner.
+
+ 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_IncognitoSplit) {
+ LoadExtension("downloads_split");
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ GoOnTheRecord();
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ current_browser()->profile(), false, GetExtensionId(), 42));
+
+ GoOffTheRecord();
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ current_browser()->profile(), false, GetExtensionId(), 5));
+
+ // Start an on-record download.
+ GoOnTheRecord();
+ 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,"
+ " \"incognito\": false,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename events.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ current_browser()->profile(),
+ false,
+ GetExtensionId(),
+ 42,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("42.txt")),
+ 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("42.txt.crdownload").c_str(),
+ GetFilename("42.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+
+ // Start an incognito download for comparison.
+ GoOffTheRecord();
+ 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\": true,"
+ " \"id\": %d,"
+ " \"mime\": \"text/plain\","
+ " \"paused\": false,"
+ " \"url\": \"%s\"}]",
+ result_id,
+ download_url.c_str())));
+ // On-Record renderers should not see events for off-record items.
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/5",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"incognito\": true,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ current_browser()->profile(),
+ false,
+ GetExtensionId(),
+ 5,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("5.txt")),
+ 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("5.txt.crdownload").c_str(),
+ GetFilename("5.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_IncognitoSpanning) {
+ LoadExtension("downloads_spanning");
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+
+ GoOnTheRecord();
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ current_browser()->profile(), true, GetExtensionId(), 42));
+
+ // There is a single extension renderer that sees both on-record and
+ // off-record events. The extension functions see the on-record profile with
+ // include_incognito=true.
+
+ // Start an on-record download.
+ GoOnTheRecord();
+ 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,"
+ " \"incognito\": false,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename events.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ current_browser()->profile(),
+ true,
+ GetExtensionId(),
+ 42,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("42.txt")),
+ 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("42.txt.crdownload").c_str(),
+ GetFilename("42.txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+
+ // Start an incognito download for comparison.
+ GoOffTheRecord();
+ 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\": true,"
+ " \"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,"
+ " \"incognito\": true,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ // Respond to the onDeterminingFilename.
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ current_browser()->profile(),
+ true,
+ GetExtensionId(),
+ 42,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("42.txt")),
+ 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("42 (1).txt.crdownload").c_str(),
+ GetFilename("42 (1).txt").c_str())));
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"complete\"}}]",
+ result_id)));
+}
+
+// Test download interruption while extensions determining filename, re-run
+// through fan-out and fan-in.
+// TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test.
+IN_PROC_BROWSER_TEST_F(
+ DownloadExtensionTest,
+ FAILS_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) {
+ LoadExtension("downloads_split");
+ CHECK(StartTestServer());
+ std::string download_url = test_server()->GetURL("slow?0").spec();
+ GoOnTheRecord();
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ current_browser()->profile(), false, GetExtensionId(), 42));
+
+ // TODO Interrupt the download instead of responding to onDeterminingFilename.
+ scoped_refptr<content::TestFileErrorInjector> injector(
+ content::TestFileErrorInjector::Create(
+ GetCurrentManager()));
+ content::TestFileErrorInjector::FileErrorInfo error_info = {
+ download_url,
+ content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
+ 0,
+ content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
+ injector->AddError(error_info);
+ injector->InjectErrors();
+
+ // Start a download.
+ 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 event.
+ 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,"
+ " \"incognito\": false,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+
+ ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
+ base::StringPrintf("[{\"id\": %d,"
+ " \"state\": {"
+ " \"previous\": \"in_progress\","
+ " \"current\": \"interrupted\"}}]",
+ result_id)));
+ ASSERT_TRUE(item->IsInterrupted());
+ item->ResumeInterruptedDownload();
+
+ // Wait for and respond to the onDeterminingFilename event.
+ ASSERT_TRUE(WaitFor(
+ std::string(events::kOnDownloadDeterminingFilename) + "/42",
+ base::StringPrintf("[{\"id\": %d,"
+ " \"incognito\": false,"
+ " \"filename\":\"slow.txt\"}]",
+ result_id)));
+ ASSERT_TRUE(item->GetTargetFilePath().empty());
+ ASSERT_TRUE(item->IsInProgress());
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
+ current_browser()->profile(),
+ false,
+ GetExtensionId(),
+ 42,
+ result_id,
+ base::FilePath(FILE_PATH_LITERAL("42.txt")),
+ 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("42.txt.crdownload").c_str(),
+ GetFilename("42.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_DisableExtension) {
+ GoOnTheRecord();
+ LoadExtension("downloads_split");
+ ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
+ browser()->profile(), false, 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