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

Side by Side 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: @r184166 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/string_util.h"
13 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
14 #include "chrome/browser/download/download_file_icon_extractor.h" 15 #include "chrome/browser/download/download_file_icon_extractor.h"
15 #include "chrome/browser/download/download_service.h" 16 #include "chrome/browser/download/download_service.h"
16 #include "chrome/browser/download/download_service_factory.h" 17 #include "chrome/browser/download/download_service_factory.h"
17 #include "chrome/browser/download/download_test_file_activity_observer.h" 18 #include "chrome/browser/download/download_test_file_activity_observer.h"
18 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 19 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
19 #include "chrome/browser/extensions/event_names.h" 20 #include "chrome/browser/extensions/event_names.h"
20 #include "chrome/browser/extensions/extension_apitest.h" 21 #include "chrome/browser/extensions/extension_apitest.h"
21 #include "chrome/browser/extensions/extension_function_test_utils.h" 22 #include "chrome/browser/extensions/extension_function_test_utils.h"
23 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/history/download_row.h" 24 #include "chrome/browser/history/download_row.h"
23 #include "chrome/browser/net/url_request_mock_util.h" 25 #include "chrome/browser/net/url_request_mock_util.h"
24 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h" 27 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h" 28 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/common/chrome_notification_types.h" 29 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
29 #include "chrome/test/base/in_process_browser_test.h" 31 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h" 32 #include "chrome/test/base/ui_test_utils.h"
31 #include "content/public/browser/browser_context.h" 33 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/download_item.h" 35 #include "content/public/browser/download_item.h"
34 #include "content/public/browser/download_manager.h" 36 #include "content/public/browser/download_manager.h"
35 #include "content/public/browser/notification_service.h" 37 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/storage_partition.h" 38 #include "content/public/browser/storage_partition.h"
37 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_contents.h"
38 #include "content/public/common/page_transition_types.h" 40 #include "content/public/common/page_transition_types.h"
39 #include "content/public/test/download_test_observer.h" 41 #include "content/public/test/download_test_observer.h"
42 #include "content/public/test/test_file_error_injector.h"
40 #include "content/test/net/url_request_slow_download_job.h" 43 #include "content/test/net/url_request_slow_download_job.h"
41 #include "net/base/data_url.h" 44 #include "net/base/data_url.h"
42 #include "net/base/net_util.h" 45 #include "net/base/net_util.h"
43 #include "net/url_request/url_request.h" 46 #include "net/url_request/url_request.h"
44 #include "net/url_request/url_request_context.h" 47 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_job.h" 48 #include "net/url_request/url_request_job.h"
46 #include "net/url_request/url_request_job_factory.h" 49 #include "net/url_request/url_request_job_factory.h"
47 #include "net/url_request/url_request_job_factory_impl.h" 50 #include "net/url_request/url_request_job_factory_impl.h"
48 #include "webkit/blob/blob_data.h" 51 #include "webkit/blob/blob_data.h"
49 #include "webkit/blob/blob_storage_controller.h" 52 #include "webkit/blob/blob_storage_controller.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 args_(base::JSONReader::Read(json_args)), 99 args_(base::JSONReader::Read(json_args)),
97 caught_(caught) { 100 caught_(caught) {
98 } 101 }
99 102
100 const base::Time& caught() { return caught_; } 103 const base::Time& caught() { return caught_; }
101 104
102 bool Equals(const Event& other) { 105 bool Equals(const Event& other) {
103 if ((profile_ != other.profile_) || 106 if ((profile_ != other.profile_) ||
104 (event_name_ != other.event_name_)) 107 (event_name_ != other.event_name_))
105 return false; 108 return false;
106 if ((event_name_ == events::kOnDownloadCreated || 109 if ((StartsWithASCII(event_name_,
107 event_name_ == events::kOnDownloadChanged) && 110 events::kOnDownloadDeterminingFilename,
111 true/*case_sensitive*/) ||
112 (event_name_ == events::kOnDownloadCreated) ||
113 (event_name_ == events::kOnDownloadChanged)) &&
108 args_.get() && 114 args_.get() &&
109 other.args_.get()) { 115 other.args_.get()) {
110 base::ListValue* left_list = NULL; 116 base::ListValue* left_list = NULL;
111 base::DictionaryValue* left_dict = NULL; 117 base::DictionaryValue* left_dict = NULL;
112 base::ListValue* right_list = NULL; 118 base::ListValue* right_list = NULL;
113 base::DictionaryValue* right_dict = NULL; 119 base::DictionaryValue* right_dict = NULL;
114 if (!args_->GetAsList(&left_list) || 120 if (!args_->GetAsList(&left_list) ||
115 !other.args_->GetAsList(&right_list) || 121 !other.args_->GetAsList(&right_list) ||
116 !left_list->GetDictionary(0, &left_dict) || 122 !left_list->GetDictionary(0, &left_dict) ||
117 !right_list->GetDictionary(0, &right_dict)) 123 !right_list->GetDictionary(0, &right_dict))
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 }; 257 };
252 258
253 void LoadExtension(const char* name) { 259 void LoadExtension(const char* name) {
254 // Store the created Extension object so that we can attach it to 260 // Store the created Extension object so that we can attach it to
255 // ExtensionFunctions. Also load the extension in incognito profiles for 261 // ExtensionFunctions. Also load the extension in incognito profiles for
256 // testing incognito. 262 // testing incognito.
257 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name)); 263 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name));
258 CHECK(extension_); 264 CHECK(extension_);
259 } 265 }
260 266
267 void AddFilenameDeterminer(int sub_event_id) {
268 ExtensionDownloadsEventRouter* router =
269 DownloadServiceFactory::GetForProfile(current_browser()->profile())->
270 GetExtensionEventRouter();
271 std::string event = base::StringPrintf(
272 "%s/%d",
273 extensions::event_names::kOnDownloadDeterminingFilename,
274 sub_event_id);
275 router->OnListenerAdded(extensions::EventListenerInfo(
276 event, GetExtensionId()));
277 }
278
279 void RemoveFilenameDeterminer(int sub_event_id) {
280 ExtensionDownloadsEventRouter* router =
281 DownloadServiceFactory::GetForProfile(current_browser()->profile())->
282 GetExtensionEventRouter();
283 std::string event = base::StringPrintf(
284 "%s/%d",
285 extensions::event_names::kOnDownloadDeterminingFilename,
286 sub_event_id);
287 router->OnListenerRemoved(extensions::EventListenerInfo(
288 event, GetExtensionId()));
289 }
290
261 Browser* current_browser() { return current_browser_; } 291 Browser* current_browser() { return current_browser_; }
262 292
263 // InProcessBrowserTest 293 // InProcessBrowserTest
264 virtual void SetUpOnMainThread() OVERRIDE { 294 virtual void SetUpOnMainThread() OVERRIDE {
265 BrowserThread::PostTask( 295 BrowserThread::PostTask(
266 BrowserThread::IO, FROM_HERE, 296 BrowserThread::IO, FROM_HERE,
267 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 297 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
268 InProcessBrowserTest::SetUpOnMainThread(); 298 InProcessBrowserTest::SetUpOnMainThread();
269 GoOnTheRecord(); 299 GoOnTheRecord();
270 CreateAndSetDownloadsDirectory(); 300 CreateAndSetDownloadsDirectory();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 " \"state\": {" 336 " \"state\": {"
307 " \"previous\": \"in_progress\"," 337 " \"previous\": \"in_progress\","
308 " \"current\": \"interrupted\"}}]", 338 " \"current\": \"interrupted\"}}]",
309 item->GetId(), 339 item->GetId(),
310 expected_error)); 340 expected_error));
311 } 341 }
312 342
313 std::string GetExtensionURL() { 343 std::string GetExtensionURL() {
314 return extension_->url().spec(); 344 return extension_->url().spec();
315 } 345 }
346 std::string GetExtensionId() {
347 return extension_->id();
348 }
349 void DisableExtension() {
350 browser()->profile()->GetExtensionService()->DisableExtension(
351 GetExtensionId(), extensions::Extension::DISABLE_USER_ACTION);
352 }
353 void EnableExtension() {
354 browser()->profile()->GetExtensionService()->EnableExtension(
355 GetExtensionId());
356 }
316 357
317 std::string GetFilename(const char* path) { 358 std::string GetFilename(const char* path) {
318 std::string result = 359 std::string result =
319 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 360 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
320 #if defined(OS_WIN) 361 #if defined(OS_WIN)
321 for (std::string::size_type next = result.find("\\"); 362 for (std::string::size_type next = result.find("\\");
322 next != std::string::npos; 363 next != std::string::npos;
323 next = result.find("\\", next)) { 364 next = result.find("\\", next)) {
324 result.replace(next, 1, "\\\\"); 365 result.replace(next, 1, "\\\\");
325 next += 2; 366 next += 2;
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 return blob_storage_controller_.get(); 709 return blob_storage_controller_.get();
669 } 710 }
670 711
671 private: 712 private:
672 net::URLRequestJobFactoryImpl job_factory_; 713 net::URLRequestJobFactoryImpl job_factory_;
673 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_; 714 scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_;
674 715
675 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext); 716 DISALLOW_COPY_AND_ASSIGN(TestURLRequestContext);
676 }; 717 };
677 718
678 // TODO(benjhayden): Comment. 719 // Writes an HTML5 file so that it can be downloaded.
679 class HTML5FileWriter { 720 class HTML5FileWriter {
680 public: 721 public:
681 HTML5FileWriter( 722 HTML5FileWriter(
682 Profile* profile, 723 Profile* profile,
683 const std::string& filename, 724 const std::string& filename,
684 const std::string& origin, 725 const std::string& origin,
685 DownloadsEventsListener* events_listener, 726 DownloadsEventsListener* events_listener,
686 const std::string& payload) 727 const std::string& payload)
687 : profile_(profile), 728 : profile_(profile),
688 filename_(filename), 729 filename_(filename),
(...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 1496
1456 // Test that we can start a download and that the correct sequence of events is 1497 // Test that we can start a download and that the correct sequence of events is
1457 // fired for it. 1498 // fired for it.
1458 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1499 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1459 DownloadExtensionTest_Download_Basic) { 1500 DownloadExtensionTest_Download_Basic) {
1460 LoadExtension("downloads_split"); 1501 LoadExtension("downloads_split");
1461 CHECK(StartTestServer()); 1502 CHECK(StartTestServer());
1462 std::string download_url = test_server()->GetURL("slow?0").spec(); 1503 std::string download_url = test_server()->GetURL("slow?0").spec();
1463 GoOnTheRecord(); 1504 GoOnTheRecord();
1464 1505
1506 // Start downloading a file.
1465 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1507 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1466 new DownloadsDownloadFunction(), base::StringPrintf( 1508 new DownloadsDownloadFunction(), base::StringPrintf(
1467 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1509 "[{\"url\": \"%s\"}]", download_url.c_str())));
1468 ASSERT_TRUE(result.get()); 1510 ASSERT_TRUE(result.get());
1469 int result_id = -1; 1511 int result_id = -1;
1470 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1512 ASSERT_TRUE(result->GetAsInteger(&result_id));
1471 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1513 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1472 ASSERT_TRUE(item); 1514 ASSERT_TRUE(item);
1473 ScopedCancellingItem canceller(item); 1515 ScopedCancellingItem canceller(item);
1474 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1516 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 20 matching lines...) Expand all
1495 1537
1496 // Test that we can start a download from an incognito context, and that the 1538 // Test that we can start a download from an incognito context, and that the
1497 // download knows that it's incognito. 1539 // download knows that it's incognito.
1498 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1540 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1499 DownloadExtensionTest_Download_Incognito) { 1541 DownloadExtensionTest_Download_Incognito) {
1500 LoadExtension("downloads_split"); 1542 LoadExtension("downloads_split");
1501 CHECK(StartTestServer()); 1543 CHECK(StartTestServer());
1502 GoOffTheRecord(); 1544 GoOffTheRecord();
1503 std::string download_url = test_server()->GetURL("slow?0").spec(); 1545 std::string download_url = test_server()->GetURL("slow?0").spec();
1504 1546
1547 // Start downloading a file.
1505 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1548 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1506 new DownloadsDownloadFunction(), base::StringPrintf( 1549 new DownloadsDownloadFunction(), base::StringPrintf(
1507 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1550 "[{\"url\": \"%s\"}]", download_url.c_str())));
1508 ASSERT_TRUE(result.get()); 1551 ASSERT_TRUE(result.get());
1509 int result_id = -1; 1552 int result_id = -1;
1510 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1553 ASSERT_TRUE(result->GetAsInteger(&result_id));
1511 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1554 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1512 ASSERT_TRUE(item); 1555 ASSERT_TRUE(item);
1513 ScopedCancellingItem canceller(item); 1556 ScopedCancellingItem canceller(item);
1514 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1557 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1683 " \"current\": \"complete\"}}]", 1726 " \"current\": \"complete\"}}]",
1684 result_id, 1727 result_id,
1685 GetFilename("slow.txt.crdownload").c_str(), 1728 GetFilename("slow.txt.crdownload").c_str(),
1686 GetFilename("slow.txt").c_str()))); 1729 GetFilename("slow.txt").c_str())));
1687 } 1730 }
1688 1731
1689 // Valid data URLs are valid URLs. 1732 // Valid data URLs are valid URLs.
1690 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1733 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1691 DownloadExtensionTest_Download_DataURL) { 1734 DownloadExtensionTest_Download_DataURL) {
1692 LoadExtension("downloads_split"); 1735 LoadExtension("downloads_split");
1693 CHECK(StartTestServer());
1694 std::string download_url = "data:text/plain,hello"; 1736 std::string download_url = "data:text/plain,hello";
1695 GoOnTheRecord(); 1737 GoOnTheRecord();
1696 1738
1697 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1739 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1698 new DownloadsDownloadFunction(), base::StringPrintf( 1740 new DownloadsDownloadFunction(), base::StringPrintf(
1699 "[{\"url\": \"%s\"," 1741 "[{\"url\": \"%s\","
1700 " \"filename\": \"data.txt\"}]", download_url.c_str()))); 1742 " \"filename\": \"data.txt\"}]", download_url.c_str())));
1701 ASSERT_TRUE(result.get()); 1743 ASSERT_TRUE(result.get());
1702 int result_id = -1; 1744 int result_id = -1;
1703 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1745 ASSERT_TRUE(result->GetAsInteger(&result_id));
(...skipping 27 matching lines...) Expand all
1731 // Disabled due to crbug.com/175711 1773 // Disabled due to crbug.com/175711
1732 #define MAYBE_DownloadExtensionTest_Download_File \ 1774 #define MAYBE_DownloadExtensionTest_Download_File \
1733 DISABLED_DownloadExtensionTest_Download_File 1775 DISABLED_DownloadExtensionTest_Download_File
1734 #else 1776 #else
1735 #define MAYBE_DownloadExtensionTest_Download_File \ 1777 #define MAYBE_DownloadExtensionTest_Download_File \
1736 DownloadExtensionTest_Download_File 1778 DownloadExtensionTest_Download_File
1737 #endif 1779 #endif
1738 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1780 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1739 MAYBE_DownloadExtensionTest_Download_File) { 1781 MAYBE_DownloadExtensionTest_Download_File) {
1740 GoOnTheRecord(); 1782 GoOnTheRecord();
1741 CHECK(StartTestServer());
1742 LoadExtension("downloads_split"); 1783 LoadExtension("downloads_split");
1743 std::string download_url = "file:///"; 1784 std::string download_url = "file:///";
1744 #if defined(OS_WIN) 1785 #if defined(OS_WIN)
1745 download_url += "C:/"; 1786 download_url += "C:/";
1746 #endif 1787 #endif
1747 1788
1748 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1789 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1749 new DownloadsDownloadFunction(), base::StringPrintf( 1790 new DownloadsDownloadFunction(), base::StringPrintf(
1750 "[{\"url\": \"%s\"," 1791 "[{\"url\": \"%s\","
1751 " \"filename\": \"file.txt\"}]", download_url.c_str()))); 1792 " \"filename\": \"file.txt\"}]", download_url.c_str())));
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2138 " \"state\": {" 2179 " \"state\": {"
2139 " \"previous\": \"in_progress\"," 2180 " \"previous\": \"in_progress\","
2140 " \"current\": \"complete\"}}]", 2181 " \"current\": \"complete\"}}]",
2141 result_id, 2182 result_id,
2142 GetFilename("on_record.txt.crdownload").c_str(), 2183 GetFilename("on_record.txt.crdownload").c_str(),
2143 GetFilename("on_record.txt").c_str()))); 2184 GetFilename("on_record.txt").c_str())));
2144 std::string disk_data; 2185 std::string disk_data;
2145 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2186 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2146 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2187 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2147 } 2188 }
2189
2190 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2191 DownloadExtensionTest_OnDeterminingFilename_NoChange) {
2192 GoOnTheRecord();
2193 LoadExtension("downloads_split");
2194 AddFilenameDeterminer(42);
2195 CHECK(StartTestServer());
2196 std::string download_url = test_server()->GetURL("slow?0").spec();
2197
2198 // Start downloading a file.
2199 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2200 new DownloadsDownloadFunction(), base::StringPrintf(
2201 "[{\"url\": \"%s\"}]", download_url.c_str())));
2202 ASSERT_TRUE(result.get());
2203 int result_id = -1;
2204 ASSERT_TRUE(result->GetAsInteger(&result_id));
2205 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2206 ASSERT_TRUE(item);
2207 ScopedCancellingItem canceller(item);
2208 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2209
2210 // Wait for the onCreated and onDeterminingFilename events.
2211 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2212 base::StringPrintf("[{\"danger\": \"safe\","
2213 " \"incognito\": false,"
2214 " \"id\": %d,"
2215 " \"mime\": \"text/plain\","
2216 " \"paused\": false,"
2217 " \"url\": \"%s\"}]",
2218 result_id,
2219 download_url.c_str())));
2220 ASSERT_TRUE(WaitFor(
2221 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2222 base::StringPrintf("[{\"id\": %d,"
2223 " \"filename\":\"slow.txt\"}]",
2224 result_id)));
2225 ASSERT_TRUE(item->GetTargetFilePath().empty());
2226 ASSERT_TRUE(item->IsInProgress());
2227
2228 // Respond to the onDeterminingFilename.
2229 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2230 browser()->profile(),
2231 false,
2232 GetExtensionId(),
2233 42,
2234 result_id,
2235 base::FilePath(),
2236 false));
2237
2238 // The download should complete successfully.
2239 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2240 base::StringPrintf("[{\"id\": %d,"
2241 " \"filename\": {"
2242 " \"previous\": \"%s\","
2243 " \"current\": \"%s\"},"
2244 " \"state\": {"
2245 " \"previous\": \"in_progress\","
2246 " \"current\": \"complete\"}}]",
2247 result_id,
2248 GetFilename("slow.txt.crdownload").c_str(),
2249 GetFilename("slow.txt").c_str())));
2250 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2251 base::StringPrintf("[{\"id\": %d,"
2252 " \"state\": {"
2253 " \"previous\": \"in_progress\","
2254 " \"current\": \"complete\"}}]",
2255 result_id)));
2256 }
2257
2258 IN_PROC_BROWSER_TEST_F(
2259 DownloadExtensionTest,
2260 DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) {
2261 GoOnTheRecord();
2262 LoadExtension("downloads_split");
2263 AddFilenameDeterminer(42);
2264 CHECK(StartTestServer());
2265 std::string download_url = test_server()->GetURL("slow?0").spec();
2266
2267 // Start downloading a file.
2268 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2269 new DownloadsDownloadFunction(), base::StringPrintf(
2270 "[{\"url\": \"%s\"}]", download_url.c_str())));
2271 ASSERT_TRUE(result.get());
2272 int result_id = -1;
2273 ASSERT_TRUE(result->GetAsInteger(&result_id));
2274 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2275 ASSERT_TRUE(item);
2276 ScopedCancellingItem canceller(item);
2277 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2278
2279 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2280 base::StringPrintf("[{\"danger\": \"safe\","
2281 " \"incognito\": false,"
2282 " \"id\": %d,"
2283 " \"mime\": \"text/plain\","
2284 " \"paused\": false,"
2285 " \"url\": \"%s\"}]",
2286 result_id,
2287 download_url.c_str())));
2288 ASSERT_TRUE(WaitFor(
2289 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2290 base::StringPrintf("[{\"id\": %d,"
2291 " \"filename\":\"slow.txt\"}]",
2292 result_id)));
2293 ASSERT_TRUE(item->GetTargetFilePath().empty());
2294 ASSERT_TRUE(item->IsInProgress());
2295
2296 // Respond to the onDeterminingFilename.
2297 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2298 browser()->profile(),
2299 false,
2300 GetExtensionId(),
2301 42,
2302 result_id,
2303 base::FilePath(FILE_PATH_LITERAL("overridden.swf")),
2304 false));
2305
2306 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2307 base::StringPrintf("[{\"id\": %d,"
2308 " \"danger\": {"
2309 " \"previous\":\"safe\","
2310 " \"current\":\"file\"},"
2311 " \"dangerAccepted\": {"
2312 " \"current\":false}}]",
2313 result_id)));
2314
2315 item->DangerousDownloadValidated();
2316 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2317 base::StringPrintf("[{\"id\": %d,"
2318 " \"dangerAccepted\": {"
2319 " \"previous\":false,"
2320 " \"current\":true}}]",
2321 result_id)));
2322 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2323 base::StringPrintf("[{\"id\": %d,"
2324 " \"filename\": {"
2325 " \"previous\": \"%s\","
2326 " \"current\": \"%s\"},"
2327 " \"state\": {"
2328 " \"previous\": \"in_progress\","
2329 " \"current\": \"complete\"}}]",
2330 result_id,
2331 GetFilename("overridden.swf.crdownload").c_str(),
2332 GetFilename("overridden.swf").c_str())));
2333 }
2334
2335 IN_PROC_BROWSER_TEST_F(
2336 DownloadExtensionTest,
2337 DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) {
2338 GoOnTheRecord();
2339 LoadExtension("downloads_split");
2340 AddFilenameDeterminer(42);
2341 CHECK(StartTestServer());
2342 std::string download_url = test_server()->GetURL("slow?0").spec();
2343
2344 // Start downloading a file.
2345 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2346 new DownloadsDownloadFunction(), base::StringPrintf(
2347 "[{\"url\": \"%s\"}]", download_url.c_str())));
2348 ASSERT_TRUE(result.get());
2349 int result_id = -1;
2350 ASSERT_TRUE(result->GetAsInteger(&result_id));
2351 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2352 ASSERT_TRUE(item);
2353 ScopedCancellingItem canceller(item);
2354 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2355
2356 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2357 base::StringPrintf("[{\"danger\": \"safe\","
2358 " \"incognito\": false,"
2359 " \"id\": %d,"
2360 " \"mime\": \"text/plain\","
2361 " \"paused\": false,"
2362 " \"url\": \"%s\"}]",
2363 result_id,
2364 download_url.c_str())));
2365 ASSERT_TRUE(WaitFor(
2366 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2367 base::StringPrintf("[{\"id\": %d,"
2368 " \"filename\":\"slow.txt\"}]",
2369 result_id)));
2370 ASSERT_TRUE(item->GetTargetFilePath().empty());
2371 ASSERT_TRUE(item->IsInProgress());
2372
2373 // Respond to the onDeterminingFilename.
2374 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2375 browser()->profile(),
2376 false,
2377 GetExtensionId(),
2378 42,
2379 result_id,
2380 base::FilePath(FILE_PATH_LITERAL("sneaky/../../sneaky.txt")),
2381 false));
2382 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2383 base::StringPrintf("[{\"id\": %d,"
2384 " \"filename\": {"
2385 " \"previous\": \"%s\","
2386 " \"current\": \"%s\"},"
2387 " \"state\": {"
2388 " \"previous\": \"in_progress\","
2389 " \"current\": \"complete\"}}]",
2390 result_id,
2391 GetFilename("slow.txt.crdownload").c_str(),
2392 GetFilename("slow.txt").c_str())));
2393 }
2394
2395 IN_PROC_BROWSER_TEST_F(
2396 DownloadExtensionTest,
2397 DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) {
2398 GoOnTheRecord();
2399 LoadExtension("downloads_split");
2400 AddFilenameDeterminer(42);
2401 CHECK(StartTestServer());
2402 std::string download_url = test_server()->GetURL("slow?0").spec();
2403
2404 // Start downloading a file.
2405 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2406 new DownloadsDownloadFunction(), base::StringPrintf(
2407 "[{\"url\": \"%s\"}]", download_url.c_str())));
2408 ASSERT_TRUE(result.get());
2409 int result_id = -1;
2410 ASSERT_TRUE(result->GetAsInteger(&result_id));
2411 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2412 ASSERT_TRUE(item);
2413 ScopedCancellingItem canceller(item);
2414 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2415
2416 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2417 base::StringPrintf("[{\"danger\": \"safe\","
2418 " \"incognito\": false,"
2419 " \"id\": %d,"
2420 " \"mime\": \"text/plain\","
2421 " \"paused\": false,"
2422 " \"url\": \"%s\"}]",
2423 result_id,
2424 download_url.c_str())));
2425 ASSERT_TRUE(WaitFor(
2426 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2427 base::StringPrintf("[{\"id\": %d,"
2428 " \"filename\":\"slow.txt\"}]",
2429 result_id)));
2430 ASSERT_TRUE(item->GetTargetFilePath().empty());
2431 ASSERT_TRUE(item->IsInProgress());
2432
2433 // Respond to the onDeterminingFilename.
2434 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2435 browser()->profile(),
2436 false,
2437 GetExtensionId(),
2438 42,
2439 result_id,
2440 base::FilePath(FILE_PATH_LITERAL(".")),
2441 false));
2442 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2443 base::StringPrintf("[{\"id\": %d,"
2444 " \"filename\": {"
2445 " \"previous\": \"%s\","
2446 " \"current\": \"%s\"},"
2447 " \"state\": {"
2448 " \"previous\": \"in_progress\","
2449 " \"current\": \"complete\"}}]",
2450 result_id,
2451 GetFilename("slow.txt.crdownload").c_str(),
2452 GetFilename("slow.txt").c_str())));
2453 }
2454
2455 IN_PROC_BROWSER_TEST_F(
2456 DownloadExtensionTest,
2457 DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) {
2458 CHECK(StartTestServer());
2459 GoOnTheRecord();
2460 LoadExtension("downloads_split");
2461 AddFilenameDeterminer(42);
2462 std::string download_url = test_server()->GetURL("slow?0").spec();
2463
2464 // Start downloading a file.
2465 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2466 new DownloadsDownloadFunction(), base::StringPrintf(
2467 "[{\"url\": \"%s\"}]", download_url.c_str())));
2468 ASSERT_TRUE(result.get());
2469 int result_id = -1;
2470 ASSERT_TRUE(result->GetAsInteger(&result_id));
2471 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2472 ASSERT_TRUE(item);
2473 ScopedCancellingItem canceller(item);
2474 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2475
2476 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2477 base::StringPrintf("[{\"danger\": \"safe\","
2478 " \"incognito\": false,"
2479 " \"id\": %d,"
2480 " \"mime\": \"text/plain\","
2481 " \"paused\": false,"
2482 " \"url\": \"%s\"}]",
2483 result_id,
2484 download_url.c_str())));
2485 ASSERT_TRUE(WaitFor(
2486 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2487 base::StringPrintf("[{\"id\": %d,"
2488 " \"filename\":\"slow.txt\"}]",
2489 result_id)));
2490 ASSERT_TRUE(item->GetTargetFilePath().empty());
2491 ASSERT_TRUE(item->IsInProgress());
2492
2493 // Respond to the onDeterminingFilename.
2494 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2495 browser()->profile(),
2496 false,
2497 GetExtensionId(),
2498 42,
2499 result_id,
2500 base::FilePath(FILE_PATH_LITERAL("..")),
2501 false));
2502 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2503 base::StringPrintf("[{\"id\": %d,"
2504 " \"filename\": {"
2505 " \"previous\": \"%s\","
2506 " \"current\": \"%s\"},"
2507 " \"state\": {"
2508 " \"previous\": \"in_progress\","
2509 " \"current\": \"complete\"}}]",
2510 result_id,
2511 GetFilename("slow.txt.crdownload").c_str(),
2512 GetFilename("slow.txt").c_str())));
2513 }
2514
2515 IN_PROC_BROWSER_TEST_F(
2516 DownloadExtensionTest,
2517 DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) {
2518 GoOnTheRecord();
2519 LoadExtension("downloads_split");
2520 AddFilenameDeterminer(42);
2521 CHECK(StartTestServer());
2522 std::string download_url = test_server()->GetURL("slow?0").spec();
2523
2524 // Start downloading a file.
2525 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2526 new DownloadsDownloadFunction(), base::StringPrintf(
2527 "[{\"url\": \"%s\"}]", download_url.c_str())));
2528 ASSERT_TRUE(result.get());
2529 int result_id = -1;
2530 ASSERT_TRUE(result->GetAsInteger(&result_id));
2531 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2532 ASSERT_TRUE(item);
2533 ScopedCancellingItem canceller(item);
2534 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2535
2536 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2537 base::StringPrintf("[{\"danger\": \"safe\","
2538 " \"incognito\": false,"
2539 " \"id\": %d,"
2540 " \"mime\": \"text/plain\","
2541 " \"paused\": false,"
2542 " \"url\": \"%s\"}]",
2543 result_id,
2544 download_url.c_str())));
2545 ASSERT_TRUE(WaitFor(
2546 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2547 base::StringPrintf("[{\"id\": %d,"
2548 " \"filename\":\"slow.txt\"}]",
2549 result_id)));
2550 ASSERT_TRUE(item->GetTargetFilePath().empty());
2551 ASSERT_TRUE(item->IsInProgress());
2552
2553 // Respond to the onDeterminingFilename. Absolute paths should be rejected.
2554 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2555 browser()->profile(),
2556 false,
2557 GetExtensionId(),
2558 42,
2559 result_id,
2560 downloads_directory().Append(FILE_PATH_LITERAL("sneaky.txt")),
2561 false));
2562
2563 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2564 base::StringPrintf("[{\"id\": %d,"
2565 " \"filename\": {"
2566 " \"previous\": \"%s\","
2567 " \"current\": \"%s\"},"
2568 " \"state\": {"
2569 " \"previous\": \"in_progress\","
2570 " \"current\": \"complete\"}}]",
2571 result_id,
2572 GetFilename("slow.txt.crdownload").c_str(),
2573 GetFilename("slow.txt").c_str())));
2574 }
2575
2576 IN_PROC_BROWSER_TEST_F(
2577 DownloadExtensionTest,
2578 DownloadExtensionTest_OnDeterminingFilename_Override) {
2579 GoOnTheRecord();
2580 LoadExtension("downloads_split");
2581 AddFilenameDeterminer(42);
2582 CHECK(StartTestServer());
2583 std::string download_url = test_server()->GetURL("slow?0").spec();
2584
2585 // Start downloading a file.
2586 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2587 new DownloadsDownloadFunction(), base::StringPrintf(
2588 "[{\"url\": \"%s\"}]", download_url.c_str())));
2589 ASSERT_TRUE(result.get());
2590 int result_id = -1;
2591 ASSERT_TRUE(result->GetAsInteger(&result_id));
2592 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2593 ASSERT_TRUE(item);
2594 ScopedCancellingItem canceller(item);
2595 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2596 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2597 base::StringPrintf("[{\"danger\": \"safe\","
2598 " \"incognito\": false,"
2599 " \"id\": %d,"
2600 " \"mime\": \"text/plain\","
2601 " \"paused\": false,"
2602 " \"url\": \"%s\"}]",
2603 result_id,
2604 download_url.c_str())));
2605 ASSERT_TRUE(WaitFor(
2606 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2607 base::StringPrintf("[{\"id\": %d,"
2608 " \"filename\":\"slow.txt\"}]",
2609 result_id)));
2610 ASSERT_TRUE(item->GetTargetFilePath().empty());
2611 ASSERT_TRUE(item->IsInProgress());
2612
2613 // Respond to the onDeterminingFilename.
2614 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2615 browser()->profile(),
2616 false,
2617 GetExtensionId(),
2618 42,
2619 result_id,
2620 base::FilePath(),
2621 false));
2622
2623 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2624 base::StringPrintf("[{\"id\": %d,"
2625 " \"filename\": {"
2626 " \"previous\": \"%s\","
2627 " \"current\": \"%s\"},"
2628 " \"state\": {"
2629 " \"previous\": \"in_progress\","
2630 " \"current\": \"complete\"}}]",
2631 result_id,
2632 GetFilename("slow.txt.crdownload").c_str(),
2633 GetFilename("slow.txt").c_str())));
2634 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2635 base::StringPrintf("[{\"id\": %d,"
2636 " \"state\": {"
2637 " \"previous\": \"in_progress\","
2638 " \"current\": \"complete\"}}]",
2639 result_id)));
2640
2641 // Start downloading a file.
2642 result.reset(RunFunctionAndReturnResult(
2643 new DownloadsDownloadFunction(), base::StringPrintf(
2644 "[{\"url\": \"%s\"}]", download_url.c_str())));
2645 ASSERT_TRUE(result.get());
2646 result_id = -1;
2647 ASSERT_TRUE(result->GetAsInteger(&result_id));
2648 item = GetCurrentManager()->GetDownload(result_id);
2649 ASSERT_TRUE(item);
2650 ScopedCancellingItem canceller2(item);
2651 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2652
2653 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2654 base::StringPrintf("[{\"danger\": \"safe\","
2655 " \"incognito\": false,"
2656 " \"id\": %d,"
2657 " \"mime\": \"text/plain\","
2658 " \"paused\": false,"
2659 " \"url\": \"%s\"}]",
2660 result_id,
2661 download_url.c_str())));
2662 ASSERT_TRUE(WaitFor(
2663 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2664 base::StringPrintf("[{\"id\": %d,"
2665 " \"filename\":\"slow.txt\"}]",
2666 result_id)));
2667 ASSERT_TRUE(item->GetTargetFilePath().empty());
2668 ASSERT_TRUE(item->IsInProgress());
2669
2670 // Respond to the onDeterminingFilename.
2671 // Also test that DetermineFilename allows (chrome) extensions to set
2672 // filenames without (filename) extensions. (Don't ask about v8 extensions or
2673 // python extensions or kernel extensions or firefox extensions...)
2674 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2675 browser()->profile(),
2676 false,
2677 GetExtensionId(),
2678 42,
2679 result_id,
2680 base::FilePath(FILE_PATH_LITERAL("foo")),
2681 true));
2682
2683 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2684 base::StringPrintf("[{\"id\": %d,"
2685 " \"filename\": {"
2686 " \"previous\": \"%s\","
2687 " \"current\": \"%s\"},"
2688 " \"state\": {"
2689 " \"previous\": \"in_progress\","
2690 " \"current\": \"complete\"}}]",
2691 result_id,
2692 GetFilename("foo.crdownload").c_str(),
2693 GetFilename("foo").c_str())));
2694 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2695 base::StringPrintf("[{\"id\": %d,"
2696 " \"state\": {"
2697 " \"previous\": \"in_progress\","
2698 " \"current\": \"complete\"}}]",
2699 result_id)));
2700 }
2701
2702 IN_PROC_BROWSER_TEST_F(
2703 DownloadExtensionTest,
2704 DownloadExtensionTest_OnDeterminingFilename_LaterDeterminerPrecedence) {
2705 CHECK(StartTestServer());
2706 GoOnTheRecord();
2707 LoadExtension("downloads_split");
2708 AddFilenameDeterminer(43);
2709 AddFilenameDeterminer(5);
2710 std::string download_url = test_server()->GetURL("slow?0").spec();
2711
2712 // Start downloading a file.
2713 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2714 new DownloadsDownloadFunction(), base::StringPrintf(
2715 "[{\"url\": \"%s\"}]", download_url.c_str())));
2716 ASSERT_TRUE(result.get());
2717 int result_id = -1;
2718 ASSERT_TRUE(result->GetAsInteger(&result_id));
2719 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2720 ASSERT_TRUE(item);
2721 ScopedCancellingItem canceller(item);
2722 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2723
2724 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2725 base::StringPrintf("[{\"danger\": \"safe\","
2726 " \"incognito\": false,"
2727 " \"id\": %d,"
2728 " \"mime\": \"text/plain\","
2729 " \"paused\": false,"
2730 " \"url\": \"%s\"}]",
2731 result_id,
2732 download_url.c_str())));
2733 ASSERT_TRUE(WaitFor(
2734 std::string(events::kOnDownloadDeterminingFilename) + "/5",
2735 base::StringPrintf("[{\"id\": %d,"
2736 " \"filename\":\"slow.txt\"}]",
2737 result_id)));
2738 ASSERT_TRUE(WaitFor(
2739 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2740 base::StringPrintf("[{\"id\": %d,"
2741 " \"filename\":\"slow.txt\"}]",
2742 result_id)));
2743 ASSERT_TRUE(item->GetTargetFilePath().empty());
2744 ASSERT_TRUE(item->IsInProgress());
2745
2746 // Respond to the onDeterminingFilename.
2747 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2748 browser()->profile(),
2749 false,
2750 GetExtensionId(),
2751 5,
2752 result_id,
2753 base::FilePath(FILE_PATH_LITERAL("5.txt")),
2754 false));
2755 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2756 browser()->profile(),
2757 false,
2758 GetExtensionId(),
2759 43,
2760 result_id,
2761 base::FilePath(FILE_PATH_LITERAL("43.txt")),
2762 false));
2763
2764 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2765 base::StringPrintf("[{\"id\": %d,"
2766 " \"filename\": {"
2767 " \"previous\": \"%s\","
2768 " \"current\": \"%s\"},"
2769 " \"state\": {"
2770 " \"previous\": \"in_progress\","
2771 " \"current\": \"complete\"}}]",
2772 result_id,
2773 GetFilename("43.txt.crdownload").c_str(),
2774 GetFilename("43.txt").c_str())));
2775 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2776 base::StringPrintf("[{\"id\": %d,"
2777 " \"state\": {"
2778 " \"previous\": \"in_progress\","
2779 " \"current\": \"complete\"}}]",
2780 result_id)));
2781
2782 // Start downloading a file.
2783 result.reset(RunFunctionAndReturnResult(
2784 new DownloadsDownloadFunction(), base::StringPrintf(
2785 "[{\"url\": \"%s\"}]", download_url.c_str())));
2786 ASSERT_TRUE(result.get());
2787 result_id = -1;
2788 ASSERT_TRUE(result->GetAsInteger(&result_id));
2789 item = GetCurrentManager()->GetDownload(result_id);
2790 ASSERT_TRUE(item);
2791 ScopedCancellingItem canceller2(item);
2792 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2793
2794 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2795 base::StringPrintf("[{\"danger\": \"safe\","
2796 " \"incognito\": false,"
2797 " \"id\": %d,"
2798 " \"mime\": \"text/plain\","
2799 " \"paused\": false,"
2800 " \"url\": \"%s\"}]",
2801 result_id,
2802 download_url.c_str())));
2803 ASSERT_TRUE(WaitFor(
2804 std::string(events::kOnDownloadDeterminingFilename) + "/5",
2805 base::StringPrintf("[{\"id\": %d,"
2806 " \"filename\":\"slow.txt\"}]",
2807 result_id)));
2808 ASSERT_TRUE(WaitFor(
2809 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2810 base::StringPrintf("[{\"id\": %d,"
2811 " \"filename\":\"slow.txt\"}]",
2812 result_id)));
2813 ASSERT_TRUE(item->GetTargetFilePath().empty());
2814 ASSERT_TRUE(item->IsInProgress());
2815
2816 // Respond to the onDeterminingFilename.
2817 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2818 browser()->profile(),
2819 false,
2820 GetExtensionId(),
2821 43,
2822 result_id,
2823 base::FilePath(FILE_PATH_LITERAL("43b.txt")),
2824 false));
2825 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2826 browser()->profile(),
2827 false,
2828 GetExtensionId(),
2829 5,
2830 result_id,
2831 base::FilePath(FILE_PATH_LITERAL("5b.txt")),
2832 false));
2833
2834 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2835 base::StringPrintf("[{\"id\": %d,"
2836 " \"filename\": {"
2837 " \"previous\": \"%s\","
2838 " \"current\": \"%s\"},"
2839 " \"state\": {"
2840 " \"previous\": \"in_progress\","
2841 " \"current\": \"complete\"}}]",
2842 result_id,
2843 GetFilename("43b.txt.crdownload").c_str(),
2844 GetFilename("43b.txt").c_str())));
2845 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2846 base::StringPrintf("[{\"id\": %d,"
2847 " \"state\": {"
2848 " \"previous\": \"in_progress\","
2849 " \"current\": \"complete\"}}]",
2850 result_id)));
2851 }
2852
2853 // TODO test precedence rules: install_time
2854
2855 IN_PROC_BROWSER_TEST_F(
2856 DownloadExtensionTest,
2857 DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) {
2858 CHECK(StartTestServer());
2859 GoOnTheRecord();
2860 LoadExtension("downloads_split");
2861 AddFilenameDeterminer(42);
2862 AddFilenameDeterminer(43);
2863 std::string download_url = test_server()->GetURL("slow?0").spec();
2864
2865 // Start downloading a file.
2866 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2867 new DownloadsDownloadFunction(), base::StringPrintf(
2868 "[{\"url\": \"%s\"}]", download_url.c_str())));
2869 ASSERT_TRUE(result.get());
2870 int result_id = -1;
2871 ASSERT_TRUE(result->GetAsInteger(&result_id));
2872 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2873 ASSERT_TRUE(item);
2874 ScopedCancellingItem canceller(item);
2875 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2876
2877 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2878 base::StringPrintf("[{\"danger\": \"safe\","
2879 " \"incognito\": false,"
2880 " \"id\": %d,"
2881 " \"mime\": \"text/plain\","
2882 " \"paused\": false,"
2883 " \"url\": \"%s\"}]",
2884 result_id,
2885 download_url.c_str())));
2886 ASSERT_TRUE(WaitFor(
2887 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2888 base::StringPrintf("[{\"id\": %d,"
2889 " \"filename\":\"slow.txt\"}]",
2890 result_id)));
2891 ASSERT_TRUE(item->GetTargetFilePath().empty());
2892 ASSERT_TRUE(item->IsInProgress());
2893
2894 // Respond to the onDeterminingFilename.
2895 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2896 browser()->profile(),
2897 false,
2898 GetExtensionId(),
2899 43,
2900 result_id,
2901 base::FilePath(),
2902 false));
2903
2904 // Remove a determiner while waiting for it.
2905 RemoveFilenameDeterminer(42);
2906
2907 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2908 base::StringPrintf("[{\"id\": %d,"
2909 " \"state\": {"
2910 " \"previous\": \"in_progress\","
2911 " \"current\": \"complete\"}}]",
2912 result_id)));
2913
2914 // Start downloading a file.
2915 result.reset(RunFunctionAndReturnResult(
2916 new DownloadsDownloadFunction(), base::StringPrintf(
2917 "[{\"url\": \"%s\"}]", download_url.c_str())));
2918 ASSERT_TRUE(result.get());
2919 result_id = -1;
2920 ASSERT_TRUE(result->GetAsInteger(&result_id));
2921 item = GetCurrentManager()->GetDownload(result_id);
2922 ASSERT_TRUE(item);
2923 ScopedCancellingItem canceller2(item);
2924 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2925
2926 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2927 base::StringPrintf("[{\"danger\": \"safe\","
2928 " \"incognito\": false,"
2929 " \"id\": %d,"
2930 " \"mime\": \"text/plain\","
2931 " \"paused\": false,"
2932 " \"url\": \"%s\"}]",
2933 result_id,
2934 download_url.c_str())));
2935 ASSERT_TRUE(WaitFor(
2936 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2937 base::StringPrintf("[{\"id\": %d,"
2938 " \"filename\":\"slow.txt\"}]",
2939 result_id)));
2940 ASSERT_TRUE(item->GetTargetFilePath().empty());
2941 ASSERT_TRUE(item->IsInProgress());
2942
2943 // Respond to the onDeterminingFilename.
2944 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2945 browser()->profile(),
2946 false,
2947 GetExtensionId(),
2948 43,
2949 result_id,
2950 base::FilePath(),
2951 false));
2952 // Should not wait for the 42 determiner.
2953
2954 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2955 base::StringPrintf("[{\"id\": %d,"
2956 " \"state\": {"
2957 " \"previous\": \"in_progress\","
2958 " \"current\": \"complete\"}}]",
2959 result_id)));
2960 }
2961
2962 IN_PROC_BROWSER_TEST_F(
2963 DownloadExtensionTest,
2964 DownloadExtensionTest_OnDeterminingFilename_IncognitoSplit) {
2965 LoadExtension("downloads_split");
2966 CHECK(StartTestServer());
2967 std::string download_url = test_server()->GetURL("slow?0").spec();
2968
2969 GoOnTheRecord();
2970 AddFilenameDeterminer(42);
2971
2972 GoOffTheRecord();
2973 AddFilenameDeterminer(5);
2974
2975 // Start an on-record download.
2976 GoOnTheRecord();
2977 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2978 new DownloadsDownloadFunction(), base::StringPrintf(
2979 "[{\"url\": \"%s\"}]", download_url.c_str())));
2980 ASSERT_TRUE(result.get());
2981 int result_id = -1;
2982 ASSERT_TRUE(result->GetAsInteger(&result_id));
2983 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2984 ASSERT_TRUE(item);
2985 ScopedCancellingItem canceller(item);
2986 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2987
2988 // Wait for the onCreated and onDeterminingFilename events.
2989 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2990 base::StringPrintf("[{\"danger\": \"safe\","
2991 " \"incognito\": false,"
2992 " \"id\": %d,"
2993 " \"mime\": \"text/plain\","
2994 " \"paused\": false,"
2995 " \"url\": \"%s\"}]",
2996 result_id,
2997 download_url.c_str())));
2998 ASSERT_TRUE(WaitFor(
2999 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3000 base::StringPrintf("[{\"id\": %d,"
3001 " \"incognito\": false,"
3002 " \"filename\":\"slow.txt\"}]",
3003 result_id)));
3004 ASSERT_TRUE(item->GetTargetFilePath().empty());
3005 ASSERT_TRUE(item->IsInProgress());
3006
3007 // Respond to the onDeterminingFilename events.
3008 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3009 current_browser()->profile(),
3010 false,
3011 GetExtensionId(),
3012 42,
3013 result_id,
3014 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3015 false));
3016
3017 // The download should complete successfully.
3018 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3019 base::StringPrintf("[{\"id\": %d,"
3020 " \"filename\": {"
3021 " \"previous\": \"%s\","
3022 " \"current\": \"%s\"},"
3023 " \"state\": {"
3024 " \"previous\": \"in_progress\","
3025 " \"current\": \"complete\"}}]",
3026 result_id,
3027 GetFilename("42.txt.crdownload").c_str(),
3028 GetFilename("42.txt").c_str())));
3029 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3030 base::StringPrintf("[{\"id\": %d,"
3031 " \"state\": {"
3032 " \"previous\": \"in_progress\","
3033 " \"current\": \"complete\"}}]",
3034 result_id)));
3035
3036 // Start an incognito download for comparison.
3037 GoOffTheRecord();
3038 result.reset(RunFunctionAndReturnResult(
3039 new DownloadsDownloadFunction(), base::StringPrintf(
3040 "[{\"url\": \"%s\"}]", download_url.c_str())));
3041 ASSERT_TRUE(result.get());
3042 result_id = -1;
3043 ASSERT_TRUE(result->GetAsInteger(&result_id));
3044 item = GetCurrentManager()->GetDownload(result_id);
3045 ASSERT_TRUE(item);
3046 ScopedCancellingItem canceller2(item);
3047 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3048
3049 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3050 base::StringPrintf("[{\"danger\": \"safe\","
3051 " \"incognito\": true,"
3052 " \"id\": %d,"
3053 " \"mime\": \"text/plain\","
3054 " \"paused\": false,"
3055 " \"url\": \"%s\"}]",
3056 result_id,
3057 download_url.c_str())));
3058 // On-Record renderers should not see events for off-record items.
3059 ASSERT_TRUE(WaitFor(
3060 std::string(events::kOnDownloadDeterminingFilename) + "/5",
3061 base::StringPrintf("[{\"id\": %d,"
3062 " \"incognito\": true,"
3063 " \"filename\":\"slow.txt\"}]",
3064 result_id)));
3065 ASSERT_TRUE(item->GetTargetFilePath().empty());
3066 ASSERT_TRUE(item->IsInProgress());
3067
3068 // Respond to the onDeterminingFilename.
3069 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3070 current_browser()->profile(),
3071 false,
3072 GetExtensionId(),
3073 5,
3074 result_id,
3075 base::FilePath(FILE_PATH_LITERAL("5.txt")),
3076 false));
3077
3078 // The download should complete successfully.
3079 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3080 base::StringPrintf("[{\"id\": %d,"
3081 " \"filename\": {"
3082 " \"previous\": \"%s\","
3083 " \"current\": \"%s\"},"
3084 " \"state\": {"
3085 " \"previous\": \"in_progress\","
3086 " \"current\": \"complete\"}}]",
3087 result_id,
3088 GetFilename("5.txt.crdownload").c_str(),
3089 GetFilename("5.txt").c_str())));
3090 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3091 base::StringPrintf("[{\"id\": %d,"
3092 " \"state\": {"
3093 " \"previous\": \"in_progress\","
3094 " \"current\": \"complete\"}}]",
3095 result_id)));
3096 }
3097
3098 IN_PROC_BROWSER_TEST_F(
3099 DownloadExtensionTest,
3100 DownloadExtensionTest_OnDeterminingFilename_IncognitoSpanning) {
3101 LoadExtension("downloads_spanning");
3102 CHECK(StartTestServer());
3103 std::string download_url = test_server()->GetURL("slow?0").spec();
3104
3105 GoOnTheRecord();
3106 AddFilenameDeterminer(42);
3107
3108 // There is a single extension renderer that sees both on-record and
3109 // off-record events. The extension functions see the on-record profile with
3110 // include_incognito=true.
3111
3112 // Start an on-record download.
3113 GoOnTheRecord();
3114 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
3115 new DownloadsDownloadFunction(), base::StringPrintf(
3116 "[{\"url\": \"%s\"}]", download_url.c_str())));
3117 ASSERT_TRUE(result.get());
3118 int result_id = -1;
3119 ASSERT_TRUE(result->GetAsInteger(&result_id));
3120 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
3121 ASSERT_TRUE(item);
3122 ScopedCancellingItem canceller(item);
3123 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3124
3125 // Wait for the onCreated and onDeterminingFilename events.
3126 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3127 base::StringPrintf("[{\"danger\": \"safe\","
3128 " \"incognito\": false,"
3129 " \"id\": %d,"
3130 " \"mime\": \"text/plain\","
3131 " \"paused\": false,"
3132 " \"url\": \"%s\"}]",
3133 result_id,
3134 download_url.c_str())));
3135 ASSERT_TRUE(WaitFor(
3136 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3137 base::StringPrintf("[{\"id\": %d,"
3138 " \"incognito\": false,"
3139 " \"filename\":\"slow.txt\"}]",
3140 result_id)));
3141 ASSERT_TRUE(item->GetTargetFilePath().empty());
3142 ASSERT_TRUE(item->IsInProgress());
3143
3144 // Respond to the onDeterminingFilename events.
3145 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3146 current_browser()->profile(),
3147 true,
3148 GetExtensionId(),
3149 42,
3150 result_id,
3151 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3152 false));
3153
3154 // The download should complete successfully.
3155 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3156 base::StringPrintf("[{\"id\": %d,"
3157 " \"filename\": {"
3158 " \"previous\": \"%s\","
3159 " \"current\": \"%s\"},"
3160 " \"state\": {"
3161 " \"previous\": \"in_progress\","
3162 " \"current\": \"complete\"}}]",
3163 result_id,
3164 GetFilename("42.txt.crdownload").c_str(),
3165 GetFilename("42.txt").c_str())));
3166 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3167 base::StringPrintf("[{\"id\": %d,"
3168 " \"state\": {"
3169 " \"previous\": \"in_progress\","
3170 " \"current\": \"complete\"}}]",
3171 result_id)));
3172
3173 // Start an incognito download for comparison.
3174 GoOffTheRecord();
3175 result.reset(RunFunctionAndReturnResult(
3176 new DownloadsDownloadFunction(), base::StringPrintf(
3177 "[{\"url\": \"%s\"}]", download_url.c_str())));
3178 ASSERT_TRUE(result.get());
3179 result_id = -1;
3180 ASSERT_TRUE(result->GetAsInteger(&result_id));
3181 item = GetCurrentManager()->GetDownload(result_id);
3182 ASSERT_TRUE(item);
3183 ScopedCancellingItem canceller2(item);
3184 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3185
3186 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3187 base::StringPrintf("[{\"danger\": \"safe\","
3188 " \"incognito\": true,"
3189 " \"id\": %d,"
3190 " \"mime\": \"text/plain\","
3191 " \"paused\": false,"
3192 " \"url\": \"%s\"}]",
3193 result_id,
3194 download_url.c_str())));
3195 ASSERT_TRUE(WaitFor(
3196 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3197 base::StringPrintf("[{\"id\": %d,"
3198 " \"incognito\": true,"
3199 " \"filename\":\"slow.txt\"}]",
3200 result_id)));
3201 ASSERT_TRUE(item->GetTargetFilePath().empty());
3202 ASSERT_TRUE(item->IsInProgress());
3203
3204 // Respond to the onDeterminingFilename.
3205 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3206 current_browser()->profile(),
3207 true,
3208 GetExtensionId(),
3209 42,
3210 result_id,
3211 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3212 false));
3213
3214 // The download should complete successfully.
3215 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3216 base::StringPrintf("[{\"id\": %d,"
3217 " \"filename\": {"
3218 " \"previous\": \"%s\","
3219 " \"current\": \"%s\"},"
3220 " \"state\": {"
3221 " \"previous\": \"in_progress\","
3222 " \"current\": \"complete\"}}]",
3223 result_id,
3224 GetFilename("42 (1).txt.crdownload").c_str(),
3225 GetFilename("42 (1).txt").c_str())));
3226 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3227 base::StringPrintf("[{\"id\": %d,"
3228 " \"state\": {"
3229 " \"previous\": \"in_progress\","
3230 " \"current\": \"complete\"}}]",
3231 result_id)));
3232 }
3233
3234 // Test download interruption while extensions determining filename, re-run
3235 // through fan-out and fan-in.
3236 // TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test.
3237 IN_PROC_BROWSER_TEST_F(
3238 DownloadExtensionTest,
3239 FAILS_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) {
3240 LoadExtension("downloads_split");
3241 CHECK(StartTestServer());
3242 std::string download_url = test_server()->GetURL("slow?0").spec();
3243 GoOnTheRecord();
3244 AddFilenameDeterminer(42);
3245
3246 // TODO Interrupt the download instead of responding to onDeterminingFilename.
3247 scoped_refptr<content::TestFileErrorInjector> injector(
3248 content::TestFileErrorInjector::Create(
3249 GetCurrentManager()));
3250 content::TestFileErrorInjector::FileErrorInfo error_info = {
3251 download_url,
3252 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
3253 0,
3254 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
3255 injector->AddError(error_info);
3256 injector->InjectErrors();
3257
3258 // Start a download.
3259 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
3260 new DownloadsDownloadFunction(), base::StringPrintf(
3261 "[{\"url\": \"%s\"}]", download_url.c_str())));
3262 ASSERT_TRUE(result.get());
3263 int result_id = -1;
3264 ASSERT_TRUE(result->GetAsInteger(&result_id));
3265 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
3266 ASSERT_TRUE(item);
3267 ScopedCancellingItem canceller(item);
3268 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3269
3270 // Wait for the onCreated and onDeterminingFilename event.
3271 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3272 base::StringPrintf("[{\"danger\": \"safe\","
3273 " \"incognito\": false,"
3274 " \"id\": %d,"
3275 " \"mime\": \"text/plain\","
3276 " \"paused\": false,"
3277 " \"url\": \"%s\"}]",
3278 result_id,
3279 download_url.c_str())));
3280 ASSERT_TRUE(WaitFor(
3281 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3282 base::StringPrintf("[{\"id\": %d,"
3283 " \"incognito\": false,"
3284 " \"filename\":\"slow.txt\"}]",
3285 result_id)));
3286 ASSERT_TRUE(item->GetTargetFilePath().empty());
3287 ASSERT_TRUE(item->IsInProgress());
3288
3289 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3290 base::StringPrintf("[{\"id\": %d,"
3291 " \"state\": {"
3292 " \"previous\": \"in_progress\","
3293 " \"current\": \"interrupted\"}}]",
3294 result_id)));
3295 ASSERT_TRUE(item->IsInterrupted());
3296 item->ResumeInterruptedDownload();
3297
3298 // Wait for and respond to the onDeterminingFilename event.
3299 ASSERT_TRUE(WaitFor(
3300 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3301 base::StringPrintf("[{\"id\": %d,"
3302 " \"incognito\": false,"
3303 " \"filename\":\"slow.txt\"}]",
3304 result_id)));
3305 ASSERT_TRUE(item->GetTargetFilePath().empty());
3306 ASSERT_TRUE(item->IsInProgress());
3307 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3308 current_browser()->profile(),
3309 false,
3310 GetExtensionId(),
3311 42,
3312 result_id,
3313 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3314 false));
3315
3316 // The download should complete successfully.
3317 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3318 base::StringPrintf("[{\"id\": %d,"
3319 " \"filename\": {"
3320 " \"previous\": \"%s\","
3321 " \"current\": \"%s\"},"
3322 " \"state\": {"
3323 " \"previous\": \"in_progress\","
3324 " \"current\": \"complete\"}}]",
3325 result_id,
3326 GetFilename("42.txt.crdownload").c_str(),
3327 GetFilename("42.txt").c_str())));
3328 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3329 base::StringPrintf("[{\"id\": %d,"
3330 " \"state\": {"
3331 " \"previous\": \"in_progress\","
3332 " \"current\": \"complete\"}}]",
3333 result_id)));
3334 }
3335
3336 IN_PROC_BROWSER_TEST_F(
3337 DownloadExtensionTest,
3338 DownloadExtensionTest_OnDeterminingFilename_DisableExtension) {
3339 GoOnTheRecord();
3340 LoadExtension("downloads_split");
3341 AddFilenameDeterminer(42);
3342 CHECK(StartTestServer());
3343 std::string download_url = test_server()->GetURL("slow?0").spec();
3344
3345 // Start downloading a file.
3346 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
3347 new DownloadsDownloadFunction(), base::StringPrintf(
3348 "[{\"url\": \"%s\"}]", download_url.c_str())));
3349 ASSERT_TRUE(result.get());
3350 int result_id = -1;
3351 ASSERT_TRUE(result->GetAsInteger(&result_id));
3352 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
3353 ASSERT_TRUE(item);
3354 ScopedCancellingItem canceller(item);
3355 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3356
3357 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3358 base::StringPrintf("[{\"danger\": \"safe\","
3359 " \"incognito\": false,"
3360 " \"id\": %d,"
3361 " \"mime\": \"text/plain\","
3362 " \"paused\": false,"
3363 " \"url\": \"%s\"}]",
3364 result_id,
3365 download_url.c_str())));
3366 ASSERT_TRUE(WaitFor(
3367 std::string(events::kOnDownloadDeterminingFilename) + "/42",
3368 base::StringPrintf("[{\"id\": %d,"
3369 " \"filename\":\"slow.txt\"}]",
3370 result_id)));
3371 ASSERT_TRUE(item->GetTargetFilePath().empty());
3372 ASSERT_TRUE(item->IsInProgress());
3373
3374 DisableExtension();
3375 // This happens automatically in production, but not in testing for some
3376 // reason.
3377 RemoveFilenameDeterminer(42);
3378
3379 // The download should complete because it isn't waiting for any other
3380 // determiners.
3381 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3382 base::StringPrintf("[{\"id\": %d,"
3383 " \"state\": {"
3384 " \"previous\": \"in_progress\","
3385 " \"current\": \"complete\"}}]",
3386 result_id)));
3387 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698