| 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..8367de56413d93ce3b353bcd18172816138f25c8
|
| --- /dev/null
|
| +++ b/components/subresource_filter/content/browser/content_subresource_filter_throttle_manager_unittest.cc
|
| @@ -0,0 +1,132 @@
|
| +// 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();
|
| +
|
| + // 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_->MaybeInsertNavigationThrottles(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);
|
| +};
|
| +
|
| +} // namespace subresource_filter
|
|
|