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

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: Address comments Created 7 years, 7 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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
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
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
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());
1480 } 1486 }
1487
1488 TEST_F(DownloadTargetDeterminerTest,
1489 TargetDeterminer_InitialVirtualPathUnsafe) {
1490 const base::FilePath::CharType* kInitialPath =
1491 FILE_PATH_LITERAL("some_path/bar.html");
1492
1493 const DownloadTestCase kInitialPathTestCases[] = {
1494 {
1495 // 0: Save As Save. The path generated based on the DownloadItem is safe,
1496 // but the initial path is unsafe. However, the download is not considered
1497 // dangerous since the user has been prompted.
1498 SAVE_AS,
1499 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1500 "http://example.com/foo.txt", "text/plain",
1501 FILE_PATH_LITERAL(""),
1502
1503 FILE_PATH_LITERAL(""),
1504 kInitialPath,
1505 DownloadItem::TARGET_DISPOSITION_PROMPT,
1506
1507 EXPECT_CRDOWNLOAD
1508 },
1509 };
1510
1511 for (size_t i = 0; i < arraysize(kInitialPathTestCases); ++i) {
Randy Smith (Not in Mondays) 2013/05/24 01:53:55 Why bother to have the test case loop if we're onl
asanka 2013/05/29 22:30:11 Removed loop. It was supposed to contain multiple
1512 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1513 const DownloadTestCase& test_case = kInitialPathTestCases[i];
1514 scoped_ptr<content::MockDownloadItem> item(
1515 CreateActiveDownloadItem(i, test_case));
1516 EXPECT_CALL(*item, GetLastReason())
1517 .WillRepeatedly(Return(
1518 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1519 EXPECT_CALL(*item, GetTargetDisposition())
1520 .WillRepeatedly(Return(DownloadItem::TARGET_DISPOSITION_PROMPT));
1521 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1522 }
1523 }
1524
1525 // Prompting behavior for resumed downloads is based on the last interrupt
1526 // reason. If the reason indicates that the target path may not be suitable for
1527 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1528 // prompted, and not otherwise. These test cases shouldn't result in prompting
1529 // since the error is set to NETWORK_FAILED.
1530 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedNoPrompt) {
1531 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1532 // path.
1533 const base::FilePath::CharType* kInitialPath =
1534 FILE_PATH_LITERAL("some_path/bar.txt");
1535
1536 const DownloadTestCase kResumedTestCases[] = {
1537 {
1538 // 0: Automatic Safe: Initial path is ignored since the user has not been
1539 // prompted before.
1540 AUTOMATIC,
1541 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1542 "http://example.com/foo.txt", "text/plain",
1543 FILE_PATH_LITERAL(""),
1544
1545 FILE_PATH_LITERAL(""),
1546 FILE_PATH_LITERAL("foo.txt"),
1547 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1548
1549 EXPECT_CRDOWNLOAD
1550 },
1551
1552 {
1553 // 1: Save_As Safe: Initial path used.
1554 SAVE_AS,
1555 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1556 "http://example.com/foo.txt", "text/plain",
1557 FILE_PATH_LITERAL(""),
1558
1559 FILE_PATH_LITERAL(""),
1560 kInitialPath,
1561 DownloadItem::TARGET_DISPOSITION_PROMPT,
1562
1563 EXPECT_CRDOWNLOAD
1564 },
1565
1566 {
1567 // 2: Automatic Dangerous: Initial path is ignored since the user hasn't
1568 // been prompted before.
1569 AUTOMATIC,
1570 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1571 "http://example.com/foo.html", "",
1572 FILE_PATH_LITERAL(""),
1573
1574 FILE_PATH_LITERAL(""),
1575 FILE_PATH_LITERAL("foo.html"),
1576 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1577
1578 EXPECT_UNCONFIRMED
1579 },
1580
1581 {
1582 // 3: Forced Safe: Initial path is ignored due to the forced path.
1583 FORCED,
1584 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1585 "http://example.com/foo.txt", "",
1586 FILE_PATH_LITERAL("forced-foo.txt"),
1587
1588 FILE_PATH_LITERAL(""),
1589 FILE_PATH_LITERAL("forced-foo.txt"),
1590 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1591
1592 EXPECT_LOCAL_PATH
1593 },
1594 };
1595
1596 // The test assumes that .html files have a danger level of
1597 // AllowOnUserGesture.
1598 ASSERT_EQ(download_util::AllowOnUserGesture,
1599 download_util::GetFileDangerLevel(
1600 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
1601 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1602 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1603 const DownloadTestCase& test_case = kResumedTestCases[i];
1604 scoped_ptr<content::MockDownloadItem> item(
1605 CreateActiveDownloadItem(i, test_case));
1606 base::FilePath expected_path =
1607 GetPathInDownloadDir(test_case.expected_local_path);
1608 ON_CALL(*item.get(), GetLastReason())
1609 .WillByDefault(Return(
1610 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1611 // Extensions should be notified if a new path is being generated and there
1612 // is no forced path. In the test cases above, this is true for tests with
1613 // type == AUTOMATIC.
1614 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1615 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1616 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1617 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _))
1618 .Times(0);
1619 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1620 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1621 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1622 }
1623
1624 }
1625
1626 // Test that a forced download doesn't prompt, even if the interrupt reason
1627 // suggests that the target path may not be suitable for downloads.
1628 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedForcedDownload) {
1629 const base::FilePath::CharType* kInitialPath =
1630 FILE_PATH_LITERAL("some_path/bar.txt");
1631 const DownloadTestCase kResumedForcedDownload = {
1632 // 3: Forced Safe
1633 FORCED,
1634 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1635 "http://example.com/foo.txt", "",
1636 FILE_PATH_LITERAL("forced-foo.txt"),
1637
1638 FILE_PATH_LITERAL(""),
1639 FILE_PATH_LITERAL("forced-foo.txt"),
1640 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1641
1642 EXPECT_LOCAL_PATH
1643 };
1644
1645 const DownloadTestCase& test_case = kResumedForcedDownload;
1646 base::FilePath expected_path =
1647 GetPathInDownloadDir(test_case.expected_local_path);
1648 scoped_ptr<content::MockDownloadItem> item(
1649 CreateActiveDownloadItem(0, test_case));
1650 ON_CALL(*item.get(), GetLastReason())
1651 .WillByDefault(Return(
1652 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1653 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1654 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1655 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1656 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, _, _))
1657 .Times(0);
1658 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1659 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1660 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1661 }
1662
1663 // Prompting behavior for resumed downloads is based on the last interrupt
1664 // reason. If the reason indicates that the target path may not be suitable for
1665 // the download (ACCESS_DENIED, NO_SPACE, etc..), then the user should be
1666 // prompted, and not otherwise. These test cases result in prompting since the
1667 // error is set to NO_SPACE.
1668 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ResumedWithPrompt) {
1669 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1670 // path.
1671 const base::FilePath::CharType* kInitialPath =
1672 FILE_PATH_LITERAL("some_path/bar.txt");
1673
1674 const DownloadTestCase kResumedTestCases[] = {
1675 {
1676 // 0: Automatic Safe
1677 AUTOMATIC,
1678 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1679 "http://example.com/foo.txt", "text/plain",
1680 FILE_PATH_LITERAL(""),
1681
1682 FILE_PATH_LITERAL(""),
1683 FILE_PATH_LITERAL("foo.txt"),
1684 DownloadItem::TARGET_DISPOSITION_PROMPT,
1685
1686 EXPECT_CRDOWNLOAD
1687 },
1688
1689 {
1690 // 1: Save_As Safe
1691 SAVE_AS,
1692 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1693 "http://example.com/foo.txt", "text/plain",
1694 FILE_PATH_LITERAL(""),
1695
1696 FILE_PATH_LITERAL(""),
1697 kInitialPath,
1698 DownloadItem::TARGET_DISPOSITION_PROMPT,
1699
1700 EXPECT_CRDOWNLOAD
1701 },
1702
1703 {
1704 // 2: Automatic Dangerous
1705 AUTOMATIC,
1706 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1707 "http://example.com/foo.html", "",
1708 FILE_PATH_LITERAL(""),
1709
1710 FILE_PATH_LITERAL(""),
1711 FILE_PATH_LITERAL("foo.html"),
1712 DownloadItem::TARGET_DISPOSITION_PROMPT,
1713
1714 EXPECT_CRDOWNLOAD
1715 },
1716 };
1717
1718 // The test assumes that .html files have a danger level of
1719 // AllowOnUserGesture.
1720 ASSERT_EQ(download_util::AllowOnUserGesture,
1721 download_util::GetFileDangerLevel(
1722 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
1723 for (size_t i = 0; i < arraysize(kResumedTestCases); ++i) {
1724 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1725 const DownloadTestCase& test_case = kResumedTestCases[i];
1726 base::FilePath expected_path =
1727 GetPathInDownloadDir(test_case.expected_local_path);
1728 scoped_ptr<content::MockDownloadItem> item(
1729 CreateActiveDownloadItem(i, test_case));
1730 ON_CALL(*item.get(), GetLastReason())
1731 .WillByDefault(Return(
1732 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
1733 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1734 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1735 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1736 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _));
1737 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1738 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1739 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1740 }
1741 }
1742
1481 } // namespace 1743 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698