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 27 matching lines...) Expand all Loading... |
38 | 38 |
39 private: | 39 private: |
40 // The result returned by the TestNavigationThrottle. | 40 // The result returned by the TestNavigationThrottle. |
41 NavigationThrottle::ThrottleCheckResult result_; | 41 NavigationThrottle::ThrottleCheckResult result_; |
42 | 42 |
43 // The number of times WillStartRequest and WillRedirectRequest were called. | 43 // The number of times WillStartRequest and WillRedirectRequest were called. |
44 int will_start_calls_; | 44 int will_start_calls_; |
45 int will_redirect_calls_; | 45 int will_redirect_calls_; |
46 }; | 46 }; |
47 | 47 |
| 48 // Test version of a NavigationThrottle that will add a header when called. |
| 49 class TestHeaderAddingNavigationThrottle : public NavigationThrottle { |
| 50 public: |
| 51 TestHeaderAddingNavigationThrottle(NavigationHandle* handle, |
| 52 std::string key, |
| 53 std::string value) |
| 54 : NavigationThrottle(handle), key_(key), value_(value) {} |
| 55 |
| 56 ~TestHeaderAddingNavigationThrottle() override {} |
| 57 |
| 58 NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
| 59 static_cast<NavigationHandleImpl*>(navigation_handle()) |
| 60 ->AddExtraHeader(key_, value_); |
| 61 return PROCEED; |
| 62 } |
| 63 |
| 64 NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override { |
| 65 static_cast<NavigationHandleImpl*>(navigation_handle()) |
| 66 ->AddExtraHeader(key_, value_); |
| 67 return PROCEED; |
| 68 } |
| 69 |
| 70 private: |
| 71 const std::string key_; |
| 72 const std::string value_; |
| 73 }; |
| 74 |
48 class NavigationHandleImplTest : public RenderViewHostImplTestHarness { | 75 class NavigationHandleImplTest : public RenderViewHostImplTestHarness { |
49 public: | 76 public: |
50 NavigationHandleImplTest() | 77 NavigationHandleImplTest() |
51 : was_callback_called_(false), | 78 : was_callback_called_(false), |
52 callback_result_(NavigationThrottle::DEFER) {} | 79 callback_result_(NavigationThrottle::DEFER) {} |
53 | 80 |
54 void SetUp() override { | 81 void SetUp() override { |
55 RenderViewHostImplTestHarness::SetUp(); | 82 RenderViewHostImplTestHarness::SetUp(); |
56 test_handle_ = NavigationHandleImpl::Create( | 83 test_handle_ = NavigationHandleImpl::Create( |
57 GURL(), main_test_rfh()->frame_tree_node()); | 84 GURL(), main_test_rfh()->frame_tree_node()); |
(...skipping 16 matching lines...) Expand all Loading... |
74 | 101 |
75 bool IsCanceling() { | 102 bool IsCanceling() { |
76 return test_handle_->state() == NavigationHandleImpl::CANCELING; | 103 return test_handle_->state() == NavigationHandleImpl::CANCELING; |
77 } | 104 } |
78 | 105 |
79 // Helper function to call WillStartRequest on |handle|. If this function | 106 // Helper function to call WillStartRequest on |handle|. If this function |
80 // returns DEFER, |callback_result_| will be set to the actual result of | 107 // returns DEFER, |callback_result_| will be set to the actual result of |
81 // the throttle checks when they are finished. | 108 // the throttle checks when they are finished. |
82 void SimulateWillStartRequest() { | 109 void SimulateWillStartRequest() { |
83 was_callback_called_ = false; | 110 was_callback_called_ = false; |
| 111 extra_headers_.clear(); |
84 callback_result_ = NavigationThrottle::DEFER; | 112 callback_result_ = NavigationThrottle::DEFER; |
85 | 113 |
86 // It's safe to use base::Unretained since the NavigationHandle is owned by | 114 // It's safe to use base::Unretained since the NavigationHandle is owned by |
87 // the NavigationHandleImplTest. | 115 // the NavigationHandleImplTest. |
88 test_handle_->WillStartRequest( | 116 test_handle_->WillStartRequest( |
89 false, Referrer(), false, ui::PAGE_TRANSITION_LINK, false, | 117 false, Referrer(), false, ui::PAGE_TRANSITION_LINK, false, |
90 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, | 118 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, |
91 base::Unretained(this))); | 119 base::Unretained(this))); |
92 } | 120 } |
93 | 121 |
94 // Helper function to call WillRedirectRequest on |handle|. If this function | 122 // Helper function to call WillRedirectRequest on |handle|. If this function |
95 // returns DEFER, |callback_result_| will be set to the actual result of the | 123 // returns DEFER, |callback_result_| will be set to the actual result of the |
96 // throttle checks when they are finished. | 124 // throttle checks when they are finished. |
97 // TODO(clamy): this should also simulate that WillStartRequest was called if | 125 // TODO(clamy): this should also simulate that WillStartRequest was called if |
98 // it has not been called before. | 126 // it has not been called before. |
99 void SimulateWillRedirectRequest() { | 127 void SimulateWillRedirectRequest() { |
100 was_callback_called_ = false; | 128 was_callback_called_ = false; |
| 129 extra_headers_.clear(); |
101 callback_result_ = NavigationThrottle::DEFER; | 130 callback_result_ = NavigationThrottle::DEFER; |
102 | 131 |
103 // It's safe to use base::Unretained since the NavigationHandle is owned by | 132 // It's safe to use base::Unretained since the NavigationHandle is owned by |
104 // the NavigationHandleImplTest. | 133 // the NavigationHandleImplTest. |
105 test_handle_->WillRedirectRequest( | 134 test_handle_->WillRedirectRequest( |
106 GURL(), false, GURL(), false, | 135 GURL(), false, GURL(), false, |
107 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, | 136 base::Bind(&NavigationHandleImplTest::UpdateThrottleCheckResult, |
108 base::Unretained(this))); | 137 base::Unretained(this))); |
109 } | 138 } |
110 | 139 |
(...skipping 12 matching lines...) Expand all Loading... |
123 // |result| on checks. | 152 // |result| on checks. |
124 TestNavigationThrottle* CreateTestNavigationThrottle( | 153 TestNavigationThrottle* CreateTestNavigationThrottle( |
125 NavigationThrottle::ThrottleCheckResult result) { | 154 NavigationThrottle::ThrottleCheckResult result) { |
126 TestNavigationThrottle* test_throttle = | 155 TestNavigationThrottle* test_throttle = |
127 new TestNavigationThrottle(test_handle(), result); | 156 new TestNavigationThrottle(test_handle(), result); |
128 test_handle()->RegisterThrottleForTesting( | 157 test_handle()->RegisterThrottleForTesting( |
129 scoped_ptr<TestNavigationThrottle>(test_throttle)); | 158 scoped_ptr<TestNavigationThrottle>(test_throttle)); |
130 return test_throttle; | 159 return test_throttle; |
131 } | 160 } |
132 | 161 |
| 162 // Creates and registers a TestHeaderAddingNavigationThrottle that will add a |
| 163 // header with key |key| and value |value| on checks. |
| 164 void AddHeaderAddingThrottle(std::string key, std::string value) { |
| 165 test_handle()->RegisterThrottleForTesting(scoped_ptr<NavigationThrottle>( |
| 166 new TestHeaderAddingNavigationThrottle(test_handle(), key, value))); |
| 167 } |
| 168 |
| 169 const NavigationHandleImpl::ExtraHeadersList& extra_headers() const { |
| 170 return extra_headers_; |
| 171 } |
| 172 |
133 private: | 173 private: |
134 // The callback provided to NavigationHandleImpl::WillStartRequest and | 174 // The callback provided to NavigationHandleImpl::WillStartRequest and |
135 // NavigationHandleImpl::WillRedirectRequest during the tests. | 175 // NavigationHandleImpl::WillRedirectRequest during the tests. |
136 void UpdateThrottleCheckResult( | 176 void UpdateThrottleCheckResult( |
137 NavigationThrottle::ThrottleCheckResult result) { | 177 NavigationThrottle::ThrottleCheckResult result, |
| 178 const NavigationHandleImpl::ExtraHeadersList& extra_headers) { |
138 callback_result_ = result; | 179 callback_result_ = result; |
139 was_callback_called_ = true; | 180 was_callback_called_ = true; |
| 181 extra_headers_ = extra_headers; |
140 } | 182 } |
141 | 183 |
142 scoped_ptr<NavigationHandleImpl> test_handle_; | 184 scoped_ptr<NavigationHandleImpl> test_handle_; |
143 bool was_callback_called_; | 185 bool was_callback_called_; |
144 NavigationThrottle::ThrottleCheckResult callback_result_; | 186 NavigationThrottle::ThrottleCheckResult callback_result_; |
| 187 NavigationHandleImpl::ExtraHeadersList extra_headers_; |
145 }; | 188 }; |
146 | 189 |
147 // Checks that a deferred navigation can be properly resumed. | 190 // Checks that a deferred navigation can be properly resumed. |
148 TEST_F(NavigationHandleImplTest, ResumeDeferred) { | 191 TEST_F(NavigationHandleImplTest, ResumeDeferred) { |
149 TestNavigationThrottle* test_throttle = | 192 TestNavigationThrottle* test_throttle = |
150 CreateTestNavigationThrottle(NavigationThrottle::DEFER); | 193 CreateTestNavigationThrottle(NavigationThrottle::DEFER); |
151 EXPECT_FALSE(IsDeferringStart()); | 194 EXPECT_FALSE(IsDeferringStart()); |
152 EXPECT_FALSE(IsDeferringRedirect()); | 195 EXPECT_FALSE(IsDeferringRedirect()); |
153 EXPECT_EQ(0, test_throttle->will_start_calls()); | 196 EXPECT_EQ(0, test_throttle->will_start_calls()); |
154 EXPECT_EQ(0, test_throttle->will_redirect_calls()); | 197 EXPECT_EQ(0, test_throttle->will_redirect_calls()); |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 EXPECT_FALSE(IsDeferringStart()); | 523 EXPECT_FALSE(IsDeferringStart()); |
481 EXPECT_FALSE(IsDeferringRedirect()); | 524 EXPECT_FALSE(IsDeferringRedirect()); |
482 EXPECT_TRUE(was_callback_called()); | 525 EXPECT_TRUE(was_callback_called()); |
483 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); | 526 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, callback_result()); |
484 EXPECT_EQ(0, cancel_throttle->will_start_calls()); | 527 EXPECT_EQ(0, cancel_throttle->will_start_calls()); |
485 EXPECT_EQ(1, cancel_throttle->will_redirect_calls()); | 528 EXPECT_EQ(1, cancel_throttle->will_redirect_calls()); |
486 EXPECT_EQ(0, proceed_throttle->will_start_calls()); | 529 EXPECT_EQ(0, proceed_throttle->will_start_calls()); |
487 EXPECT_EQ(0, proceed_throttle->will_redirect_calls()); | 530 EXPECT_EQ(0, proceed_throttle->will_redirect_calls()); |
488 } | 531 } |
489 | 532 |
| 533 // Checks that extra headers are properly returned to the caller of |
| 534 // NavigationHandleImpl::WillStartRequest and |
| 535 // NavigationHandleImpl::WillSendRequest. In particular, they should be |
| 536 // returned in the order they were registered by the throttles. |
| 537 TEST_F(NavigationHandleImplTest, AddExtraHeaders) { |
| 538 const std::string kKey1 = "foo"; |
| 539 const std::string kValue1 = "foobar"; |
| 540 const std::string kKey2 = "bar"; |
| 541 const std::string kValue2 = "barfoo"; |
| 542 AddHeaderAddingThrottle(kKey1, kValue1); |
| 543 AddHeaderAddingThrottle(kKey2, kValue2); |
| 544 EXPECT_EQ(0u, extra_headers().size()); |
| 545 EXPECT_FALSE(was_callback_called()); |
| 546 |
| 547 // Simulate WillStartRequest. The headers should have been returned in the |
| 548 // right order. |
| 549 SimulateWillStartRequest(); |
| 550 EXPECT_TRUE(was_callback_called()); |
| 551 NavigationHandleImpl::ExtraHeadersList headers = extra_headers(); |
| 552 ASSERT_TRUE(headers.size() == 2); |
| 553 EXPECT_EQ(kKey1, headers[0].first); |
| 554 EXPECT_EQ(kValue1, headers[0].second); |
| 555 EXPECT_EQ(kKey2, headers[1].first); |
| 556 EXPECT_EQ(kValue2, headers[1].second); |
| 557 |
| 558 // Simulate WillRedirectRequest. The headers should have been returned in the |
| 559 // right order. |
| 560 SimulateWillRedirectRequest(); |
| 561 EXPECT_TRUE(was_callback_called()); |
| 562 headers = extra_headers(); |
| 563 ASSERT_TRUE(headers.size() == 2); |
| 564 EXPECT_EQ(kKey1, headers[0].first); |
| 565 EXPECT_EQ(kValue1, headers[0].second); |
| 566 EXPECT_EQ(kKey2, headers[1].first); |
| 567 EXPECT_EQ(kValue2, headers[1].second); |
| 568 } |
| 569 |
490 } // namespace content | 570 } // namespace content |
OLD | NEW |