Chromium Code Reviews| 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 "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/render_process_host.h" | |
| 11 #include "content/public/browser/resource_controller.h" | |
| 12 #include "content/public/browser/resource_throttle.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/browser/web_contents_delegate.h" | |
| 15 #include "content/public/test/test_renderer_host.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 #if defined(OS_ANDROID) | |
| 20 #include "chrome/browser/android/download/mock_download_controller_android.h" | |
| 21 #endif | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const char kTestUrl[] = "http://www.test.com/"; | |
|
asanka
2015/07/15 20:23:01
Nit: example.com
qinmin
2015/07/17 00:42:36
Done.
| |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 class MockWebContentsDelegate : public content::WebContentsDelegate { | |
| 30 public: | |
| 31 MockWebContentsDelegate() {} | |
| 32 ~MockWebContentsDelegate() override {} | |
| 33 }; | |
| 34 | |
| 35 class MockResourceController : public content::ResourceController { | |
| 36 public: | |
| 37 MOCK_METHOD0(Cancel, void()); | |
| 38 MOCK_METHOD0(CancelAndIgnore, void()); | |
| 39 MOCK_METHOD1(CancelWithError, void(int)); | |
| 40 MOCK_METHOD0(Resume, void()); | |
| 41 }; | |
| 42 | |
| 43 class DownloadResourceThrottleTest : public content::RenderViewHostTestHarness { | |
| 44 public: | |
| 45 DownloadResourceThrottleTest() | |
| 46 : throttle_(nullptr), limiter_(new DownloadRequestLimiter()) {} | |
| 47 | |
| 48 void SetUp() override { | |
| 49 RenderViewHostTestHarness::SetUp(); | |
| 50 web_contents()->SetDelegate(&delegate_); | |
| 51 #if defined(OS_ANDROID) | |
| 52 content::DownloadControllerAndroid::SetDownloadControllerAndroid( | |
| 53 &download_controller_); | |
| 54 #endif | |
| 55 } | |
| 56 | |
| 57 void TearDown() override { | |
| 58 content::BrowserThread::DeleteSoon( | |
| 59 content::BrowserThread::IO, FROM_HERE, throttle_); | |
| 60 #if defined(OS_ANDROID) | |
| 61 content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr); | |
| 62 #endif | |
| 63 RenderViewHostTestHarness::TearDown(); | |
| 64 } | |
| 65 | |
| 66 void StartThrottleOnIOThread(int process_id, int render_view_id) { | |
| 67 throttle_ = new DownloadResourceThrottle( | |
| 68 limiter_, process_id, render_view_id, GURL(kTestUrl), "GET"); | |
| 69 throttle_->set_controller_for_testing(&resource_controller_); | |
| 70 bool defer; | |
| 71 throttle_->WillStartRequest(&defer); | |
| 72 EXPECT_EQ(true, defer); | |
| 73 } | |
| 74 | |
| 75 void StartThrottle() { | |
| 76 content::BrowserThread::PostTask( | |
| 77 content::BrowserThread::IO, | |
| 78 FROM_HERE, | |
| 79 base::Bind( | |
| 80 &DownloadResourceThrottleTest::StartThrottleOnIOThread, | |
| 81 base::Unretained(this), | |
| 82 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), | |
| 83 web_contents()->GetRoutingID())); | |
| 84 base::RunLoop().RunUntilIdle(); | |
|
asanka
2015/07/15 20:23:00
The code as writtten assumes that the thread bundl
qinmin
2015/07/17 00:42:36
Done. Test will now post QuitClosure() to the UI t
| |
| 85 } | |
| 86 | |
| 87 protected: | |
| 88 content::ResourceThrottle* throttle_; | |
| 89 MockWebContentsDelegate delegate_; | |
| 90 scoped_refptr<DownloadRequestLimiter> limiter_; | |
| 91 ::testing::NiceMock<MockResourceController> resource_controller_; | |
| 92 #if defined(OS_ANDROID) | |
| 93 chrome::android::MockDownloadControllerAndroid download_controller_; | |
| 94 #endif | |
| 95 }; | |
| 96 | |
| 97 TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { | |
| 98 EXPECT_CALL(resource_controller_, Resume()); | |
| 99 StartThrottle(); | |
| 100 } | |
| 101 | |
| 102 #if defined(OS_ANDROID) | |
| 103 TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { | |
| 104 content::DownloadControllerAndroid::Get()-> | |
| 105 SetApproveFileAccessRequestForTesting(false); | |
| 106 EXPECT_CALL(resource_controller_, Cancel()); | |
| 107 StartThrottle(); | |
| 108 } | |
| 109 #endif | |
| OLD | NEW |