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

Side by Side Diff: content/browser/download/download_manager_impl_unittest.cc

Issue 2251643003: Remove the BeginSaveFile and BeginDownload methods from ResourceDispatcherHostImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move the blob code to RDHI::BeginURLRequest Created 4 years, 3 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
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 "content/browser/download/download_manager_impl.h" 5 #include "content/browser/download/download_manager_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <string> 12 #include <string>
13 #include <utility> 13 #include <utility>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
17 #include "base/guid.h" 17 #include "base/guid.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/weak_ptr.h" 19 #include "base/memory/weak_ptr.h"
20 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h"
21 #include "base/stl_util.h" 22 #include "base/stl_util.h"
22 #include "base/strings/string16.h" 23 #include "base/strings/string16.h"
23 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
24 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
25 #include "build/build_config.h" 26 #include "build/build_config.h"
26 #include "content/browser/byte_stream.h" 27 #include "content/browser/byte_stream.h"
28 #include "content/browser/child_process_security_policy_impl.h"
27 #include "content/browser/download/download_create_info.h" 29 #include "content/browser/download/download_create_info.h"
28 #include "content/browser/download/download_file_factory.h" 30 #include "content/browser/download/download_file_factory.h"
29 #include "content/browser/download/download_item_factory.h" 31 #include "content/browser/download/download_item_factory.h"
30 #include "content/browser/download/download_item_impl.h" 32 #include "content/browser/download/download_item_impl.h"
31 #include "content/browser/download/download_item_impl_delegate.h" 33 #include "content/browser/download/download_item_impl_delegate.h"
32 #include "content/browser/download/download_request_handle.h" 34 #include "content/browser/download/download_request_handle.h"
35 #include "content/browser/download/download_resource_handler.h"
33 #include "content/browser/download/mock_download_file.h" 36 #include "content/browser/download/mock_download_file.h"
37 #include "content/browser/loader/resource_dispatcher_host_impl.h"
34 #include "content/public/browser/browser_context.h" 38 #include "content/public/browser/browser_context.h"
35 #include "content/public/browser/download_interrupt_reasons.h" 39 #include "content/public/browser/download_interrupt_reasons.h"
36 #include "content/public/browser/download_item.h" 40 #include "content/public/browser/download_item.h"
37 #include "content/public/browser/download_manager_delegate.h" 41 #include "content/public/browser/download_manager_delegate.h"
42 #include "content/public/browser/render_frame_host.h"
43 #include "content/public/browser/render_process_host.h"
44 #include "content/public/browser/resource_context.h"
45 #include "content/public/browser/site_instance.h"
46 #include "content/public/browser/web_contents.h"
47 #include "content/public/browser/web_contents_observer.h"
38 #include "content/public/browser/zoom_level_delegate.h" 48 #include "content/public/browser/zoom_level_delegate.h"
39 #include "content/public/test/mock_download_item.h" 49 #include "content/public/test/mock_download_item.h"
40 #include "content/public/test/test_browser_context.h" 50 #include "content/public/test/test_browser_context.h"
41 #include "content/public/test/test_browser_thread.h" 51 #include "content/public/test/test_browser_thread.h"
52 #include "content/public/test/test_browser_thread_bundle.h"
42 #include "net/log/net_log.h" 53 #include "net/log/net_log.h"
54 #include "net/url_request/url_request_job_factory.h"
55 #include "net/url_request/url_request_test_job.h"
56 #include "net/url_request/url_request_test_util.h"
43 #include "testing/gmock/include/gmock/gmock.h" 57 #include "testing/gmock/include/gmock/gmock.h"
44 #include "testing/gmock_mutant.h" 58 #include "testing/gmock_mutant.h"
45 #include "testing/gtest/include/gtest/gtest.h" 59 #include "testing/gtest/include/gtest/gtest.h"
46 #include "url/origin.h" 60 #include "url/origin.h"
47 61
48 using ::testing::AllOf; 62 using ::testing::AllOf;
49 using ::testing::DoAll; 63 using ::testing::DoAll;
50 using ::testing::Eq; 64 using ::testing::Eq;
51 using ::testing::Ref; 65 using ::testing::Ref;
52 using ::testing::Return; 66 using ::testing::Return;
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 EXPECT_CALL(GetMockDownloadItem(0), Remove()); 800 EXPECT_CALL(GetMockDownloadItem(0), Remove());
787 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0); 801 EXPECT_CALL(GetMockDownloadItem(1), Remove()).Times(0);
788 802
789 base::Callback<bool(const GURL&)> url_filter = 803 base::Callback<bool(const GURL&)> url_filter =
790 GetSingleURLFilter(download_urls_[0]); 804 GetSingleURLFilter(download_urls_[0]);
791 int remove_count = download_manager_->RemoveDownloadsByURLAndTime( 805 int remove_count = download_manager_->RemoveDownloadsByURLAndTime(
792 url_filter, base::Time(), base::Time::Max()); 806 url_filter, base::Time(), base::Time::Max());
793 EXPECT_EQ(remove_count, 1); 807 EXPECT_EQ(remove_count, 1);
794 } 808 }
795 809
810 // For verifying notifications sent to the WebContents.
811 class TestDownloadWebContentsObserver : public WebContentsObserver {
812 public:
813 explicit TestDownloadWebContentsObserver(WebContents* web_contents)
814 : WebContentsObserver(web_contents),
815 resource_request_redirect_count_(0),
816 resource_response_start_count_(0) {}
817
818 void DidGetRedirectForResourceRequest(
819 RenderFrameHost* render_frame_host,
820 const ResourceRedirectDetails& details) override {
821 ++resource_request_redirect_count_;
822 }
823
824 void DidGetResourceResponseStart(
825 const ResourceRequestDetails& details) override {
826 ++resource_response_start_count_;
827 }
828
829 int resource_request_redirect_count() {
830 return resource_request_redirect_count_;
831 }
832
833 int resource_response_start_count() { return resource_response_start_count_; }
834
835 private:
836 int resource_request_redirect_count_;
837 int resource_response_start_count_;
838
839 DISALLOW_COPY_AND_ASSIGN(TestDownloadWebContentsObserver);
840 };
841
842 // Intercepts URLRequests and returns an instance of URLRequestTestJob for
843 // handling these requests if the scheme is a supported scheme.
844 class TestDownloadURLRequestFactory : public net::URLRequestJobFactory {
845 public:
846 explicit TestDownloadURLRequestFactory() {}
847
848 void HandleScheme(const std::string& scheme) {
849 supported_schemes_.insert(scheme);
850 }
851
852 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
853 const std::string& scheme,
854 net::URLRequest* request,
855 net::NetworkDelegate* network_delegate) const override {
856 if (!IsHandledProtocol(scheme))
857 return nullptr;
858 return new net::URLRequestTestJob(request, network_delegate, false);
859 }
860
861 net::URLRequestJob* MaybeInterceptRedirect(
862 net::URLRequest* request,
863 net::NetworkDelegate* network_delegate,
864 const GURL& location) const override {
865 return nullptr;
866 }
867
868 net::URLRequestJob* MaybeInterceptResponse(
869 net::URLRequest* request,
870 net::NetworkDelegate* network_delegate) const override {
871 return nullptr;
872 }
873
874 bool IsHandledProtocol(const std::string& scheme) const override {
875 return supported_schemes_.count(scheme) > 0;
876 }
877
878 bool IsHandledURL(const GURL& url) const override {
879 return supported_schemes_.count(url.scheme()) > 0;
880 }
881
882 bool IsSafeRedirectTarget(const GURL& location) const override {
883 return false;
884 }
885
886 private:
887 std::set<std::string> supported_schemes_;
888
889 DISALLOW_COPY_AND_ASSIGN(TestDownloadURLRequestFactory);
890 };
891
892 // This class provides functionality for testing downloads initiated via URL
893 // navigations.
894 class DownloadRequestTest : public testing::Test {
895 public:
896 DownloadRequestTest()
897 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
898 host_(base::Bind(&DownloadResourceHandler::Create)) {
899 browser_context_.reset(new TestBrowserContext());
900 BrowserContext::EnsureResourceContextInitialized(browser_context_.get());
901 base::RunLoop().RunUntilIdle();
902 }
903
904 ~DownloadRequestTest() override {}
905
906 // testing::Test overrides.
907 void SetUp() override {
908 // Boiler plate code necessary to initiate end to end navigations.
909 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0);
910 scoped_refptr<SiteInstance> site_instance =
911 SiteInstance::Create(browser_context_.get());
912 web_contents_.reset(
913 WebContents::Create(WebContents::CreateParams(browser_context_.get())));
914 web_contents_observer_.reset(
915 new TestDownloadWebContentsObserver(web_contents_.get()));
916 child_ids_.insert(web_contents_->GetRenderProcessHost()->GetID());
917 net::URLRequestContext* request_context =
918 browser_context_->GetResourceContext()->GetRequestContext();
919 job_factory_.reset(new TestDownloadURLRequestFactory());
920 request_context->set_job_factory(job_factory_.get());
921 request_context->set_network_delegate(&network_delegate_);
922 HandleScheme("test");
923 }
924
925 void TearDown() override {
926 web_contents_observer_.reset();
927 web_contents_.reset();
928
929 for (std::set<int>::iterator it = child_ids_.begin();
930 it != child_ids_.end(); ++it) {
931 host_.CancelRequestsForProcess(*it);
932 }
933
934 host_.Shutdown();
935
936 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(0);
937
938 // Flush the message loop to make application verifiers happy.
939 if (ResourceDispatcherHostImpl::Get()) {
940 ResourceDispatcherHostImpl::Get()->CancelRequestsForContext(
941 browser_context_->GetResourceContext());
942 }
943
944 browser_context_.reset();
945 base::RunLoop().RunUntilIdle();
946 }
947
948 // Intercepts requests for the given protocol.
949 void HandleScheme(const std::string& scheme) {
950 job_factory_->HandleScheme(scheme);
951 ChildProcessSecurityPolicyImpl* policy =
952 ChildProcessSecurityPolicyImpl::GetInstance();
953 if (!policy->IsWebSafeScheme(scheme))
954 policy->RegisterWebSafeScheme(scheme);
955 }
956
957 void InitiateDownloadRequest(int request_id, const GURL& url) {
958 net::URLRequestContext* request_context =
959 browser_context_->GetResourceContext()->GetRequestContext();
960 std::unique_ptr<net::URLRequest> request(
961 request_context->CreateRequest(url, net::DEFAULT_PRIORITY, NULL));
962
963 DownloadManagerImpl::BeginDownloadRequest(
964 std::move(request), Referrer(), browser_context_->GetResourceContext(),
965 false, // is_content_initiated
966 web_contents_->GetRenderProcessHost()->GetID(),
967 web_contents_->GetRoutingID(),
968 web_contents_->GetMainFrame()->GetRoutingID(), false);
969 }
970
971 protected:
972 // The thread_bundle_ needs to be the first in the list of members to
973 // ensure that the BrowserThread globals are initialized.
974 content::TestBrowserThreadBundle thread_bundle_;
975 ResourceDispatcherHostImpl host_;
976 std::unique_ptr<TestBrowserContext> browser_context_;
977 std::unique_ptr<TestDownloadURLRequestFactory> job_factory_;
978 net::TestNetworkDelegate network_delegate_;
979 std::unique_ptr<WebContents> web_contents_;
980 std::unique_ptr<TestDownloadWebContentsObserver> web_contents_observer_;
981 // List of child ids granted access via policy.
982 std::set<int> child_ids_;
983
984 DISALLOW_COPY_AND_ASSIGN(DownloadRequestTest);
985 };
986
987 // Confirm that resource response started notifications for downloads are not
988 // transmitted to the WebContents.
989 TEST_F(DownloadRequestTest, TransferResponseStartedDownload) {
990 int initial_count(web_contents_observer_->resource_response_start_count());
991
992 InitiateDownloadRequest(1, net::URLRequestTestJob::test_url_1());
993 base::RunLoop().RunUntilIdle();
994 EXPECT_EQ(initial_count,
995 web_contents_observer_->resource_response_start_count());
996 }
997
998 // Confirm that request redirected notifications for downloads are not
999 // transmitted to the WebContents.
1000 TEST_F(DownloadRequestTest, TransferRequestRedirectedDownload) {
1001 int initial_count(web_contents_observer_->resource_request_redirect_count());
1002
1003 InitiateDownloadRequest(1,
1004 net::URLRequestTestJob::test_url_redirect_to_url_2());
1005
1006 base::RunLoop().RunUntilIdle();
1007 EXPECT_EQ(initial_count,
1008 web_contents_observer_->resource_request_redirect_count());
1009 }
1010
Randy Smith (Not in Mondays) 2016/08/25 20:58:50 Wow, I'm sorry, I guess I'm doing a poor job of ex
ananta 2016/08/25 22:20:50 Based on our discussion, keeping the test in resou
796 } // namespace content 1011 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698