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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/subresource_filter/content/browser/subframe_filtering_navig ation_throttle.h"
6
7 #include <memory>
8
9 #include "base/callback_forward.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h"
14 #include "components/subresource_filter/core/common/activation_level.h"
15 #include "components/subresource_filter/core/common/activation_state.h"
16 #include "components/subresource_filter/core/common/proto/rules.pb.h"
17 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
18 #include "components/subresource_filter/core/common/test_ruleset_utils.h"
19 #include "content/public/browser/navigation_handle.h"
20 #include "content/public/common/referrer.h"
21 #include "content/public/test/test_renderer_host.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace subresource_filter {
25
26 class SubframeFilteringNavigationThrottleTest
27 : public content::RenderViewHostTestHarness {
28 public:
29 SubframeFilteringNavigationThrottleTest()
30 : blocking_task_runner_(new base::TestSimpleTaskRunner) {}
31 ~SubframeFilteringNavigationThrottleTest() override {}
32
33 void SetUp() override {
34 content::RenderViewHostTestHarness::SetUp();
35 NavigateAndCommit(GURL("https://example.test"));
36 }
37
38 void TearDown() override {
39 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
40 content::RenderViewHostTestHarness::TearDown();
41 }
42
43 void InitializeDocumentSubresourceFilter(const GURL& document_url,
44 ActivationLevel activation_level) {
45 ASSERT_NO_FATAL_FAILURE(
46 test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
47 "filter.html", &test_ruleset_pair_));
48 dealer_handle_ =
49 base::MakeUnique<VerifiedRulesetDealer::Handle>(blocking_task_runner_);
50 dealer_handle_->SetRulesetFile(
51 testing::TestRuleset::Open(test_ruleset_pair_.indexed));
52 ruleset_handle_ =
53 base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get());
54 // Unretained is safe because the test blocks waiting until the task is
55 // posted.
pkalinnikov 2017/02/14 12:11:07 nit: until the task is run.
Charlie Harrison 2017/02/14 14:09:38 Done.
56 async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>(
57 ruleset_handle_.get(),
58 AsyncDocumentSubresourceFilter::InitializationParams(
59 document_url, activation_level, false /* measure_performance */),
60 base::Bind(&SubframeFilteringNavigationThrottleTest::ExpectActivation,
61 base::Unretained(this), ActivationState(activation_level)),
62 base::OnceClosure());
63 RunUntilIdle();
64 }
65
66 void ExpectActivation(ActivationState expected, ActivationState state) {
67 EXPECT_EQ(expected.activation_level, state.activation_level);
68 }
69
70 void RunUntilIdle() {
71 blocking_task_runner_->RunUntilIdle();
72 base::RunLoop().RunUntilIdle();
73 }
74
75 void CreateTestSubframeNavigation(const GURL& first_url) {
76 subframe_ = content::RenderFrameHostTester::For(main_rfh())
77 ->AppendChild("subframe");
78 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting(
79 first_url, subframe_);
80 test_handle_->RegisterThrottleForTesting(
81 base::MakeUnique<SubframeFilteringNavigationThrottle>(
82 test_handle_.get(), async_filter_.get()));
83 }
84
85 content::NavigationThrottle::ThrottleCheckResult SimulateWillStart() {
86 return test_handle_->CallWillStartRequestForTesting(
87 false /* is_post */, content::Referrer(), true /* has_user_gesture */,
88 ui::PageTransition::PAGE_TRANSITION_LINK,
89 false /* is_external_protocol */);
90 }
91
92 content::NavigationThrottle::ThrottleCheckResult SimulateWillRedirect(
93 const GURL& new_url) {
94 return test_handle_->CallWillRedirectRequestForTesting(
95 new_url, false /* new_method_is_post */, GURL(),
96 false /* new_is_external_protocol */);
97 }
98
99 content::NavigationThrottle::ThrottleCheckResult
100 SimulateWillProcessResponse() {
101 return test_handle_->CallWillProcessResponseForTesting(subframe_, "");
102 }
103
104 bool NavigationIsCancelling() {
105 return test_handle_->IsCancellingForTesting();
106 }
107
108 private:
109 scoped_refptr<base::TestSimpleTaskRunner> blocking_task_runner_;
110
111 testing::TestRulesetCreator test_ruleset_creator_;
112 testing::TestRulesetPair test_ruleset_pair_;
113
114 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
115 std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_;
116
117 std::unique_ptr<AsyncDocumentSubresourceFilter> async_filter_;
118
119 std::unique_ptr<content::NavigationHandle> test_handle_;
120
121 content::RenderFrameHost* subframe_ = nullptr;
122
123 DISALLOW_COPY_AND_ASSIGN(SubframeFilteringNavigationThrottleTest);
124 };
125
126 TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnStart) {
127 InitializeDocumentSubresourceFilter(GURL("https://example.test"),
128 ActivationLevel::ENABLED);
129 CreateTestSubframeNavigation(GURL("https://example.test/filter.html"));
130 EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
131 RunUntilIdle();
132 EXPECT_TRUE(NavigationIsCancelling());
133 }
134
135 TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnRedirect) {
136 InitializeDocumentSubresourceFilter(GURL("https://example.test"),
137 ActivationLevel::ENABLED);
138 CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
139
140 EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
141 RunUntilIdle();
142 EXPECT_FALSE(NavigationIsCancelling());
143
144 EXPECT_EQ(content::NavigationThrottle::DEFER,
145 SimulateWillRedirect(GURL("https://example.test/filter.html")));
146 RunUntilIdle();
147 EXPECT_TRUE(NavigationIsCancelling());
148 }
149
150 TEST_F(SubframeFilteringNavigationThrottleTest, FilterOnSecondRedirect) {
151 InitializeDocumentSubresourceFilter(GURL("https://example.test"),
152 ActivationLevel::ENABLED);
153 CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
154
155 EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
156 RunUntilIdle();
157 EXPECT_FALSE(NavigationIsCancelling());
158
159 EXPECT_EQ(content::NavigationThrottle::DEFER,
160 SimulateWillRedirect(GURL("https://example.test/proceed2.html")));
161 RunUntilIdle();
162 EXPECT_FALSE(NavigationIsCancelling());
163
164 EXPECT_EQ(content::NavigationThrottle::DEFER,
165 SimulateWillRedirect(GURL("https://example.test/filter.html")));
166 RunUntilIdle();
167 EXPECT_TRUE(NavigationIsCancelling());
168 }
169
170 TEST_F(SubframeFilteringNavigationThrottleTest, NeverFilterNonMatchingRule) {
171 InitializeDocumentSubresourceFilter(GURL("https://example.test"),
172 ActivationLevel::ENABLED);
173 CreateTestSubframeNavigation(GURL("https://example.test/proceed.html"));
174
175 EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
176 RunUntilIdle();
177 EXPECT_FALSE(NavigationIsCancelling());
178
179 EXPECT_EQ(content::NavigationThrottle::DEFER,
180 SimulateWillRedirect(GURL("https://example.test/proceed2.html")));
181 RunUntilIdle();
182 EXPECT_FALSE(NavigationIsCancelling());
183
184 EXPECT_EQ(content::NavigationThrottle::PROCEED,
185 SimulateWillProcessResponse());
186 RunUntilIdle();
187 EXPECT_FALSE(NavigationIsCancelling());
188 }
189
190 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.
191 InitializeDocumentSubresourceFilter(GURL("https://example.test"),
192 ActivationLevel::DISABLED);
193 CreateTestSubframeNavigation(GURL("https://example.test/filter.html"));
194
195 EXPECT_EQ(content::NavigationThrottle::DEFER, SimulateWillStart());
196 RunUntilIdle();
197 EXPECT_FALSE(NavigationIsCancelling());
198
199 EXPECT_EQ(content::NavigationThrottle::DEFER,
200 SimulateWillRedirect(GURL("https://example.test/filter.html")));
201 RunUntilIdle();
202 EXPECT_FALSE(NavigationIsCancelling());
203
204 EXPECT_EQ(content::NavigationThrottle::PROCEED,
205 SimulateWillProcessResponse());
206 RunUntilIdle();
207 EXPECT_FALSE(NavigationIsCancelling());
208 }
209
210 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698