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..b42a0eab0789325900572f73eba76cb99432563d |
--- /dev/null |
+++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc |
@@ -0,0 +1,151 @@ |
+// 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_->OnPageStateActivationComputed( |
+ 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, |
+ public content::WebContentsObserver, |
+ public ContentSubresourceFilterThrottleManager::Delegate { |
+ public: |
+ ContentSubresourceFilterThrottleManagerTest() |
+ : ContentSubresourceFilterThrottleManager::Delegate(), |
+ blocking_task_runner_(new base::TestSimpleTaskRunner) {} |
+ ~ContentSubresourceFilterThrottleManagerTest() override {} |
+ |
+ void SetUp() override { |
+ content::RenderViewHostTestHarness::SetUp(); |
+ InitializeRulesetDealer(); |
+ } |
+ |
+ void TearDown() override { |
+ dealer_handle_.reset(); |
+ RunUntilIdle(); |
+ content::RenderViewHostTestHarness::TearDown(); |
+ } |
+ |
+ void RunUntilIdle() { |
+ blocking_task_runner_->RunUntilIdle(); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ |
+ void CreateTestNavigationForSubframe( |
+ const GURL& first_url, |
+ const ActivationState& parent_activation_state) { |
+ subframe_ = content::RenderFrameHostTester::For(main_rfh()) |
+ ->AppendChild("subframe"); |
+ } |
+ |
+ // content::WebContentsObserver |
+ void DidStartNavigation( |
+ content::NavigationHandle* navigation_handle) override { |
+ navigation_handle->RegisterThrottleForTesting( |
+ base::MakeUnique<MockPageStateActivationThrottle>( |
+ navigation_handle, throttle_manager_.get())); |
+ std::unique_ptr<content::NavigationThrottle> filtering_throttle = |
+ throttle_manager_->MaybeCreateSubframeNavigationFilteringThrottle( |
+ navigation_handle); |
+ std::unique_ptr<content::NavigationThrottle> activation_throttle = |
+ throttle_manager_->MaybeCreateActivationStateComputingThrottle( |
+ navigation_handle); |
+ if (filtering_throttle) |
+ navigation_handle->RegisterThrottleForTesting( |
+ std::move(filtering_throttle)); |
+ if (activation_throttle) |
+ navigation_handle->RegisterThrottleForTesting( |
+ std::move(activation_throttle)); |
+ } |
+ |
+ // ContentSubresourceFilterThrottleManager::Delegate: |
+ VerifiedRulesetDealer::Handle* GetRulesetDealerHandle() override { |
+ return dealer_handle_.get(); |
+ } |
+ |
+ void OnFirstSubresourceLoadDisallowed() override {} |
+ |
+ void OnActivationStateComputedForCommittingLoad( |
+ content::NavigationHandle* navigation_handle, |
+ const ActivationState& activation_state) override {} |
+ |
+ bool ShouldVetoActivation( |
+ content::NavigationHandle* navigation_handle) override { |
+ return false; |
+ } |
+ |
+ void InitializeRulesetDealer() { |
+ 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)); |
+ } |
+ |
+ 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_; |
+ |
+ content::RenderFrameHost* subframe_ = nullptr; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentSubresourceFilterThrottleManagerTest); |
+}; |
+ |
+} // namespace subresource_filter |