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

Side by Side Diff: chrome/browser/download/download_target_determiner_unittest.cc

Issue 14640020: [Resumption 9/11] Handle filename determination for resumed downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with r203194 Created 7 years, 6 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 "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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 199
199 // Set the kPromptForDownload user preference to |prompt|. 200 // Set the kPromptForDownload user preference to |prompt|.
200 void SetPromptForDownload(bool prompt); 201 void SetPromptForDownload(bool prompt);
201 202
202 // Given the relative path |path|, returns the full path under the temporary 203 // Given the relative path |path|, returns the full path under the temporary
203 // downloads directory. 204 // downloads directory.
204 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); 205 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path);
205 206
206 // Run |test_case| using |item|. 207 // Run |test_case| using |item|.
207 void RunTestCase(const DownloadTestCase& test_case, 208 void RunTestCase(const DownloadTestCase& test_case,
209 const base::FilePath& initial_virtual_path,
208 content::MockDownloadItem* item); 210 content::MockDownloadItem* item);
209 211
210 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem 212 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem
211 // will be created for each test case and destroyed when the test case is 213 // will be created for each test case and destroyed when the test case is
212 // complete. 214 // complete.
213 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[], 215 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[],
214 size_t test_case_count); 216 size_t test_case_count);
215 217
216 const base::FilePath& test_download_dir() const { 218 const base::FilePath& test_download_dir() const {
217 return test_download_dir_.path(); 219 return test_download_dir_.path();
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 ON_CALL(*item, GetDangerType()) 297 ON_CALL(*item, GetDangerType())
296 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); 298 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
297 ON_CALL(*item, GetForcedFilePath()) 299 ON_CALL(*item, GetForcedFilePath())
298 .WillByDefault(ReturnRefOfCopy(forced_file_path)); 300 .WillByDefault(ReturnRefOfCopy(forced_file_path));
299 ON_CALL(*item, GetFullPath()) 301 ON_CALL(*item, GetFullPath())
300 .WillByDefault(ReturnRefOfCopy(base::FilePath())); 302 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
301 ON_CALL(*item, GetHash()) 303 ON_CALL(*item, GetHash())
302 .WillByDefault(ReturnRefOfCopy(std::string())); 304 .WillByDefault(ReturnRefOfCopy(std::string()));
303 ON_CALL(*item, GetId()) 305 ON_CALL(*item, GetId())
304 .WillByDefault(Return(id)); 306 .WillByDefault(Return(id));
307 ON_CALL(*item, GetLastReason())
308 .WillByDefault(Return(content::DOWNLOAD_INTERRUPT_REASON_NONE));
305 ON_CALL(*item, GetMimeType()) 309 ON_CALL(*item, GetMimeType())
306 .WillByDefault(Return(test_case.mime_type)); 310 .WillByDefault(Return(test_case.mime_type));
307 ON_CALL(*item, GetReferrerUrl()) 311 ON_CALL(*item, GetReferrerUrl())
308 .WillByDefault(ReturnRefOfCopy(download_url)); 312 .WillByDefault(ReturnRefOfCopy(download_url));
309 ON_CALL(*item, GetState()) 313 ON_CALL(*item, GetState())
310 .WillByDefault(Return(DownloadItem::IN_PROGRESS)); 314 .WillByDefault(Return(DownloadItem::IN_PROGRESS));
311 ON_CALL(*item, GetTargetDisposition()) 315 ON_CALL(*item, GetTargetDisposition())
312 .WillByDefault(Return(initial_disposition)); 316 .WillByDefault(Return(initial_disposition));
313 ON_CALL(*item, GetTargetFilePath()) 317 ON_CALL(*item, GetTargetFilePath())
314 .WillByDefault(ReturnRefOfCopy(base::FilePath())); 318 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( 353 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir(
350 const base::FilePath::StringType& relative_path) { 354 const base::FilePath::StringType& relative_path) {
351 if (relative_path.empty()) 355 if (relative_path.empty())
352 return base::FilePath(); 356 return base::FilePath();
353 base::FilePath full_path(test_download_dir().Append(relative_path)); 357 base::FilePath full_path(test_download_dir().Append(relative_path));
354 return full_path.NormalizePathSeparators(); 358 return full_path.NormalizePathSeparators();
355 } 359 }
356 360
357 void DownloadTargetDeterminerTest::RunTestCase( 361 void DownloadTargetDeterminerTest::RunTestCase(
358 const DownloadTestCase& test_case, 362 const DownloadTestCase& test_case,
363 const base::FilePath& initial_virtual_path,
359 content::MockDownloadItem* item) { 364 content::MockDownloadItem* item) {
360 // Kick off the test. 365 // Kick off the test.
361 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); 366 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this);
362 base::RunLoop run_loop; 367 base::RunLoop run_loop;
363 DownloadTargetDeterminer::Start( 368 DownloadTargetDeterminer::Start(
364 item, download_prefs_.get(), delegate(), 369 item, initial_virtual_path, download_prefs_.get(), delegate(),
365 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, 370 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier,
366 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); 371 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case));
367 run_loop.Run(); 372 run_loop.Run();
368 ::testing::Mock::VerifyAndClearExpectations(delegate()); 373 ::testing::Mock::VerifyAndClearExpectations(delegate());
369 } 374 }
370 375
371 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( 376 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem(
372 const DownloadTestCase test_cases[], 377 const DownloadTestCase test_cases[],
373 size_t test_case_count) { 378 size_t test_case_count) {
374 for (size_t i = 0; i < test_case_count; ++i) { 379 for (size_t i = 0; i < test_case_count; ++i) {
375 scoped_ptr<content::MockDownloadItem> item( 380 scoped_ptr<content::MockDownloadItem> item(
376 CreateActiveDownloadItem(i, test_cases[i])); 381 CreateActiveDownloadItem(i, test_cases[i]));
377 SCOPED_TRACE(testing::Message() << "Running test case " << i); 382 SCOPED_TRACE(testing::Message() << "Running test case " << i);
378 RunTestCase(test_cases[i], item.get()); 383 RunTestCase(test_cases[i], base::FilePath(), item.get());
379 } 384 }
380 } 385 }
381 386
382 void DownloadTargetDeterminerTest::DownloadTargetVerifier( 387 void DownloadTargetDeterminerTest::DownloadTargetVerifier(
383 const base::Closure& closure, 388 const base::Closure& closure,
384 const DownloadTestCase& test_case, 389 const DownloadTestCase& test_case,
385 const base::FilePath& local_path, 390 const base::FilePath& local_path,
386 DownloadItem::TargetDisposition disposition, 391 DownloadItem::TargetDisposition disposition,
387 content::DownloadDangerType danger_type, 392 content::DownloadDangerType danger_type,
388 const base::FilePath& intermediate_path) { 393 const base::FilePath& intermediate_path) {
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 SCOPED_TRACE(testing::Message() << "Running test case " << i); 958 SCOPED_TRACE(testing::Message() << "Running test case " << i);
954 const DownloadTestCase& test_case = kInactiveTestCases[i]; 959 const DownloadTestCase& test_case = kInactiveTestCases[i];
955 scoped_ptr<content::MockDownloadItem> item( 960 scoped_ptr<content::MockDownloadItem> item(
956 CreateActiveDownloadItem(i, test_case)); 961 CreateActiveDownloadItem(i, test_case));
957 EXPECT_CALL(*item.get(), GetState()) 962 EXPECT_CALL(*item.get(), GetState())
958 .WillRepeatedly(Return(content::DownloadItem::CANCELLED)); 963 .WillRepeatedly(Return(content::DownloadItem::CANCELLED));
959 // Even though one is a SAVE_AS download, no prompt will be displayed to 964 // Even though one is a SAVE_AS download, no prompt will be displayed to
960 // the user because the download is inactive. 965 // the user because the download is inactive.
961 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _)) 966 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
962 .Times(0); 967 .Times(0);
963 RunTestCase(kInactiveTestCases[i], item.get()); 968 RunTestCase(test_case, base::FilePath(), item.get());
964 } 969 }
965 } 970 }
966 971
967 // If the reserved path could not be verified, then the user should see a 972 // If the reserved path could not be verified, then the user should see a
968 // prompt. 973 // prompt.
969 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) { 974 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) {
970 const DownloadTestCase kReservationFailedCases[] = { 975 const DownloadTestCase kReservationFailedCases[] = {
971 { 976 {
972 // 0: Automatic download. Since the reservation fails, the disposition of 977 // 0: Automatic download. Since the reservation fails, the disposition of
973 // the target is to prompt, but the returned path is used. 978 // the target is to prompt, but the returned path is used.
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 // First case: An extension sets the conflict_action to OVERWRITE. 1407 // First case: An extension sets the conflict_action to OVERWRITE.
1403 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) 1408 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1404 .WillOnce(WithArg<2>( 1409 .WillOnce(WithArg<2>(
1405 ScheduleCallback2(overridden_path, 1410 ScheduleCallback2(overridden_path,
1406 DownloadPathReservationTracker::OVERWRITE))); 1411 DownloadPathReservationTracker::OVERWRITE)));
1407 EXPECT_CALL(*delegate(), ReserveVirtualPath( 1412 EXPECT_CALL(*delegate(), ReserveVirtualPath(
1408 _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE, 1413 _, full_overridden_path, true, DownloadPathReservationTracker::OVERWRITE,
1409 _)).WillOnce(WithArg<4>( 1414 _)).WillOnce(WithArg<4>(
1410 ScheduleCallback2(full_overridden_path, true))); 1415 ScheduleCallback2(full_overridden_path, true)));
1411 1416
1412 RunTestCase(test_case, item.get()); 1417 RunTestCase(test_case, base::FilePath(), item.get());
1413 1418
1414 // Second case: An extension sets the conflict_action to PROMPT. 1419 // Second case: An extension sets the conflict_action to PROMPT.
1415 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) 1420 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1416 .WillOnce(WithArg<2>( 1421 .WillOnce(WithArg<2>(
1417 ScheduleCallback2(overridden_path, 1422 ScheduleCallback2(overridden_path,
1418 DownloadPathReservationTracker::PROMPT))); 1423 DownloadPathReservationTracker::PROMPT)));
1419 EXPECT_CALL(*delegate(), ReserveVirtualPath( 1424 EXPECT_CALL(*delegate(), ReserveVirtualPath(
1420 _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _)) 1425 _, full_overridden_path, true, DownloadPathReservationTracker::PROMPT, _))
1421 .WillOnce(WithArg<4>( 1426 .WillOnce(WithArg<4>(
1422 ScheduleCallback2(full_overridden_path, true))); 1427 ScheduleCallback2(full_overridden_path, true)));
1423 RunTestCase(test_case, item.get()); 1428 RunTestCase(test_case, base::FilePath(), item.get());
1424 } 1429 }
1425 1430
1426 // Test that relative paths returned by extensions are always relative to the 1431 // Test that relative paths returned by extensions are always relative to the
1427 // default downloads path. 1432 // default downloads path.
1428 TEST_F(DownloadTargetDeterminerTest, 1433 TEST_F(DownloadTargetDeterminerTest,
1429 TargetDeterminer_NotifyExtensionsDefaultPath) { 1434 TargetDeterminer_NotifyExtensionsDefaultPath) {
1430 const DownloadTestCase kNotifyExtensionsTestCase = { 1435 const DownloadTestCase kNotifyExtensionsTestCase = {
1431 SAVE_AS, 1436 SAVE_AS,
1432 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 1437 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1433 "http://example.com/foo.txt", "text/plain", 1438 "http://example.com/foo.txt", "text/plain",
(...skipping 17 matching lines...) Expand all
1451 FILE_PATH_LITERAL("last_selected"))); 1456 FILE_PATH_LITERAL("last_selected")));
1452 1457
1453 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) 1458 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1454 .WillOnce(WithArg<2>( 1459 .WillOnce(WithArg<2>(
1455 ScheduleCallback2(overridden_path, 1460 ScheduleCallback2(overridden_path,
1456 DownloadPathReservationTracker::UNIQUIFY))); 1461 DownloadPathReservationTracker::UNIQUIFY)));
1457 EXPECT_CALL(*delegate(), 1462 EXPECT_CALL(*delegate(),
1458 PromptUserForDownloadPath(_, full_overridden_path, _)) 1463 PromptUserForDownloadPath(_, full_overridden_path, _))
1459 .WillOnce(WithArg<2>( 1464 .WillOnce(WithArg<2>(
1460 ScheduleCallback(full_overridden_path))); 1465 ScheduleCallback(full_overridden_path)));
1461 RunTestCase(test_case, item.get()); 1466 RunTestCase(test_case, base::FilePath(), item.get());
1462 } 1467 }
1468
1469 TEST_F(DownloadTargetDeterminerTest,
1470 TargetDeterminer_InitialVirtualPathUnsafe) {
1471 const base::FilePath::CharType* kInitialPath =
1472 FILE_PATH_LITERAL("some_path/bar.html");
1473
1474 const DownloadTestCase kInitialPathTestCase = {
1475 // 0: Save As Save. The path generated based on the DownloadItem is safe,
1476 // but the initial path is unsafe. However, the download is not considered
1477 // dangerous since the user has been prompted.
1478 SAVE_AS,
1479 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1480 "http://example.com/foo.txt", "text/plain",
1481 FILE_PATH_LITERAL(""),
1482
1483 FILE_PATH_LITERAL(""),
1484 kInitialPath,
1485 DownloadItem::TARGET_DISPOSITION_PROMPT,
1486
1487 EXPECT_CRDOWNLOAD
1488 };
1489
1490 const DownloadTestCase& test_case = kInitialPathTestCase;
1491 scoped_ptr<content::MockDownloadItem> item(
1492 CreateActiveDownloadItem(1, test_case));
1493 EXPECT_CALL(*item, GetLastReason())
1494 .WillRepeatedly(Return(
1495 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1496 EXPECT_CALL(*item, GetTargetDisposition())
1497 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT));
1498 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1499 }
1500
1501 // Prompting behavior for resumed downloads is based on the last interrupt
1502 // reason. If the reason indicates that the target path may not be suitable for
1503 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1504 // prompted, and not otherwise. These test cases shouldn't result in prompting
1505 // since the error is set to NETWORK_FAILED.
1506 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedNoPrompt) {
1507 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1508 // path.
1509 const base::FilePath::CharType* kInitialPath =
1510 FILE_PATH_LITERAL("some_path/bar.txt");
1511
1512 const DownloadTestCase kResumedTestCases[] = {
1513 {
1514 // 0: Automatic Safe: Initial path is ignored since the user has not been
1515 // prompted before.
1516 AUTOMATIC,
1517 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1518 "http://example.com/foo.txt", "text/plain",
1519 FILE_PATH_LITERAL(""),
1520
1521 FILE_PATH_LITERAL(""),
1522 FILE_PATH_LITERAL("foo.txt"),
1523 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1524
1525 EXPECT_CRDOWNLOAD
1526 },
1527
1528 {
1529 // 1: Save_As Safe: Initial path used.
1530 SAVE_AS,
1531 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1532 "http://example.com/foo.txt", "text/plain",
1533 FILE_PATH_LITERAL(""),
1534
1535 FILE_PATH_LITERAL(""),
1536 kInitialPath,
1537 DownloadItem::TARGET_DISPOSITION_PROMPT,
1538
1539 EXPECT_CRDOWNLOAD
1540 },
1541
1542 {
1543 // 2: Automatic Dangerous: Initial path is ignored since the user hasn't
1544 // been prompted before.
1545 AUTOMATIC,
1546 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1547 "http://example.com/foo.html", "",
1548 FILE_PATH_LITERAL(""),
1549
1550 FILE_PATH_LITERAL(""),
1551 FILE_PATH_LITERAL("foo.html"),
1552 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1553
1554 EXPECT_UNCONFIRMED
1555 },
1556
1557 {
1558 // 3: Forced Safe: Initial path is ignored due to the forced path.
1559 FORCED,
1560 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1561 "http://example.com/foo.txt", "",
1562 FILE_PATH_LITERAL("forced-foo.txt"),
1563
1564 FILE_PATH_LITERAL(""),
1565 FILE_PATH_LITERAL("forced-foo.txt"),
1566 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1567
1568 EXPECT_LOCAL_PATH
1569 },
1570 };
1571
1572 // The test assumes that .html files have a danger level of
1573 // AllowOnUserGesture.
1574 ASSERT_EQ(download_util::AllowOnUserGesture,
1575 download_util::GetFileDangerLevel(
1576 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
1577 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1578 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1579 const DownloadTestCase& test_case = kResumedTestCases[i];
1580 scoped_ptr<content::MockDownloadItem> item(
1581 CreateActiveDownloadItem(i, test_case));
1582 base::FilePath expected_path =
1583 GetPathInDownloadDir(test_case.expected_local_path);
1584 ON_CALL(*item.get(), GetLastReason())
1585 .WillByDefault(Return(
1586 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1587 // Extensions should be notified if a new path is being generated and there
1588 // is no forced path. In the test cases above, this is true for tests with
1589 // type == AUTOMATIC.
1590 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1591 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1592 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1593 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _))
1594 .Times(0);
1595 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1596 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1597 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1598 }
1599
1600 }
1601
1602 // Test that a forced download doesn't prompt, even if the interrupt reason
1603 // suggests that the target path may not be suitable for downloads.
1604 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedForcedDownload) {
1605 const base::FilePath::CharType* kInitialPath =
1606 FILE_PATH_LITERAL("some_path/bar.txt");
1607 const DownloadTestCase kResumedForcedDownload = {
1608 // 3: Forced Safe
1609 FORCED,
1610 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1611 "http://example.com/foo.txt", "",
1612 FILE_PATH_LITERAL("forced-foo.txt"),
1613
1614 FILE_PATH_LITERAL(""),
1615 FILE_PATH_LITERAL("forced-foo.txt"),
1616 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1617
1618 EXPECT_LOCAL_PATH
1619 };
1620
1621 const DownloadTestCase& test_case = kResumedForcedDownload;
1622 base::FilePath expected_path =
1623 GetPathInDownloadDir(test_case.expected_local_path);
1624 scoped_ptr<content::MockDownloadItem> item(
1625 CreateActiveDownloadItem(0, test_case));
1626 ON_CALL(*item.get(), GetLastReason())
1627 .WillByDefault(Return(
1628 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1629 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1630 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1631 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1632 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
1633 .Times(0);
1634 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1635 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1636 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1637 }
1638
1639 // Prompting behavior for resumed downloads is based on the last interrupt
1640 // reason. If the reason indicates that the target path may not be suitable for
1641 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1642 // prompted, and not otherwise. These test cases result in prompting since the
1643 // error is set to NO_SPACE.
1644 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedWithPrompt) {
1645 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1646 // path.
1647 const base::FilePath::CharType* kInitialPath =
1648 FILE_PATH_LITERAL("some_path/bar.txt");
1649
1650 const DownloadTestCase kResumedTestCases[] = {
1651 {
1652 // 0: Automatic Safe
1653 AUTOMATIC,
1654 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1655 "http://example.com/foo.txt", "text/plain",
1656 FILE_PATH_LITERAL(""),
1657
1658 FILE_PATH_LITERAL(""),
1659 FILE_PATH_LITERAL("foo.txt"),
1660 DownloadItem::TARGET_DISPOSITION_PROMPT,
1661
1662 EXPECT_CRDOWNLOAD
1663 },
1664
1665 {
1666 // 1: Save_As Safe
1667 SAVE_AS,
1668 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1669 "http://example.com/foo.txt", "text/plain",
1670 FILE_PATH_LITERAL(""),
1671
1672 FILE_PATH_LITERAL(""),
1673 kInitialPath,
1674 DownloadItem::TARGET_DISPOSITION_PROMPT,
1675
1676 EXPECT_CRDOWNLOAD
1677 },
1678
1679 {
1680 // 2: Automatic Dangerous
1681 AUTOMATIC,
1682 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1683 "http://example.com/foo.html", "",
1684 FILE_PATH_LITERAL(""),
1685
1686 FILE_PATH_LITERAL(""),
1687 FILE_PATH_LITERAL("foo.html"),
1688 DownloadItem::TARGET_DISPOSITION_PROMPT,
1689
1690 EXPECT_CRDOWNLOAD
1691 },
1692 };
1693
1694 // The test assumes that .html files have a danger level of
1695 // AllowOnUserGesture.
1696 ASSERT_EQ(download_util::AllowOnUserGesture,
1697 download_util::GetFileDangerLevel(
1698 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
1699 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1700 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1701 const DownloadTestCase& test_case = kResumedTestCases[i];
1702 base::FilePath expected_path =
1703 GetPathInDownloadDir(test_case.expected_local_path);
1704 scoped_ptr<content::MockDownloadItem> item(
1705 CreateActiveDownloadItem(i, test_case));
1706 ON_CALL(*item.get(), GetLastReason())
1707 .WillByDefault(Return(
1708 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1709 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1710 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1711 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1712 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _));
1713 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1714 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1715 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1716 }
1717 }
1718
1463 } // namespace 1719 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698