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

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: @r177190 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)) ==
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 const content::NotificationDetails& details) { 166 const content::NotificationDetails& details) {
163 switch (type) { 167 switch (type) {
164 case chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT: 168 case chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT:
165 { 169 {
166 DownloadsNotificationSource* dns = 170 DownloadsNotificationSource* dns =
167 content::Source<DownloadsNotificationSource>(source).ptr(); 171 content::Source<DownloadsNotificationSource>(source).ptr();
168 Event* new_event = new Event( 172 Event* new_event = new Event(
169 dns->profile, 173 dns->profile,
170 dns->event_name, 174 dns->event_name,
171 *content::Details<std::string>(details).ptr(), base::Time::Now()); 175 *content::Details<std::string>(details).ptr(), base::Time::Now());
176 LOG(INFO) << "occam caught " << new_event->Debug();
172 events_.push_back(new_event); 177 events_.push_back(new_event);
173 if (waiting_ && 178 if (waiting_ &&
174 waiting_for_.get() && 179 waiting_for_.get() &&
175 waiting_for_->Equals(*new_event)) { 180 waiting_for_->Equals(*new_event)) {
176 waiting_ = false; 181 waiting_ = false;
177 MessageLoopForUI::current()->Quit(); 182 MessageLoopForUI::current()->Quit();
178 } 183 }
179 break; 184 break;
180 } 185 }
181 default: 186 default:
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 " \"state\": {" 310 " \"state\": {"
306 " \"previous\": \"in_progress\"," 311 " \"previous\": \"in_progress\","
307 " \"current\": \"interrupted\"}}]", 312 " \"current\": \"interrupted\"}}]",
308 item->GetId(), 313 item->GetId(),
309 expected_error)); 314 expected_error));
310 } 315 }
311 316
312 std::string GetExtensionURL() { 317 std::string GetExtensionURL() {
313 return extension_->url().spec(); 318 return extension_->url().spec();
314 } 319 }
320 std::string GetExtensionId() {
321 return extension_->id();
322 }
323 void DisableExtension() {
324 browser()->profile()->GetExtensionService()->DisableExtension(
325 GetExtensionId(), extensions::Extension::DISABLE_USER_ACTION);
326 }
327 void EnableExtension() {
328 browser()->profile()->GetExtensionService()->EnableExtension(
329 GetExtensionId());
330 }
315 331
316 std::string GetFilename(const char* path) { 332 std::string GetFilename(const char* path) {
317 std::string result = 333 std::string result =
318 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 334 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
319 #if defined(OS_WIN) 335 #if defined(OS_WIN)
320 for (std::string::size_type next = result.find("\\"); 336 for (std::string::size_type next = result.find("\\");
321 next != std::string::npos; 337 next != std::string::npos;
322 next = result.find("\\", next)) { 338 next = result.find("\\", next)) {
323 result.replace(next, 1, "\\\\"); 339 result.replace(next, 1, "\\\\");
324 next += 2; 340 next += 2;
(...skipping 1377 matching lines...) Expand 10 before | Expand all | Expand 10 after
1702 " \"current\": \"complete\"}}]", 1718 " \"current\": \"complete\"}}]",
1703 result_id, 1719 result_id,
1704 GetFilename("slow.txt.crdownload").c_str(), 1720 GetFilename("slow.txt.crdownload").c_str(),
1705 GetFilename("slow.txt").c_str()))); 1721 GetFilename("slow.txt").c_str())));
1706 } 1722 }
1707 1723
1708 // Valid data URLs are valid URLs. 1724 // Valid data URLs are valid URLs.
1709 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1725 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1710 DownloadExtensionTest_Download_DataURL) { 1726 DownloadExtensionTest_Download_DataURL) {
1711 LoadExtension("downloads_split"); 1727 LoadExtension("downloads_split");
1712 CHECK(StartTestServer());
1713 std::string download_url = "data:text/plain,hello"; 1728 std::string download_url = "data:text/plain,hello";
1714 GoOnTheRecord(); 1729 GoOnTheRecord();
1715 1730
1716 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1731 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1717 new DownloadsDownloadFunction(), base::StringPrintf( 1732 new DownloadsDownloadFunction(), base::StringPrintf(
1718 "[{\"url\": \"%s\"," 1733 "[{\"url\": \"%s\","
1719 " \"filename\": \"data.txt\"}]", download_url.c_str()))); 1734 " \"filename\": \"data.txt\"}]", download_url.c_str())));
1720 ASSERT_TRUE(result.get()); 1735 ASSERT_TRUE(result.get());
1721 int result_id = -1; 1736 int result_id = -1;
1722 ASSERT_TRUE(result->GetAsInteger(&result_id)); 1737 ASSERT_TRUE(result->GetAsInteger(&result_id));
(...skipping 19 matching lines...) Expand all
1742 " \"current\": \"complete\"}}]", 1757 " \"current\": \"complete\"}}]",
1743 result_id, 1758 result_id,
1744 GetFilename("data.txt.crdownload").c_str(), 1759 GetFilename("data.txt.crdownload").c_str(),
1745 GetFilename("data.txt").c_str()))); 1760 GetFilename("data.txt").c_str())));
1746 } 1761 }
1747 1762
1748 // Valid file URLs are valid URLs. 1763 // Valid file URLs are valid URLs.
1749 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 1764 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
1750 DownloadExtensionTest_Download_File) { 1765 DownloadExtensionTest_Download_File) {
1751 GoOnTheRecord(); 1766 GoOnTheRecord();
1752 CHECK(StartTestServer());
1753 LoadExtension("downloads_split"); 1767 LoadExtension("downloads_split");
1754 std::string download_url = "file:///"; 1768 std::string download_url = "file:///";
1755 #if defined(OS_WIN) 1769 #if defined(OS_WIN)
1756 download_url += "C:/"; 1770 download_url += "C:/";
1757 #endif 1771 #endif
1758 1772
1759 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 1773 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
1760 new DownloadsDownloadFunction(), base::StringPrintf( 1774 new DownloadsDownloadFunction(), base::StringPrintf(
1761 "[{\"url\": \"%s\"," 1775 "[{\"url\": \"%s\","
1762 " \"filename\": \"file.txt\"}]", download_url.c_str()))); 1776 " \"filename\": \"file.txt\"}]", download_url.c_str())));
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2149 " \"state\": {" 2163 " \"state\": {"
2150 " \"previous\": \"in_progress\"," 2164 " \"previous\": \"in_progress\","
2151 " \"current\": \"complete\"}}]", 2165 " \"current\": \"complete\"}}]",
2152 result_id, 2166 result_id,
2153 GetFilename("on_record.txt.crdownload").c_str(), 2167 GetFilename("on_record.txt.crdownload").c_str(),
2154 GetFilename("on_record.txt").c_str()))); 2168 GetFilename("on_record.txt").c_str())));
2155 std::string disk_data; 2169 std::string disk_data;
2156 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2170 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2157 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2171 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2158 } 2172 }
2173
2174 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
2175 DownloadExtensionTest_OnDeterminingFilename_NoChange) {
2176 GoOnTheRecord();
2177 LoadExtension("downloads_split");
2178 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2179 browser()->profile(),
2180 GetExtensionId(),
2181 "42"));
2182 CHECK(StartTestServer());
2183 std::string download_url = test_server()->GetURL("slow?0").spec();
2184 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2185 new DownloadsDownloadFunction(), base::StringPrintf(
2186 "[{\"url\": \"%s\"}]", download_url.c_str())));
2187 ASSERT_TRUE(result.get());
2188 int result_id = -1;
2189 ASSERT_TRUE(result->GetAsInteger(&result_id));
2190 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2191 ASSERT_TRUE(item);
2192 ScopedCancellingItem canceller(item);
2193 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2194 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2195 base::StringPrintf("[{\"danger\": \"safe\","
2196 " \"incognito\": false,"
2197 " \"id\": %d,"
2198 " \"mime\": \"text/plain\","
2199 " \"paused\": false,"
2200 " \"url\": \"%s\"}]",
2201 result_id,
2202 download_url.c_str())));
2203 ASSERT_TRUE(WaitFor(
2204 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2205 base::StringPrintf("[{\"id\": %d,"
2206 " \"filename\":\"slow.txt\"}]",
2207 result_id)));
2208 ASSERT_TRUE(item->GetTargetFilePath().empty());
2209 ASSERT_TRUE(item->IsInProgress());
2210 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2211 browser()->profile(),
2212 false,
2213 GetExtensionId(),
2214 "42",
2215 result_id,
2216 FilePath(),
2217 false));
2218 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2219 base::StringPrintf("[{\"id\": %d,"
2220 " \"filename\": {"
2221 " \"previous\": \"%s\","
2222 " \"current\": \"%s\"},"
2223 " \"state\": {"
2224 " \"previous\": \"in_progress\","
2225 " \"current\": \"complete\"}}]",
2226 result_id,
2227 GetFilename("slow.txt.crdownload").c_str(),
2228 GetFilename("slow.txt").c_str())));
2229 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2230 base::StringPrintf("[{\"id\": %d,"
2231 " \"state\": {"
2232 " \"previous\": \"in_progress\","
2233 " \"current\": \"complete\"}}]",
2234 result_id)));
2235 }
2236
2237 IN_PROC_BROWSER_TEST_F(
2238 DownloadExtensionTest,
2239 DownloadExtensionTest_OnDeterminingFilename_DangerousOverride) {
2240 GoOnTheRecord();
2241 LoadExtension("downloads_split");
2242 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2243 browser()->profile(),
2244 GetExtensionId(),
2245 "42"));
2246 CHECK(StartTestServer());
2247 std::string download_url = test_server()->GetURL("slow?0").spec();
2248 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2249 new DownloadsDownloadFunction(), base::StringPrintf(
2250 "[{\"url\": \"%s\"}]", download_url.c_str())));
2251 ASSERT_TRUE(result.get());
2252 int result_id = -1;
2253 ASSERT_TRUE(result->GetAsInteger(&result_id));
2254 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2255 ASSERT_TRUE(item);
2256 ScopedCancellingItem canceller(item);
2257 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2258 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2259 base::StringPrintf("[{\"danger\": \"safe\","
2260 " \"incognito\": false,"
2261 " \"id\": %d,"
2262 " \"mime\": \"text/plain\","
2263 " \"paused\": false,"
2264 " \"url\": \"%s\"}]",
2265 result_id,
2266 download_url.c_str())));
2267 ASSERT_TRUE(WaitFor(
2268 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2269 base::StringPrintf("[{\"id\": %d,"
2270 " \"filename\":\"slow.txt\"}]",
2271 result_id)));
2272 ASSERT_TRUE(item->GetTargetFilePath().empty());
2273 ASSERT_TRUE(item->IsInProgress());
2274 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2275 browser()->profile(),
2276 false,
2277 GetExtensionId(),
2278 "42",
2279 result_id,
2280 FilePath(FILE_PATH_LITERAL("overridden.swf")),
2281 false));
2282 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2283 base::StringPrintf("[{\"id\": %d,"
2284 " \"danger\": {"
2285 " \"previous\":\"safe\","
2286 " \"current\":\"file\"},"
2287 " \"dangerAccepted\": {"
2288 " \"current\":false}}]",
2289 result_id)));
2290 item->DangerousDownloadValidated();
2291 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2292 base::StringPrintf("[{\"id\": %d,"
2293 " \"dangerAccepted\": {"
2294 " \"previous\":false,"
2295 " \"current\":true}}]",
2296 result_id)));
2297 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2298 base::StringPrintf("[{\"id\": %d,"
2299 " \"filename\": {"
2300 " \"previous\": \"%s\","
2301 " \"current\": \"%s\"},"
2302 " \"state\": {"
2303 " \"previous\": \"in_progress\","
2304 " \"current\": \"complete\"}}]",
2305 result_id,
2306 GetFilename("overridden.swf.crdownload").c_str(),
2307 GetFilename("overridden.swf").c_str())));
2308 }
2309
2310 IN_PROC_BROWSER_TEST_F(
2311 DownloadExtensionTest,
2312 DownloadExtensionTest_OnDeterminingFilename_ReferencesParentInvalid) {
2313 GoOnTheRecord();
2314 LoadExtension("downloads_split");
2315 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2316 browser()->profile(),
2317 GetExtensionId(),
2318 "42"));
2319 CHECK(StartTestServer());
2320 std::string download_url = test_server()->GetURL("slow?0").spec();
2321 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2322 new DownloadsDownloadFunction(), base::StringPrintf(
2323 "[{\"url\": \"%s\"}]", download_url.c_str())));
2324 ASSERT_TRUE(result.get());
2325 int result_id = -1;
2326 ASSERT_TRUE(result->GetAsInteger(&result_id));
2327 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2328 ASSERT_TRUE(item);
2329 ScopedCancellingItem canceller(item);
2330 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2331 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2332 base::StringPrintf("[{\"danger\": \"safe\","
2333 " \"incognito\": false,"
2334 " \"id\": %d,"
2335 " \"mime\": \"text/plain\","
2336 " \"paused\": false,"
2337 " \"url\": \"%s\"}]",
2338 result_id,
2339 download_url.c_str())));
2340 ASSERT_TRUE(WaitFor(
2341 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2342 base::StringPrintf("[{\"id\": %d,"
2343 " \"filename\":\"slow.txt\"}]",
2344 result_id)));
2345 ASSERT_TRUE(item->GetTargetFilePath().empty());
2346 ASSERT_TRUE(item->IsInProgress());
2347 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2348 browser()->profile(),
2349 false,
2350 GetExtensionId(),
2351 "42",
2352 result_id,
2353 FilePath(FILE_PATH_LITERAL("../sneaky.txt")),
2354 false));
2355 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2356 base::StringPrintf("[{\"id\": %d,"
2357 " \"filename\": {"
2358 " \"previous\": \"%s\","
2359 " \"current\": \"%s\"},"
2360 " \"state\": {"
2361 " \"previous\": \"in_progress\","
2362 " \"current\": \"complete\"}}]",
2363 result_id,
2364 GetFilename("slow.txt.crdownload").c_str(),
2365 GetFilename("slow.txt").c_str())));
2366 }
2367
2368 IN_PROC_BROWSER_TEST_F(
2369 DownloadExtensionTest,
2370 DownloadExtensionTest_OnDeterminingFilename_CurDirInvalid) {
2371 GoOnTheRecord();
2372 LoadExtension("downloads_split");
2373 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2374 browser()->profile(),
2375 GetExtensionId(),
2376 "42"));
2377 CHECK(StartTestServer());
2378 std::string download_url = test_server()->GetURL("slow?0").spec();
2379 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2380 new DownloadsDownloadFunction(), base::StringPrintf(
2381 "[{\"url\": \"%s\"}]", download_url.c_str())));
2382 ASSERT_TRUE(result.get());
2383 int result_id = -1;
2384 ASSERT_TRUE(result->GetAsInteger(&result_id));
2385 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2386 ASSERT_TRUE(item);
2387 ScopedCancellingItem canceller(item);
2388 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
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 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2406 browser()->profile(),
2407 false,
2408 GetExtensionId(),
2409 "42",
2410 result_id,
2411 FilePath(FILE_PATH_LITERAL(".")),
2412 false));
2413 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2414 base::StringPrintf("[{\"id\": %d,"
2415 " \"filename\": {"
2416 " \"previous\": \"%s\","
2417 " \"current\": \"%s\"},"
2418 " \"state\": {"
2419 " \"previous\": \"in_progress\","
2420 " \"current\": \"complete\"}}]",
2421 result_id,
2422 GetFilename("slow.txt.crdownload").c_str(),
2423 GetFilename("slow.txt").c_str())));
2424 }
2425
2426 IN_PROC_BROWSER_TEST_F(
2427 DownloadExtensionTest,
2428 DownloadExtensionTest_OnDeterminingFilename_ParentDirInvalid) {
2429 CHECK(StartTestServer());
2430 GoOnTheRecord();
2431 LoadExtension("downloads_split");
2432 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2433 browser()->profile(),
2434 GetExtensionId(),
2435 "42"));
2436 std::string download_url = test_server()->GetURL("slow?0").spec();
2437 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2438 new DownloadsDownloadFunction(), base::StringPrintf(
2439 "[{\"url\": \"%s\"}]", download_url.c_str())));
2440 ASSERT_TRUE(result.get());
2441 int result_id = -1;
2442 ASSERT_TRUE(result->GetAsInteger(&result_id));
2443 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2444 ASSERT_TRUE(item);
2445 ScopedCancellingItem canceller(item);
2446 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2447 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2448 base::StringPrintf("[{\"danger\": \"safe\","
2449 " \"incognito\": false,"
2450 " \"id\": %d,"
2451 " \"mime\": \"text/plain\","
2452 " \"paused\": false,"
2453 " \"url\": \"%s\"}]",
2454 result_id,
2455 download_url.c_str())));
2456 ASSERT_TRUE(WaitFor(
2457 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2458 base::StringPrintf("[{\"id\": %d,"
2459 " \"filename\":\"slow.txt\"}]",
2460 result_id)));
2461 ASSERT_TRUE(item->GetTargetFilePath().empty());
2462 ASSERT_TRUE(item->IsInProgress());
2463 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2464 browser()->profile(),
2465 false,
2466 GetExtensionId(),
2467 "42",
2468 result_id,
2469 FilePath(FILE_PATH_LITERAL("..")),
2470 false));
2471 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2472 base::StringPrintf("[{\"id\": %d,"
2473 " \"filename\": {"
2474 " \"previous\": \"%s\","
2475 " \"current\": \"%s\"},"
2476 " \"state\": {"
2477 " \"previous\": \"in_progress\","
2478 " \"current\": \"complete\"}}]",
2479 result_id,
2480 GetFilename("slow.txt.crdownload").c_str(),
2481 GetFilename("slow.txt").c_str())));
2482 }
2483
2484 IN_PROC_BROWSER_TEST_F(
2485 DownloadExtensionTest,
2486 DownloadExtensionTest_OnDeterminingFilename_AbsPathInvalid) {
2487 GoOnTheRecord();
2488 LoadExtension("downloads_split");
2489 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2490 browser()->profile(),
2491 GetExtensionId(),
2492 "42"));
2493 CHECK(StartTestServer());
2494 std::string download_url = test_server()->GetURL("slow?0").spec();
2495 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2496 new DownloadsDownloadFunction(), base::StringPrintf(
2497 "[{\"url\": \"%s\"}]", download_url.c_str())));
2498 ASSERT_TRUE(result.get());
2499 int result_id = -1;
2500 ASSERT_TRUE(result->GetAsInteger(&result_id));
2501 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2502 ASSERT_TRUE(item);
2503 ScopedCancellingItem canceller(item);
2504 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2505 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2506 base::StringPrintf("[{\"danger\": \"safe\","
2507 " \"incognito\": false,"
2508 " \"id\": %d,"
2509 " \"mime\": \"text/plain\","
2510 " \"paused\": false,"
2511 " \"url\": \"%s\"}]",
2512 result_id,
2513 download_url.c_str())));
2514 ASSERT_TRUE(WaitFor(
2515 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2516 base::StringPrintf("[{\"id\": %d,"
2517 " \"filename\":\"slow.txt\"}]",
2518 result_id)));
2519 ASSERT_TRUE(item->GetTargetFilePath().empty());
2520 ASSERT_TRUE(item->IsInProgress());
2521 ASSERT_FALSE(ExtensionDownloadsEventRouter::DetermineFilename(
2522 browser()->profile(),
2523 false,
2524 GetExtensionId(),
2525 "42",
2526 result_id,
2527 FilePath(FILE_PATH_LITERAL("/bin/cat")),
2528 false));
2529 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2530 base::StringPrintf("[{\"id\": %d,"
2531 " \"filename\": {"
2532 " \"previous\": \"%s\","
2533 " \"current\": \"%s\"},"
2534 " \"state\": {"
2535 " \"previous\": \"in_progress\","
2536 " \"current\": \"complete\"}}]",
2537 result_id,
2538 GetFilename("slow.txt.crdownload").c_str(),
2539 GetFilename("slow.txt").c_str())));
2540 }
2541
2542 IN_PROC_BROWSER_TEST_F(
2543 DownloadExtensionTest,
2544 DownloadExtensionTest_OnDeterminingFilename_Override) {
2545 GoOnTheRecord();
2546 LoadExtension("downloads_split");
2547 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2548 browser()->profile(),
2549 GetExtensionId(),
2550 "42"));
2551 CHECK(StartTestServer());
2552 std::string download_url = test_server()->GetURL("slow?0").spec();
2553
2554 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2555 new DownloadsDownloadFunction(), base::StringPrintf(
2556 "[{\"url\": \"%s\"}]", download_url.c_str())));
2557 ASSERT_TRUE(result.get());
2558 int result_id = -1;
2559 ASSERT_TRUE(result->GetAsInteger(&result_id));
2560 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2561 ASSERT_TRUE(item);
2562 ScopedCancellingItem canceller(item);
2563 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2564 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2565 base::StringPrintf("[{\"danger\": \"safe\","
2566 " \"incognito\": false,"
2567 " \"id\": %d,"
2568 " \"mime\": \"text/plain\","
2569 " \"paused\": false,"
2570 " \"url\": \"%s\"}]",
2571 result_id,
2572 download_url.c_str())));
2573 ASSERT_TRUE(WaitFor(
2574 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2575 base::StringPrintf("[{\"id\": %d,"
2576 " \"filename\":\"slow.txt\"}]",
2577 result_id)));
2578 ASSERT_TRUE(item->GetTargetFilePath().empty());
2579 ASSERT_TRUE(item->IsInProgress());
2580 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2581 browser()->profile(),
2582 false,
2583 GetExtensionId(),
2584 "42",
2585 result_id,
2586 FilePath(),
2587 false));
2588 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2589 base::StringPrintf("[{\"id\": %d,"
2590 " \"filename\": {"
2591 " \"previous\": \"%s\","
2592 " \"current\": \"%s\"},"
2593 " \"state\": {"
2594 " \"previous\": \"in_progress\","
2595 " \"current\": \"complete\"}}]",
2596 result_id,
2597 GetFilename("slow.txt.crdownload").c_str(),
2598 GetFilename("slow.txt").c_str())));
2599 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2600 base::StringPrintf("[{\"id\": %d,"
2601 " \"state\": {"
2602 " \"previous\": \"in_progress\","
2603 " \"current\": \"complete\"}}]",
2604 result_id)));
2605
2606 result.reset(RunFunctionAndReturnResult(
2607 new DownloadsDownloadFunction(), base::StringPrintf(
2608 "[{\"url\": \"%s\"}]", download_url.c_str())));
2609 ASSERT_TRUE(result.get());
2610 result_id = -1;
2611 ASSERT_TRUE(result->GetAsInteger(&result_id));
2612 item = GetCurrentManager()->GetDownload(result_id);
2613 ASSERT_TRUE(item);
2614 ScopedCancellingItem canceller2(item);
2615 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2616 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2617 base::StringPrintf("[{\"danger\": \"safe\","
2618 " \"incognito\": false,"
2619 " \"id\": %d,"
2620 " \"mime\": \"text/plain\","
2621 " \"paused\": false,"
2622 " \"url\": \"%s\"}]",
2623 result_id,
2624 download_url.c_str())));
2625 ASSERT_TRUE(WaitFor(
2626 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2627 base::StringPrintf("[{\"id\": %d,"
2628 " \"filename\":\"slow.txt\"}]",
2629 result_id)));
2630 ASSERT_TRUE(item->GetTargetFilePath().empty());
2631 ASSERT_TRUE(item->IsInProgress());
2632 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2633 browser()->profile(),
2634 false,
2635 GetExtensionId(),
2636 "42",
2637 result_id,
2638 FilePath(FILE_PATH_LITERAL("slow.txt")),
2639 true));
2640 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2641 base::StringPrintf("[{\"id\": %d,"
2642 " \"filename\": {"
2643 " \"previous\": \"%s\","
2644 " \"current\": \"%s\"},"
2645 " \"state\": {"
2646 " \"previous\": \"in_progress\","
2647 " \"current\": \"complete\"}}]",
2648 result_id,
2649 GetFilename("slow.txt.crdownload").c_str(),
2650 GetFilename("slow.txt").c_str())));
2651 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2652 base::StringPrintf("[{\"id\": %d,"
2653 " \"state\": {"
2654 " \"previous\": \"in_progress\","
2655 " \"current\": \"complete\"}}]",
2656 result_id)));
2657 }
2658
2659 IN_PROC_BROWSER_TEST_F(
2660 DownloadExtensionTest,
2661 DownloadExtensionTest_OnDeterminingFilename_LaterDeterminerPrecedence) {
2662 CHECK(StartTestServer());
2663 LOG(INFO) << "occam";
2664 GoOnTheRecord();
2665 LOG(INFO) << "occam";
2666 LoadExtension("downloads_split");
2667 LOG(INFO) << "occam";
2668 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2669 browser()->profile(),
2670 GetExtensionId(),
2671 "42"));
2672 LOG(INFO) << "occam";
2673 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2674 browser()->profile(),
2675 GetExtensionId(),
2676 "43"));
2677 LOG(INFO) << "occam";
2678 std::string download_url = test_server()->GetURL("slow?0").spec();
2679 LOG(INFO) << "occam";
2680 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2681 new DownloadsDownloadFunction(), base::StringPrintf(
2682 "[{\"url\": \"%s\"}]", download_url.c_str())));
2683 LOG(INFO) << "occam";
2684 ASSERT_TRUE(result.get());
2685 LOG(INFO) << "occam";
2686 int result_id = -1;
2687 ASSERT_TRUE(result->GetAsInteger(&result_id));
2688 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2689 ASSERT_TRUE(item);
2690 ScopedCancellingItem canceller(item);
2691 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2692 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2693 base::StringPrintf("[{\"danger\": \"safe\","
2694 " \"incognito\": false,"
2695 " \"id\": %d,"
2696 " \"mime\": \"text/plain\","
2697 " \"paused\": false,"
2698 " \"url\": \"%s\"}]",
2699 result_id,
2700 download_url.c_str())));
2701 ASSERT_TRUE(WaitFor(
2702 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2703 base::StringPrintf("[{\"id\": %d,"
2704 " \"filename\":\"slow.txt\"}]",
2705 result_id)));
2706 ASSERT_TRUE(WaitFor(
2707 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2708 base::StringPrintf("[{\"id\": %d,"
2709 " \"filename\":\"slow.txt\"}]",
2710 result_id)));
2711 ASSERT_TRUE(item->GetTargetFilePath().empty());
2712 ASSERT_TRUE(item->IsInProgress());
2713 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2714 browser()->profile(),
2715 false,
2716 GetExtensionId(),
2717 "42",
2718 result_id,
2719 FilePath(FILE_PATH_LITERAL("42.txt")),
2720 false));
2721 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2722 browser()->profile(),
2723 false,
2724 GetExtensionId(),
2725 "43",
2726 result_id,
2727 FilePath(FILE_PATH_LITERAL("43.txt")),
2728 false));
2729 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2730 base::StringPrintf("[{\"id\": %d,"
2731 " \"filename\": {"
2732 " \"previous\": \"%s\","
2733 " \"current\": \"%s\"},"
2734 " \"state\": {"
2735 " \"previous\": \"in_progress\","
2736 " \"current\": \"complete\"}}]",
2737 result_id,
2738 GetFilename("43.txt.crdownload").c_str(),
2739 GetFilename("43.txt").c_str())));
2740 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2741 base::StringPrintf("[{\"id\": %d,"
2742 " \"state\": {"
2743 " \"previous\": \"in_progress\","
2744 " \"current\": \"complete\"}}]",
2745 result_id)));
2746
2747 result.reset(RunFunctionAndReturnResult(
2748 new DownloadsDownloadFunction(), base::StringPrintf(
2749 "[{\"url\": \"%s\"}]", download_url.c_str())));
2750 ASSERT_TRUE(result.get());
2751 result_id = -1;
2752 ASSERT_TRUE(result->GetAsInteger(&result_id));
2753 item = GetCurrentManager()->GetDownload(result_id);
2754 ASSERT_TRUE(item);
2755 ScopedCancellingItem canceller2(item);
2756 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2757 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2758 base::StringPrintf("[{\"danger\": \"safe\","
2759 " \"incognito\": false,"
2760 " \"id\": %d,"
2761 " \"mime\": \"text/plain\","
2762 " \"paused\": false,"
2763 " \"url\": \"%s\"}]",
2764 result_id,
2765 download_url.c_str())));
2766 ASSERT_TRUE(WaitFor(
2767 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2768 base::StringPrintf("[{\"id\": %d,"
2769 " \"filename\":\"slow.txt\"}]",
2770 result_id)));
2771 ASSERT_TRUE(WaitFor(
2772 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2773 base::StringPrintf("[{\"id\": %d,"
2774 " \"filename\":\"slow.txt\"}]",
2775 result_id)));
2776 ASSERT_TRUE(item->GetTargetFilePath().empty());
2777 ASSERT_TRUE(item->IsInProgress());
2778 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2779 browser()->profile(),
2780 false,
2781 GetExtensionId(),
2782 "43",
2783 result_id,
2784 FilePath(FILE_PATH_LITERAL("43b.txt")),
2785 false));
2786 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2787 browser()->profile(),
2788 false,
2789 GetExtensionId(),
2790 "42",
2791 result_id,
2792 FilePath(FILE_PATH_LITERAL("42b.txt")),
2793 false));
2794 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2795 base::StringPrintf("[{\"id\": %d,"
2796 " \"filename\": {"
2797 " \"previous\": \"%s\","
2798 " \"current\": \"%s\"},"
2799 " \"state\": {"
2800 " \"previous\": \"in_progress\","
2801 " \"current\": \"complete\"}}]",
2802 result_id,
2803 GetFilename("43b.txt.crdownload").c_str(),
2804 GetFilename("43b.txt").c_str())));
2805 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2806 base::StringPrintf("[{\"id\": %d,"
2807 " \"state\": {"
2808 " \"previous\": \"in_progress\","
2809 " \"current\": \"complete\"}}]",
2810 result_id)));
2811 }
2812
2813 // TODO test precedence rules: install_time
2814
2815 IN_PROC_BROWSER_TEST_F(
2816 DownloadExtensionTest,
2817 DownloadExtensionTest_OnDeterminingFilename_RemoveFilenameDeterminer) {
2818 CHECK(StartTestServer());
2819 GoOnTheRecord();
2820 LoadExtension("downloads_split");
2821 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2822 browser()->profile(),
2823 GetExtensionId(),
2824 "42"));
2825 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2826 browser()->profile(),
2827 GetExtensionId(),
2828 "43"));
2829 std::string download_url = test_server()->GetURL("slow?0").spec();
2830 ASSERT_TRUE(ExtensionDownloadsEventRouter::RemoveFilenameDeterminer(
2831 browser()->profile(),
2832 GetExtensionId(),
2833 "42"));
2834 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2835 new DownloadsDownloadFunction(), base::StringPrintf(
2836 "[{\"url\": \"%s\"}]", download_url.c_str())));
2837 ASSERT_TRUE(result.get());
2838 int result_id = -1;
2839 ASSERT_TRUE(result->GetAsInteger(&result_id));
2840 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2841 ASSERT_TRUE(item);
2842 ScopedCancellingItem canceller(item);
2843 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2844 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2845 base::StringPrintf("[{\"danger\": \"safe\","
2846 " \"incognito\": false,"
2847 " \"id\": %d,"
2848 " \"mime\": \"text/plain\","
2849 " \"paused\": false,"
2850 " \"url\": \"%s\"}]",
2851 result_id,
2852 download_url.c_str())));
2853 ASSERT_TRUE(WaitFor(
2854 std::string(events::kOnDownloadDeterminingFilename) + "/43",
2855 base::StringPrintf("[{\"id\": %d,"
2856 " \"filename\":\"slow.txt\"}]",
2857 result_id)));
2858 ASSERT_TRUE(item->GetTargetFilePath().empty());
2859 ASSERT_TRUE(item->IsInProgress());
2860 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
2861 browser()->profile(),
2862 false,
2863 GetExtensionId(),
2864 "43",
2865 result_id,
2866 FilePath(),
2867 false));
2868 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2869 base::StringPrintf("[{\"id\": %d,"
2870 " \"state\": {"
2871 " \"previous\": \"in_progress\","
2872 " \"current\": \"complete\"}}]",
2873 result_id)));
2874 }
2875
2876 // TODO test incognito
2877 // TODO test incognito=spanning
2878
2879 IN_PROC_BROWSER_TEST_F(
2880 DownloadExtensionTest,
2881 DownloadExtensionTest_OnDeterminingFilename_DisableExtension) {
2882 GoOnTheRecord();
2883 LoadExtension("downloads_split");
2884 ASSERT_TRUE(ExtensionDownloadsEventRouter::AddFilenameDeterminer(
2885 browser()->profile(),
2886 GetExtensionId(),
2887 "42"));
2888 CHECK(StartTestServer());
2889 std::string download_url = test_server()->GetURL("slow?0").spec();
2890 scoped_ptr<base::Value> result(RunFunctionAndReturnResult(
2891 new DownloadsDownloadFunction(), base::StringPrintf(
2892 "[{\"url\": \"%s\"}]", download_url.c_str())));
2893 ASSERT_TRUE(result.get());
2894 int result_id = -1;
2895 ASSERT_TRUE(result->GetAsInteger(&result_id));
2896 DownloadItem* item = GetCurrentManager()->GetDownload(result_id);
2897 ASSERT_TRUE(item);
2898 ScopedCancellingItem canceller(item);
2899 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
2900 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
2901 base::StringPrintf("[{\"danger\": \"safe\","
2902 " \"incognito\": false,"
2903 " \"id\": %d,"
2904 " \"mime\": \"text/plain\","
2905 " \"paused\": false,"
2906 " \"url\": \"%s\"}]",
2907 result_id,
2908 download_url.c_str())));
2909 ASSERT_TRUE(WaitFor(
2910 std::string(events::kOnDownloadDeterminingFilename) + "/42",
2911 base::StringPrintf("[{\"id\": %d,"
2912 " \"filename\":\"slow.txt\"}]",
2913 result_id)));
2914 ASSERT_TRUE(item->GetTargetFilePath().empty());
2915 ASSERT_TRUE(item->IsInProgress());
2916 DisableExtension();
2917 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
2918 base::StringPrintf("[{\"id\": %d,"
2919 " \"state\": {"
2920 " \"previous\": \"in_progress\","
2921 " \"current\": \"complete\"}}]",
2922 result_id)));
2923 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698