Chromium Code Reviews| Index: components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc |
| diff --git a/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc b/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..394d505538414ae281b8591735828864d9d441fb |
| --- /dev/null |
| +++ b/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc |
| @@ -0,0 +1,120 @@ |
| +// Copyright 2016 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 "components/web_restrictions/browser/web_restrictions_client.h" |
| +#include "components/web_restrictions/browser/web_restrictions_resource_throttle.h" |
| +#include "content/public/browser/resource_controller.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/url_request/redirect_info.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace web_restrictions { |
| + |
| +namespace { |
| + |
| +class TestResourceController : public content::ResourceController { |
| + public: |
| + bool resume_called_; |
|
Bernhard Bauer
2016/02/19 13:46:26
Add accessor for these (at least the ones you need
aberent
2016/02/19 19:40:09
Done.
|
| + bool cancel_with_error_called_; |
| + int error_code_; |
| + base::Closure quit_closure_; |
| + |
| + TestResourceController(const base::Closure quit_closure) |
|
Bernhard Bauer
2016/02/19 13:46:26
Pass by const ref.
aberent
2016/02/19 19:40:09
Done.
|
| + : quit_closure_(quit_closure) {} |
| + |
| + void Cancel() override {} |
| + void CancelAndIgnore() override {} |
| + void CancelWithError(int error_code) override { |
| + cancel_with_error_called_ = true; |
| + error_code_ = error_code; |
| + quit_closure_.Run(); |
| + } |
| + void Resume() override { |
| + resume_called_ = true; |
| + quit_closure_.Run(); |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +/* |
| + * Tests of C++ WebRestrictionsResourceThrottle. Note that these tests use a |
| + * dummy version of the Java WebRestrictionsClient which gives predefined |
| + * responses based on the authority and the URL. They will not work if |
| + * built with the real version of the Java WebRestrictionsClient. |
| + */ |
| + |
| +TEST(WebRestrictionsResourceThrottle, TestWillStartRequest) { |
| + content::TestBrowserThreadBundle thread_bundle; |
|
Bernhard Bauer
2016/02/19 13:46:26
It might be worth moving these into a test fixture
aberent
2016/02/19 19:40:09
Done.
|
| + WebRestrictionsClient provider; |
| + WebRestrictionsResourceThrottle throttle(&provider, |
| + GURL("http://example.com"), true); |
| + bool defer; |
| + throttle.WillStartRequest(&defer); |
| + // If there is no authority the request won't be deferred. |
| + EXPECT_FALSE(defer); |
| + |
| + // Test deferring with a resouce provider, and that the correct results |
| + // are received. |
| + provider.SetAuthority("Good"); |
| + base::RunLoop run_loop; |
| + TestResourceController controller(run_loop.QuitClosure()); |
| + throttle.set_controller_for_testing(&controller); |
| + throttle.WillStartRequest(&defer); |
| + EXPECT_TRUE(defer); |
| + run_loop.Run(); |
| + EXPECT_TRUE(controller.resume_called_); |
| + EXPECT_FALSE(controller.cancel_with_error_called_); |
| + provider.SetAuthority("Bad"); |
| + base::RunLoop run_loop2; |
| + TestResourceController controller2(run_loop2.QuitClosure()); |
| + throttle.set_controller_for_testing(&controller2); |
| + throttle.WillStartRequest(&defer); |
| + EXPECT_TRUE(defer); |
| + run_loop2.Run(); |
| + EXPECT_FALSE(controller2.resume_called_); |
| + EXPECT_TRUE(controller2.cancel_with_error_called_); |
| + EXPECT_EQ(net::ERR_BLOCKED_BY_ADMINISTRATOR, controller2.error_code_); |
| + |
| + // Only the main frame should be deferred. |
| + WebRestrictionsResourceThrottle throttle2(&provider, |
| + GURL("http://example.com"), false); |
| + base::RunLoop run_loop3; |
| + TestResourceController controller3(run_loop3.QuitClosure()); |
| + throttle2.set_controller_for_testing(&controller3); |
| + throttle2.WillStartRequest(&defer); |
| + EXPECT_FALSE(defer); |
| +} |
| + |
| +TEST(WebRestrictionsResourceThrottle, TestWillRedirectRequest) { |
| + content::TestBrowserThreadBundle thread_bundle; |
| + WebRestrictionsClient provider; |
| + |
| + // Set up a cached url. |
| + WebRestrictionsResourceThrottle throttle(&provider, |
| + GURL("http://example.com"), true); |
| + bool defer; |
| + provider.SetAuthority("Good"); |
| + base::RunLoop run_loop; |
| + TestResourceController controller(run_loop.QuitClosure()); |
| + throttle.set_controller_for_testing(&controller); |
| + throttle.WillStartRequest(&defer); |
| + run_loop.Run(); |
| + |
| + // Using the same URL should not be deferred |
| + net::RedirectInfo redirect; |
| + redirect.new_url = GURL("http://example.com"); |
| + throttle.WillRedirectRequest(redirect, &defer); |
| + EXPECT_FALSE(defer); |
| + |
| + // Using a different URL should be deferred |
| + redirect.new_url = GURL("http://example.com/2"); |
| + throttle.WillRedirectRequest(redirect, &defer); |
| + EXPECT_TRUE(defer); |
| +} |
| + |
| +} // namespace web_restrictions |