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

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: @r178299 Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View 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/stl_util.h" 11 #include "base/stl_util.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "chrome/browser/download/download_file_icon_extractor.h" 13 #include "chrome/browser/download/download_file_icon_extractor.h"
14 #include "chrome/browser/download/download_service.h" 14 #include "chrome/browser/download/download_service.h"
15 #include "chrome/browser/download/download_service_factory.h" 15 #include "chrome/browser/download/download_service_factory.h"
16 #include "chrome/browser/download/download_test_file_chooser_observer.h" 16 #include "chrome/browser/download/download_test_file_chooser_observer.h"
17 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 17 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
18 #include "chrome/browser/extensions/event_names.h" 18 #include "chrome/browser/extensions/event_names.h"
19 #include "chrome/browser/extensions/extension_apitest.h" 19 #include "chrome/browser/extensions/extension_apitest.h"
20 #include "chrome/browser/extensions/extension_function_test_utils.h" 20 #include "chrome/browser/extensions/extension_function_test_utils.h"
21 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/history/download_row.h" 22 #include "chrome/browser/history/download_row.h"
22 #include "chrome/browser/net/url_request_mock_util.h" 23 #include "chrome/browser/net/url_request_mock_util.h"
23 #include "chrome/browser/prefs/pref_service.h" 24 #include "chrome/browser/prefs/pref_service.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h" 26 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h" 27 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/common/chrome_notification_types.h" 28 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
29 #include "chrome/test/base/in_process_browser_test.h" 30 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/ui_test_utils.h" 31 #include "chrome/test/base/ui_test_utils.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 args_(base::JSONReader::Read(json_args)), 97 args_(base::JSONReader::Read(json_args)),
97 caught_(caught) { 98 caught_(caught) {
98 } 99 }
99 100
100 const base::Time& caught() { return caught_; } 101 const base::Time& caught() { return caught_; }
101 102
102 bool Equals(const Event& other) { 103 bool Equals(const Event& other) {
103 if ((profile_ != other.profile_) || 104 if ((profile_ != other.profile_) ||
104 (event_name_ != other.event_name_)) 105 (event_name_ != other.event_name_))
105 return false; 106 return false;
106 if ((event_name_ == events::kOnDownloadCreated || 107 if (((event_name_ == events::kOnDownloadCreated) ||
107 event_name_ == events::kOnDownloadChanged) && 108 (event_name_.substr(
109 0, strlen(events::kOnDownloadDeterminingFilename)) ==
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Huh? (If you consider my confusion understandable
benjhayden 2013/01/25 16:21:36 Sorry, this is 'starts with', which I now see is a
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Confusion understanding fail :-}. Why is the file
benjhayden 2013/02/04 19:34:52 The actual name of the event includes the sub_even
110 events::kOnDownloadDeterminingFilename) ||
111 (event_name_ == events::kOnDownloadChanged)) &&
108 args_.get() && 112 args_.get() &&
109 other.args_.get()) { 113 other.args_.get()) {
110 base::ListValue* left_list = NULL; 114 base::ListValue* left_list = NULL;
111 base::DictionaryValue* left_dict = NULL; 115 base::DictionaryValue* left_dict = NULL;
112 base::ListValue* right_list = NULL; 116 base::ListValue* right_list = NULL;
113 base::DictionaryValue* right_dict = NULL; 117 base::DictionaryValue* right_dict = NULL;
114 if (!args_->GetAsList(&left_list) || 118 if (!args_->GetAsList(&left_list) ||
115 !other.args_->GetAsList(&right_list) || 119 !other.args_->GetAsList(&right_list) ||
116 !left_list->GetDictionary(0, &left_dict) || 120 !left_list->GetDictionary(0, &left_dict) ||
117 !right_list->GetDictionary(0, &right_dict)) 121 !right_list->GetDictionary(0, &right_dict))
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 " \"state\": {" 309 " \"state\": {"
306 " \"previous\": \"in_progress\"," 310 " \"previous\": \"in_progress\","
307 " \"current\": \"interrupted\"}}]", 311 " \"current\": \"interrupted\"}}]",
308 item->GetId(), 312 item->GetId(),
309 expected_error)); 313 expected_error));
310 } 314 }
311 315
312 std::string GetExtensionURL() { 316 std::string GetExtensionURL() {
313 return extension_->url().spec(); 317 return extension_->url().spec();
314 } 318 }
319 std::string GetExtensionId() {
320 return extension_->id();
321 }
322 void DisableExtension() {
323 browser()->profile()->GetExtensionService()->DisableExtension(
324 GetExtensionId(), extensions::Extension::DISABLE_USER_ACTION);
325 }
326 void EnableExtension() {
327 browser()->profile()->GetExtensionService()->EnableExtension(
328 GetExtensionId());
329 }
315 330
316 std::string GetFilename(const char* path) { 331 std::string GetFilename(const char* path) {
317 std::string result = 332 std::string result =
318 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 333 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
319 #if defined(OS_WIN) 334 #if defined(OS_WIN)
320 for (std::string::size_type next = result.find("\\"); 335 for (std::string::size_type next = result.find("\\");
321 next != std::string::npos; 336 next != std::string::npos;
322 next = result.find("\\", next)) { 337 next = result.find("\\", next)) {
323 result.replace(next, 1, "\\\\"); 338 result.replace(next, 1, "\\\\");
324 next += 2; 339 next += 2;
(...skipping 1125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1450 1465
1451 // Test that we can start a download and that the correct sequence of events is 1466 // Test that we can start a download and that the correct sequence of events is
1452 // fired for it. 1467 // fired for it.
1453 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1468 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1454 DownloadExtensionTest_Download_Basic) { 1469 DownloadExtensionTest_Download_Basic) {
1455 LoadExtension("downloads_split"); 1470 LoadExtension("downloads_split");
1456 CHECK(StartTestServer()); 1471 CHECK(StartTestServer());
1457 std::string download_url = test_server()->GetURL("slow?0").spec(); 1472 std::string download_url = test_server()->GetURL("slow?0").spec();
1458 GoOnTheRecord(); 1473 GoOnTheRecord();
1459 1474
1475 // Start downloading a file.
1460 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1476 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1461 new DownloadsDownloadFunction(), base::StringPrintf( 1477 new DownloadsDownloadFunction(), base::StringPrintf(
1462 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1478 "[{\"url\": \"%s\"}]", download_url.c_str())));
1463 ASSERT_TRUE(result.get()); 1479 ASSERT_TRUE(result.get());
1464 int result_id = -1; 1480 int result_id = -1;
1465 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1481 ASSERT_TRUE(result->GetAsInteger(&result_id));
1466 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1482 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1467 ASSERT_TRUE(item); 1483 ASSERT_TRUE(item);
1468 ScopedCancellingItem canceller(item); 1484 ScopedCancellingItem canceller(item);
1469 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1485 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 20 matching lines...) Expand all
1490 1506
1491 // Test that we can start a download from an incognito context, and that the 1507 // Test that we can start a download from an incognito context, and that the
1492 // download knows that it's incognito. 1508 // download knows that it's incognito.
1493 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1509 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1494 DownloadExtensionTest_Download_Incognito) { 1510 DownloadExtensionTest_Download_Incognito) {
1495 LoadExtension("downloads_split"); 1511 LoadExtension("downloads_split");
1496 CHECK(StartTestServer()); 1512 CHECK(StartTestServer());
1497 GoOffTheRecord(); 1513 GoOffTheRecord();
1498 std::string download_url = test_server()->GetURL("slow?0").spec(); 1514 std::string download_url = test_server()->GetURL("slow?0").spec();
1499 1515
1516 // Start downloading a file.
1500 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1517 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1501 new DownloadsDownloadFunction(), base::StringPrintf( 1518 new DownloadsDownloadFunction(), base::StringPrintf(
1502 "[{\"url\": \"%s\"}]", download_url.c_str()))); 1519 "[{\"url\": \"%s\"}]", download_url.c_str())));
1503 ASSERT_TRUE(result.get()); 1520 ASSERT_TRUE(result.get());
1504 int result_id = -1; 1521 int result_id = -1;
1505 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1522 ASSERT_TRUE(result->GetAsInteger(&result_id));
1506 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 1523 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
1507 ASSERT_TRUE(item); 1524 ASSERT_TRUE(item);
1508 ScopedCancellingItem canceller(item); 1525 ScopedCancellingItem canceller(item);
1509 ASSERT_EQ(download_url, item->GetOriginalUrl().spec()); 1526 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
1678 " \"current\": \"complete\"}}]", 1695 " \"current\": \"complete\"}}]",
1679 result_id, 1696 result_id,
1680 GetFilename("slow.txt.crdownload").c_str(), 1697 GetFilename("slow.txt.crdownload").c_str(),
1681 GetFilename("slow.txt").c_str()))); 1698 GetFilename("slow.txt").c_str())));
1682 } 1699 }
1683 1700
1684 // Valid data URLs are valid URLs. 1701 // Valid data URLs are valid URLs.
1685 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1702 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1686 DownloadExtensionTest_Download_DataURL) { 1703 DownloadExtensionTest_Download_DataURL) {
1687 LoadExtension("downloads_split"); 1704 LoadExtension("downloads_split");
1688 CHECK(StartTestServer());
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 What controls which tests this should be in and wh
benjhayden 2013/01/25 16:21:36 A test needs to start the test server if it plans
1689 std::string download_url = "data:text/plain,hello"; 1705 std::string download_url = "data:text/plain,hello";
1690 GoOnTheRecord(); 1706 GoOnTheRecord();
1691 1707
1692 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1708 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1693 new DownloadsDownloadFunction(), base::StringPrintf( 1709 new DownloadsDownloadFunction(), base::StringPrintf(
1694 "[{\"url\": \"%s\"," 1710 "[{\"url\": \"%s\","
1695 " \"filename\": \"data.txt\"}]", download_url.c_str()))); 1711 " \"filename\": \"data.txt\"}]", download_url.c_str())));
1696 ASSERT_TRUE(result.get()); 1712 ASSERT_TRUE(result.get());
1697 int result_id = -1; 1713 int result_id = -1;
1698 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1714 ASSERT_TRUE(result->GetAsInteger(&result_id));
(...skipping 19 matching lines...) Expand all
1718 " \"current\": \"complete\"}}]", 1734 " \"current\": \"complete\"}}]",
1719 result_id, 1735 result_id,
1720 GetFilename("data.txt.crdownload").c_str(), 1736 GetFilename("data.txt.crdownload").c_str(),
1721 GetFilename("data.txt").c_str()))); 1737 GetFilename("data.txt").c_str())));
1722 } 1738 }
1723 1739
1724 // Valid file URLs are valid URLs. 1740 // Valid file URLs are valid URLs.
1725 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1741 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1726 DownloadExtensionTest_Download_File) { 1742 DownloadExtensionTest_Download_File) {
1727 GoOnTheRecord(); 1743 GoOnTheRecord();
1728 CHECK(StartTestServer());
1729 LoadExtension("downloads_split"); 1744 LoadExtension("downloads_split");
1730 std::string download_url = "file:///"; 1745 std::string download_url = "file:///";
1731 #if defined(OS_WIN) 1746 #if defined(OS_WIN)
1732 download_url += "C:/"; 1747 download_url += "C:/";
1733 #endif 1748 #endif
1734 1749
1735 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1750 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1736 new DownloadsDownloadFunction(), base::StringPrintf( 1751 new DownloadsDownloadFunction(), base::StringPrintf(
1737 "[{\"url\": \"%s\"," 1752 "[{\"url\": \"%s\","
1738 " \"filename\": \"file.txt\"}]", download_url.c_str()))); 1753 " \"filename\": \"file.txt\"}]", download_url.c_str())));
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2125 " \"state\": {" 2140 " \"state\": {"
2126 " \"previous\": \"in_progress\"," 2141 " \"previous\": \"in_progress\","
2127 " \"current\": \"complete\"}}]", 2142 " \"current\": \"complete\"}}]",
2128 result_id, 2143 result_id,
2129 GetFilename("on_record.txt.crdownload").c_str(), 2144 GetFilename("on_record.txt.crdownload").c_str(),
2130 GetFilename("on_record.txt").c_str()))); 2145 GetFilename("on_record.txt").c_str())));
2131 std::string disk_data; 2146 std::string disk_data;
2132 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2147 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2133 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2148 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2134 } 2149 }
2150
2151 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2152 DownloadExtensionTest_OnDeterminingFilename_NoChange) {
2153 GoOnTheRecord();
2154 LoadExtension("downloads_split");
2155 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2156 browser()->profile(),
2157 GetExtensionId(),
2158 "42"));
2159 CHECK(StartTestServer());
2160 std::string download_url = test_server()->GetURL("slow?0").spec();
2161
2162 // Start downloading a file.
2163 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2164 new DownloadsDownloadFunction(), base::StringPrintf(
2165 "[{\"url\": \"%s\"}]", download_url.c_str())));
2166 ASSERT_TRUE(result.get());
2167 int result_id = -1;
2168 ASSERT_TRUE(result->GetAsInteger(&result_id));
2169 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2170 ASSERT_TRUE(item);
2171 ScopedCancellingItem canceller(item);
2172 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2173
2174 // Wait for the onCreated and onDeterminingFilename events.
2175 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2176 base::StringPrintf("[{\"danger\": \"safe\","
2177 " \"incognito\": false,"
2178 " \"id\": %d,"
2179 " \"mime\": \"text/plain\","
2180 " \"paused\": false,"
2181 " \"url\": \"%s\"}]",
2182 result_id,
2183 download_url.c_str())));
2184 ASSERT_TRUE(WaitFor(
2185 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2186 base::StringPrintf("[{\"id\": %d,"
2187 " \"filename\":\"slow.txt\"}]",
2188 result_id)));
2189 ASSERT_TRUE(item->GetTargetFilePath().empty());
2190 ASSERT_TRUE(item->IsInProgress());
2191
2192 // Respond to the onDeterminingFilename.
2193 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2194 browser()->profile(),
2195 false,
2196 GetExtensionId(),
2197 "42",
2198 result_id,
2199 FilePath(),
2200 false));
2201
2202 // The download should complete successfully.
2203 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2204 base::StringPrintf("[{\"id\": %d,"
2205 " \"filename\": {"
2206 " \"previous\": \"%s\","
2207 " \"current\": \"%s\"},"
2208 " \"state\": {"
2209 " \"previous\": \"in_progress\","
2210 " \"current\": \"complete\"}}]",
2211 result_id,
2212 GetFilename("slow.txt.crdownload").c_str(),
2213 GetFilename("slow.txt").c_str())));
2214 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2215 base::StringPrintf("[{\"id\": %d,"
2216 " \"state\": {"
2217 " \"previous\": \"in_progress\","
2218 " \"current\": \"complete\"}}]",
2219 result_id)));
2220 }
2221
2222 IN_PROC_BROWSER_TEST_F(
2223 DownloadExtensionTest,
2224 DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) {
2225 GoOnTheRecord();
2226 LoadExtension("downloads_split");
2227 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2228 browser()->profile(),
2229 GetExtensionId(),
2230 "42"));
2231 CHECK(StartTestServer());
2232 std::string download_url = test_server()->GetURL("slow?0").spec();
2233
2234 // Start downloading a file.
2235 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2236 new DownloadsDownloadFunction(), base::StringPrintf(
2237 "[{\"url\": \"%s\"}]", download_url.c_str())));
2238 ASSERT_TRUE(result.get());
2239 int result_id = -1;
2240 ASSERT_TRUE(result->GetAsInteger(&result_id));
2241 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2242 ASSERT_TRUE(item);
2243 ScopedCancellingItem canceller(item);
2244 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2245
2246 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2247 base::StringPrintf("[{\"danger\": \"safe\","
2248 " \"incognito\": false,"
2249 " \"id\": %d,"
2250 " \"mime\": \"text/plain\","
2251 " \"paused\": false,"
2252 " \"url\": \"%s\"}]",
2253 result_id,
2254 download_url.c_str())));
2255 ASSERT_TRUE(WaitFor(
2256 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2257 base::StringPrintf("[{\"id\": %d,"
2258 " \"filename\":\"slow.txt\"}]",
2259 result_id)));
2260 ASSERT_TRUE(item->GetTargetFilePath().empty());
2261 ASSERT_TRUE(item->IsInProgress());
2262
2263 // Respond to the onDeterminingFilename.
2264 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2265 browser()->profile(),
2266 false,
2267 GetExtensionId(),
2268 "42",
2269 result_id,
2270 FilePath(FILE_PATH_LITERAL("overridden.swf")),
2271 false));
2272
2273 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2274 base::StringPrintf("[{\"id\": %d,"
2275 " \"danger\": {"
2276 " \"previous\":\"safe\","
2277 " \"current\":\"file\"},"
2278 " \"dangerAccepted\": {"
2279 " \"current\":false}}]",
2280 result_id)));
2281
2282 item->DangerousDownloadValidated();
2283 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2284 base::StringPrintf("[{\"id\": %d,"
2285 " \"dangerAccepted\": {"
2286 " \"previous\":false,"
2287 " \"current\":true}}]",
2288 result_id)));
2289 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2290 base::StringPrintf("[{\"id\": %d,"
2291 " \"filename\": {"
2292 " \"previous\": \"%s\","
2293 " \"current\": \"%s\"},"
2294 " \"state\": {"
2295 " \"previous\": \"in_progress\","
2296 " \"current\": \"complete\"}}]",
2297 result_id,
2298 GetFilename("overridden.swf.crdownload").c_str(),
2299 GetFilename("overridden.swf").c_str())));
2300 }
2301
2302 IN_PROC_BROWSER_TEST_F(
2303 DownloadExtensionTest,
2304 DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) {
2305 GoOnTheRecord();
2306 LoadExtension("downloads_split");
2307 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2308 browser()->profile(),
2309 GetExtensionId(),
2310 "42"));
2311 CHECK(StartTestServer());
2312 std::string download_url = test_server()->GetURL("slow?0").spec();
2313
2314 // Start downloading a file.
2315 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2316 new DownloadsDownloadFunction(), base::StringPrintf(
2317 "[{\"url\": \"%s\"}]", download_url.c_str())));
2318 ASSERT_TRUE(result.get());
2319 int result_id = -1;
2320 ASSERT_TRUE(result->GetAsInteger(&result_id));
2321 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2322 ASSERT_TRUE(item);
2323 ScopedCancellingItem canceller(item);
2324 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2325
2326 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2327 base::StringPrintf("[{\"danger\": \"safe\","
2328 " \"incognito\": false,"
2329 " \"id\": %d,"
2330 " \"mime\": \"text/plain\","
2331 " \"paused\": false,"
2332 " \"url\": \"%s\"}]",
2333 result_id,
2334 download_url.c_str())));
2335 ASSERT_TRUE(WaitFor(
2336 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2337 base::StringPrintf("[{\"id\": %d,"
2338 " \"filename\":\"slow.txt\"}]",
2339 result_id)));
2340 ASSERT_TRUE(item->GetTargetFilePath().empty());
2341 ASSERT_TRUE(item->IsInProgress());
2342
2343 // Respond to the onDeterminingFilename.
2344 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2345 browser()->profile(),
2346 false,
2347 GetExtensionId(),
2348 "42",
2349 result_id,
2350 FilePath(FILE_PATH_LITERAL("sneaky/../../sneaky.txt")),
2351 false));
2352 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2353 base::StringPrintf("[{\"id\": %d,"
2354 " \"filename\": {"
2355 " \"previous\": \"%s\","
2356 " \"current\": \"%s\"},"
2357 " \"state\": {"
2358 " \"previous\": \"in_progress\","
2359 " \"current\": \"complete\"}}]",
2360 result_id,
2361 GetFilename("slow.txt.crdownload").c_str(),
2362 GetFilename("slow.txt").c_str())));
2363 }
2364
2365 IN_PROC_BROWSER_TEST_F(
2366 DownloadExtensionTest,
2367 DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) {
2368 GoOnTheRecord();
2369 LoadExtension("downloads_split");
2370 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2371 browser()->profile(),
2372 GetExtensionId(),
2373 "42"));
2374 CHECK(StartTestServer());
2375 std::string download_url = test_server()->GetURL("slow?0").spec();
2376
2377 // Start downloading a file.
2378 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2379 new DownloadsDownloadFunction(), base::StringPrintf(
2380 "[{\"url\": \"%s\"}]", download_url.c_str())));
2381 ASSERT_TRUE(result.get());
2382 int result_id = -1;
2383 ASSERT_TRUE(result->GetAsInteger(&result_id));
2384 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2385 ASSERT_TRUE(item);
2386 ScopedCancellingItem canceller(item);
2387 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2388
2389 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2390 base::StringPrintf("[{\"danger\": \"safe\","
2391 " \"incognito\": false,"
2392 " \"id\": %d,"
2393 " \"mime\": \"text/plain\","
2394 " \"paused\": false,"
2395 " \"url\": \"%s\"}]",
2396 result_id,
2397 download_url.c_str())));
2398 ASSERT_TRUE(WaitFor(
2399 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2400 base::StringPrintf("[{\"id\": %d,"
2401 " \"filename\":\"slow.txt\"}]",
2402 result_id)));
2403 ASSERT_TRUE(item->GetTargetFilePath().empty());
2404 ASSERT_TRUE(item->IsInProgress());
2405
2406 // Respond to the onDeterminingFilename.
2407 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2408 browser()->profile(),
2409 false,
2410 GetExtensionId(),
2411 "42",
2412 result_id,
2413 FilePath(FILE_PATH_LITERAL(".")),
2414 false));
2415 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 I find myself wondering about the interface here.
benjhayden 2013/01/25 16:21:36 When an extension returns an invalid filename, a r
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Ok, that's fine.
2416 base::StringPrintf("[{\"id\": %d,"
2417 " \"filename\": {"
2418 " \"previous\": \"%s\","
2419 " \"current\": \"%s\"},"
2420 " \"state\": {"
2421 " \"previous\": \"in_progress\","
2422 " \"current\": \"complete\"}}]",
2423 result_id,
2424 GetFilename("slow.txt.crdownload").c_str(),
2425 GetFilename("slow.txt").c_str())));
2426 }
2427
2428 IN_PROC_BROWSER_TEST_F(
2429 DownloadExtensionTest,
2430 DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) {
2431 CHECK(StartTestServer());
2432 GoOnTheRecord();
2433 LoadExtension("downloads_split");
2434 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2435 browser()->profile(),
2436 GetExtensionId(),
2437 "42"));
2438 std::string download_url = test_server()->GetURL("slow?0").spec();
2439
2440 // Start downloading a file.
2441 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2442 new DownloadsDownloadFunction(), base::StringPrintf(
2443 "[{\"url\": \"%s\"}]", download_url.c_str())));
2444 ASSERT_TRUE(result.get());
2445 int result_id = -1;
2446 ASSERT_TRUE(result->GetAsInteger(&result_id));
2447 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2448 ASSERT_TRUE(item);
2449 ScopedCancellingItem canceller(item);
2450 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2451
2452 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2453 base::StringPrintf("[{\"danger\": \"safe\","
2454 " \"incognito\": false,"
2455 " \"id\": %d,"
2456 " \"mime\": \"text/plain\","
2457 " \"paused\": false,"
2458 " \"url\": \"%s\"}]",
2459 result_id,
2460 download_url.c_str())));
2461 ASSERT_TRUE(WaitFor(
2462 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2463 base::StringPrintf("[{\"id\": %d,"
2464 " \"filename\":\"slow.txt\"}]",
2465 result_id)));
2466 ASSERT_TRUE(item->GetTargetFilePath().empty());
2467 ASSERT_TRUE(item->IsInProgress());
2468
2469 // Respond to the onDeterminingFilename.
2470 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2471 browser()->profile(),
2472 false,
2473 GetExtensionId(),
2474 "42",
2475 result_id,
2476 FilePath(FILE_PATH_LITERAL("..")),
2477 false));
2478 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2479 base::StringPrintf("[{\"id\": %d,"
2480 " \"filename\": {"
2481 " \"previous\": \"%s\","
2482 " \"current\": \"%s\"},"
2483 " \"state\": {"
2484 " \"previous\": \"in_progress\","
2485 " \"current\": \"complete\"}}]",
2486 result_id,
2487 GetFilename("slow.txt.crdownload").c_str(),
2488 GetFilename("slow.txt").c_str())));
2489 }
2490
2491 IN_PROC_BROWSER_TEST_F(
2492 DownloadExtensionTest,
2493 DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) {
2494 GoOnTheRecord();
2495 LoadExtension("downloads_split");
2496 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2497 browser()->profile(),
2498 GetExtensionId(),
2499 "42"));
2500 CHECK(StartTestServer());
2501 std::string download_url = test_server()->GetURL("slow?0").spec();
2502
2503 // Start downloading a file.
2504 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2505 new DownloadsDownloadFunction(), base::StringPrintf(
2506 "[{\"url\": \"%s\"}]", download_url.c_str())));
2507 ASSERT_TRUE(result.get());
2508 int result_id = -1;
2509 ASSERT_TRUE(result->GetAsInteger(&result_id));
2510 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2511 ASSERT_TRUE(item);
2512 ScopedCancellingItem canceller(item);
2513 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2514
2515 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2516 base::StringPrintf("[{\"danger\": \"safe\","
2517 " \"incognito\": false,"
2518 " \"id\": %d,"
2519 " \"mime\": \"text/plain\","
2520 " \"paused\": false,"
2521 " \"url\": \"%s\"}]",
2522 result_id,
2523 download_url.c_str())));
2524 ASSERT_TRUE(WaitFor(
2525 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2526 base::StringPrintf("[{\"id\": %d,"
2527 " \"filename\":\"slow.txt\"}]",
2528 result_id)));
2529 ASSERT_TRUE(item->GetTargetFilePath().empty());
2530 ASSERT_TRUE(item->IsInProgress());
2531
2532 // Respond to the onDeterminingFilename. Absolute paths should be rejected.
2533 FilePath override;
2534 #if defined(OS_WIN)
2535 override = FilePath(FILE_PATH_LITERAL("C:\\sneaky\\sneaky.dll"));
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Unless you do a mkdir, I wouldn't expect these dir
benjhayden 2013/01/25 16:21:36 These paths are invalid because they're absolute.
Randy Smith (Not in Mondays) 2013/01/25 18:22:45 Yes, but that's the behavior we're *testing*, so w
benjhayden 2013/02/04 19:34:52 Done.
2536 #else
2537 override = FilePath(FILE_PATH_LITERAL("/sneaky/sneaky"));
2538 #endif
2539 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2540 browser()->profile(),
2541 false,
2542 GetExtensionId(),
2543 "42",
2544 result_id,
2545 override,
2546 false));
2547
2548 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2549 base::StringPrintf("[{\"id\": %d,"
2550 " \"filename\": {"
2551 " \"previous\": \"%s\","
2552 " \"current\": \"%s\"},"
2553 " \"state\": {"
2554 " \"previous\": \"in_progress\","
2555 " \"current\": \"complete\"}}]",
2556 result_id,
2557 GetFilename("slow.txt.crdownload").c_str(),
2558 GetFilename("slow.txt").c_str())));
2559 }
2560
2561 IN_PROC_BROWSER_TEST_F(
2562 DownloadExtensionTest,
2563 DownloadExtensionTest_OnDeterminingFilename_Override) {
2564 GoOnTheRecord();
2565 LoadExtension("downloads_split");
2566 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2567 browser()->profile(),
2568 GetExtensionId(),
2569 "42"));
2570 CHECK(StartTestServer());
2571 std::string download_url = test_server()->GetURL("slow?0").spec();
2572
2573 // Start downloading a file.
2574 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2575 new DownloadsDownloadFunction(), base::StringPrintf(
2576 "[{\"url\": \"%s\"}]", download_url.c_str())));
2577 ASSERT_TRUE(result.get());
2578 int result_id = -1;
2579 ASSERT_TRUE(result->GetAsInteger(&result_id));
2580 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2581 ASSERT_TRUE(item);
2582 ScopedCancellingItem canceller(item);
2583 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2584 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2585 base::StringPrintf("[{\"danger\": \"safe\","
2586 " \"incognito\": false,"
2587 " \"id\": %d,"
2588 " \"mime\": \"text/plain\","
2589 " \"paused\": false,"
2590 " \"url\": \"%s\"}]",
2591 result_id,
2592 download_url.c_str())));
2593 ASSERT_TRUE(WaitFor(
2594 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2595 base::StringPrintf("[{\"id\": %d,"
2596 " \"filename\":\"slow.txt\"}]",
2597 result_id)));
2598 ASSERT_TRUE(item->GetTargetFilePath().empty());
2599 ASSERT_TRUE(item->IsInProgress());
2600
2601 // Respond to the onDeterminingFilename.
2602 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2603 browser()->profile(),
2604 false,
2605 GetExtensionId(),
2606 "42",
2607 result_id,
2608 FilePath(),
2609 false));
2610
2611 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2612 base::StringPrintf("[{\"id\": %d,"
2613 " \"filename\": {"
2614 " \"previous\": \"%s\","
2615 " \"current\": \"%s\"},"
2616 " \"state\": {"
2617 " \"previous\": \"in_progress\","
2618 " \"current\": \"complete\"}}]",
2619 result_id,
2620 GetFilename("slow.txt.crdownload").c_str(),
2621 GetFilename("slow.txt").c_str())));
2622 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2623 base::StringPrintf("[{\"id\": %d,"
2624 " \"state\": {"
2625 " \"previous\": \"in_progress\","
2626 " \"current\": \"complete\"}}]",
2627 result_id)));
2628
2629 // Start downloading a file.
2630 result.reset(RunFunctionAndReturnResult(
2631 new DownloadsDownloadFunction(), base::StringPrintf(
2632 "[{\"url\": \"%s\"}]", download_url.c_str())));
2633 ASSERT_TRUE(result.get());
2634 result_id = -1;
2635 ASSERT_TRUE(result->GetAsInteger(&result_id));
2636 item = GetCurrentManager()->GetDownload(result_id);
2637 ASSERT_TRUE(item);
2638 ScopedCancellingItem canceller2(item);
2639 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2640
2641 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2642 base::StringPrintf("[{\"danger\": \"safe\","
2643 " \"incognito\": false,"
2644 " \"id\": %d,"
2645 " \"mime\": \"text/plain\","
2646 " \"paused\": false,"
2647 " \"url\": \"%s\"}]",
2648 result_id,
2649 download_url.c_str())));
2650 ASSERT_TRUE(WaitFor(
2651 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2652 base::StringPrintf("[{\"id\": %d,"
2653 " \"filename\":\"slow.txt\"}]",
2654 result_id)));
2655 ASSERT_TRUE(item->GetTargetFilePath().empty());
2656 ASSERT_TRUE(item->IsInProgress());
2657
2658 // Respond to the onDeterminingFilename.
2659 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2660 browser()->profile(),
2661 false,
2662 GetExtensionId(),
2663 "42",
2664 result_id,
2665 FilePath(FILE_PATH_LITERAL("slow.txt")),
2666 true));
2667
2668 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2669 base::StringPrintf("[{\"id\": %d,"
2670 " \"filename\": {"
2671 " \"previous\": \"%s\","
2672 " \"current\": \"%s\"},"
2673 " \"state\": {"
2674 " \"previous\": \"in_progress\","
2675 " \"current\": \"complete\"}}]",
2676 result_id,
2677 GetFilename("slow.txt.crdownload").c_str(),
2678 GetFilename("slow.txt").c_str())));
2679 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2680 base::StringPrintf("[{\"id\": %d,"
2681 " \"state\": {"
2682 " \"previous\": \"in_progress\","
2683 " \"current\": \"complete\"}}]",
2684 result_id)));
2685 }
2686
2687 IN_PROC_BROWSER_TEST_F(
2688 DownloadExtensionTest,
2689 DownloadExtensionTest_OnDeterminingFilename_LaterDeterminerPrecedence) {
2690 CHECK(StartTestServer());
2691 GoOnTheRecord();
2692 LoadExtension("downloads_split");
2693 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2694 browser()->profile(),
2695 GetExtensionId(),
2696 "42"));
2697 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2698 browser()->profile(),
2699 GetExtensionId(),
2700 "43"));
2701 std::string download_url = test_server()->GetURL("slow?0").spec();
2702
2703 // Start downloading a file.
2704 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2705 new DownloadsDownloadFunction(), base::StringPrintf(
2706 "[{\"url\": \"%s\"}]", download_url.c_str())));
2707 ASSERT_TRUE(result.get());
2708 int result_id = -1;
2709 ASSERT_TRUE(result->GetAsInteger(&result_id));
2710 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2711 ASSERT_TRUE(item);
2712 ScopedCancellingItem canceller(item);
2713 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2714
2715 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2716 base::StringPrintf("[{\"danger\": \"safe\","
2717 " \"incognito\": false,"
2718 " \"id\": %d,"
2719 " \"mime\": \"text/plain\","
2720 " \"paused\": false,"
2721 " \"url\": \"%s\"}]",
2722 result_id,
2723 download_url.c_str())));
2724 ASSERT_TRUE(WaitFor(
2725 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2726 base::StringPrintf("[{\"id\": %d,"
2727 " \"filename\":\"slow.txt\"}]",
2728 result_id)));
2729 ASSERT_TRUE(WaitFor(
2730 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2731 base::StringPrintf("[{\"id\": %d,"
2732 " \"filename\":\"slow.txt\"}]",
2733 result_id)));
2734 ASSERT_TRUE(item->GetTargetFilePath().empty());
2735 ASSERT_TRUE(item->IsInProgress());
2736
2737 // Respond to the onDeterminingFilename.
2738 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2739 browser()->profile(),
2740 false,
2741 GetExtensionId(),
2742 "42",
2743 result_id,
2744 FilePath(FILE_PATH_LITERAL("42.txt")),
2745 false));
2746 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2747 browser()->profile(),
2748 false,
2749 GetExtensionId(),
2750 "43",
2751 result_id,
2752 FilePath(FILE_PATH_LITERAL("43.txt")),
2753 false));
2754
2755 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2756 base::StringPrintf("[{\"id\": %d,"
2757 " \"filename\": {"
2758 " \"previous\": \"%s\","
2759 " \"current\": \"%s\"},"
2760 " \"state\": {"
2761 " \"previous\": \"in_progress\","
2762 " \"current\": \"complete\"}}]",
2763 result_id,
2764 GetFilename("43.txt.crdownload").c_str(),
2765 GetFilename("43.txt").c_str())));
2766 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2767 base::StringPrintf("[{\"id\": %d,"
2768 " \"state\": {"
2769 " \"previous\": \"in_progress\","
2770 " \"current\": \"complete\"}}]",
2771 result_id)));
2772
2773 // Start downloading a file.
2774 result.reset(RunFunctionAndReturnResult(
2775 new DownloadsDownloadFunction(), base::StringPrintf(
2776 "[{\"url\": \"%s\"}]", download_url.c_str())));
2777 ASSERT_TRUE(result.get());
2778 result_id = -1;
2779 ASSERT_TRUE(result->GetAsInteger(&result_id));
2780 item = GetCurrentManager()->GetDownload(result_id);
2781 ASSERT_TRUE(item);
2782 ScopedCancellingItem canceller2(item);
2783 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2784
2785 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2786 base::StringPrintf("[{\"danger\": \"safe\","
2787 " \"incognito\": false,"
2788 " \"id\": %d,"
2789 " \"mime\": \"text/plain\","
2790 " \"paused\": false,"
2791 " \"url\": \"%s\"}]",
2792 result_id,
2793 download_url.c_str())));
2794 ASSERT_TRUE(WaitFor(
2795 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2796 base::StringPrintf("[{\"id\": %d,"
2797 " \"filename\":\"slow.txt\"}]",
2798 result_id)));
2799 ASSERT_TRUE(WaitFor(
2800 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2801 base::StringPrintf("[{\"id\": %d,"
2802 " \"filename\":\"slow.txt\"}]",
2803 result_id)));
2804 ASSERT_TRUE(item->GetTargetFilePath().empty());
2805 ASSERT_TRUE(item->IsInProgress());
2806
2807 // Respond to the onDeterminingFilename.
2808 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2809 browser()->profile(),
2810 false,
2811 GetExtensionId(),
2812 "43",
2813 result_id,
2814 FilePath(FILE_PATH_LITERAL("43b.txt")),
2815 false));
2816 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2817 browser()->profile(),
2818 false,
2819 GetExtensionId(),
2820 "42",
2821 result_id,
2822 FilePath(FILE_PATH_LITERAL("42b.txt")),
2823 false));
2824
2825 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2826 base::StringPrintf("[{\"id\": %d,"
2827 " \"filename\": {"
2828 " \"previous\": \"%s\","
2829 " \"current\": \"%s\"},"
2830 " \"state\": {"
2831 " \"previous\": \"in_progress\","
2832 " \"current\": \"complete\"}}]",
2833 result_id,
2834 GetFilename("43b.txt.crdownload").c_str(),
2835 GetFilename("43b.txt").c_str())));
2836 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2837 base::StringPrintf("[{\"id\": %d,"
2838 " \"state\": {"
2839 " \"previous\": \"in_progress\","
2840 " \"current\": \"complete\"}}]",
2841 result_id)));
2842 }
2843
2844 // TODO test precedence rules: install_time
2845
2846 IN_PROC_BROWSER_TEST_F(
2847 DownloadExtensionTest,
2848 DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) {
2849 CHECK(StartTestServer());
2850 GoOnTheRecord();
2851 LoadExtension("downloads_split");
2852 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2853 browser()->profile(),
2854 GetExtensionId(),
2855 "42"));
2856 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2857 browser()->profile(),
2858 GetExtensionId(),
2859 "43"));
2860 std::string download_url = test_server()->GetURL("slow?0").spec();
2861 ASSERT_TRUE(ExtensionDownloadsEventRouter::RemoveFilenameDeterminer(
Randy Smith (Not in Mondays) 2013/01/24 19:12:46 Do you want to do this after filename determinatio
benjhayden 2013/01/25 16:21:36 Done.
2862 browser()->profile(),
2863 GetExtensionId(),
2864 "42"));
2865
2866 // Start downloading a file.
2867 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2868 new DownloadsDownloadFunction(), base::StringPrintf(
2869 "[{\"url\": \"%s\"}]", download_url.c_str())));
2870 ASSERT_TRUE(result.get());
2871 int result_id = -1;
2872 ASSERT_TRUE(result->GetAsInteger(&result_id));
2873 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2874 ASSERT_TRUE(item);
2875 ScopedCancellingItem canceller(item);
2876 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2877
2878 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2879 base::StringPrintf("[{\"danger\": \"safe\","
2880 " \"incognito\": false,"
2881 " \"id\": %d,"
2882 " \"mime\": \"text/plain\","
2883 " \"paused\": false,"
2884 " \"url\": \"%s\"}]",
2885 result_id,
2886 download_url.c_str())));
2887 ASSERT_TRUE(WaitFor(
2888 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2889 base::StringPrintf("[{\"id\": %d,"
2890 " \"filename\":\"slow.txt\"}]",
2891 result_id)));
2892 ASSERT_TRUE(item->GetTargetFilePath().empty());
2893 ASSERT_TRUE(item->IsInProgress());
2894
2895 // Respond to the onDeterminingFilename.
2896 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2897 browser()->profile(),
2898 false,
2899 GetExtensionId(),
2900 "43",
2901 result_id,
2902 FilePath(),
2903 false));
2904
2905 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2906 base::StringPrintf("[{\"id\": %d,"
2907 " \"state\": {"
2908 " \"previous\": \"in_progress\","
2909 " \"current\": \"complete\"}}]",
2910 result_id)));
2911 }
2912
2913 // TODO test incognito
2914 // TODO test incognito=spanning
2915 // TODO test interruption while extensions determining filename, re-run through
2916 // fan-out and fan-in
2917
2918 IN_PROC_BROWSER_TEST_F(
2919 DownloadExtensionTest,
2920 DownloadExtensionTest_OnDeterminingFilename_DisableExtension) {
2921 GoOnTheRecord();
2922 LoadExtension("downloads_split");
2923 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2924 browser()->profile(),
2925 GetExtensionId(),
2926 "42"));
2927 CHECK(StartTestServer());
2928 std::string download_url = test_server()->GetURL("slow?0").spec();
2929
2930 // Start downloading a file.
2931 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2932 new DownloadsDownloadFunction(), base::StringPrintf(
2933 "[{\"url\": \"%s\"}]", download_url.c_str())));
2934 ASSERT_TRUE(result.get());
2935 int result_id = -1;
2936 ASSERT_TRUE(result->GetAsInteger(&result_id));
2937 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2938 ASSERT_TRUE(item);
2939 ScopedCancellingItem canceller(item);
2940 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2941
2942 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2943 base::StringPrintf("[{\"danger\": \"safe\","
2944 " \"incognito\": false,"
2945 " \"id\": %d,"
2946 " \"mime\": \"text/plain\","
2947 " \"paused\": false,"
2948 " \"url\": \"%s\"}]",
2949 result_id,
2950 download_url.c_str())));
2951 ASSERT_TRUE(WaitFor(
2952 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2953 base::StringPrintf("[{\"id\": %d,"
2954 " \"filename\":\"slow.txt\"}]",
2955 result_id)));
2956 ASSERT_TRUE(item->GetTargetFilePath().empty());
2957 ASSERT_TRUE(item->IsInProgress());
2958
2959 DisableExtension();
2960
2961 // The download should complete because it isn't waiting for any other
2962 // determiners.
2963 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2964 base::StringPrintf("[{\"id\": %d,"
2965 " \"state\": {"
2966 " \"previous\": \"in_progress\","
2967 " \"current\": \"complete\"}}]",
2968 result_id)));
2969 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698