Chromium Code Reviews| Index: components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc |
| diff --git a/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2030f5ed56691081531f2bc5ea6a3d6ab23e3bb6 |
| --- /dev/null |
| +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc |
| @@ -0,0 +1,145 @@ |
| +// Copyright 2017 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 "components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "base/test/test_simple_task_runner.h" |
| +#include "components/subresource_filter/content/browser/async_document_subresource_filter.h" |
| +#include "components/subresource_filter/content/browser/async_document_subresource_filter_test_utils.h" |
| +#include "components/subresource_filter/core/common/activation_level.h" |
| +#include "components/subresource_filter/core/common/activation_state.h" |
| +#include "components/subresource_filter/core/common/proto/rules.pb.h" |
| +#include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| +#include "components/subresource_filter/core/common/test_ruleset_utils.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/navigation_throttle.h" |
| +#include "content/public/common/referrer.h" |
| +#include "content/public/test/test_renderer_host.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace subresource_filter { |
| + |
| +class MockPageStateActivationThrottle : public content::NavigationThrottle { |
| + public: |
| + MockPageStateActivationThrottle( |
| + content::NavigationHandle* navigation_handle, |
| + ContentSubresourceFilterThrottleManager* throttle_manager) |
| + : content::NavigationThrottle(navigation_handle), |
| + throttle_manager_(throttle_manager) {} |
| + ~MockPageStateActivationThrottle() override {} |
| + |
| + // content::NavigationThrottle: |
| + content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
| + override { |
| + if (navigation_handle()->GetURL() == url_to_activate_) { |
| + throttle_manager_->NotifyPageActivationComputed( |
| + navigation_handle(), ActivationState(ActivationLevel::ENABLED)); |
| + } |
| + return content::NavigationThrottle::PROCEED; |
| + } |
| + |
| + private: |
| + const GURL url_to_activate_; |
| + ContentSubresourceFilterThrottleManager* throttle_manager_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockPageStateActivationThrottle); |
| +}; |
| + |
| +class ContentSubresourceFilterThrottleManagerTest |
| + : public content::RenderViewHostTestHarness, |
|
engedy
2017/02/20 15:58:13
nit: Could you please check if there are any prece
Charlie Harrison
2017/03/01 00:02:59
I couldn't find any. Happy to refactor if you want
engedy
2017/03/10 17:36:20
Fine by me. Both the WC and WCO seems pretty robus
|
| + public content::WebContentsObserver, |
| + public ContentSubresourceFilterThrottleManager::Delegate { |
| + public: |
| + ContentSubresourceFilterThrottleManagerTest() |
| + : ContentSubresourceFilterThrottleManager::Delegate(), |
| + blocking_task_runner_(new base::TestSimpleTaskRunner) {} |
| + ~ContentSubresourceFilterThrottleManagerTest() override {} |
| + |
| + void SetUp() override { |
| + content::RenderViewHostTestHarness::SetUp(); |
| + |
| + // Initialize the ruleset dealer. |
| + std::vector<proto::UrlRule> rules; |
| + rules.push_back(testing::CreateWhitelistRuleForDocument( |
| + "whitelist.com", proto::ACTIVATION_TYPE_DOCUMENT, {"parent.com"})); |
| + ASSERT_NO_FATAL_FAILURE(test_ruleset_creator_.CreateRulesetWithRules( |
| + rules, &test_ruleset_pair_)); |
| + dealer_handle_ = |
| + base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_); |
| + dealer_handle_->SetRulesetFile( |
| + testing::TestRuleset::Open(test_ruleset_pair_.indexed)); |
| + |
| + throttle_manager_ = |
| + base::MakeUnique<ContentSubresourceFilterThrottleManager>( |
| + this, dealer_handle_.get(), |
| + RenderViewHostTestHarness::web_contents()); |
| + } |
| + |
| + void TearDown() override { |
| + throttle_manager_.reset(); |
| + dealer_handle_.reset(); |
| + RunUntilIdle(); |
| + content::RenderViewHostTestHarness::TearDown(); |
| + } |
| + |
| + void RunUntilIdle() { |
| + blocking_task_runner_->RunUntilIdle(); |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + |
| + // content::WebContentsObserver |
| + void DidStartNavigation( |
| + content::NavigationHandle* navigation_handle) override { |
| + // Inject the proper throttles at this time. |
| + std::vector<std::unique_ptr<content::NavigationThrottle>> throttles; |
| + throttles.push_back(base::MakeUnique<MockPageStateActivationThrottle>( |
| + navigation_handle, throttle_manager_.get())); |
| + throttle_manager_->MaybeAppendNavigationThrottles(navigation_handle, |
| + &throttles); |
| + for (auto& it : throttles) { |
| + navigation_handle->RegisterThrottleForTesting(std::move(it)); |
| + } |
| + } |
| + |
| + // ContentSubresourceFilterThrottleManager::Delegate: |
| + void OnFirstSubresourceLoadDisallowed() override {} |
| + |
| + bool ShouldVetoActivation( |
| + content::NavigationHandle* navigation_handle) override { |
| + return false; |
| + } |
| + |
| + private: |
| + scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_; |
| + |
| + testing::TestRulesetCreator test_ruleset_creator_; |
| + testing::TestRulesetPair test_ruleset_pair_; |
| + |
| + std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_; |
| + |
| + std::unique_ptr<ContentSubresourceFilterThrottleManager> throttle_manager_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContentSubresourceFilterThrottleManagerTest); |
| +}; |
| + |
| +// TODO(csharrison): Make sure the following conditions are exercised in tests: |
| +// |
| +// - The delegate is not notified multiple times per page load of disallowed |
| +// loads. Test that the bool resets on new navigations. |
| +// |
| +// - Delegate veto works. |
| +// |
| +// - Destroying and re-generating the ruleset handle works. |
| +// |
| +// - ActivationState is properly propagated to subframes. |
| +// |
| +// - Verify IPCs are sent on activation. |
| + |
| +} // namespace subresource_filter |