Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Unified Diff: components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc

Issue 2696493003: Introduce SubframeNavigationFilteringThrottle (Closed)
Patch Set: fix test names Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
diff --git a/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc b/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b9fa4c1b56e72f17f3b6fcf041a7f634d25ec1ac
--- /dev/null
+++ b/components/subresource_filter/content/browser/subframe_filtering_navigation_throttle_unittest.cc
@@ -0,0 +1,210 @@
+// 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/subframe_filtering_navigation_throttle.h"
+
+#include <memory>
+
+#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/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/common/referrer.h"
+#include "content/public/test/test_renderer_host.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace subresource_filter {
+
+class SubframeFilteringNavigationThrottleTest
+ : public content::RenderViewHostTestHarness {
+ public:
+ SubframeFilteringNavigationThrottleTest()
+ : blocking_task_runner_(new base::TestSimpleTaskRunner) {}
+ ~SubframeFilteringNavigationThrottleTest() override {}
+
+ void SetUp() override {
+ content::RenderViewHostTestHarness::SetUp();
+ NavigateAndCommit(GURL("https://example.test"));
+ }
+
+ void TearDown() override {
+ test_handle_.reset();
pkalinnikov 2017/02/14 12:11:07 Don't you need to reset the other handles here, e.
Charlie Harrison 2017/02/14 14:09:38 Yep, my non-cosmetic patch (PS1) did catch this pr
+ content::RenderViewHostTestHarness::TearDown();
+ }
+
+ void InitializeDocumentSubresourceFilter(const GURL& document_url,
+ ActivationLevel activation_level) {
+ ASSERT_NO_FATAL_FAILURE(
+ test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
+ "filter.html", &test_ruleset_pair_));
+ dealer_handle_ =
+ base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_);
+ dealer_handle_->SetRulesetFile(
+ testing::TestRuleset::Open(test_ruleset_pair_.indexed));
+ ruleset_handle_ =
+ base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get());
+ // Unretained is safe because the test blocks waiting until the task is
+ // posted.
pkalinnikov 2017/02/14 12:11:07 nit: until the task is run.
Charlie Harrison 2017/02/14 14:09:38 Done.
+ async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>(
+ ruleset_handle_.get(),
+ AsyncDocumentSubresourceFilter::InitializationParams(
+ document_url, activation_level, false /* measure_performance */),
+ base::Bind(&SubframeFilteringNavigationThrottleTest::ExpectActivation,
+ base::Unretained(this), ActivationState(activation_level)),
+ base::OnceClosure());
+ RunUntilIdle();
+ }
+
+ void ExpectActivation(ActivationState expected, ActivationState state) {
+ EXPECT_EQ(expected.activation_level, state.activation_level);
+ }
+
+ void RunUntilIdle() {
+ blocking_task_runner_->RunUntilIdle();
+ base::RunLoop().RunUntilIdle();
+ }
+
+ void CreateTestSubframeNavigation(const GURL& first_url) {
+ subframe_ = content::RenderFrameHostTester::For(main_rfh())
+ ->AppendChild("subframe");
+ test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting(
+ first_url, subframe_);
+ test_handle_->RegisterThrottleForTesting(
+ base::MakeUnique<SubframeFilteringNavigationThrottle>(
+ test_handle_.get(), async_filter_.get()));
+ }
+
+ content::NavigationThrottle::ThrottleCheckResult SimulateWillStart() {
+ return test_handle_->CallWillStartRequestForTesting(
+ false /* is_post */, content::Referrer(), true /* has_user_gesture */,
+ ui::PageTransition::PAGE_TRANSITION_LINK,
+ false /* is_external_protocol */);
+ }
+
+ content::NavigationThrottle::ThrottleCheckResult SimulateWillRedirect(
+ const GURL& new_url) {
+ return test_handle_->CallWillRedirectRequestForTesting(
+ new_url, false /* new_method_is_post */, GURL(),
+ false /* new_is_external_protocol */);
+ }
+
+ content::NavigationThrottle::ThrottleCheckResult
+ SimulateWillProcessResponse() {
+ return test_handle_->CallWillProcessResponseForTesting(subframe_, "");
+ }
+
+ bool NavigationIsCancelling() {
+ return test_handle_->IsCancellingForTesting();
+ }
+
+ 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<VerifiedRuleset::Handle> ruleset_handle_;
+
+ std::unique_ptr<AsyncDocumentSubresourceFilter> async_filter_;
+
+ std::unique_ptr<content::NavigationHandle> test_handle_;
+
+ content::RenderFrameHost* subframe_ = nullptr;
+
+ DISALLOW_COPY_AND_ASSIGN(SubframeFilteringNavigationThrottleTest);
+};
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnStart) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"),
+ ActivationLevel::ENABLED);
+ CreateTestSubframeNavigation(GURL("https://example.test/filter.html"));
+ EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
+ RunUntilIdle();
+ EXPECT_TRUE(NavigationIsCancelling());
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnRedirect) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"),
+ ActivationLevel::ENABLED);
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER,
+ SimulateWillRedirect(GURL("https://example.test/filter.html")));
+ RunUntilIdle();
+ EXPECT_TRUE(NavigationIsCancelling());
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnSecondRedirect) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"),
+ ActivationLevel::ENABLED);
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER,
+ SimulateWillRedirect(GURL("https://example.test/proceed2.html")));
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER,
+ SimulateWillRedirect(GURL("https://example.test/filter.html")));
+ RunUntilIdle();
+ EXPECT_TRUE(NavigationIsCancelling());
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, NeverFilterNonMatchingRule) {
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"),
+ ActivationLevel::ENABLED);
+ CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER,
+ SimulateWillRedirect(GURL("https://example.test/proceed2.html")));
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::PROCEED,
+ SimulateWillProcessResponse());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+}
+
+TEST_F(SubframeFilteringNavigationThrottleTest, NeverFilterWithInactiveParent) {
pkalinnikov 2017/02/14 12:11:07 This test is probably not needed any more because
Charlie Harrison 2017/02/14 14:09:39 Done.
+ InitializeDocumentSubresourceFilter(GURL("https://example.test"),
+ ActivationLevel::DISABLED);
+ CreateTestSubframeNavigation(GURL("https://example.test/filter.html"));
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::DEFER,
+ SimulateWillRedirect(GURL("https://example.test/filter.html")));
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+
+ EXPECT_EQ(content::NavigationThrottle::PROCEED,
+ SimulateWillProcessResponse());
+ RunUntilIdle();
+ EXPECT_FALSE(NavigationIsCancelling());
+}
+
+} // namespace subresource_filter

Powered by Google App Engine
This is Rietveld 408576698