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

Side by Side Diff: components/subresource_filter/content/browser/subframe_navigation_filtering_throttle_unittest.cc

Issue 2698393002: Allow asynchronous deferral in NavigationSimulator (Closed)
Patch Set: Remove the wait API Created 3 years, 9 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_navigation_filt ering_throttle.h"
6
7 #include <memory>
8
9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "components/subresource_filter/content/browser/async_document_subresour ce_filter.h"
15 #include "components/subresource_filter/core/common/activation_level.h"
16 #include "components/subresource_filter/core/common/activation_state.h"
17 #include "components/subresource_filter/core/common/proto/rules.pb.h"
engedy 2017/03/09 11:41:34 nit: Do we use this and test_ruleset_utils.h?
Charlie Harrison 2017/03/09 14:36:01 Nope, removed.
18 #include "components/subresource_filter/core/common/test_ruleset_creator.h"
19 #include "components/subresource_filter/core/common/test_ruleset_utils.h"
20 #include "content/public/browser/navigation_handle.h"
21 #include "content/public/browser/web_contents_observer.h"
22 #include "content/public/test/navigation_simulator.h"
23 #include "content/public/test/test_renderer_host.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace subresource_filter {
27
28 class SubframeNavigationFilteringThrottleTest
29 : public content::RenderViewHostTestHarness,
30 public content::WebContentsObserver {
31 public:
32 SubframeNavigationFilteringThrottleTest() {}
33 ~SubframeNavigationFilteringThrottleTest() override {}
34
35 void SetUp() override {
36 content::RenderViewHostTestHarness::SetUp();
37 NavigateAndCommit(GURL("https://example.test"));
38 Observe(RenderViewHostTestHarness::web_contents());
39 }
40
41 void TearDown() override {
42 dealer_handle_.reset();
43 ruleset_handle_.reset();
44 async_filter_.reset();
45 RunUntilIdle();
46 content::RenderViewHostTestHarness::TearDown();
47 }
48
49 // content::WebContentsObserver:
50 void DidStartNavigation(
51 content::NavigationHandle* navigation_handle) override {
52 ASSERT_FALSE(navigation_handle->IsInMainFrame());
53 // The |async_filter_| is the parent frame's filter. Do not register a
54 // throttle if the parent is not activated with a valid filter.
55 if (async_filter_) {
engedy 2017/03/09 11:41:34 nit: s/async_filter_/{main_frame|parent}_{filter|d
Charlie Harrison 2017/03/09 14:36:01 Done.
56 navigation_handle->RegisterThrottleForTesting(
57 base::MakeUnique<SubframeNavigationFilteringThrottle>(
58 navigation_handle, async_filter_.get()));
59 }
60 }
61
62 void InitializeDocumentSubresourceFilter(const GURL& document_url) {
63 ASSERT_NO_FATAL_FAILURE(
64 test_ruleset_creator_.CreateRulesetToDisallowURLsWithPathSuffix(
65 "disallowed.html", &test_ruleset_pair_));
66
67 // Make the blocking task runner run on the current task runner for the
68 // tests, to ensure that the NavigationSimulator properly runs all necessary
69 // tasks while waiting for throttle checks to finish.
70 dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>(
71 base::MessageLoop::current()->task_runner());
72 dealer_handle_->SetRulesetFile(
73 testing::TestRuleset::Open(test_ruleset_pair_.indexed));
74 ruleset_handle_ =
75 base::MakeUnique<VerifiedRuleset::Handle>(dealer_handle_.get());
76
77 async_filter_ = base::MakeUnique<AsyncDocumentSubresourceFilter>(
78 ruleset_handle_.get(),
79 AsyncDocumentSubresourceFilter::InitializationParams(
80 document_url, ActivationLevel::ENABLED,
81 false /* measure_performance */),
82 base::Bind([](ActivationState state) {
83 EXPECT_EQ(ActivationLevel::ENABLED, state.activation_level);
84 }),
85 base::OnceClosure());
86 RunUntilIdle();
87 }
88
89 void RunUntilIdle() { base::RunLoop().RunUntilIdle(); }
90
91 void CreateTestSubframeNavigation(const GURL& first_url,
engedy 2017/03/09 11:41:34 nit: CreatTestSubframeAndInitNavigation or somethi
Charlie Harrison 2017/03/09 14:36:01 Done.
92 content::RenderFrameHost* parent) {
93 navigating_subframe_ =
engedy 2017/03/09 11:41:34 nit: Member variable only used within this method.
Charlie Harrison 2017/03/09 14:36:01 Done, good catch.
94 content::RenderFrameHostTester::For(parent)->AppendChild(
95 base::StringPrintf("subframe-%s", first_url.spec().c_str()));
96 navigation_simulator_ =
97 content::NavigationSimulator::CreateRendererInitiated(
98 first_url, navigating_subframe_);
99 }
100
101 void SimulateStartAndExpectResult(
102 content::NavigationThrottle::ThrottleCheckResult expect_result) {
103 navigation_simulator_->Start();
104 EXPECT_EQ(expect_result,
105 navigation_simulator_->GetLastThrottleCheckResult());
106 }
107
108 void SimulateRedirectAndExpectResult(
109 const GURL& new_url,
110 content::NavigationThrottle::ThrottleCheckResult expect_result) {
111 navigation_simulator_->Redirect(new_url);
112 EXPECT_EQ(expect_result,
113 navigation_simulator_->GetLastThrottleCheckResult());
114 }
115
116 void SimulateCommitAndExpectResult(
117 content::NavigationThrottle::ThrottleCheckResult expect_result) {
118 navigation_simulator_->Commit();
119 EXPECT_EQ(expect_result,
120 navigation_simulator_->GetLastThrottleCheckResult());
121 }
122
123 private:
124 testing::TestRulesetCreator test_ruleset_creator_;
125 testing::TestRulesetPair test_ruleset_pair_;
126
127 std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_;
128 std::unique_ptr<VerifiedRuleset::Handle> ruleset_handle_;
129
130 std::unique_ptr<AsyncDocumentSubresourceFilter> async_filter_;
131
132 content::RenderFrameHost* navigating_subframe_ = nullptr;
133
134 std::unique_ptr<content::NavigationSimulator> navigation_simulator_;
135
136 DISALLOW_COPY_AND_ASSIGN(SubframeNavigationFilteringThrottleTest);
137 };
138
139 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnStart) {
140 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
141 CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"),
142 main_rfh());
143 SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL);
144 }
145
146 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnRedirect) {
147 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
148 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
149 main_rfh());
150
151 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
152 SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"),
153 content::NavigationThrottle::CANCEL);
154 }
155
156 TEST_F(SubframeNavigationFilteringThrottleTest, FilterOnSecondRedirect) {
157 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
158 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
159 main_rfh());
160
161 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
162 SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"),
163 content::NavigationThrottle::PROCEED);
164 SimulateRedirectAndExpectResult(GURL("https://example.test/disallowed.html"),
165 content::NavigationThrottle::CANCEL);
166 }
167
168 TEST_F(SubframeNavigationFilteringThrottleTest, NeverFilterNonMatchingRule) {
169 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
170 CreateTestSubframeNavigation(GURL("https://example.test/allowed.html"),
171 main_rfh());
172
173 SimulateStartAndExpectResult(content::NavigationThrottle::PROCEED);
174 SimulateRedirectAndExpectResult(GURL("https://example.test/allowed2.html"),
175 content::NavigationThrottle::PROCEED);
176 SimulateCommitAndExpectResult(content::NavigationThrottle::PROCEED);
177 }
178
179 TEST_F(SubframeNavigationFilteringThrottleTest, FilterSubsubframe) {
180 // Fake an activation of the subframe.
181 content::RenderFrameHost* parent_subframe =
182 content::RenderFrameHostTester::For(main_rfh())
183 ->AppendChild("parent-sub");
184 GURL test_url = GURL("https://example.test");
185 content::RenderFrameHostTester::For(parent_subframe)
186 ->SimulateNavigationStart(test_url);
187 InitializeDocumentSubresourceFilter(GURL("https://example.test"));
188 content::RenderFrameHostTester::For(parent_subframe)
189 ->SimulateNavigationCommit(test_url);
190
191 CreateTestSubframeNavigation(GURL("https://example.test/disallowed.html"),
192 parent_subframe);
193 SimulateStartAndExpectResult(content::NavigationThrottle::CANCEL);
194 }
195
196 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698