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, |
| 47 FROM_HERE, 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( |
| 72 content::BrowserThread::IO, FROM_HERE, 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, |
| 91 FROM_HERE, |
| 92 base::Bind( |
| 93 &DownloadResourceThrottleTest::StartThrottleOnIOThread, |
| 94 base::Unretained(this), |
| 95 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 96 web_contents()->GetRoutingID())); |
| 97 run_loop_->Run(); |
| 98 } |
| 99 |
| 100 protected: |
| 101 content::ResourceThrottle* throttle_; |
| 102 MockWebContentsDelegate delegate_; |
| 103 scoped_refptr<DownloadRequestLimiter> limiter_; |
| 104 ::testing::NiceMock<MockResourceController> resource_controller_; |
| 105 scoped_ptr<base::RunLoop> run_loop_; |
| 106 #if defined(OS_ANDROID) |
| 107 chrome::android::MockDownloadControllerAndroid download_controller_; |
| 108 #endif |
| 109 }; |
| 110 |
| 111 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { |
| 112 EXPECT_CALL(resource_controller_, Resume()).WillOnce( |
| 113 QuitLoop(run_loop_->QuitClosure())); |
| 114 StartThrottle(); |
| 115 } |
| 116 |
| 117 #if defined(OS_ANDROID) |
| 118 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { |
| 119 content::DownloadControllerAndroid::Get()-> |
| 120 SetApproveFileAccessRequestForTesting(false); |
| 121 EXPECT_CALL(resource_controller_, Cancel()).WillOnce( |
| 122 QuitLoop(run_loop_->QuitClosure())); |
| 123 StartThrottle(); |
| 124 } |
| 125 #endif |
OLD | NEW |