OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/bind.h" | |
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" | |
16 | |
17 using content::NavigationThrottle; | |
18 using testing::_; | |
19 using testing::Eq; | |
20 using testing::Ne; | |
21 using testing::Property; | |
22 using testing::Return; | |
23 | |
24 namespace navigation_interception { | |
25 | |
26 namespace { | |
27 | |
28 const char kTestUrl[] = "http://www.test.com/"; | |
29 | |
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 | |
35 // used instead. | |
36 MATCHER(NavigationParamsUrlIsTest, "") { | |
37 return arg.url() == GURL(kTestUrl); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 // MockInterceptCallbackReceiver ---------------------------------------------- | |
43 | |
44 class MockInterceptCallbackReceiver { | |
45 public: | |
46 MOCK_METHOD2(ShouldIgnoreNavigation, | |
47 bool(content::WebContents* source, | |
48 const NavigationParams& navigation_params)); | |
49 }; | |
50 | |
51 // InterceptNavigationThrottleTest ------------------------------------ | |
52 | |
53 class InterceptNavigationThrottleTest | |
54 : public content::RenderViewHostTestHarness { | |
55 public: | |
56 InterceptNavigationThrottleTest() | |
57 : mock_callback_receiver_(new MockInterceptCallbackReceiver()) {} | |
58 | |
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( | |
67 test_handle.get(), | |
68 base::Bind( | |
69 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation, | |
70 base::Unretained(mock_callback_receiver_.get())))) | |
71 .Pass()); | |
72 return test_handle->CallWillStartRequestForTesting( | |
73 is_post, content::Referrer(), false, ui::PAGE_TRANSITION_LINK, false); | |
74 } | |
75 | |
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( | |
83 test_handle.get(), | |
84 base::Bind( | |
85 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation, | |
86 base::Unretained(mock_callback_receiver_.get())))) | |
87 .Pass()); | |
88 test_handle->CallWillStartRequestForTesting( | |
89 true, content::Referrer(), false, ui::PAGE_TRANSITION_LINK, false); | |
90 return test_handle->CallWillRedirectRequestForTesting(GURL(kTestUrl), false, | |
91 GURL(), false); | |
92 } | |
93 | |
94 scoped_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_; | |
95 }; | |
96 | |
97 TEST_F(InterceptNavigationThrottleTest, | |
98 RequestDeferredAndResumedIfNavigationNotIgnored) { | |
99 ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _)) | |
100 .WillByDefault(Return(false)); | |
101 EXPECT_CALL( | |
102 *mock_callback_receiver_, | |
103 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); | |
104 NavigationThrottle::ThrottleCheckResult result = | |
105 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); | |
106 | |
107 EXPECT_EQ(NavigationThrottle::PROCEED, result); | |
108 } | |
109 | |
110 TEST_F(InterceptNavigationThrottleTest, | |
111 RequestDeferredAndCancelledIfNavigationIgnored) { | |
112 ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _)) | |
113 .WillByDefault(Return(true)); | |
114 EXPECT_CALL( | |
115 *mock_callback_receiver_, | |
116 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); | |
117 NavigationThrottle::ThrottleCheckResult result = | |
118 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); | |
119 | |
120 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result); | |
121 } | |
122 | |
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)); | |
129 | |
130 NavigationThrottle::ThrottleCheckResult result = | |
131 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); | |
132 | |
133 EXPECT_EQ(NavigationThrottle::PROCEED, result); | |
134 } | |
135 | |
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); | |
144 | |
145 EXPECT_EQ(NavigationThrottle::PROCEED, result); | |
146 } | |
147 | |
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(); | |
161 | |
162 EXPECT_EQ(NavigationThrottle::PROCEED, result); | |
163 } | |
164 | |
165 } // namespace navigation_interception | |
OLD | NEW |