| 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 <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/files/scoped_temp_dir.h" | 17 #include "base/files/scoped_temp_dir.h" |
| 18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/metrics/field_trial.h" |
| 22 #include "base/metrics/field_trial_params.h" |
| 21 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
| 22 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 26 #include "base/test/mock_entropy_provider.h" |
| 27 #include "base/test/scoped_feature_list.h" |
| 24 #include "base/threading/platform_thread.h" | 28 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/sequenced_worker_pool.h" | 29 #include "base/threading/sequenced_worker_pool.h" |
| 26 #include "base/threading/thread_restrictions.h" | 30 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/time/time.h" | 31 #include "base/time/time.h" |
| 28 #include "build/build_config.h" | 32 #include "build/build_config.h" |
| 29 #include "content/browser/byte_stream.h" | 33 #include "content/browser/byte_stream.h" |
| 30 #include "content/browser/download/download_file_factory.h" | 34 #include "content/browser/download/download_file_factory.h" |
| 31 #include "content/browser/download/download_file_impl.h" | 35 #include "content/browser/download/download_file_impl.h" |
| 32 #include "content/browser/download/download_item_impl.h" | 36 #include "content/browser/download/download_item_impl.h" |
| 33 #include "content/browser/download/download_manager_impl.h" | 37 #include "content/browser/download/download_manager_impl.h" |
| 34 #include "content/browser/download/download_resource_handler.h" | 38 #include "content/browser/download/download_resource_handler.h" |
| 39 #include "content/browser/download/parallel_download_utils.h" |
| 35 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 40 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 36 #include "content/browser/web_contents/web_contents_impl.h" | 41 #include "content/browser/web_contents/web_contents_impl.h" |
| 37 #include "content/public/browser/download_danger_type.h" | 42 #include "content/public/browser/download_danger_type.h" |
| 38 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 43 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 39 #include "content/public/browser/resource_throttle.h" | 44 #include "content/public/browser/resource_throttle.h" |
| 40 #include "content/public/common/content_features.h" | 45 #include "content/public/common/content_features.h" |
| 41 #include "content/public/common/webplugininfo.h" | 46 #include "content/public/common/webplugininfo.h" |
| 42 #include "content/public/test/browser_test_utils.h" | 47 #include "content/public/test/browser_test_utils.h" |
| 43 #include "content/public/test/content_browser_test.h" | 48 #include "content/public/test/content_browser_test.h" |
| 44 #include "content/public/test/content_browser_test_utils.h" | 49 #include "content/public/test/content_browser_test_utils.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 75 using ::testing::_; | 80 using ::testing::_; |
| 76 | 81 |
| 77 namespace net { | 82 namespace net { |
| 78 class NetLogWithSource; | 83 class NetLogWithSource; |
| 79 } | 84 } |
| 80 | 85 |
| 81 namespace content { | 86 namespace content { |
| 82 | 87 |
| 83 namespace { | 88 namespace { |
| 84 | 89 |
| 90 // Default request count for parallel download tests. |
| 91 constexpr int kTestRequestCount = 3; |
| 92 |
| 85 class MockDownloadItemObserver : public DownloadItem::Observer { | 93 class MockDownloadItemObserver : public DownloadItem::Observer { |
| 86 public: | 94 public: |
| 87 MockDownloadItemObserver() {} | 95 MockDownloadItemObserver() {} |
| 88 virtual ~MockDownloadItemObserver() {} | 96 virtual ~MockDownloadItemObserver() {} |
| 89 | 97 |
| 90 MOCK_METHOD1(OnDownloadUpdated, void(DownloadItem*)); | 98 MOCK_METHOD1(OnDownloadUpdated, void(DownloadItem*)); |
| 91 MOCK_METHOD1(OnDownloadOpened, void(DownloadItem*)); | 99 MOCK_METHOD1(OnDownloadOpened, void(DownloadItem*)); |
| 92 MOCK_METHOD1(OnDownloadRemoved, void(DownloadItem*)); | 100 MOCK_METHOD1(OnDownloadRemoved, void(DownloadItem*)); |
| 93 MOCK_METHOD1(OnDownloadDestroyed, void(DownloadItem*)); | 101 MOCK_METHOD1(OnDownloadDestroyed, void(DownloadItem*)); |
| 94 }; | 102 }; |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 BrowserThread::IO, FROM_HERE, | 589 BrowserThread::IO, FROM_HERE, |
| 582 base::Bind(&net::URLRequestSlowDownloadJob::AddUrlHandler)); | 590 base::Bind(&net::URLRequestSlowDownloadJob::AddUrlHandler)); |
| 583 base::FilePath mock_base(GetTestFilePath("download", "")); | 591 base::FilePath mock_base(GetTestFilePath("download", "")); |
| 584 BrowserThread::PostTask( | 592 BrowserThread::PostTask( |
| 585 BrowserThread::IO, FROM_HERE, | 593 BrowserThread::IO, FROM_HERE, |
| 586 base::Bind( | 594 base::Bind( |
| 587 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base, | 595 &net::URLRequestMockHTTPJob::AddUrlHandlers, mock_base, |
| 588 make_scoped_refptr(content::BrowserThread::GetBlockingPool()))); | 596 make_scoped_refptr(content::BrowserThread::GetBlockingPool()))); |
| 589 } | 597 } |
| 590 | 598 |
| 591 void SetUpCommandLine(base::CommandLine* commnad_line) override { | 599 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 592 IsolateAllSitesForTesting(commnad_line); | 600 IsolateAllSitesForTesting(command_line); |
| 593 } | 601 } |
| 594 | 602 |
| 595 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() { | 603 TestShellDownloadManagerDelegate* GetDownloadManagerDelegate() { |
| 596 return test_delegate_.get(); | 604 return test_delegate_.get(); |
| 597 } | 605 } |
| 598 | 606 |
| 599 const base::FilePath& GetDownloadDirectory() const { | 607 const base::FilePath& GetDownloadDirectory() const { |
| 600 return downloads_directory_.GetPath(); | 608 return downloads_directory_.GetPath(); |
| 601 } | 609 } |
| 602 | 610 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 *result = false; | 747 *result = false; |
| 740 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 748 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 741 base::MessageLoop::QuitWhenIdleClosure()); | 749 base::MessageLoop::QuitWhenIdleClosure()); |
| 742 } | 750 } |
| 743 | 751 |
| 744 // Location of the downloads directory for these tests | 752 // Location of the downloads directory for these tests |
| 745 base::ScopedTempDir downloads_directory_; | 753 base::ScopedTempDir downloads_directory_; |
| 746 std::unique_ptr<TestShellDownloadManagerDelegate> test_delegate_; | 754 std::unique_ptr<TestShellDownloadManagerDelegate> test_delegate_; |
| 747 }; | 755 }; |
| 748 | 756 |
| 757 // Test fixture for parallel downloading. |
| 758 class ParallelDownloadTest : public DownloadContentTest { |
| 759 protected: |
| 760 void SetUp() override { |
| 761 field_trial_list_ = base::MakeUnique<base::FieldTrialList>( |
| 762 base::MakeUnique<base::MockEntropyProvider>()); |
| 763 SetupConfig(); |
| 764 DownloadContentTest::SetUp(); |
| 765 } |
| 766 |
| 767 private: |
| 768 // TODO(xingliu): Use this technique in parallel download unit tests to load |
| 769 // the finch configuration. |
| 770 void SetupConfig() { |
| 771 const std::string kTrialName = "trial_name"; |
| 772 const std::string kGroupName = "group_name"; |
| 773 |
| 774 std::map<std::string, std::string> params; |
| 775 params[content::kMinSliceSizeFinchKey] = "1"; |
| 776 params[content::kParallelRequestCountFinchKey] = |
| 777 base::IntToString(kTestRequestCount); |
| 778 params[content::kParallelRequestDelayFinchKey] = "0"; |
| 779 params[content::kParallelRequestRemainingTimeFinchKey] = "0"; |
| 780 |
| 781 scoped_refptr<base::FieldTrial> trial = |
| 782 base::FieldTrialList::CreateFieldTrial(kTrialName, kGroupName); |
| 783 base::AssociateFieldTrialParams(kTrialName, kGroupName, params); |
| 784 std::unique_ptr<base::FeatureList> feature_list = |
| 785 base::MakeUnique<base::FeatureList>(); |
| 786 feature_list->RegisterFieldTrialOverride( |
| 787 features::kParallelDownloading.name, |
| 788 base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial.get()); |
| 789 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); |
| 790 } |
| 791 |
| 792 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 793 base::test::ScopedFeatureList scoped_feature_list_; |
| 794 }; |
| 795 |
| 749 } // namespace | 796 } // namespace |
| 750 | 797 |
| 751 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { | 798 IN_PROC_BROWSER_TEST_F(DownloadContentTest, DownloadCancelled) { |
| 752 SetupEnsureNoPendingDownloads(); | 799 SetupEnsureNoPendingDownloads(); |
| 753 | 800 |
| 754 // Create a download, wait until it's started, and confirm | 801 // Create a download, wait until it's started, and confirm |
| 755 // we're in the expected state. | 802 // we're in the expected state. |
| 756 DownloadItem* download = StartDownloadAndReturnItem( | 803 DownloadItem* download = StartDownloadAndReturnItem( |
| 757 shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 804 shell(), GURL(net::URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
| 758 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 805 ASSERT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| (...skipping 1888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2647 frame_url = frame_url.ReplaceComponents(replacements); | 2694 frame_url = frame_url.ReplaceComponents(replacements); |
| 2648 GURL document_url = | 2695 GURL document_url = |
| 2649 origin_two.GetURL("/iframe-host.html?target=" + frame_url.spec()); | 2696 origin_two.GetURL("/iframe-host.html?target=" + frame_url.spec()); |
| 2650 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url); | 2697 DownloadItem* download = StartDownloadAndReturnItem(shell(), document_url); |
| 2651 WaitForCompletion(download); | 2698 WaitForCompletion(download); |
| 2652 | 2699 |
| 2653 EXPECT_STREQ(FILE_PATH_LITERAL("download-test.lib"), | 2700 EXPECT_STREQ(FILE_PATH_LITERAL("download-test.lib"), |
| 2654 download->GetTargetFilePath().BaseName().value().c_str()); | 2701 download->GetTargetFilePath().BaseName().value().c_str()); |
| 2655 } | 2702 } |
| 2656 | 2703 |
| 2704 IN_PROC_BROWSER_TEST_F(ParallelDownloadTest, ParallelDownloadComplete) { |
| 2705 EXPECT_TRUE(base::FeatureList::IsEnabled(features::kParallelDownloading)); |
| 2706 |
| 2707 TestDownloadRequestHandler request_handler; |
| 2708 TestDownloadRequestHandler::Parameters parameters; |
| 2709 parameters.etag = "ABC"; |
| 2710 parameters.size = 5097152; |
| 2711 // Only parallel download needs to specify the connection type to http 1.1, |
| 2712 // other tests will automatically fall back to non-parallel download even if |
| 2713 // the ParallelDownloading feature is enabled based on |
| 2714 // fieldtrial_testing_config.json. |
| 2715 parameters.connection_type = net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1; |
| 2716 request_handler.StartServing(parameters); |
| 2717 |
| 2718 DownloadItem* download = |
| 2719 StartDownloadAndReturnItem(shell(), request_handler.url()); |
| 2720 WaitForCompletion(download); |
| 2721 |
| 2722 TestDownloadRequestHandler::CompletedRequests completed_requests; |
| 2723 request_handler.GetCompletedRequestInfo(&completed_requests); |
| 2724 EXPECT_EQ(kTestRequestCount, static_cast<int>(completed_requests.size())); |
| 2725 |
| 2726 ReadAndVerifyFileContents(parameters.pattern_generator_seed, parameters.size, |
| 2727 download->GetTargetFilePath()); |
| 2728 } |
| 2729 |
| 2657 } // namespace content | 2730 } // namespace content |
| OLD | NEW |