OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/test/navigation_simulator.h" | 5 #include "content/public/test/navigation_simulator.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <tuple> | 8 #include <tuple> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/navigation_handle.h" | 15 #include "content/public/browser/navigation_handle.h" |
16 #include "content/public/browser/navigation_throttle.h" | 16 #include "content/public/browser/navigation_throttle.h" |
17 #include "content/public/browser/web_contents_observer.h" | 17 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "content/public/test/cancelling_navigation_throttle.h" |
18 #include "content/test/test_render_frame_host.h" | 19 #include "content/test/test_render_frame_host.h" |
19 #include "content/test/test_render_view_host.h" | 20 #include "content/test/test_render_view_host.h" |
20 #include "content/test/test_web_contents.h" | 21 #include "content/test/test_web_contents.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 enum CancelTime { | 27 class NavigationSimulatorTest |
27 WILL_SEND_REQUEST, | 28 : public RenderViewHostImplTestHarness, |
28 WILL_REDIRECT_REQUEST, | 29 public WebContentsObserver, |
29 WILL_PROCESS_RESPONSE, | 30 public testing::WithParamInterface< |
30 NEVER, | 31 std::tuple<CancellingNavigationThrottle::CancelTime, |
31 }; | 32 CancellingNavigationThrottle::ResultSynchrony>> { |
32 | |
33 enum ResultSynchrony { | |
34 SYNCHRONOUS, | |
35 ASYNCHRONOUS, | |
36 }; | |
37 | |
38 std::string CancelTimeToString(CancelTime cancel_time) { | |
39 switch (cancel_time) { | |
40 case WILL_SEND_REQUEST: | |
41 return "WILL_SEND_REQUEST"; | |
42 case WILL_REDIRECT_REQUEST: | |
43 return "WILL_REDIRECT_REQUEST"; | |
44 case WILL_PROCESS_RESPONSE: | |
45 return "WILL_PROCESS_RESPONSE"; | |
46 case NEVER: | |
47 return "NEVER"; | |
48 } | |
49 NOTREACHED(); | |
50 return ""; | |
51 } | |
52 | |
53 std::string ResultSynchronyToString(ResultSynchrony sync) { | |
54 return sync == SYNCHRONOUS ? "SYNCHRONOUS" : "ASYNCHRONOUS"; | |
55 } | |
56 | |
57 // TODO(csharrison): Expose this class in the content public API. | |
58 class CancellingNavigationThrottle : public NavigationThrottle { | |
59 public: | |
60 CancellingNavigationThrottle(NavigationHandle* handle, | |
61 CancelTime cancel_time, | |
62 ResultSynchrony sync) | |
63 : NavigationThrottle(handle), | |
64 cancel_time_(cancel_time), | |
65 sync_(sync), | |
66 weak_ptr_factory_(this) {} | |
67 | |
68 ~CancellingNavigationThrottle() override {} | |
69 | |
70 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { | |
71 return ProcessState(cancel_time_ == WILL_SEND_REQUEST); | |
72 } | |
73 | |
74 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override { | |
75 return ProcessState(cancel_time_ == WILL_REDIRECT_REQUEST); | |
76 } | |
77 | |
78 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { | |
79 return ProcessState(cancel_time_ == WILL_PROCESS_RESPONSE); | |
80 } | |
81 | |
82 const char* GetNameForLogging() override { | |
83 return "CancellingNavigationThrottle"; | |
84 } | |
85 | |
86 NavigationThrottle::ThrottleCheckResult ProcessState(bool should_cancel) { | |
87 if (sync_ == ASYNCHRONOUS) { | |
88 BrowserThread::PostTask( | |
89 BrowserThread::UI, FROM_HERE, | |
90 base::Bind(&CancellingNavigationThrottle::MaybeCancel, | |
91 weak_ptr_factory_.GetWeakPtr(), should_cancel)); | |
92 return NavigationThrottle::DEFER; | |
93 } | |
94 return should_cancel ? NavigationThrottle::CANCEL | |
95 : NavigationThrottle::PROCEED; | |
96 } | |
97 | |
98 void MaybeCancel(bool cancel) { | |
99 if (cancel) | |
100 navigation_handle()->CancelDeferredNavigation(NavigationThrottle::CANCEL); | |
101 else | |
102 navigation_handle()->Resume(); | |
103 } | |
104 | |
105 private: | |
106 const CancelTime cancel_time_; | |
107 const ResultSynchrony sync_; | |
108 base::WeakPtrFactory<CancellingNavigationThrottle> weak_ptr_factory_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(CancellingNavigationThrottle); | |
111 }; | |
112 | |
113 class NavigationSimulatorTest : public RenderViewHostImplTestHarness, | |
114 public WebContentsObserver, | |
115 public testing::WithParamInterface< | |
116 std::tuple<CancelTime, ResultSynchrony>> { | |
117 public: | 33 public: |
118 NavigationSimulatorTest() {} | 34 NavigationSimulatorTest() {} |
119 ~NavigationSimulatorTest() override {} | 35 ~NavigationSimulatorTest() override {} |
120 | 36 |
121 void SetUp() override { | 37 void SetUp() override { |
122 RenderViewHostImplTestHarness::SetUp(); | 38 RenderViewHostImplTestHarness::SetUp(); |
123 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); | 39 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); |
124 Observe(RenderViewHostImplTestHarness::web_contents()); | 40 Observe(RenderViewHostImplTestHarness::web_contents()); |
125 std::tie(cancel_time_, sync_) = GetParam(); | 41 std::tie(cancel_time_, sync_) = GetParam(); |
126 simulator_ = NavigationSimulator::CreateRendererInitiated( | 42 simulator_ = NavigationSimulator::CreateRendererInitiated( |
127 GURL("https://example.test"), main_rfh()); | 43 GURL("https://example.test"), main_rfh()); |
128 } | 44 } |
129 | 45 |
130 void TearDown() override { | 46 void TearDown() override { |
131 EXPECT_TRUE(did_finish_navigation_); | 47 EXPECT_TRUE(did_finish_navigation_); |
132 RenderViewHostImplTestHarness::TearDown(); | 48 RenderViewHostImplTestHarness::TearDown(); |
133 } | 49 } |
134 | 50 |
135 void DidStartNavigation(content::NavigationHandle* handle) override { | 51 void DidStartNavigation(content::NavigationHandle* handle) override { |
136 handle->RegisterThrottleForTesting( | 52 handle->RegisterThrottleForTesting( |
137 base::MakeUnique<CancellingNavigationThrottle>(handle, cancel_time_, | 53 base::MakeUnique<CancellingNavigationThrottle>(handle, cancel_time_, |
138 sync_)); | 54 sync_)); |
139 } | 55 } |
140 | 56 |
141 void DidFinishNavigation(content::NavigationHandle* handle) override { | 57 void DidFinishNavigation(content::NavigationHandle* handle) override { |
142 did_finish_navigation_ = true; | 58 did_finish_navigation_ = true; |
143 } | 59 } |
144 | 60 |
145 CancelTime cancel_time_; | 61 CancellingNavigationThrottle::CancelTime cancel_time_; |
146 ResultSynchrony sync_; | 62 CancellingNavigationThrottle::ResultSynchrony sync_; |
147 std::unique_ptr<NavigationSimulator> simulator_; | 63 std::unique_ptr<NavigationSimulator> simulator_; |
148 bool did_finish_navigation_ = false; | 64 bool did_finish_navigation_ = false; |
149 | 65 |
150 private: | 66 private: |
151 DISALLOW_COPY_AND_ASSIGN(NavigationSimulatorTest); | 67 DISALLOW_COPY_AND_ASSIGN(NavigationSimulatorTest); |
152 }; | 68 }; |
153 | 69 |
154 // Stress test the navigation simulator by having a navigation throttle cancel | 70 // Stress test the navigation simulator by having a navigation throttle cancel |
155 // the navigation at various points in the flow, both synchronously and | 71 // the navigation at various points in the flow, both synchronously and |
156 // asynchronously. | 72 // asynchronously. |
157 TEST_P(NavigationSimulatorTest, Cancel) { | 73 TEST_P(NavigationSimulatorTest, Cancel) { |
158 SCOPED_TRACE(::testing::Message() | 74 SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time_ |
159 << "CancelTime: " << CancelTimeToString(cancel_time_) | 75 << " ResultSynchrony: " << sync_); |
160 << " ResultSynchrony: " << ResultSynchronyToString(sync_)); | |
161 simulator_->Start(); | 76 simulator_->Start(); |
162 if (cancel_time_ == WILL_SEND_REQUEST) { | 77 if (cancel_time_ == CancellingNavigationThrottle::WILL_START_REQUEST) { |
163 EXPECT_EQ(NavigationThrottle::CANCEL, | 78 EXPECT_EQ(NavigationThrottle::CANCEL, |
164 simulator_->GetLastThrottleCheckResult()); | 79 simulator_->GetLastThrottleCheckResult()); |
165 return; | 80 return; |
166 } | 81 } |
167 EXPECT_EQ(NavigationThrottle::PROCEED, | 82 EXPECT_EQ(NavigationThrottle::PROCEED, |
168 simulator_->GetLastThrottleCheckResult()); | 83 simulator_->GetLastThrottleCheckResult()); |
169 simulator_->Redirect(GURL("https://example.redirect")); | 84 simulator_->Redirect(GURL("https://example.redirect")); |
170 if (cancel_time_ == WILL_REDIRECT_REQUEST) { | 85 if (cancel_time_ == CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) { |
171 EXPECT_EQ(NavigationThrottle::CANCEL, | 86 EXPECT_EQ(NavigationThrottle::CANCEL, |
172 simulator_->GetLastThrottleCheckResult()); | 87 simulator_->GetLastThrottleCheckResult()); |
173 return; | 88 return; |
174 } | 89 } |
175 EXPECT_EQ(NavigationThrottle::PROCEED, | 90 EXPECT_EQ(NavigationThrottle::PROCEED, |
176 simulator_->GetLastThrottleCheckResult()); | 91 simulator_->GetLastThrottleCheckResult()); |
177 simulator_->Commit(); | 92 simulator_->Commit(); |
178 if (cancel_time_ == WILL_PROCESS_RESPONSE) { | 93 if (cancel_time_ == CancellingNavigationThrottle::WILL_PROCESS_RESPONSE) { |
179 EXPECT_EQ(NavigationThrottle::CANCEL, | 94 EXPECT_EQ(NavigationThrottle::CANCEL, |
180 simulator_->GetLastThrottleCheckResult()); | 95 simulator_->GetLastThrottleCheckResult()); |
181 return; | 96 return; |
182 } | 97 } |
183 EXPECT_EQ(NavigationThrottle::PROCEED, | 98 EXPECT_EQ(NavigationThrottle::PROCEED, |
184 simulator_->GetLastThrottleCheckResult()); | 99 simulator_->GetLastThrottleCheckResult()); |
185 } | 100 } |
186 | 101 |
187 INSTANTIATE_TEST_CASE_P( | 102 INSTANTIATE_TEST_CASE_P( |
188 CancelMethod, | 103 CancelMethod, |
189 NavigationSimulatorTest, | 104 NavigationSimulatorTest, |
190 ::testing::Values(std::make_tuple(WILL_SEND_REQUEST, SYNCHRONOUS), | 105 ::testing::Combine( |
191 std::make_tuple(WILL_SEND_REQUEST, ASYNCHRONOUS), | 106 ::testing::Values(CancellingNavigationThrottle::WILL_START_REQUEST, |
192 std::make_tuple(WILL_REDIRECT_REQUEST, SYNCHRONOUS), | 107 CancellingNavigationThrottle::WILL_REDIRECT_REQUEST, |
193 std::make_tuple(WILL_REDIRECT_REQUEST, ASYNCHRONOUS), | 108 CancellingNavigationThrottle::WILL_PROCESS_RESPONSE, |
194 std::make_tuple(WILL_PROCESS_RESPONSE, SYNCHRONOUS), | 109 CancellingNavigationThrottle::NEVER), |
195 std::make_tuple(WILL_PROCESS_RESPONSE, ASYNCHRONOUS), | 110 ::testing::Values(CancellingNavigationThrottle::SYNCHRONOUS, |
196 std::make_tuple(NEVER, SYNCHRONOUS), | 111 CancellingNavigationThrottle::ASYNCHRONOUS))); |
197 std::make_tuple(NEVER, ASYNCHRONOUS))); | |
198 | 112 |
199 } // namespace content | 113 } // namespace content |
OLD | NEW |