| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "content/browser/frame_host/navigation_handle_impl.h" | 6 #include "content/browser/frame_host/navigation_handle_impl.h" |
| 7 #include "content/public/browser/navigation_throttle.h" | 7 #include "content/public/browser/navigation_throttle.h" |
| 8 #include "content/test/test_render_frame_host.h" | 8 #include "content/test/test_render_frame_host.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 private: | 49 private: |
| 50 // The result returned by the TestNavigationThrottle. | 50 // The result returned by the TestNavigationThrottle. |
| 51 NavigationThrottle::ThrottleCheckResult result_; | 51 NavigationThrottle::ThrottleCheckResult result_; |
| 52 | 52 |
| 53 // The number of times each handler was called. | 53 // The number of times each handler was called. |
| 54 int will_start_calls_; | 54 int will_start_calls_; |
| 55 int will_redirect_calls_; | 55 int will_redirect_calls_; |
| 56 int will_process_response_calls_; | 56 int will_process_response_calls_; |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 // A NavigationThrottle which adds console messages in WillStartRequest, |
| 60 // WillRedirectRequest, and always returns PROCEED. |
| 61 class TestConsoleMessagesThrottle : public NavigationThrottle { |
| 62 public: |
| 63 TestConsoleMessagesThrottle(NavigationHandle* handle) |
| 64 : NavigationThrottle(handle) {} |
| 65 |
| 66 ~TestConsoleMessagesThrottle() override {} |
| 67 |
| 68 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
| 69 navigation_handle()->QueueConsoleMessage(CONSOLE_MESSAGE_LEVEL_LOG, |
| 70 "WillStartRequest"); |
| 71 return NavigationThrottle::PROCEED; |
| 72 } |
| 73 |
| 74 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override { |
| 75 navigation_handle()->QueueConsoleMessage(CONSOLE_MESSAGE_LEVEL_LOG, |
| 76 "WillRedirectRequest"); |
| 77 return NavigationThrottle::PROCEED; |
| 78 } |
| 79 |
| 80 NavigationThrottle::ThrottleCheckResult WillProcessResponse() override { |
| 81 navigation_handle()->QueueConsoleMessage(CONSOLE_MESSAGE_LEVEL_LOG, |
| 82 "WillProcessResponse"); |
| 83 return NavigationThrottle::PROCEED; |
| 84 } |
| 85 }; |
| 86 |
| 59 class NavigationHandleImplTest : public RenderViewHostImplTestHarness { | 87 class NavigationHandleImplTest : public RenderViewHostImplTestHarness { |
| 60 public: | 88 public: |
| 61 NavigationHandleImplTest() | 89 NavigationHandleImplTest() |
| 62 : was_callback_called_(false), | 90 : was_callback_called_(false), |
| 63 callback_result_(NavigationThrottle::DEFER) {} | 91 callback_result_(NavigationThrottle::DEFER) {} |
| 64 | 92 |
| 65 void SetUp() override { | 93 void SetUp() override { |
| 66 RenderViewHostImplTestHarness::SetUp(); | 94 RenderViewHostImplTestHarness::SetUp(); |
| 67 test_handle_ = NavigationHandleImpl::Create( | 95 test_handle_ = NavigationHandleImpl::Create( |
| 68 GURL(), main_test_rfh()->frame_tree_node(), true, false, false, | 96 GURL(), main_test_rfh()->frame_tree_node(), true, false, false, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 85 } | 113 } |
| 86 | 114 |
| 87 bool IsDeferringResponse() { | 115 bool IsDeferringResponse() { |
| 88 return test_handle_->state() == NavigationHandleImpl::DEFERRING_RESPONSE; | 116 return test_handle_->state() == NavigationHandleImpl::DEFERRING_RESPONSE; |
| 89 } | 117 } |
| 90 | 118 |
| 91 bool IsCanceling() { | 119 bool IsCanceling() { |
| 92 return test_handle_->state() == NavigationHandleImpl::CANCELING; | 120 return test_handle_->state() == NavigationHandleImpl::CANCELING; |
| 93 } | 121 } |
| 94 | 122 |
| 123 const std::queue<std::pair<ConsoleMessageLevel, const std::string>>& |
| 124 GetConsoleQueue() const { |
| 125 return test_handle_->console_queue_; |
| 126 } |
| 127 |
| 95 // Helper function to call WillStartRequest on |handle|. If this function | 128 // Helper function to call WillStartRequest on |handle|. If this function |
| 96 // returns DEFER, |callback_result_| will be set to the actual result of | 129 // returns DEFER, |callback_result_| will be set to the actual result of |
| 97 // the throttle checks when they are finished. | 130 // the throttle checks when they are finished. |
| 98 void SimulateWillStartRequest() { | 131 void SimulateWillStartRequest() { |
| 99 was_callback_called_ = false; | 132 was_callback_called_ = false; |
| 100 callback_result_ = NavigationThrottle::DEFER; | 133 callback_result_ = NavigationThrottle::DEFER; |
| 101 | 134 |
| 102 // It's safe to use base::Unretained since the NavigationHandle is owned by | 135 // It's safe to use base::Unretained since the NavigationHandle is owned by |
| 103 // the NavigationHandleImplTest. | 136 // the NavigationHandleImplTest. |
| 104 test_handle_->WillStartRequest( | 137 test_handle_->WillStartRequest( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // |result| on checks. | 190 // |result| on checks. |
| 158 TestNavigationThrottle* CreateTestNavigationThrottle( | 191 TestNavigationThrottle* CreateTestNavigationThrottle( |
| 159 NavigationThrottle::ThrottleCheckResult result) { | 192 NavigationThrottle::ThrottleCheckResult result) { |
| 160 TestNavigationThrottle* test_throttle = | 193 TestNavigationThrottle* test_throttle = |
| 161 new TestNavigationThrottle(test_handle(), result); | 194 new TestNavigationThrottle(test_handle(), result); |
| 162 test_handle()->RegisterThrottleForTesting( | 195 test_handle()->RegisterThrottleForTesting( |
| 163 std::unique_ptr<TestNavigationThrottle>(test_throttle)); | 196 std::unique_ptr<TestNavigationThrottle>(test_throttle)); |
| 164 return test_throttle; | 197 return test_throttle; |
| 165 } | 198 } |
| 166 | 199 |
| 200 // Creates, register and returns a TestConsoleMessagesThrottle. |
| 201 TestConsoleMessagesThrottle* CreateTestConsoleMessagesThrottle() { |
| 202 TestConsoleMessagesThrottle* test_throttle = |
| 203 new TestConsoleMessagesThrottle(test_handle()); |
| 204 test_handle()->RegisterThrottleForTesting( |
| 205 std::unique_ptr<TestConsoleMessagesThrottle>(test_throttle)); |
| 206 return test_throttle; |
| 207 } |
| 208 |
| 167 private: | 209 private: |
| 168 // The callback provided to NavigationHandleImpl::WillStartRequest and | 210 // The callback provided to NavigationHandleImpl::WillStartRequest and |
| 169 // NavigationHandleImpl::WillRedirectRequest during the tests. | 211 // NavigationHandleImpl::WillRedirectRequest during the tests. |
| 170 void UpdateThrottleCheckResult( | 212 void UpdateThrottleCheckResult( |
| 171 NavigationThrottle::ThrottleCheckResult result) { | 213 NavigationThrottle::ThrottleCheckResult result) { |
| 172 callback_result_ = result; | 214 callback_result_ = result; |
| 173 was_callback_called_ = true; | 215 was_callback_called_ = true; |
| 174 } | 216 } |
| 175 | 217 |
| 176 std::unique_ptr<NavigationHandleImpl> test_handle_; | 218 std::unique_ptr<NavigationHandleImpl> test_handle_; |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 638 EXPECT_TRUE(was_callback_called()); | 680 EXPECT_TRUE(was_callback_called()); |
| 639 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); | 681 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
| 640 EXPECT_EQ(0, cancel_throttle->will_start_calls()); | 682 EXPECT_EQ(0, cancel_throttle->will_start_calls()); |
| 641 EXPECT_EQ(0, cancel_throttle->will_redirect_calls()); | 683 EXPECT_EQ(0, cancel_throttle->will_redirect_calls()); |
| 642 EXPECT_EQ(1, cancel_throttle->will_process_response_calls()); | 684 EXPECT_EQ(1, cancel_throttle->will_process_response_calls()); |
| 643 EXPECT_EQ(0, proceed_throttle->will_start_calls()); | 685 EXPECT_EQ(0, proceed_throttle->will_start_calls()); |
| 644 EXPECT_EQ(0, proceed_throttle->will_redirect_calls()); | 686 EXPECT_EQ(0, proceed_throttle->will_redirect_calls()); |
| 645 EXPECT_EQ(0, proceed_throttle->will_process_response_calls()); | 687 EXPECT_EQ(0, proceed_throttle->will_process_response_calls()); |
| 646 } | 688 } |
| 647 | 689 |
| 690 TEST_F(NavigationHandleImplTest, ConsoleMessages) { |
| 691 TestConsoleMessagesThrottle* throttle = CreateTestConsoleMessagesThrottle(); |
| 692 DCHECK(throttle); |
| 693 SimulateWillStartRequest(); |
| 694 DCHECK_EQ(1u, GetConsoleQueue().size()); |
| 695 DCHECK_EQ("WillStartRequest", GetConsoleQueue().back().second); |
| 696 |
| 697 SimulateWillRedirectRequest(); |
| 698 DCHECK_EQ(2u, GetConsoleQueue().size()); |
| 699 DCHECK_EQ("WillRedirectRequest", GetConsoleQueue().back().second); |
| 700 |
| 701 SimulateWillProcessResponse(); |
| 702 // Calling 'WillProcessResponse' should dump the queue. |
| 703 DCHECK_EQ(0u, GetConsoleQueue().size()); |
| 704 |
| 705 // Verify that messages got to the RFH correctly. |
| 706 DCHECK_EQ(3u, main_test_rfh()->GetConsoleMessages().size()); |
| 707 DCHECK_EQ("WillStartRequest", main_test_rfh()->GetConsoleMessages()[0]); |
| 708 DCHECK_EQ("WillRedirectRequest", main_test_rfh()->GetConsoleMessages()[1]); |
| 709 DCHECK_EQ("WillProcessResponse", main_test_rfh()->GetConsoleMessages()[2]); |
| 710 } |
| 711 |
| 648 } // namespace content | 712 } // namespace content |
| OLD | NEW |