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/file_path.h" | 9 #include "base/file_path.h" |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "content/browser/download/download_file_factory.h" | 12 #include "content/browser/download/download_file_factory.h" |
12 #include "content/browser/download/download_file_impl.h" | 13 #include "content/browser/download/download_file_impl.h" |
13 #include "content/browser/download/download_item_impl.h" | 14 #include "content/browser/download/download_item_impl.h" |
14 #include "content/browser/download/download_manager_impl.h" | 15 #include "content/browser/download/download_manager_impl.h" |
15 #include "content/browser/power_save_blocker.h" | 16 #include "content/browser/power_save_blocker.h" |
16 #include "content/browser/web_contents/web_contents_impl.h" | 17 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/public/common/content_switches.h" |
17 #include "content/public/test/download_test_observer.h" | 19 #include "content/public/test/download_test_observer.h" |
18 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
19 #include "content/shell/shell.h" | 21 #include "content/shell/shell.h" |
20 #include "content/shell/shell_browser_context.h" | 22 #include "content/shell/shell_browser_context.h" |
21 #include "content/shell/shell_download_manager_delegate.h" | 23 #include "content/shell/shell_download_manager_delegate.h" |
22 #include "content/test/content_browser_test.h" | 24 #include "content/test/content_browser_test.h" |
23 #include "content/test/content_browser_test_utils.h" | 25 #include "content/test/content_browser_test_utils.h" |
24 #include "content/test/net/url_request_mock_http_job.h" | 26 #include "content/test/net/url_request_mock_http_job.h" |
25 #include "content/test/net/url_request_slow_download_job.h" | 27 #include "content/test/net/url_request_slow_download_job.h" |
26 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "net/test/test_server.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
29 | 32 |
30 using ::testing::_; | 33 using ::testing::_; |
31 using ::testing::AllOf; | 34 using ::testing::AllOf; |
32 using ::testing::Field; | 35 using ::testing::Field; |
33 using ::testing::InSequence; | 36 using ::testing::InSequence; |
34 using ::testing::Property; | 37 using ::testing::Property; |
35 using ::testing::Return; | 38 using ::testing::Return; |
36 using ::testing::StrictMock; | 39 using ::testing::StrictMock; |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 std::vector<DownloadOpenDelayedCallback>* callbacks) { | 339 std::vector<DownloadOpenDelayedCallback>* callbacks) { |
337 callbacks->swap(delayed_callbacks_); | 340 callbacks->swap(delayed_callbacks_); |
338 } | 341 } |
339 private: | 342 private: |
340 virtual ~TestShellDownloadManagerDelegate() {} | 343 virtual ~TestShellDownloadManagerDelegate() {} |
341 | 344 |
342 bool delay_download_open_; | 345 bool delay_download_open_; |
343 std::vector<DownloadOpenDelayedCallback> delayed_callbacks_; | 346 std::vector<DownloadOpenDelayedCallback> delayed_callbacks_; |
344 }; | 347 }; |
345 | 348 |
| 349 // Filter for handling resumption; don't return true until |
| 350 // we see first a transition to IN_PROGRESS then a transition to |
| 351 // some final state. Since this is a stateful filter, we |
| 352 // must provide a flag in which to store the state; that flag |
| 353 // must be false on initialization. The flag must be the first argument |
| 354 // so that Bind() can be used to produce the callback expected by |
| 355 // DownloadUpdatedObserver. |
| 356 bool DownloadResumptionFilter(bool* state_flag, DownloadItem* download) { |
| 357 if (*state_flag && DownloadItem::IN_PROGRESS != download->GetState()) { |
| 358 return true; |
| 359 } |
| 360 |
| 361 if (DownloadItem::IN_PROGRESS == download->GetState()) |
| 362 *state_flag = true; |
| 363 |
| 364 return false; |
| 365 } |
| 366 |
| 367 // Filter for waiting for intermediate file rename. |
| 368 bool IntermediateFileRenameFilter(DownloadItem* download) { |
| 369 return !download->GetFullPath().empty(); |
| 370 } |
| 371 |
346 } // namespace | 372 } // namespace |
347 | 373 |
348 class DownloadContentTest : public ContentBrowserTest { | 374 class DownloadContentTest : public ContentBrowserTest { |
349 protected: | 375 protected: |
350 virtual void SetUpOnMainThread() OVERRIDE { | 376 virtual void SetUpOnMainThread() OVERRIDE { |
351 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 377 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
352 | 378 |
353 TestShellDownloadManagerDelegate* delegate = | 379 TestShellDownloadManagerDelegate* delegate = |
354 new TestShellDownloadManagerDelegate(); | 380 new TestShellDownloadManagerDelegate(); |
355 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); | 381 delegate->SetDownloadBehaviorForTesting(downloads_directory_.path()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 bool EnsureNoPendingDownloads() { | 425 bool EnsureNoPendingDownloads() { |
400 bool result = true; | 426 bool result = true; |
401 BrowserThread::PostTask( | 427 BrowserThread::PostTask( |
402 BrowserThread::IO, FROM_HERE, | 428 BrowserThread::IO, FROM_HERE, |
403 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 429 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
404 MessageLoop::current()->Run(); | 430 MessageLoop::current()->Run(); |
405 return result && | 431 return result && |
406 (CountingDownloadFile::GetNumberActiveFilesFromFileThread() == 0); | 432 (CountingDownloadFile::GetNumberActiveFilesFromFileThread() == 0); |
407 } | 433 } |
408 | 434 |
409 void DownloadAndWait(Shell* shell, const GURL& url) { | 435 void DownloadAndWait(Shell* shell, const GURL& url, |
| 436 DownloadItem::DownloadState expected_terminal_state) { |
410 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); | 437 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); |
411 NavigateToURL(shell, url); | 438 NavigateToURL(shell, url); |
412 observer->WaitForFinished(); | 439 observer->WaitForFinished(); |
413 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); | 440 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(expected_terminal_state)); |
414 } | 441 } |
415 | 442 |
416 // Checks that |path| is has |file_size| bytes, and matches the |value| | 443 // Checks that |path| is has |file_size| bytes, and matches the |value| |
417 // string. | 444 // string. |
418 bool VerifyFile(const FilePath& path, | 445 bool VerifyFile(const FilePath& path, |
419 const std::string& value, | 446 const std::string& value, |
420 const int64 file_size) { | 447 const int64 file_size) { |
421 std::string file_contents; | 448 std::string file_contents; |
422 | 449 |
423 bool read = file_util::ReadFileToString(path, &file_contents); | 450 bool read = file_util::ReadFileToString(path, &file_contents); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 std::vector<DownloadItem*> downloads; | 520 std::vector<DownloadItem*> downloads; |
494 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 521 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
495 ASSERT_EQ(1u, downloads.size()); | 522 ASSERT_EQ(1u, downloads.size()); |
496 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); | 523 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); |
497 DownloadItem* download1 = downloads[0]; // The only download. | 524 DownloadItem* download1 = downloads[0]; // The only download. |
498 | 525 |
499 // Start the second download and wait until it's done. | 526 // Start the second download and wait until it's done. |
500 FilePath file(FILE_PATH_LITERAL("download-test.lib")); | 527 FilePath file(FILE_PATH_LITERAL("download-test.lib")); |
501 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); | 528 GURL url(URLRequestMockHTTPJob::GetMockUrl(file)); |
502 // Download the file and wait. | 529 // Download the file and wait. |
503 DownloadAndWait(shell(), url); | 530 DownloadAndWait(shell(), url, DownloadItem::COMPLETE); |
504 | 531 |
505 // Should now have 2 items on the manager. | 532 // Should now have 2 items on the manager. |
506 downloads.clear(); | 533 downloads.clear(); |
507 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 534 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
508 ASSERT_EQ(2u, downloads.size()); | 535 ASSERT_EQ(2u, downloads.size()); |
509 // We don't know the order of the downloads. | 536 // We don't know the order of the downloads. |
510 DownloadItem* download2 = downloads[(download1 == downloads[0]) ? 1 : 0]; | 537 DownloadItem* download2 = downloads[(download1 == downloads[0]) ? 1 : 0]; |
511 | 538 |
512 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); | 539 ASSERT_EQ(DownloadItem::IN_PROGRESS, download1->GetState()); |
513 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); | 540 ASSERT_EQ(DownloadItem::COMPLETE, download2->GetState()); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
726 | 753 |
727 MockDownloadItemObserver observer; | 754 MockDownloadItemObserver observer; |
728 items[0]->AddObserver(&observer); | 755 items[0]->AddObserver(&observer); |
729 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); | 756 EXPECT_CALL(observer, OnDownloadDestroyed(items[0])); |
730 | 757 |
731 // Shutdown the download manager. Mostly this is confirming a lack of | 758 // Shutdown the download manager. Mostly this is confirming a lack of |
732 // crashes. | 759 // crashes. |
733 DownloadManagerForShell(shell())->Shutdown(); | 760 DownloadManagerForShell(shell())->Shutdown(); |
734 } | 761 } |
735 | 762 |
| 763 IN_PROC_BROWSER_TEST_F(DownloadContentTest, ResumeInterruptedDownload) { |
| 764 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 765 switches::kEnableDownloadResumption); |
| 766 ASSERT_TRUE(test_server()->Start()); |
| 767 // Default behavior is a 15K file with a RST at 10K. We request |
| 768 // a hold on the response so that we can get the target name determined |
| 769 // before handling the RST. |
| 770 // TODO(rdsmith): Figure out how to handle the races needed |
| 771 // so that we don't have to wait for the target name determination. |
| 772 GURL url = test_server()->GetURL("rangereset?hold"); |
| 773 |
| 774 // Download and wait for file determination. |
| 775 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
| 776 NavigateToURL(shell(), url); |
| 777 observer->WaitForFinished(); |
| 778 |
| 779 std::vector<DownloadItem*> downloads; |
| 780 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
| 781 ASSERT_EQ(1u, downloads.size()); |
| 782 DownloadItem* download(downloads[0]); |
| 783 if (download->GetFullPath().empty()) { |
| 784 DownloadUpdatedObserver intermediate_observer( |
| 785 download, base::Bind(&IntermediateFileRenameFilter)); |
| 786 intermediate_observer.WaitForEvent(); |
| 787 } |
| 788 |
| 789 // Unleash the RST! |
| 790 scoped_ptr<DownloadTestObserver> rst_observer(CreateWaiter(shell(), 1)); |
| 791 GURL release_url = test_server()->GetURL("download-finish"); |
| 792 NavigateToURL(shell(), release_url); |
| 793 rst_observer->WaitForFinished(); |
| 794 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
| 795 EXPECT_EQ(4000u, download->GetReceivedBytes()); |
| 796 EXPECT_EQ(8000u, download->GetTotalBytes()); |
| 797 EXPECT_EQ(FILE_PATH_LITERAL("rangereset.crdownload"), |
| 798 download->GetFullPath().BaseName().value()); |
| 799 |
| 800 { |
| 801 std::string file_contents; |
| 802 std::string expected_contents(4000, 'X'); |
| 803 ASSERT_TRUE(file_util::ReadFileToString( |
| 804 download->GetFullPath(), &file_contents)); |
| 805 EXPECT_EQ(4000u, file_contents.size()); |
| 806 // In conditional to avoid spamming the console with two 4000 char strings. |
| 807 if (expected_contents != file_contents) |
| 808 EXPECT_TRUE(false) << "File contents do not have expected value."; |
| 809 } |
| 810 |
| 811 // Resume; should get entire file (note that a restart will fail |
| 812 // here because it'll produce another RST). |
| 813 bool flag(false); |
| 814 DownloadUpdatedObserver complete_observer( |
| 815 download, base::Bind(&DownloadResumptionFilter, &flag)); |
| 816 download->ResumeInterruptedDownload(); |
| 817 NavigateToURL(shell(), release_url); // Needed to get past hold. |
| 818 complete_observer.WaitForEvent(); |
| 819 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 820 EXPECT_EQ(8000u, download->GetReceivedBytes()); |
| 821 EXPECT_EQ(8000u, download->GetTotalBytes()); |
| 822 EXPECT_EQ(FILE_PATH_LITERAL("rangereset"), |
| 823 download->GetFullPath().BaseName().value()) |
| 824 << "Target path name: " << download->GetTargetFilePath().value(); |
| 825 |
| 826 { |
| 827 std::string file_contents; |
| 828 std::string expected_contents(8000, 'X'); |
| 829 ASSERT_TRUE(file_util::ReadFileToString( |
| 830 download->GetFullPath(), &file_contents)); |
| 831 EXPECT_EQ(8000u, file_contents.size()); |
| 832 // In conditional to avoid spamming the console with two 800 char strings. |
| 833 if (expected_contents != file_contents) |
| 834 EXPECT_TRUE(false) << "File contents do not have expected value."; |
| 835 } |
| 836 } |
| 837 |
736 } // namespace content | 838 } // namespace content |
OLD | NEW |