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.
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"
20 #if defined(OS_ANDROID)
21 #include "chrome/browser/android/download/mock_download_controller_android.h"
26 const char kTestUrl
[] = "http://www.example.com/";
30 class MockWebContentsDelegate
: public content::WebContentsDelegate
{
32 MockWebContentsDelegate() {}
33 ~MockWebContentsDelegate() override
{}
36 class MockResourceController
: public content::ResourceController
{
38 MOCK_METHOD0(Cancel
, void());
39 MOCK_METHOD0(CancelAndIgnore
, void());
40 MOCK_METHOD1(CancelWithError
, void(int));
41 MOCK_METHOD0(Resume
, void());
44 // Posts |quit_closure| to UI thread.
45 ACTION_P(QuitLoop
, quit_closure
) {
46 content::BrowserThread::PostTask(content::BrowserThread::UI
, FROM_HERE
,
50 class DownloadResourceThrottleTest
: public ChromeRenderViewHostTestHarness
{
52 DownloadResourceThrottleTest()
53 : throttle_(nullptr), limiter_(new DownloadRequestLimiter()) {
54 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness.
55 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD
);
58 ~DownloadResourceThrottleTest() override
{}
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_
);
70 void TearDown() override
{
71 content::BrowserThread::DeleteSoon(content::BrowserThread::IO
, FROM_HERE
,
73 #if defined(OS_ANDROID)
74 content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr);
76 ChromeRenderViewHostTestHarness::TearDown();
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_
);
84 throttle_
->WillStartRequest(&defer
);
85 EXPECT_EQ(true, defer
);
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()));
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_
;
109 TEST_F(DownloadResourceThrottleTest
, StartDownloadThrottle_Basic
) {
110 EXPECT_CALL(resource_controller_
, Resume())
111 .WillOnce(QuitLoop(run_loop_
->QuitClosure()));
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()));