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 // This file contains download browser tests that are known to be runnable | 5 // This file contains download browser tests that are known to be runnable |
6 // in a pure content context. Over time tests should be migrated here. | 6 // in a pure content context. Over time tests should be migrated here. |
7 | 7 |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 int received_bytes, int total_bytes, | 657 int received_bytes, int total_bytes, |
658 const base::FilePath& expected_filename) { | 658 const base::FilePath& expected_filename) { |
659 // expected_filename is only known if the file exists. | 659 // expected_filename is only known if the file exists. |
660 ASSERT_EQ(file_exists, !expected_filename.empty()); | 660 ASSERT_EQ(file_exists, !expected_filename.empty()); |
661 EXPECT_EQ(received_bytes, download->GetReceivedBytes()); | 661 EXPECT_EQ(received_bytes, download->GetReceivedBytes()); |
662 EXPECT_EQ(total_bytes, download->GetTotalBytes()); | 662 EXPECT_EQ(total_bytes, download->GetTotalBytes()); |
663 EXPECT_EQ(expected_filename.value(), | 663 EXPECT_EQ(expected_filename.value(), |
664 download->GetFullPath().BaseName().value()); | 664 download->GetFullPath().BaseName().value()); |
665 EXPECT_EQ(file_exists, | 665 EXPECT_EQ(file_exists, |
666 (!download->GetFullPath().empty() && | 666 (!download->GetFullPath().empty() && |
667 file_util::PathExists(download->GetFullPath()))); | 667 base::PathExists(download->GetFullPath()))); |
668 | 668 |
669 if (file_exists) { | 669 if (file_exists) { |
670 std::string file_contents; | 670 std::string file_contents; |
671 EXPECT_TRUE(file_util::ReadFileToString( | 671 EXPECT_TRUE(file_util::ReadFileToString( |
672 download->GetFullPath(), &file_contents)); | 672 download->GetFullPath(), &file_contents)); |
673 | 673 |
674 ASSERT_EQ(static_cast<size_t>(received_bytes), file_contents.size()); | 674 ASSERT_EQ(static_cast<size_t>(received_bytes), file_contents.size()); |
675 for (int i = 0; i < received_bytes; ++i) { | 675 for (int i = 0; i < received_bytes; ++i) { |
676 EXPECT_EQ(static_cast<char>((i * 2 + 15) % 256), file_contents[i]) | 676 EXPECT_EQ(static_cast<char>((i * 2 + 15) % 256), file_contents[i]) |
677 << "File contents diverged at position " << i | 677 << "File contents diverged at position " << i |
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1405 DownloadItem* download(StartDownloadAndReturnItem(url1)); | 1405 DownloadItem* download(StartDownloadAndReturnItem(url1)); |
1406 WaitForData(download, GetSafeBufferChunk()); | 1406 WaitForData(download, GetSafeBufferChunk()); |
1407 | 1407 |
1408 ReleaseRSTAndConfirmInterruptForResume(download); | 1408 ReleaseRSTAndConfirmInterruptForResume(download); |
1409 ConfirmFileStatusForResume( | 1409 ConfirmFileStatusForResume( |
1410 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1410 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
1411 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1411 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
1412 | 1412 |
1413 base::FilePath intermediate_path(download->GetFullPath()); | 1413 base::FilePath intermediate_path(download->GetFullPath()); |
1414 ASSERT_FALSE(intermediate_path.empty()); | 1414 ASSERT_FALSE(intermediate_path.empty()); |
1415 EXPECT_TRUE(file_util::PathExists(intermediate_path)); | 1415 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1416 | 1416 |
1417 download->Cancel(true /* user_cancel */); | 1417 download->Cancel(true /* user_cancel */); |
1418 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1418 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1419 RunAllPendingInMessageLoop(); | 1419 RunAllPendingInMessageLoop(); |
1420 | 1420 |
1421 // The intermediate file should now be gone. | 1421 // The intermediate file should now be gone. |
1422 EXPECT_FALSE(file_util::PathExists(intermediate_path)); | 1422 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1423 EXPECT_TRUE(download->GetFullPath().empty()); | 1423 EXPECT_TRUE(download->GetFullPath().empty()); |
1424 } | 1424 } |
1425 | 1425 |
1426 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveDownload) { | 1426 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveDownload) { |
1427 CommandLine::ForCurrentProcess()->AppendSwitch( | 1427 CommandLine::ForCurrentProcess()->AppendSwitch( |
1428 switches::kEnableDownloadResumption); | 1428 switches::kEnableDownloadResumption); |
1429 ASSERT_TRUE(test_server()->Start()); | 1429 ASSERT_TRUE(test_server()->Start()); |
1430 | 1430 |
1431 // An interrupted download should remove the intermediate file when it is | 1431 // An interrupted download should remove the intermediate file when it is |
1432 // removed. | 1432 // removed. |
1433 { | 1433 { |
1434 GURL url1 = test_server()->GetURL( | 1434 GURL url1 = test_server()->GetURL( |
1435 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1435 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
1436 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 1436 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
1437 | 1437 |
1438 DownloadItem* download(StartDownloadAndReturnItem(url1)); | 1438 DownloadItem* download(StartDownloadAndReturnItem(url1)); |
1439 WaitForData(download, GetSafeBufferChunk()); | 1439 WaitForData(download, GetSafeBufferChunk()); |
1440 ReleaseRSTAndConfirmInterruptForResume(download); | 1440 ReleaseRSTAndConfirmInterruptForResume(download); |
1441 ConfirmFileStatusForResume( | 1441 ConfirmFileStatusForResume( |
1442 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1442 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
1443 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1443 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
1444 | 1444 |
1445 base::FilePath intermediate_path(download->GetFullPath()); | 1445 base::FilePath intermediate_path(download->GetFullPath()); |
1446 ASSERT_FALSE(intermediate_path.empty()); | 1446 ASSERT_FALSE(intermediate_path.empty()); |
1447 EXPECT_TRUE(file_util::PathExists(intermediate_path)); | 1447 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1448 | 1448 |
1449 download->Remove(); | 1449 download->Remove(); |
1450 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1450 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1451 RunAllPendingInMessageLoop(); | 1451 RunAllPendingInMessageLoop(); |
1452 | 1452 |
1453 // The intermediate file should now be gone. | 1453 // The intermediate file should now be gone. |
1454 EXPECT_FALSE(file_util::PathExists(intermediate_path)); | 1454 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1455 } | 1455 } |
1456 | 1456 |
1457 // A completed download shouldn't delete the downloaded file when it is | 1457 // A completed download shouldn't delete the downloaded file when it is |
1458 // removed. | 1458 // removed. |
1459 { | 1459 { |
1460 // Start the second download and wait until it's done. | 1460 // Start the second download and wait until it's done. |
1461 base::FilePath file2(FILE_PATH_LITERAL("download-test.lib")); | 1461 base::FilePath file2(FILE_PATH_LITERAL("download-test.lib")); |
1462 GURL url2(URLRequestMockHTTPJob::GetMockUrl(file2)); | 1462 GURL url2(URLRequestMockHTTPJob::GetMockUrl(file2)); |
1463 scoped_ptr<DownloadTestObserver> completion_observer( | 1463 scoped_ptr<DownloadTestObserver> completion_observer( |
1464 CreateWaiter(shell(), 1)); | 1464 CreateWaiter(shell(), 1)); |
1465 DownloadItem* download(StartDownloadAndReturnItem(url2)); | 1465 DownloadItem* download(StartDownloadAndReturnItem(url2)); |
1466 completion_observer->WaitForFinished(); | 1466 completion_observer->WaitForFinished(); |
1467 | 1467 |
1468 // The target path should exist. | 1468 // The target path should exist. |
1469 base::FilePath target_path(download->GetTargetFilePath()); | 1469 base::FilePath target_path(download->GetTargetFilePath()); |
1470 EXPECT_TRUE(file_util::PathExists(target_path)); | 1470 EXPECT_TRUE(base::PathExists(target_path)); |
1471 download->Remove(); | 1471 download->Remove(); |
1472 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1472 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1473 RunAllPendingInMessageLoop(); | 1473 RunAllPendingInMessageLoop(); |
1474 | 1474 |
1475 // The file should still exist. | 1475 // The file should still exist. |
1476 EXPECT_TRUE(file_util::PathExists(target_path)); | 1476 EXPECT_TRUE(base::PathExists(target_path)); |
1477 } | 1477 } |
1478 } | 1478 } |
1479 | 1479 |
1480 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { | 1480 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { |
1481 SetupEnsureNoPendingDownloads(); | 1481 SetupEnsureNoPendingDownloads(); |
1482 CommandLine::ForCurrentProcess()->AppendSwitch( | 1482 CommandLine::ForCurrentProcess()->AppendSwitch( |
1483 switches::kEnableDownloadResumption); | 1483 switches::kEnableDownloadResumption); |
1484 ASSERT_TRUE(test_server()->Start()); | 1484 ASSERT_TRUE(test_server()->Start()); |
1485 | 1485 |
1486 GURL url = test_server()->GetURL( | 1486 GURL url = test_server()->GetURL( |
1487 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1487 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
1488 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 1488 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
1489 | 1489 |
1490 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1490 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
1491 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1491 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1492 | 1492 |
1493 DownloadItem* download(StartDownloadAndReturnItem(url)); | 1493 DownloadItem* download(StartDownloadAndReturnItem(url)); |
1494 WaitForData(download, GetSafeBufferChunk()); | 1494 WaitForData(download, GetSafeBufferChunk()); |
1495 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | 1495 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); |
1496 | 1496 |
1497 // Tell the server to send the RST and confirm the interrupt happens. | 1497 // Tell the server to send the RST and confirm the interrupt happens. |
1498 ReleaseRSTAndConfirmInterruptForResume(download); | 1498 ReleaseRSTAndConfirmInterruptForResume(download); |
1499 ConfirmFileStatusForResume( | 1499 ConfirmFileStatusForResume( |
1500 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1500 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
1501 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1501 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
1502 | 1502 |
1503 base::FilePath intermediate_path(download->GetFullPath()); | 1503 base::FilePath intermediate_path(download->GetFullPath()); |
1504 ASSERT_FALSE(intermediate_path.empty()); | 1504 ASSERT_FALSE(intermediate_path.empty()); |
1505 EXPECT_TRUE(file_util::PathExists(intermediate_path)); | 1505 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1506 | 1506 |
1507 // Resume and remove download. We expect only a single OnDownloadCreated() | 1507 // Resume and remove download. We expect only a single OnDownloadCreated() |
1508 // call, and that's for the second download created below. | 1508 // call, and that's for the second download created below. |
1509 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1509 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1510 download->Resume(); | 1510 download->Resume(); |
1511 download->Remove(); | 1511 download->Remove(); |
1512 | 1512 |
1513 // The intermediate file should now be gone. | 1513 // The intermediate file should now be gone. |
1514 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1514 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1515 RunAllPendingInMessageLoop(); | 1515 RunAllPendingInMessageLoop(); |
1516 EXPECT_FALSE(file_util::PathExists(intermediate_path)); | 1516 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1517 | 1517 |
1518 // Start the second download and wait until it's done. The test server is | 1518 // Start the second download and wait until it's done. The test server is |
1519 // single threaded. The response to this download request should follow the | 1519 // single threaded. The response to this download request should follow the |
1520 // response to the previous resumption request. | 1520 // response to the previous resumption request. |
1521 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); | 1521 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); |
1522 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); | 1522 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); |
1523 | 1523 |
1524 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1524 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1525 } | 1525 } |
1526 | 1526 |
(...skipping 15 matching lines...) Expand all Loading... |
1542 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | 1542 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); |
1543 | 1543 |
1544 // Tell the server to send the RST and confirm the interrupt happens. | 1544 // Tell the server to send the RST and confirm the interrupt happens. |
1545 ReleaseRSTAndConfirmInterruptForResume(download); | 1545 ReleaseRSTAndConfirmInterruptForResume(download); |
1546 ConfirmFileStatusForResume( | 1546 ConfirmFileStatusForResume( |
1547 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1547 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
1548 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1548 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
1549 | 1549 |
1550 base::FilePath intermediate_path(download->GetFullPath()); | 1550 base::FilePath intermediate_path(download->GetFullPath()); |
1551 ASSERT_FALSE(intermediate_path.empty()); | 1551 ASSERT_FALSE(intermediate_path.empty()); |
1552 EXPECT_TRUE(file_util::PathExists(intermediate_path)); | 1552 EXPECT_TRUE(base::PathExists(intermediate_path)); |
1553 | 1553 |
1554 // Resume and cancel download. We expect only a single OnDownloadCreated() | 1554 // Resume and cancel download. We expect only a single OnDownloadCreated() |
1555 // call, and that's for the second download created below. | 1555 // call, and that's for the second download created below. |
1556 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1556 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1557 download->Resume(); | 1557 download->Resume(); |
1558 download->Cancel(true); | 1558 download->Cancel(true); |
1559 | 1559 |
1560 // The intermediate file should now be gone. | 1560 // The intermediate file should now be gone. |
1561 RunAllPendingInMessageLoop(BrowserThread::FILE); | 1561 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1562 RunAllPendingInMessageLoop(); | 1562 RunAllPendingInMessageLoop(); |
1563 EXPECT_FALSE(file_util::PathExists(intermediate_path)); | 1563 EXPECT_FALSE(base::PathExists(intermediate_path)); |
1564 EXPECT_TRUE(download->GetFullPath().empty()); | 1564 EXPECT_TRUE(download->GetFullPath().empty()); |
1565 | 1565 |
1566 // Start the second download and wait until it's done. The test server is | 1566 // Start the second download and wait until it's done. The test server is |
1567 // single threaded. The response to this download request should follow the | 1567 // single threaded. The response to this download request should follow the |
1568 // response to the previous resumption request. | 1568 // response to the previous resumption request. |
1569 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); | 1569 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); |
1570 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); | 1570 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); |
1571 | 1571 |
1572 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1572 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1573 } | 1573 } |
1574 | 1574 |
1575 } // namespace content | 1575 } // namespace content |
OLD | NEW |