OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "chrome/browser/download/download_request_limiter.h" |
| 8 #include "chrome/browser/download/download_resource_throttle.h" |
| 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/resource_controller.h" |
| 13 #include "content/public/browser/resource_throttle.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/browser/web_contents_delegate.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 #if defined(OS_ANDROID) |
| 21 #include "chrome/browser/android/download/mock_download_controller_android.h" |
| 22 #endif |
| 23 |
| 24 namespace { |
| 25 |
| 26 const char kTestUrl[] = "http://www.example.com/"; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 class MockWebContentsDelegate : public content::WebContentsDelegate { |
| 31 public: |
| 32 MockWebContentsDelegate() {} |
| 33 ~MockWebContentsDelegate() override {} |
| 34 }; |
| 35 |
| 36 class MockResourceController : public content::ResourceController { |
| 37 public: |
| 38 MOCK_METHOD0(Cancel, void()); |
| 39 MOCK_METHOD0(CancelAndIgnore, void()); |
| 40 MOCK_METHOD1(CancelWithError, void(int)); |
| 41 MOCK_METHOD0(Resume, void()); |
| 42 }; |
| 43 |
| 44 // Posts |quit_closure| to UI thread. |
| 45 ACTION_P(QuitLoop, quit_closure) { |
| 46 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 47 quit_closure); |
| 48 } |
| 49 |
| 50 class DownloadResourceThrottleTest : public ChromeRenderViewHostTestHarness { |
| 51 public: |
| 52 DownloadResourceThrottleTest() |
| 53 : throttle_(nullptr), limiter_(new DownloadRequestLimiter()) { |
| 54 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. |
| 55 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 56 } |
| 57 |
| 58 ~DownloadResourceThrottleTest() override {} |
| 59 |
| 60 void SetUp() override { |
| 61 ChromeRenderViewHostTestHarness::SetUp(); |
| 62 web_contents()->SetDelegate(&delegate_); |
| 63 run_loop_.reset(new base::RunLoop()); |
| 64 #if defined(OS_ANDROID) |
| 65 content::DownloadControllerAndroid::SetDownloadControllerAndroid( |
| 66 &download_controller_); |
| 67 #endif |
| 68 } |
| 69 |
| 70 void TearDown() override { |
| 71 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE, |
| 72 throttle_); |
| 73 #if defined(OS_ANDROID) |
| 74 content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr); |
| 75 #endif |
| 76 ChromeRenderViewHostTestHarness::TearDown(); |
| 77 } |
| 78 |
| 79 void StartThrottleOnIOThread(int process_id, int render_view_id) { |
| 80 throttle_ = new DownloadResourceThrottle( |
| 81 limiter_, process_id, render_view_id, GURL(kTestUrl), "GET"); |
| 82 throttle_->set_controller_for_testing(&resource_controller_); |
| 83 bool defer; |
| 84 throttle_->WillStartRequest(&defer); |
| 85 EXPECT_EQ(true, defer); |
| 86 } |
| 87 |
| 88 void StartThrottle() { |
| 89 content::BrowserThread::PostTask( |
| 90 content::BrowserThread::IO, FROM_HERE, |
| 91 base::Bind(&DownloadResourceThrottleTest::StartThrottleOnIOThread, |
| 92 base::Unretained(this), |
| 93 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 94 web_contents()->GetRoutingID())); |
| 95 run_loop_->Run(); |
| 96 } |
| 97 |
| 98 protected: |
| 99 content::ResourceThrottle* throttle_; |
| 100 MockWebContentsDelegate delegate_; |
| 101 scoped_refptr<DownloadRequestLimiter> limiter_; |
| 102 ::testing::NiceMock<MockResourceController> resource_controller_; |
| 103 scoped_ptr<base::RunLoop> run_loop_; |
| 104 #if defined(OS_ANDROID) |
| 105 chrome::android::MockDownloadControllerAndroid download_controller_; |
| 106 #endif |
| 107 }; |
| 108 |
| 109 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { |
| 110 EXPECT_CALL(resource_controller_, Resume()) |
| 111 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
| 112 StartThrottle(); |
| 113 } |
| 114 |
| 115 #if defined(OS_ANDROID) |
| 116 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { |
| 117 content::DownloadControllerAndroid::Get() |
| 118 ->SetApproveFileAccessRequestForTesting(false); |
| 119 EXPECT_CALL(resource_controller_, Cancel()) |
| 120 .WillOnce(QuitLoop(run_loop_->QuitClosure())); |
| 121 StartThrottle(); |
| 122 } |
| 123 #endif |
OLD | NEW |