Index: chrome/browser/download/download_resource_throttle_unittest.cc |
diff --git a/chrome/browser/download/download_resource_throttle_unittest.cc b/chrome/browser/download/download_resource_throttle_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e6b77484ccdbcd79aca578de57deaa8b37a96aa9 |
--- /dev/null |
+++ b/chrome/browser/download/download_resource_throttle_unittest.cc |
@@ -0,0 +1,109 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/run_loop.h" |
+#include "chrome/browser/download/download_request_limiter.h" |
+#include "chrome/browser/download/download_resource_throttle.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/resource_controller.h" |
+#include "content/public/browser/resource_throttle.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_delegate.h" |
+#include "content/public/test/test_renderer_host.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+#if defined(OS_ANDROID) |
+#include "chrome/browser/android/download/mock_download_controller_android.h" |
+#endif |
+ |
+namespace { |
+ |
+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.
|
+ |
+} // namespace |
+ |
+class MockWebContentsDelegate : public content::WebContentsDelegate { |
+ public: |
+ MockWebContentsDelegate() {} |
+ ~MockWebContentsDelegate() override {} |
+}; |
+ |
+class MockResourceController : public content::ResourceController { |
+ public: |
+ MOCK_METHOD0(Cancel, void()); |
+ MOCK_METHOD0(CancelAndIgnore, void()); |
+ MOCK_METHOD1(CancelWithError, void(int)); |
+ MOCK_METHOD0(Resume, void()); |
+}; |
+ |
+class DownloadResourceThrottleTest : public content::RenderViewHostTestHarness { |
+ public: |
+ DownloadResourceThrottleTest() |
+ : throttle_(nullptr), limiter_(new DownloadRequestLimiter()) {} |
+ |
+ void SetUp() override { |
+ RenderViewHostTestHarness::SetUp(); |
+ web_contents()->SetDelegate(&delegate_); |
+#if defined(OS_ANDROID) |
+ content::DownloadControllerAndroid::SetDownloadControllerAndroid( |
+ &download_controller_); |
+#endif |
+ } |
+ |
+ void TearDown() override { |
+ content::BrowserThread::DeleteSoon( |
+ content::BrowserThread::IO, FROM_HERE, throttle_); |
+#if defined(OS_ANDROID) |
+ content::DownloadControllerAndroid::SetDownloadControllerAndroid(nullptr); |
+#endif |
+ RenderViewHostTestHarness::TearDown(); |
+ } |
+ |
+ void StartThrottleOnIOThread(int process_id, int render_view_id) { |
+ throttle_ = new DownloadResourceThrottle( |
+ limiter_, process_id, render_view_id, GURL(kTestUrl), "GET"); |
+ throttle_->set_controller_for_testing(&resource_controller_); |
+ bool defer; |
+ throttle_->WillStartRequest(&defer); |
+ EXPECT_EQ(true, defer); |
+ } |
+ |
+ void StartThrottle() { |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, |
+ FROM_HERE, |
+ base::Bind( |
+ &DownloadResourceThrottleTest::StartThrottleOnIOThread, |
+ base::Unretained(this), |
+ web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
+ web_contents()->GetRoutingID())); |
+ 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
|
+ } |
+ |
+ protected: |
+ content::ResourceThrottle* throttle_; |
+ MockWebContentsDelegate delegate_; |
+ scoped_refptr<DownloadRequestLimiter> limiter_; |
+ ::testing::NiceMock<MockResourceController> resource_controller_; |
+#if defined(OS_ANDROID) |
+ chrome::android::MockDownloadControllerAndroid download_controller_; |
+#endif |
+}; |
+ |
+TEST_F(DownloadResourceThrottleTest, StartDownloadThrottle_Basic) { |
+ EXPECT_CALL(resource_controller_, Resume()); |
+ StartThrottle(); |
+} |
+ |
+#if defined(OS_ANDROID) |
+TEST_F(DownloadResourceThrottleTest, DownloadWithFailedFileAcecssRequest) { |
+ content::DownloadControllerAndroid::Get()-> |
+ SetApproveFileAccessRequestForTesting(false); |
+ EXPECT_CALL(resource_controller_, Cancel()); |
+ StartThrottle(); |
+} |
+#endif |