OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "chrome/browser/download/download_request_limiter.h" | 7 #include "chrome/browser/download/download_request_limiter.h" |
8 #include "chrome/browser/download/download_resource_throttle.h" | 8 #include "chrome/browser/download/download_resource_throttle.h" |
9 #include "chrome/browser/tab_contents/tab_util.h" | 9 #include "chrome/browser/tab_contents/tab_util.h" |
10 #include "chrome/common/features.h" | 10 #include "chrome/common/features.h" |
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/resource_controller.h" | |
15 #include "content/public/browser/resource_throttle.h" | 14 #include "content/public/browser/resource_throttle.h" |
16 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_delegate.h" | 16 #include "content/public/browser/web_contents_delegate.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
21 | 20 |
22 #if BUILDFLAG(ANDROID_JAVA_UI) | 21 #if BUILDFLAG(ANDROID_JAVA_UI) |
23 #include "chrome/browser/android/download/mock_download_controller.h" | 22 #include "chrome/browser/android/download/mock_download_controller.h" |
24 #endif | 23 #endif |
25 | 24 |
26 namespace { | 25 namespace { |
27 | 26 |
28 const char kTestUrl[] = "http://www.example.com/"; | 27 const char kTestUrl[] = "http://www.example.com/"; |
29 | 28 |
30 } // namespace | 29 } // namespace |
31 | 30 |
32 class MockWebContentsDelegate : public content::WebContentsDelegate { | 31 class MockWebContentsDelegate : public content::WebContentsDelegate { |
33 public: | 32 public: |
34 MockWebContentsDelegate() {} | 33 MockWebContentsDelegate() {} |
35 ~MockWebContentsDelegate() override {} | 34 ~MockWebContentsDelegate() override {} |
36 }; | 35 }; |
37 | 36 |
38 class MockResourceController : public content::ResourceController { | 37 class MockResourceThrottleDelegate |
| 38 : public content::ResourceThrottle::Delegate { |
39 public: | 39 public: |
40 MOCK_METHOD0(Cancel, void()); | 40 MOCK_METHOD0(Cancel, void()); |
41 MOCK_METHOD0(CancelAndIgnore, void()); | 41 MOCK_METHOD0(CancelAndIgnore, void()); |
42 MOCK_METHOD1(CancelWithError, void(int)); | 42 MOCK_METHOD1(CancelWithError, void(int)); |
43 MOCK_METHOD0(Resume, void()); | 43 MOCK_METHOD0(Resume, void()); |
44 }; | 44 }; |
45 | 45 |
46 // Posts |quit_closure| to UI thread. | 46 // Posts |quit_closure| to UI thread. |
47 ACTION_P(QuitLoop, quit_closure) { | 47 ACTION_P(QuitLoop, quit_closure) { |
48 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 48 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
(...skipping 26 matching lines...) Expand all Loading... |
75 DownloadControllerBase::SetDownloadControllerBase(nullptr); | 75 DownloadControllerBase::SetDownloadControllerBase(nullptr); |
76 #endif | 76 #endif |
77 ChromeRenderViewHostTestHarness::TearDown(); | 77 ChromeRenderViewHostTestHarness::TearDown(); |
78 } | 78 } |
79 | 79 |
80 void StartThrottleOnIOThread(int process_id, int render_view_id) { | 80 void StartThrottleOnIOThread(int process_id, int render_view_id) { |
81 throttle_ = new DownloadResourceThrottle( | 81 throttle_ = new DownloadResourceThrottle( |
82 limiter_, | 82 limiter_, |
83 base::Bind(&tab_util::GetWebContentsByID, process_id, render_view_id), | 83 base::Bind(&tab_util::GetWebContentsByID, process_id, render_view_id), |
84 GURL(kTestUrl), "GET"); | 84 GURL(kTestUrl), "GET"); |
85 throttle_->set_controller_for_testing(&resource_controller_); | 85 throttle_->set_delegate_for_testing(&resource_throttle_delegate_); |
86 bool defer; | 86 bool defer; |
87 throttle_->WillStartRequest(&defer); | 87 throttle_->WillStartRequest(&defer); |
88 EXPECT_EQ(true, defer); | 88 EXPECT_EQ(true, defer); |
89 } | 89 } |
90 | 90 |
91 void StartThrottle() { | 91 void StartThrottle() { |
92 content::BrowserThread::PostTask( | 92 content::BrowserThread::PostTask( |
93 content::BrowserThread::IO, FROM_HERE, | 93 content::BrowserThread::IO, FROM_HERE, |
94 base::Bind(&DownloadResourceThrottleTest::StartThrottleOnIOThread, | 94 base::Bind(&DownloadResourceThrottleTest::StartThrottleOnIOThread, |
95 base::Unretained(this), | 95 base::Unretained(this), |
96 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), | 96 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
97 web_contents()->GetRenderViewHost()->GetRoutingID())); | 97 web_contents()->GetRenderViewHost()->GetRoutingID())); |
98 run_loop_->Run(); | 98 run_loop_->Run(); |
99 } | 99 } |
100 | 100 |
101 protected: | 101 protected: |
102 content::ResourceThrottle* throttle_; | 102 content::ResourceThrottle* throttle_; |
103 MockWebContentsDelegate delegate_; | 103 MockWebContentsDelegate delegate_; |
104 scoped_refptr<DownloadRequestLimiter> limiter_; | 104 scoped_refptr<DownloadRequestLimiter> limiter_; |
105 ::testing::NiceMock<MockResourceController> resource_controller_; | 105 ::testing::NiceMock<MockResourceThrottleDelegate> resource_throttle_delegate_; |
106 std::unique_ptr<base::RunLoop> run_loop_; | 106 std::unique_ptr<base::RunLoop> run_loop_; |
107 #if BUILDFLAG(ANDROID_JAVA_UI) | 107 #if BUILDFLAG(ANDROID_JAVA_UI) |
108 chrome::android::MockDownloadController download_controller_; | 108 chrome::android::MockDownloadController download_controller_; |
109 #endif | 109 #endif |
110 }; | 110 }; |
111 | 111 |
112 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { | 112 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { |
113 EXPECT_CALL(resource_controller_, Resume()) | 113 EXPECT_CALL(resource_throttle_delegate_, Resume()) |
114 .WillOnce(QuitLoop(run_loop_->QuitClosure())); | 114 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
115 StartThrottle(); | 115 StartThrottle(); |
116 } | 116 } |
117 | 117 |
118 #if BUILDFLAG(ANDROID_JAVA_UI) | 118 #if BUILDFLAG(ANDROID_JAVA_UI) |
119 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { | 119 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { |
120 DownloadControllerBase::Get() | 120 DownloadControllerBase::Get() |
121 ->SetApproveFileAccessRequestForTesting(false); | 121 ->SetApproveFileAccessRequestForTesting(false); |
122 EXPECT_CALL(resource_controller_, Cancel()) | 122 EXPECT_CALL(resource_throttle_delegate_, Cancel()) |
123 .WillOnce(QuitLoop(run_loop_->QuitClosure())); | 123 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
124 StartThrottle(); | 124 StartThrottle(); |
125 } | 125 } |
126 #endif | 126 #endif |
OLD | NEW |