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/test/base/chrome_render_view_host_test_harness.h" | 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
12 #include "content/public/browser/resource_controller.h" | 12 #include "content/public/browser/resource_controller.h" |
13 #include "content/public/browser/resource_throttle.h" | 13 #include "content/public/browser/resource_throttle.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_contents_delegate.h" | 15 #include "content/public/browser/web_contents_delegate.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 #if defined(OS_ANDROID) | 20 #if defined(ANDROID_JAVA_UI) |
21 #include "chrome/browser/android/download/mock_download_controller_android.h" | 21 #include "chrome/browser/android/download/mock_download_controller_android.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kTestUrl[] = "http://www.example.com/"; | 26 const char kTestUrl[] = "http://www.example.com/"; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 class MockWebContentsDelegate : public content::WebContentsDelegate { | 30 class MockWebContentsDelegate : public content::WebContentsDelegate { |
(...skipping 23 matching lines...) Expand all Loading... |
54 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. | 54 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. |
55 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 55 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
56 } | 56 } |
57 | 57 |
58 ~DownloadResourceThrottleTest() override {} | 58 ~DownloadResourceThrottleTest() override {} |
59 | 59 |
60 void SetUp() override { | 60 void SetUp() override { |
61 ChromeRenderViewHostTestHarness::SetUp(); | 61 ChromeRenderViewHostTestHarness::SetUp(); |
62 web_contents()->SetDelegate(&delegate_); | 62 web_contents()->SetDelegate(&delegate_); |
63 run_loop_.reset(new base::RunLoop()); | 63 run_loop_.reset(new base::RunLoop()); |
64 #if defined(OS_ANDROID) | 64 #if defined(ANDROID_JAVA_UI) |
65 content::DownloadControllerAndroid::SetDownloadControllerAndroid( | 65 content::DownloadControllerAndroid::SetDownloadControllerAndroid( |
66 &download_controller_); | 66 &download_controller_); |
67 #endif | 67 #endif |
68 } | 68 } |
69 | 69 |
70 void TearDown() override { | 70 void TearDown() override { |
71 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, | 71 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, |
72 throttle_); | 72 throttle_); |
73 #if defined(OS_ANDROID) | 73 #if defined(ANDROID_JAVA_UI) |
74 content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr); | 74 content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr); |
75 #endif | 75 #endif |
76 ChromeRenderViewHostTestHarness::TearDown(); | 76 ChromeRenderViewHostTestHarness::TearDown(); |
77 } | 77 } |
78 | 78 |
79 void StartThrottleOnIOThread(int process_id, int render_view_id) { | 79 void StartThrottleOnIOThread(int process_id, int render_view_id) { |
80 throttle_ = new DownloadResourceThrottle( | 80 throttle_ = new DownloadResourceThrottle( |
81 limiter_, process_id, render_view_id, GURL(kTestUrl), "GET"); | 81 limiter_, process_id, render_view_id, GURL(kTestUrl), "GET"); |
82 throttle_->set_controller_for_testing(&resource_controller_); | 82 throttle_->set_controller_for_testing(&resource_controller_); |
83 bool defer; | 83 bool defer; |
(...skipping 10 matching lines...) Expand all Loading... |
94 web_contents()->GetRoutingID())); | 94 web_contents()->GetRoutingID())); |
95 run_loop_->Run(); | 95 run_loop_->Run(); |
96 } | 96 } |
97 | 97 |
98 protected: | 98 protected: |
99 content::ResourceThrottle* throttle_; | 99 content::ResourceThrottle* throttle_; |
100 MockWebContentsDelegate delegate_; | 100 MockWebContentsDelegate delegate_; |
101 scoped_refptr<DownloadRequestLimiter> limiter_; | 101 scoped_refptr<DownloadRequestLimiter> limiter_; |
102 ::testing::NiceMock<MockResourceController> resource_controller_; | 102 ::testing::NiceMock<MockResourceController> resource_controller_; |
103 scoped_ptr<base::RunLoop> run_loop_; | 103 scoped_ptr<base::RunLoop> run_loop_; |
104 #if defined(OS_ANDROID) | 104 #if defined(ANDROID_JAVA_UI) |
105 chrome::android::MockDownloadControllerAndroid download_controller_; | 105 chrome::android::MockDownloadControllerAndroid download_controller_; |
106 #endif | 106 #endif |
107 }; | 107 }; |
108 | 108 |
109 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { | 109 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { |
110 EXPECT_CALL(resource_controller_, Resume()) | 110 EXPECT_CALL(resource_controller_, Resume()) |
111 .WillOnce(QuitLoop(run_loop_->QuitClosure())); | 111 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
112 StartThrottle(); | 112 StartThrottle(); |
113 } | 113 } |
114 | 114 |
115 #if defined(OS_ANDROID) | 115 #if defined(ANDROID_JAVA_UI) |
116 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { | 116 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { |
117 content::DownloadControllerAndroid::Get() | 117 content::DownloadControllerAndroid::Get() |
118 ->SetApproveFileAccessRequestForTesting(false); | 118 ->SetApproveFileAccessRequestForTesting(false); |
119 EXPECT_CALL(resource_controller_, Cancel()) | 119 EXPECT_CALL(resource_controller_, Cancel()) |
120 .WillOnce(QuitLoop(run_loop_->QuitClosure())); | 120 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
121 StartThrottle(); | 121 StartThrottle(); |
122 } | 122 } |
123 #endif | 123 #endif |
OLD | NEW |