| 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 e17d795cc58b9621f850e9d3e915ce4fcc3bc909..566097848a390de45d568d73310e4773993c3d81 100644
|
| --- a/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
|
| +++ b/chrome/browser/extensions/api/downloads/downloads_api_unittest.cc
|
| @@ -9,6 +9,7 @@
|
| #include "base/json/json_reader.h"
|
| #include "base/message_loop.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"
|
| @@ -18,6 +19,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"
|
| @@ -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->event_name,
|
| *content::Details<std::string>(details).ptr(), base::Time::Now());
|
| events_.push_back(new_event);
|
| + LOG(INFO) << "occam caught " << new_event->Debug();
|
| if (waiting_ &&
|
| waiting_for_.get() &&
|
| waiting_for_->Equals(*new_event)) {
|
| @@ -187,6 +194,7 @@ class DownloadsEventsListener : public content::NotificationObserver {
|
| const std::string& event_name,
|
| const std::string& json_args) {
|
| waiting_for_.reset(new Event(profile, event_name, json_args, base::Time()));
|
| + LOG(INFO) << "occam waiting " << waiting_for_->Debug();
|
| for (std::deque<Event*>::const_iterator iter = events_.begin();
|
| iter != events_.end(); ++iter) {
|
| if ((*iter)->Equals(*waiting_for_.get()))
|
| @@ -312,6 +320,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 =
|
| @@ -673,7 +692,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(
|
| @@ -1460,6 +1479,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())));
|
| @@ -1500,6 +1520,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())));
|
| @@ -1688,7 +1709,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();
|
|
|
| @@ -1728,7 +1748,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)
|
| @@ -2135,3 +2154,1222 @@ 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,
|
| + 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,
|
| + 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,
|
| + 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,
|
| + 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,
|
| + 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.
|
| + FilePath override;
|
| +#if defined(OS_WIN)
|
| + override = FilePath(FILE_PATH_LITERAL("C:\\sneaky.dll"));
|
| +#else
|
| + override = 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,
|
| + 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(), 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) + "/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(), 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,
|
| + 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,
|
| + 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(),
|
| + "24"));
|
| +
|
| + // 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,
|
| + 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) + "/24",
|
| + 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(),
|
| + "24",
|
| + result_id,
|
| + FilePath(FILE_PATH_LITERAL("24.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("24.txt.crdownload").c_str(),
|
| + GetFilename("24.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,
|
| + 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,
|
| + 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,
|
| + DISABLED_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,
|
| + 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)));
|
| +}
|
|
|