| 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 NavigationThrottle::ThrottleCheckResult ProcessState(bool should_cancel) { | |
| 83 if (sync_ == ASYNCHRONOUS) { | |
| 84 BrowserThread::PostTask( | |
| 85 BrowserThread::UI, FROM_HERE, | |
| 86 base::Bind(&CancellingNavigationThrottle::MaybeCancel, | |
| 87 weak_ptr_factory_.GetWeakPtr(), should_cancel)); | |
| 88 return NavigationThrottle::DEFER; | |
| 89 } | |
| 90 return should_cancel ? NavigationThrottle::CANCEL | |
| 91 : NavigationThrottle::PROCEED; | |
| 92 } | |
| 93 | |
| 94 void MaybeCancel(bool cancel) { | |
| 95 if (cancel) | |
| 96 navigation_handle()->CancelDeferredNavigation(NavigationThrottle::CANCEL); | |
| 97 else | |
| 98 navigation_handle()->Resume(); | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 const CancelTime cancel_time_; | |
| 103 const ResultSynchrony sync_; | |
| 104 base::WeakPtrFactory<CancellingNavigationThrottle> weak_ptr_factory_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(CancellingNavigationThrottle); | |
| 107 }; | |
| 108 | |
| 109 class NavigationSimulatorTest : public RenderViewHostImplTestHarness, | |
| 110 public WebContentsObserver, | |
| 111 public testing::WithParamInterface< | |
| 112 std::tuple<CancelTime, ResultSynchrony>> { | |
| 113 public: | 33 public: |
| 114 NavigationSimulatorTest() {} | 34 NavigationSimulatorTest() {} |
| 115 ~NavigationSimulatorTest() override {} | 35 ~NavigationSimulatorTest() override {} |
| 116 | 36 |
| 117 void SetUp() override { | 37 void SetUp() override { |
| 118 RenderViewHostImplTestHarness::SetUp(); | 38 RenderViewHostImplTestHarness::SetUp(); |
| 119 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); | 39 contents()->GetMainFrame()->InitializeRenderFrameIfNeeded(); |
| 120 Observe(RenderViewHostImplTestHarness::web_contents()); | 40 Observe(RenderViewHostImplTestHarness::web_contents()); |
| 121 std::tie(cancel_time_, sync_) = GetParam(); | 41 std::tie(cancel_time_, sync_) = GetParam(); |
| 122 simulator_ = NavigationSimulator::CreateRendererInitiated( | 42 simulator_ = NavigationSimulator::CreateRendererInitiated( |
| 123 GURL("https://example.test"), main_rfh()); | 43 GURL("https://example.test"), main_rfh()); |
| 124 } | 44 } |
| 125 | 45 |
| 126 void TearDown() override { | 46 void TearDown() override { |
| 127 EXPECT_TRUE(did_finish_navigation_); | 47 EXPECT_TRUE(did_finish_navigation_); |
| 128 RenderViewHostImplTestHarness::TearDown(); | 48 RenderViewHostImplTestHarness::TearDown(); |
| 129 } | 49 } |
| 130 | 50 |
| 131 void DidStartNavigation(content::NavigationHandle* handle) override { | 51 void DidStartNavigation(content::NavigationHandle* handle) override { |
| 132 handle->RegisterThrottleForTesting( | 52 handle->RegisterThrottleForTesting( |
| 133 base::MakeUnique<CancellingNavigationThrottle>(handle, cancel_time_, | 53 base::MakeUnique<CancellingNavigationThrottle>(handle, cancel_time_, |
| 134 sync_)); | 54 sync_)); |
| 135 } | 55 } |
| 136 | 56 |
| 137 void DidFinishNavigation(content::NavigationHandle* handle) override { | 57 void DidFinishNavigation(content::NavigationHandle* handle) override { |
| 138 did_finish_navigation_ = true; | 58 did_finish_navigation_ = true; |
| 139 } | 59 } |
| 140 | 60 |
| 141 CancelTime cancel_time_; | 61 CancellingNavigationThrottle::CancelTime cancel_time_; |
| 142 ResultSynchrony sync_; | 62 CancellingNavigationThrottle::ResultSynchrony sync_; |
| 143 std::unique_ptr<NavigationSimulator> simulator_; | 63 std::unique_ptr<NavigationSimulator> simulator_; |
| 144 bool did_finish_navigation_ = false; | 64 bool did_finish_navigation_ = false; |
| 145 | 65 |
| 146 private: | 66 private: |
| 147 DISALLOW_COPY_AND_ASSIGN(NavigationSimulatorTest); | 67 DISALLOW_COPY_AND_ASSIGN(NavigationSimulatorTest); |
| 148 }; | 68 }; |
| 149 | 69 |
| 150 // Stress test the navigation simulator by having a navigation throttle cancel | 70 // Stress test the navigation simulator by having a navigation throttle cancel |
| 151 // the navigation at various points in the flow, both synchronously and | 71 // the navigation at various points in the flow, both synchronously and |
| 152 // asynchronously. | 72 // asynchronously. |
| 153 TEST_P(NavigationSimulatorTest, Cancel) { | 73 TEST_P(NavigationSimulatorTest, Cancel) { |
| 154 SCOPED_TRACE(::testing::Message() | 74 SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time_ |
| 155 << "CancelTime: " << CancelTimeToString(cancel_time_) | 75 << " ResultSynchrony: " << sync_); |
| 156 << " ResultSynchrony: " << ResultSynchronyToString(sync_)); | |
| 157 simulator_->Start(); | 76 simulator_->Start(); |
| 158 if (cancel_time_ == WILL_SEND_REQUEST) { | 77 if (cancel_time_ == CancellingNavigationThrottle::WILL_START_REQUEST) { |
| 159 EXPECT_EQ(NavigationThrottle::CANCEL, | 78 EXPECT_EQ(NavigationThrottle::CANCEL, |
| 160 simulator_->GetLastThrottleCheckResult()); | 79 simulator_->GetLastThrottleCheckResult()); |
| 161 return; | 80 return; |
| 162 } | 81 } |
| 163 EXPECT_EQ(NavigationThrottle::PROCEED, | 82 EXPECT_EQ(NavigationThrottle::PROCEED, |
| 164 simulator_->GetLastThrottleCheckResult()); | 83 simulator_->GetLastThrottleCheckResult()); |
| 165 simulator_->Redirect(GURL("https://example.redirect")); | 84 simulator_->Redirect(GURL("https://example.redirect")); |
| 166 if (cancel_time_ == WILL_REDIRECT_REQUEST) { | 85 if (cancel_time_ == CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) { |
| 167 EXPECT_EQ(NavigationThrottle::CANCEL, | 86 EXPECT_EQ(NavigationThrottle::CANCEL, |
| 168 simulator_->GetLastThrottleCheckResult()); | 87 simulator_->GetLastThrottleCheckResult()); |
| 169 return; | 88 return; |
| 170 } | 89 } |
| 171 EXPECT_EQ(NavigationThrottle::PROCEED, | 90 EXPECT_EQ(NavigationThrottle::PROCEED, |
| 172 simulator_->GetLastThrottleCheckResult()); | 91 simulator_->GetLastThrottleCheckResult()); |
| 173 simulator_->Commit(); | 92 simulator_->Commit(); |
| 174 if (cancel_time_ == WILL_PROCESS_RESPONSE) { | 93 if (cancel_time_ == CancellingNavigationThrottle::WILL_PROCESS_RESPONSE) { |
| 175 EXPECT_EQ(NavigationThrottle::CANCEL, | 94 EXPECT_EQ(NavigationThrottle::CANCEL, |
| 176 simulator_->GetLastThrottleCheckResult()); | 95 simulator_->GetLastThrottleCheckResult()); |
| 177 return; | 96 return; |
| 178 } | 97 } |
| 179 EXPECT_EQ(NavigationThrottle::PROCEED, | 98 EXPECT_EQ(NavigationThrottle::PROCEED, |
| 180 simulator_->GetLastThrottleCheckResult()); | 99 simulator_->GetLastThrottleCheckResult()); |
| 181 } | 100 } |
| 182 | 101 |
| 183 INSTANTIATE_TEST_CASE_P( | 102 INSTANTIATE_TEST_CASE_P( |
| 184 CancelMethod, | 103 CancelMethod, |
| 185 NavigationSimulatorTest, | 104 NavigationSimulatorTest, |
| 186 ::testing::Values(std::make_tuple(WILL_SEND_REQUEST, SYNCHRONOUS), | 105 ::testing::Combine( |
| 187 std::make_tuple(WILL_SEND_REQUEST, ASYNCHRONOUS), | 106 ::testing::Values(CancellingNavigationThrottle::WILL_START_REQUEST, |
| 188 std::make_tuple(WILL_REDIRECT_REQUEST, SYNCHRONOUS), | 107 CancellingNavigationThrottle::WILL_REDIRECT_REQUEST, |
| 189 std::make_tuple(WILL_REDIRECT_REQUEST, ASYNCHRONOUS), | 108 CancellingNavigationThrottle::WILL_PROCESS_RESPONSE, |
| 190 std::make_tuple(WILL_PROCESS_RESPONSE, SYNCHRONOUS), | 109 CancellingNavigationThrottle::NEVER), |
| 191 std::make_tuple(WILL_PROCESS_RESPONSE, ASYNCHRONOUS), | 110 ::testing::Values(CancellingNavigationThrottle::SYNCHRONOUS, |
| 192 std::make_tuple(NEVER, SYNCHRONOUS), | 111 CancellingNavigationThrottle::ASYNCHRONOUS))); |
| 193 std::make_tuple(NEVER, ASYNCHRONOUS))); | |
| 194 | 112 |
| 195 } // namespace content | 113 } // namespace content |
| OLD | NEW |