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

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

Powered by Google App Engine
This is Rietveld 408576698