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 1360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1371 injector->InjectErrors(); | 1371 injector->InjectErrors(); |
1372 | 1372 |
1373 // Resume and watch completion. | 1373 // Resume and watch completion. |
1374 DownloadUpdatedObserver completion_observer( | 1374 DownloadUpdatedObserver completion_observer( |
1375 download, base::Bind(DownloadCompleteFilter)); | 1375 download, base::Bind(DownloadCompleteFilter)); |
1376 download->Resume(); | 1376 download->Resume(); |
1377 completion_observer.WaitForEvent(); | 1377 completion_observer.WaitForEvent(); |
1378 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); | 1378 EXPECT_EQ(download->GetState(), DownloadItem::COMPLETE); |
1379 } | 1379 } |
1380 | 1380 |
| 1381 // An interrupted download should remove the intermediate file when it is |
| 1382 // cancelled. |
| 1383 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelInterruptedDownload) { |
| 1384 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 1385 switches::kEnableDownloadResumption); |
| 1386 ASSERT_TRUE(test_server()->Start()); |
| 1387 |
| 1388 GURL url1 = test_server()->GetURL( |
| 1389 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
| 1390 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
| 1391 |
| 1392 DownloadItem* download(StartDownloadAndReturnItem(url1)); |
| 1393 WaitForData(download, GetSafeBufferChunk()); |
| 1394 |
| 1395 ReleaseRSTAndConfirmInterruptForResume(download); |
| 1396 ConfirmFileStatusForResume( |
| 1397 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
| 1398 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
| 1399 |
| 1400 base::FilePath intermediate_path(download->GetFullPath()); |
| 1401 ASSERT_FALSE(intermediate_path.empty()); |
| 1402 EXPECT_TRUE(file_util::PathExists(intermediate_path)); |
| 1403 |
| 1404 download->Cancel(true /* user_cancel */); |
| 1405 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1406 RunAllPendingInMessageLoop(); |
| 1407 |
| 1408 // The intermediate file should now be gone. |
| 1409 EXPECT_FALSE(file_util::PathExists(intermediate_path)); |
| 1410 EXPECT_TRUE(download->GetFullPath().empty()); |
| 1411 } |
| 1412 |
| 1413 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveDownload) { |
| 1414 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 1415 switches::kEnableDownloadResumption); |
| 1416 ASSERT_TRUE(test_server()->Start()); |
| 1417 |
| 1418 // An interrupted download should remove the intermediate file when it is |
| 1419 // removed. |
| 1420 { |
| 1421 GURL url1 = test_server()->GetURL( |
| 1422 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
| 1423 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
| 1424 |
| 1425 DownloadItem* download(StartDownloadAndReturnItem(url1)); |
| 1426 WaitForData(download, GetSafeBufferChunk()); |
| 1427 ReleaseRSTAndConfirmInterruptForResume(download); |
| 1428 ConfirmFileStatusForResume( |
| 1429 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
| 1430 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
| 1431 |
| 1432 base::FilePath intermediate_path(download->GetFullPath()); |
| 1433 ASSERT_FALSE(intermediate_path.empty()); |
| 1434 EXPECT_TRUE(file_util::PathExists(intermediate_path)); |
| 1435 |
| 1436 download->Remove(); |
| 1437 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1438 RunAllPendingInMessageLoop(); |
| 1439 |
| 1440 // The intermediate file should now be gone. |
| 1441 EXPECT_FALSE(file_util::PathExists(intermediate_path)); |
| 1442 } |
| 1443 |
| 1444 // A completed download shouldn't delete the downloaded file when it is |
| 1445 // removed. |
| 1446 { |
| 1447 // Start the second download and wait until it's done. |
| 1448 base::FilePath file2(FILE_PATH_LITERAL("download-test.lib")); |
| 1449 GURL url2(URLRequestMockHTTPJob::GetMockUrl(file2)); |
| 1450 scoped_ptr<DownloadTestObserver> completion_observer( |
| 1451 CreateWaiter(shell(), 1)); |
| 1452 DownloadItem* download(StartDownloadAndReturnItem(url2)); |
| 1453 completion_observer->WaitForFinished(); |
| 1454 |
| 1455 // The target path should exist. |
| 1456 base::FilePath target_path(download->GetTargetFilePath()); |
| 1457 EXPECT_TRUE(file_util::PathExists(target_path)); |
| 1458 download->Remove(); |
| 1459 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1460 RunAllPendingInMessageLoop(); |
| 1461 |
| 1462 // The file should still exist. |
| 1463 EXPECT_TRUE(file_util::PathExists(target_path)); |
| 1464 } |
| 1465 } |
| 1466 |
1381 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { | 1467 IN_PROC_BROWSER_TEST_F(DownloadContentTest, RemoveResumingDownload) { |
1382 SetupEnsureNoPendingDownloads(); | 1468 SetupEnsureNoPendingDownloads(); |
1383 CommandLine::ForCurrentProcess()->AppendSwitch( | 1469 CommandLine::ForCurrentProcess()->AppendSwitch( |
1384 switches::kEnableDownloadResumption); | 1470 switches::kEnableDownloadResumption); |
1385 ASSERT_TRUE(test_server()->Start()); | 1471 ASSERT_TRUE(test_server()->Start()); |
1386 | 1472 |
1387 GURL url = test_server()->GetURL( | 1473 GURL url = test_server()->GetURL( |
1388 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", | 1474 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
1389 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); | 1475 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
1390 | 1476 |
1391 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); | 1477 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
1392 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1478 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1393 | 1479 |
1394 DownloadItem* download(StartDownloadAndReturnItem(url)); | 1480 DownloadItem* download(StartDownloadAndReturnItem(url)); |
1395 WaitForData(download, GetSafeBufferChunk()); | 1481 WaitForData(download, GetSafeBufferChunk()); |
1396 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); | 1482 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); |
1397 | 1483 |
1398 // Tell the server to send the RST and confirm the interrupt happens. | 1484 // Tell the server to send the RST and confirm the interrupt happens. |
1399 ReleaseRSTAndConfirmInterruptForResume(download); | 1485 ReleaseRSTAndConfirmInterruptForResume(download); |
1400 ConfirmFileStatusForResume( | 1486 ConfirmFileStatusForResume( |
1401 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, | 1487 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
1402 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); | 1488 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
1403 | 1489 |
| 1490 base::FilePath intermediate_path(download->GetFullPath()); |
| 1491 ASSERT_FALSE(intermediate_path.empty()); |
| 1492 EXPECT_TRUE(file_util::PathExists(intermediate_path)); |
| 1493 |
1404 // Resume and remove download. We expect only a single OnDownloadCreated() | 1494 // Resume and remove download. We expect only a single OnDownloadCreated() |
1405 // call, and that's for the second download created below. | 1495 // call, and that's for the second download created below. |
1406 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); | 1496 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
1407 download->Resume(); | 1497 download->Resume(); |
1408 download->Remove(); | 1498 download->Remove(); |
1409 | 1499 |
1410 // Start the second download and wait until it's done. | 1500 // The intermediate file should now be gone. |
1411 base::FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 1501 RunAllPendingInMessageLoop(BrowserThread::FILE); |
1412 GURL url2(URLRequestMockHTTPJob::GetMockUrl(file)); | 1502 RunAllPendingInMessageLoop(); |
1413 // Download the file and wait. | 1503 EXPECT_FALSE(file_util::PathExists(intermediate_path)); |
| 1504 |
| 1505 // Start the second download and wait until it's done. The test server is |
| 1506 // single threaded. The response to this download request should follow the |
| 1507 // response to the previous resumption request. |
| 1508 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); |
1414 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); | 1509 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); |
1415 | 1510 |
1416 EXPECT_TRUE(EnsureNoPendingDownloads()); | 1511 EXPECT_TRUE(EnsureNoPendingDownloads()); |
| 1512 } |
| 1513 |
| 1514 IN_PROC_BROWSER_TEST_F(DownloadContentTest, CancelResumingDownload) { |
| 1515 SetupEnsureNoPendingDownloads(); |
| 1516 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 1517 switches::kEnableDownloadResumption); |
| 1518 ASSERT_TRUE(test_server()->Start()); |
| 1519 |
| 1520 GURL url = test_server()->GetURL( |
| 1521 base::StringPrintf("rangereset?size=%d&rst_boundary=%d", |
| 1522 GetSafeBufferChunk() * 3, GetSafeBufferChunk())); |
| 1523 |
| 1524 MockDownloadManagerObserver dm_observer(DownloadManagerForShell(shell())); |
| 1525 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
| 1526 |
| 1527 DownloadItem* download(StartDownloadAndReturnItem(url)); |
| 1528 WaitForData(download, GetSafeBufferChunk()); |
| 1529 ::testing::Mock::VerifyAndClearExpectations(&dm_observer); |
| 1530 |
| 1531 // Tell the server to send the RST and confirm the interrupt happens. |
| 1532 ReleaseRSTAndConfirmInterruptForResume(download); |
| 1533 ConfirmFileStatusForResume( |
| 1534 download, true, GetSafeBufferChunk(), GetSafeBufferChunk() * 3, |
| 1535 base::FilePath(FILE_PATH_LITERAL("rangereset.crdownload"))); |
| 1536 |
| 1537 base::FilePath intermediate_path(download->GetFullPath()); |
| 1538 ASSERT_FALSE(intermediate_path.empty()); |
| 1539 EXPECT_TRUE(file_util::PathExists(intermediate_path)); |
| 1540 |
| 1541 // Resume and cancel download. We expect only a single OnDownloadCreated() |
| 1542 // call, and that's for the second download created below. |
| 1543 EXPECT_CALL(dm_observer, OnDownloadCreated(_,_)).Times(1); |
| 1544 download->Resume(); |
| 1545 download->Cancel(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 1546 |
| 1547 // The intermediate file should now be gone. |
| 1548 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 1549 RunAllPendingInMessageLoop(); |
| 1550 EXPECT_FALSE(file_util::PathExists(intermediate_path)); |
| 1551 EXPECT_TRUE(download->GetFullPath().empty()); |
| 1552 |
| 1553 // Start the second download and wait until it's done. The test server is |
| 1554 // single threaded. The response to this download request should follow the |
| 1555 // response to the previous resumption request. |
| 1556 GURL url2(test_server()->GetURL("rangereset?size=100&rst_limit=0&token=x")); |
| 1557 DownloadAndWait(shell(), url2, DownloadItem::COMPLETE); |
| 1558 |
| 1559 EXPECT_TRUE(EnsureNoPendingDownloads()); |
1417 } | 1560 } |
1418 | 1561 |
1419 } // namespace content | 1562 } // namespace content |
OLD | NEW |