OLD | NEW |
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 "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/observer_list.h" | 8 #include "base/observer_list.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/value_conversions.h" | 13 #include "base/value_conversions.h" |
14 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
15 #include "chrome/browser/download/download_extensions.h" | 15 #include "chrome/browser/download/download_extensions.h" |
16 #include "chrome/browser/download/download_prefs.h" | 16 #include "chrome/browser/download/download_prefs.h" |
17 #include "chrome/browser/download/download_target_determiner.h" | 17 #include "chrome/browser/download/download_target_determiner.h" |
18 #include "chrome/browser/download/download_util.h" | 18 #include "chrome/browser/download/download_util.h" |
19 #include "chrome/browser/history/history_service.h" | 19 #include "chrome/browser/history/history_service.h" |
20 #include "chrome/browser/history/history_service_factory.h" | 20 #include "chrome/browser/history/history_service_factory.h" |
21 #include "chrome/browser/history/history_types.h" | 21 #include "chrome/browser/history/history_types.h" |
22 #include "chrome/common/extensions/extension.h" | 22 #include "chrome/common/extensions/extension.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 24 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
25 #include "chrome/test/base/testing_pref_service_syncable.h" | 25 #include "chrome/test/base/testing_pref_service_syncable.h" |
26 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
| 27 #include "content/public/browser/download_interrupt_reasons.h" |
27 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
28 #include "content/public/browser/web_contents_delegate.h" | 29 #include "content/public/browser/web_contents_delegate.h" |
29 #include "content/public/test/mock_download_item.h" | 30 #include "content/public/test/mock_download_item.h" |
30 #include "content/public/test/test_browser_thread.h" | 31 #include "content/public/test/test_browser_thread.h" |
31 #include "content/public/test/test_renderer_host.h" | 32 #include "content/public/test/test_renderer_host.h" |
32 #include "content/public/test/web_contents_tester.h" | 33 #include "content/public/test/web_contents_tester.h" |
33 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
35 | 36 |
36 using ::testing::AnyNumber; | 37 using ::testing::AnyNumber; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 202 |
202 // Set the kPromptForDownload user preference to |prompt|. | 203 // Set the kPromptForDownload user preference to |prompt|. |
203 void SetPromptForDownload(bool prompt); | 204 void SetPromptForDownload(bool prompt); |
204 | 205 |
205 // Given the relative path |path|, returns the full path under the temporary | 206 // Given the relative path |path|, returns the full path under the temporary |
206 // downloads directory. | 207 // downloads directory. |
207 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); | 208 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); |
208 | 209 |
209 // Run |test_case| using |item|. | 210 // Run |test_case| using |item|. |
210 void RunTestCase(const DownloadTestCase& test_case, | 211 void RunTestCase(const DownloadTestCase& test_case, |
| 212 const base::FilePath& initial_virtual_path, |
211 content::MockDownloadItem* item); | 213 content::MockDownloadItem* item); |
212 | 214 |
213 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem | 215 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem |
214 // will be created for each test case and destroyed when the test case is | 216 // will be created for each test case and destroyed when the test case is |
215 // complete. | 217 // complete. |
216 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[], | 218 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[], |
217 size_t test_case_count); | 219 size_t test_case_count); |
218 | 220 |
219 const base::FilePath& test_download_dir() const { | 221 const base::FilePath& test_download_dir() const { |
220 return test_download_dir_.path(); | 222 return test_download_dir_.path(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 ON_CALL(*item, GetDangerType()) | 305 ON_CALL(*item, GetDangerType()) |
304 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); | 306 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
305 ON_CALL(*item, GetForcedFilePath()) | 307 ON_CALL(*item, GetForcedFilePath()) |
306 .WillByDefault(ReturnRefOfCopy(forced_file_path)); | 308 .WillByDefault(ReturnRefOfCopy(forced_file_path)); |
307 ON_CALL(*item, GetFullPath()) | 309 ON_CALL(*item, GetFullPath()) |
308 .WillByDefault(ReturnRefOfCopy(base::FilePath())); | 310 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
309 ON_CALL(*item, GetHash()) | 311 ON_CALL(*item, GetHash()) |
310 .WillByDefault(ReturnRefOfCopy(std::string())); | 312 .WillByDefault(ReturnRefOfCopy(std::string())); |
311 ON_CALL(*item, GetId()) | 313 ON_CALL(*item, GetId()) |
312 .WillByDefault(Return(id)); | 314 .WillByDefault(Return(id)); |
| 315 ON_CALL(*item, GetLastReason()) |
| 316 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE)); |
313 ON_CALL(*item, GetMimeType()) | 317 ON_CALL(*item, GetMimeType()) |
314 .WillByDefault(Return(test_case.mime_type)); | 318 .WillByDefault(Return(test_case.mime_type)); |
315 ON_CALL(*item, GetReferrerUrl()) | 319 ON_CALL(*item, GetReferrerUrl()) |
316 .WillByDefault(ReturnRefOfCopy(download_url)); | 320 .WillByDefault(ReturnRefOfCopy(download_url)); |
317 ON_CALL(*item, GetState()) | 321 ON_CALL(*item, GetState()) |
318 .WillByDefault(Return(DownloadItem::IN_PROGRESS)); | 322 .WillByDefault(Return(DownloadItem::IN_PROGRESS)); |
319 ON_CALL(*item, GetTargetDisposition()) | 323 ON_CALL(*item, GetTargetDisposition()) |
320 .WillByDefault(Return(initial_disposition)); | 324 .WillByDefault(Return(initial_disposition)); |
321 ON_CALL(*item, GetTargetFilePath()) | 325 ON_CALL(*item, GetTargetFilePath()) |
322 .WillByDefault(ReturnRefOfCopy(base::FilePath())); | 326 .WillByDefault(ReturnRefOfCopy(base::FilePath())); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( | 369 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( |
366 const base::FilePath::StringType& relative_path) { | 370 const base::FilePath::StringType& relative_path) { |
367 if (relative_path.empty()) | 371 if (relative_path.empty()) |
368 return base::FilePath(); | 372 return base::FilePath(); |
369 base::FilePath full_path(test_download_dir().Append(relative_path)); | 373 base::FilePath full_path(test_download_dir().Append(relative_path)); |
370 return full_path.NormalizePathSeparators(); | 374 return full_path.NormalizePathSeparators(); |
371 } | 375 } |
372 | 376 |
373 void DownloadTargetDeterminerTest::RunTestCase( | 377 void DownloadTargetDeterminerTest::RunTestCase( |
374 const DownloadTestCase& test_case, | 378 const DownloadTestCase& test_case, |
| 379 const base::FilePath& initial_virtual_path, |
375 content::MockDownloadItem* item) { | 380 content::MockDownloadItem* item) { |
376 // Kick off the test. | 381 // Kick off the test. |
377 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); | 382 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); |
378 base::RunLoop run_loop; | 383 base::RunLoop run_loop; |
379 DownloadTargetDeterminer::Start( | 384 DownloadTargetDeterminer::Start( |
380 item, download_prefs_.get(), last_selected_directory_, delegate(), | 385 item, initial_virtual_path, download_prefs_.get(), |
| 386 last_selected_directory_, delegate(), |
381 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, | 387 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, |
382 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); | 388 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); |
383 run_loop.Run(); | 389 run_loop.Run(); |
384 ::testing::Mock::VerifyAndClearExpectations(delegate()); | 390 ::testing::Mock::VerifyAndClearExpectations(delegate()); |
385 } | 391 } |
386 | 392 |
387 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( | 393 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( |
388 const DownloadTestCase test_cases[], | 394 const DownloadTestCase test_cases[], |
389 size_t test_case_count) { | 395 size_t test_case_count) { |
390 for (size_t i = 0; i < test_case_count; ++i) { | 396 for (size_t i = 0; i < test_case_count; ++i) { |
391 scoped_ptr<content::MockDownloadItem> item( | 397 scoped_ptr<content::MockDownloadItem> item( |
392 CreateActiveDownloadItem(i, test_cases[i])); | 398 CreateActiveDownloadItem(i, test_cases[i])); |
393 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 399 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
394 RunTestCase(test_cases[i], item.get()); | 400 RunTestCase(test_cases[i], base::FilePath(), item.get()); |
395 } | 401 } |
396 } | 402 } |
397 | 403 |
398 void DownloadTargetDeterminerTest::DownloadTargetVerifier( | 404 void DownloadTargetDeterminerTest::DownloadTargetVerifier( |
399 const base::Closure& closure, | 405 const base::Closure& closure, |
400 const DownloadTestCase& test_case, | 406 const DownloadTestCase& test_case, |
401 const base::FilePath& local_path, | 407 const base::FilePath& local_path, |
402 DownloadItem::TargetDisposition disposition, | 408 DownloadItem::TargetDisposition disposition, |
403 content::DownloadDangerType danger_type, | 409 content::DownloadDangerType danger_type, |
404 const base::FilePath& intermediate_path) { | 410 const base::FilePath& intermediate_path) { |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 SCOPED_TRACE(testing::Message() << "Running test case " << i); | 975 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
970 const DownloadTestCase& test_case = kInactiveTestCases[i]; | 976 const DownloadTestCase& test_case = kInactiveTestCases[i]; |
971 scoped_ptr<content::MockDownloadItem> item( | 977 scoped_ptr<content::MockDownloadItem> item( |
972 CreateActiveDownloadItem(i, test_case)); | 978 CreateActiveDownloadItem(i, test_case)); |
973 EXPECT_CALL(*item.get(), IsInProgress()) | 979 EXPECT_CALL(*item.get(), IsInProgress()) |
974 .WillRepeatedly(Return(false)); | 980 .WillRepeatedly(Return(false)); |
975 // Even though one is a SAVE_AS download, no prompt will be displayed to | 981 // Even though one is a SAVE_AS download, no prompt will be displayed to |
976 // the user because the download is inactive. | 982 // the user because the download is inactive. |
977 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) | 983 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) |
978 .Times(0); | 984 .Times(0); |
979 RunTestCase(kInactiveTestCases[i], item.get()); | 985 RunTestCase(test_case, base::FilePath(), item.get()); |
980 } | 986 } |
981 } | 987 } |
982 | 988 |
983 // If the reserved path could not be verified, then the user should see a | 989 // If the reserved path could not be verified, then the user should see a |
984 // prompt. | 990 // prompt. |
985 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) { | 991 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) { |
986 const DownloadTestCase kReservationFailedCases[] = { | 992 const DownloadTestCase kReservationFailedCases[] = { |
987 { | 993 { |
988 // 0: Automatic download. Since the reservation fails, the disposition of | 994 // 0: Automatic download. Since the reservation fails, the disposition of |
989 // the target is to prompt, but the returned path is used. | 995 // the target is to prompt, but the returned path is used. |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1418 // First case: An extension sets the conflict_action to OVERWRITE. | 1424 // First case: An extension sets the conflict_action to OVERWRITE. |
1419 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1425 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
1420 .WillOnce(WithArg<2>( | 1426 .WillOnce(WithArg<2>( |
1421 ScheduleCallback2(overridden_path, | 1427 ScheduleCallback2(overridden_path, |
1422 DownloadPathReservationTracker::OVERWRITE))); | 1428 DownloadPathReservationTracker::OVERWRITE))); |
1423 EXPECT_CALL(*delegate(), ReserveVirtualPath( | 1429 EXPECT_CALL(*delegate(), ReserveVirtualPath( |
1424 _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE, | 1430 _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE, |
1425 _)).WillOnce(WithArg<4>( | 1431 _)).WillOnce(WithArg<4>( |
1426 ScheduleCallback2(full_overridden_path, true))); | 1432 ScheduleCallback2(full_overridden_path, true))); |
1427 | 1433 |
1428 RunTestCase(test_case, item.get()); | 1434 RunTestCase(test_case, base::FilePath(), item.get()); |
1429 | 1435 |
1430 // Second case: An extension sets the conflict_action to PROMPT. | 1436 // Second case: An extension sets the conflict_action to PROMPT. |
1431 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1437 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
1432 .WillOnce(WithArg<2>( | 1438 .WillOnce(WithArg<2>( |
1433 ScheduleCallback2(overridden_path, | 1439 ScheduleCallback2(overridden_path, |
1434 DownloadPathReservationTracker::PROMPT))); | 1440 DownloadPathReservationTracker::PROMPT))); |
1435 EXPECT_CALL(*delegate(), ReserveVirtualPath( | 1441 EXPECT_CALL(*delegate(), ReserveVirtualPath( |
1436 _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _)) | 1442 _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _)) |
1437 .WillOnce(WithArg<4>( | 1443 .WillOnce(WithArg<4>( |
1438 ScheduleCallback2(full_overridden_path, true))); | 1444 ScheduleCallback2(full_overridden_path, true))); |
1439 RunTestCase(test_case, item.get()); | 1445 RunTestCase(test_case, base::FilePath(), item.get()); |
1440 } | 1446 } |
1441 | 1447 |
1442 // Test that relative paths returned by extensions are always relative to the | 1448 // Test that relative paths returned by extensions are always relative to the |
1443 // default downloads path. | 1449 // default downloads path. |
1444 TEST_F(DownloadTargetDeterminerTest, | 1450 TEST_F(DownloadTargetDeterminerTest, |
1445 TargetDeterminer_NotifyExtensionsDefaultPath) { | 1451 TargetDeterminer_NotifyExtensionsDefaultPath) { |
1446 const DownloadTestCase kNotifyExtensionsTestCase = { | 1452 const DownloadTestCase kNotifyExtensionsTestCase = { |
1447 SAVE_AS, | 1453 SAVE_AS, |
1448 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 1454 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
1449 "http://example.com/foo.txt", "text/plain", | 1455 "http://example.com/foo.txt", "text/plain", |
(...skipping 19 matching lines...) Expand all Loading... |
1469 FILE_PATH_LITERAL("last_selected"))); | 1475 FILE_PATH_LITERAL("last_selected"))); |
1470 | 1476 |
1471 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1477 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
1472 .WillOnce(WithArg<2>( | 1478 .WillOnce(WithArg<2>( |
1473 ScheduleCallback2(overridden_path, | 1479 ScheduleCallback2(overridden_path, |
1474 DownloadPathReservationTracker::UNIQUIFY))); | 1480 DownloadPathReservationTracker::UNIQUIFY))); |
1475 EXPECT_CALL(*delegate(), | 1481 EXPECT_CALL(*delegate(), |
1476 PromptUserForDownloadPath(_, full_overridden_path, _)) | 1482 PromptUserForDownloadPath(_, full_overridden_path, _)) |
1477 .WillOnce(WithArg<2>( | 1483 .WillOnce(WithArg<2>( |
1478 ScheduleCallback(full_overridden_path))); | 1484 ScheduleCallback(full_overridden_path))); |
1479 RunTestCase(test_case, item.get()); | 1485 RunTestCase(test_case, base::FilePath(), item.get()); |
| 1486 } |
| 1487 |
| 1488 // Test that the initial virtual path is used correctly if one is specified. |
| 1489 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_InitialVirtualPath) { |
| 1490 const base::FilePath::CharType* kInitialPath = |
| 1491 FILE_PATH_LITERAL("some_path/bar.txt"); |
| 1492 |
| 1493 const DownloadTestCase kInitialPathTestCases[] = { |
| 1494 { |
| 1495 // 0: Automatic Safe |
| 1496 AUTOMATIC, |
| 1497 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1498 "http://example.com/foo.txt", "text/plain", |
| 1499 FILE_PATH_LITERAL(""), |
| 1500 |
| 1501 FILE_PATH_LITERAL(""), |
| 1502 kInitialPath, |
| 1503 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1504 |
| 1505 EXPECT_CRDOWNLOAD |
| 1506 }, |
| 1507 |
| 1508 { |
| 1509 // 1: Save_As Safe. Uses the initial path, but prompts. |
| 1510 SAVE_AS, |
| 1511 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1512 "http://example.com/foo.txt", "text/plain", |
| 1513 FILE_PATH_LITERAL(""), |
| 1514 |
| 1515 FILE_PATH_LITERAL(""), |
| 1516 kInitialPath, |
| 1517 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1518 |
| 1519 EXPECT_CRDOWNLOAD |
| 1520 }, |
| 1521 |
| 1522 { |
| 1523 // 2: Automatic Dangerous. The path generated based on the DownloadItem is |
| 1524 // dangerous, but the initial path is not. |
| 1525 AUTOMATIC, |
| 1526 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1527 "http://example.com/foo.html", "", |
| 1528 FILE_PATH_LITERAL(""), |
| 1529 |
| 1530 FILE_PATH_LITERAL(""), |
| 1531 kInitialPath, |
| 1532 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1533 |
| 1534 EXPECT_CRDOWNLOAD |
| 1535 }, |
| 1536 }; |
| 1537 |
| 1538 for (size_t i = 0; i < arraysize(kInitialPathTestCases); ++i) { |
| 1539 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1540 const DownloadTestCase& test_case = kInitialPathTestCases[i]; |
| 1541 scoped_ptr<content::MockDownloadItem> item( |
| 1542 CreateActiveDownloadItem(i, test_case)); |
| 1543 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1544 } |
| 1545 } |
| 1546 |
| 1547 // Test that the initial virtual path is used correctly if one is specified. |
| 1548 TEST_F(DownloadTargetDeterminerTest, |
| 1549 TargetDeterminer_InitialVirtualPathUnsafe) { |
| 1550 const base::FilePath::CharType* kInitialPath = |
| 1551 FILE_PATH_LITERAL("some_path/bar.html"); |
| 1552 |
| 1553 const DownloadTestCase kInitialPathTestCases[] = { |
| 1554 { |
| 1555 // 0: Automatic Safe. The path generated based on the DownloadItem is |
| 1556 // safe, but the initial path is not. |
| 1557 AUTOMATIC, |
| 1558 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1559 "http://example.com/foo.txt", "text/plain", |
| 1560 FILE_PATH_LITERAL(""), |
| 1561 |
| 1562 FILE_PATH_LITERAL(""), |
| 1563 kInitialPath, |
| 1564 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1565 |
| 1566 EXPECT_UNCONFIRMED |
| 1567 }, |
| 1568 }; |
| 1569 |
| 1570 for (size_t i = 0; i < arraysize(kInitialPathTestCases); ++i) { |
| 1571 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1572 const DownloadTestCase& test_case = kInitialPathTestCases[i]; |
| 1573 scoped_ptr<content::MockDownloadItem> item( |
| 1574 CreateActiveDownloadItem(i, test_case)); |
| 1575 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); |
| 1576 } |
| 1577 } |
| 1578 |
| 1579 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedNoPrompt) { |
| 1580 const DownloadTestCase kResumedTestCases[] = { |
| 1581 { |
| 1582 // 0: Automatic Safe |
| 1583 AUTOMATIC, |
| 1584 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1585 "http://example.com/foo.txt", "text/plain", |
| 1586 FILE_PATH_LITERAL(""), |
| 1587 |
| 1588 FILE_PATH_LITERAL(""), |
| 1589 FILE_PATH_LITERAL("foo.txt"), |
| 1590 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1591 |
| 1592 EXPECT_CRDOWNLOAD |
| 1593 }, |
| 1594 |
| 1595 { |
| 1596 // 1: Save_As Safe |
| 1597 SAVE_AS, |
| 1598 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1599 "http://example.com/foo.txt", "text/plain", |
| 1600 FILE_PATH_LITERAL(""), |
| 1601 |
| 1602 FILE_PATH_LITERAL(""), |
| 1603 FILE_PATH_LITERAL("foo.txt"), |
| 1604 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1605 |
| 1606 EXPECT_CRDOWNLOAD |
| 1607 }, |
| 1608 |
| 1609 { |
| 1610 // 2: Automatic Dangerous |
| 1611 AUTOMATIC, |
| 1612 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, |
| 1613 "http://example.com/foo.html", "", |
| 1614 FILE_PATH_LITERAL(""), |
| 1615 |
| 1616 FILE_PATH_LITERAL(""), |
| 1617 FILE_PATH_LITERAL("foo.html"), |
| 1618 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1619 |
| 1620 EXPECT_UNCONFIRMED |
| 1621 }, |
| 1622 |
| 1623 { |
| 1624 // 3: Forced Safe |
| 1625 FORCED, |
| 1626 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1627 "http://example.com/foo.txt", "", |
| 1628 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1629 |
| 1630 FILE_PATH_LITERAL(""), |
| 1631 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1632 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 1633 |
| 1634 EXPECT_LOCAL_PATH |
| 1635 }, |
| 1636 }; |
| 1637 |
| 1638 // The test assumes that .html files have a danger level of |
| 1639 // AllowOnUserGesture. |
| 1640 ASSERT_EQ(download_util::AllowOnUserGesture, |
| 1641 download_util::GetFileDangerLevel( |
| 1642 base::FilePath(FILE_PATH_LITERAL("foo.html")))); |
| 1643 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { |
| 1644 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1645 const DownloadTestCase& test_case = kResumedTestCases[i]; |
| 1646 scoped_ptr<content::MockDownloadItem> item( |
| 1647 CreateActiveDownloadItem(i, test_case)); |
| 1648 base::FilePath expected_path = |
| 1649 GetPathInDownloadDir(test_case.expected_local_path); |
| 1650 ON_CALL(*item.get(), GetLastReason()) |
| 1651 .WillByDefault(Return( |
| 1652 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); |
| 1653 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1654 .Times(0); |
| 1655 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, _, _)); |
| 1656 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)) |
| 1657 .Times(0); |
| 1658 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1659 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); |
| 1660 RunTestCase(test_case, expected_path, item.get()); |
| 1661 } |
| 1662 } |
| 1663 |
| 1664 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedWithPrompt) { |
| 1665 const DownloadTestCase kResumedTestCases[] = { |
| 1666 { |
| 1667 // 0: Automatic Safe |
| 1668 AUTOMATIC, |
| 1669 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1670 "http://example.com/foo.txt", "text/plain", |
| 1671 FILE_PATH_LITERAL(""), |
| 1672 |
| 1673 FILE_PATH_LITERAL(""), |
| 1674 FILE_PATH_LITERAL("foo.txt"), |
| 1675 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1676 |
| 1677 EXPECT_CRDOWNLOAD |
| 1678 }, |
| 1679 |
| 1680 { |
| 1681 // 1: Save_As Safe |
| 1682 SAVE_AS, |
| 1683 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1684 "http://example.com/foo.txt", "text/plain", |
| 1685 FILE_PATH_LITERAL(""), |
| 1686 |
| 1687 FILE_PATH_LITERAL(""), |
| 1688 FILE_PATH_LITERAL("foo.txt"), |
| 1689 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1690 |
| 1691 EXPECT_CRDOWNLOAD |
| 1692 }, |
| 1693 |
| 1694 { |
| 1695 // 2: Automatic Dangerous |
| 1696 AUTOMATIC, |
| 1697 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1698 "http://example.com/foo.html", "", |
| 1699 FILE_PATH_LITERAL(""), |
| 1700 |
| 1701 FILE_PATH_LITERAL(""), |
| 1702 FILE_PATH_LITERAL("foo.html"), |
| 1703 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1704 |
| 1705 EXPECT_CRDOWNLOAD |
| 1706 }, |
| 1707 |
| 1708 { |
| 1709 // 3: Forced Safe |
| 1710 FORCED, |
| 1711 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 1712 "http://example.com/foo.txt", "", |
| 1713 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1714 |
| 1715 FILE_PATH_LITERAL(""), |
| 1716 FILE_PATH_LITERAL("forced-foo.txt"), |
| 1717 DownloadItem::TARGET_DISPOSITION_PROMPT, |
| 1718 |
| 1719 EXPECT_LOCAL_PATH |
| 1720 }, |
| 1721 }; |
| 1722 |
| 1723 // The test assumes that .html files have a danger level of |
| 1724 // AllowOnUserGesture. |
| 1725 ASSERT_EQ(download_util::AllowOnUserGesture, |
| 1726 download_util::GetFileDangerLevel( |
| 1727 base::FilePath(FILE_PATH_LITERAL("foo.html")))); |
| 1728 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) { |
| 1729 SCOPED_TRACE(testing::Message() << "Running test case " << i); |
| 1730 const DownloadTestCase& test_case = kResumedTestCases[i]; |
| 1731 base::FilePath expected_path = |
| 1732 GetPathInDownloadDir(test_case.expected_local_path); |
| 1733 scoped_ptr<content::MockDownloadItem> item( |
| 1734 CreateActiveDownloadItem(i, test_case)); |
| 1735 ON_CALL(*item.get(), GetLastReason()) |
| 1736 .WillByDefault(Return( |
| 1737 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
| 1738 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
| 1739 .Times(0); |
| 1740 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, _, _)); |
| 1741 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)); |
| 1742 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); |
| 1743 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); |
| 1744 RunTestCase(test_case, expected_path, item.get()); |
| 1745 } |
1480 } | 1746 } |
1481 } // namespace | 1747 } // namespace |
OLD | NEW |