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
3 // found in the LICENSE file.
6 #include "base/bind_helpers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "components/navigation_interception/intercept_navigation_throttle.h"
9 #include "components/navigation_interception/navigation_params.h"
10 #include "content/public/browser/navigation_handle.h"
11 #include "content/public/browser/navigation_throttle.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/test/test_renderer_host.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::NavigationThrottle
;
21 using testing::Property
;
22 using testing::Return
;
24 namespace navigation_interception
{
28 const char kTestUrl
[] = "http://www.test.com/";
30 // The MS C++ compiler complains about not being able to resolve which url()
31 // method (const or non-const) to use if we use the Property matcher to check
32 // the return value of the NavigationParams::url() method.
33 // It is possible to suppress the error by specifying the types directly but
34 // that results in very ugly syntax, which is why these custom matchers are
36 MATCHER(NavigationParamsUrlIsTest
, "") {
37 return arg
.url() == GURL(kTestUrl
);
42 // MockInterceptCallbackReceiver ----------------------------------------------
44 class MockInterceptCallbackReceiver
{
46 MOCK_METHOD2(ShouldIgnoreNavigation
,
47 bool(content::WebContents
* source
,
48 const NavigationParams
& navigation_params
));
51 // InterceptNavigationThrottleTest ------------------------------------
53 class InterceptNavigationThrottleTest
54 : public content::RenderViewHostTestHarness
{
56 InterceptNavigationThrottleTest()
57 : mock_callback_receiver_(new MockInterceptCallbackReceiver()) {}
59 NavigationThrottle::ThrottleCheckResult
60 SimulateWillStart(const GURL
& url
, const GURL
& sanitized_url
, bool is_post
) {
61 scoped_ptr
<content::NavigationHandle
> test_handle
=
62 content::NavigationHandle::CreateNavigationHandleForTesting(
63 url
, true, web_contents());
64 test_handle
->RegisterThrottleForTesting(
65 scoped_ptr
<NavigationThrottle
>(
66 new InterceptNavigationThrottle(
69 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation
,
70 base::Unretained(mock_callback_receiver_
.get()))))
72 return test_handle
->CallWillStartRequestForTesting(
73 is_post
, content::Referrer(), false, ui::PAGE_TRANSITION_LINK
, false);
76 NavigationThrottle::ThrottleCheckResult
Simulate302() {
77 scoped_ptr
<content::NavigationHandle
> test_handle
=
78 content::NavigationHandle::CreateNavigationHandleForTesting(
79 GURL(kTestUrl
), true, web_contents());
80 test_handle
->RegisterThrottleForTesting(
81 scoped_ptr
<NavigationThrottle
>(
82 new InterceptNavigationThrottle(
85 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation
,
86 base::Unretained(mock_callback_receiver_
.get()))))
88 test_handle
->CallWillStartRequestForTesting(
89 true, content::Referrer(), false, ui::PAGE_TRANSITION_LINK
, false);
90 return test_handle
->CallWillRedirectRequestForTesting(GURL(kTestUrl
), false,
94 scoped_ptr
<MockInterceptCallbackReceiver
> mock_callback_receiver_
;
97 TEST_F(InterceptNavigationThrottleTest
,
98 RequestDeferredAndResumedIfNavigationNotIgnored
) {
99 ON_CALL(*mock_callback_receiver_
, ShouldIgnoreNavigation(_
, _
))
100 .WillByDefault(Return(false));
102 *mock_callback_receiver_
,
103 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest()));
104 NavigationThrottle::ThrottleCheckResult result
=
105 SimulateWillStart(GURL(kTestUrl
), GURL(kTestUrl
), false);
107 EXPECT_EQ(NavigationThrottle::PROCEED
, result
);
110 TEST_F(InterceptNavigationThrottleTest
,
111 RequestDeferredAndCancelledIfNavigationIgnored
) {
112 ON_CALL(*mock_callback_receiver_
, ShouldIgnoreNavigation(_
, _
))
113 .WillByDefault(Return(true));
115 *mock_callback_receiver_
,
116 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest()));
117 NavigationThrottle::ThrottleCheckResult result
=
118 SimulateWillStart(GURL(kTestUrl
), GURL(kTestUrl
), false);
120 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE
, result
);
123 TEST_F(InterceptNavigationThrottleTest
, CallbackIsPostFalseForGet
) {
124 EXPECT_CALL(*mock_callback_receiver_
,
125 ShouldIgnoreNavigation(
126 _
, AllOf(NavigationParamsUrlIsTest(),
127 Property(&NavigationParams::is_post
, Eq(false)))))
128 .WillOnce(Return(false));
130 NavigationThrottle::ThrottleCheckResult result
=
131 SimulateWillStart(GURL(kTestUrl
), GURL(kTestUrl
), false);
133 EXPECT_EQ(NavigationThrottle::PROCEED
, result
);
136 TEST_F(InterceptNavigationThrottleTest
, CallbackIsPostTrueForPost
) {
137 EXPECT_CALL(*mock_callback_receiver_
,
138 ShouldIgnoreNavigation(
139 _
, AllOf(NavigationParamsUrlIsTest(),
140 Property(&NavigationParams::is_post
, Eq(true)))))
141 .WillOnce(Return(false));
142 NavigationThrottle::ThrottleCheckResult result
=
143 SimulateWillStart(GURL(kTestUrl
), GURL(kTestUrl
), true);
145 EXPECT_EQ(NavigationThrottle::PROCEED
, result
);
148 TEST_F(InterceptNavigationThrottleTest
,
149 CallbackIsPostFalseForPostConvertedToGetBy302
) {
150 EXPECT_CALL(*mock_callback_receiver_
,
151 ShouldIgnoreNavigation(
152 _
, AllOf(NavigationParamsUrlIsTest(),
153 Property(&NavigationParams::is_post
, Eq(true)))))
154 .WillOnce(Return(false));
155 EXPECT_CALL(*mock_callback_receiver_
,
156 ShouldIgnoreNavigation(
157 _
, AllOf(NavigationParamsUrlIsTest(),
158 Property(&NavigationParams::is_post
, Eq(false)))))
159 .WillOnce(Return(false));
160 NavigationThrottle::ThrottleCheckResult result
= Simulate302();
162 EXPECT_EQ(NavigationThrottle::PROCEED
, result
);
165 } // namespace navigation_interception