OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/navigation_handle_tester.h" |
| 14 #include "content/public/test/test_renderer_host.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 using content::NavigationThrottle; |
| 19 using testing::_; |
| 20 using testing::Eq; |
| 21 using testing::Ne; |
| 22 using testing::Property; |
| 23 using testing::Return; |
| 24 |
| 25 namespace navigation_interception { |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kTestUrl[] = "http://www.test.com/"; |
| 30 const char kUnsafeTestUrl[] = "about:crash"; |
| 31 |
| 32 // The MS C++ compiler complains about not being able to resolve which url() |
| 33 // method (const or non-const) to use if we use the Property matcher to check |
| 34 // the return value of the NavigationParams::url() method. |
| 35 // It is possible to suppress the error by specifying the types directly but |
| 36 // that results in very ugly syntax, which is why these custom matchers are |
| 37 // used instead. |
| 38 MATCHER(NavigationParamsUrlIsTest, "") { |
| 39 return arg.url() == GURL(kTestUrl); |
| 40 } |
| 41 |
| 42 MATCHER(NavigationParamsUrlIsSafe, "") { |
| 43 return arg.url() != GURL(kUnsafeTestUrl); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 // MockInterceptCallbackReceiver ---------------------------------------------- |
| 49 |
| 50 class MockInterceptCallbackReceiver { |
| 51 public: |
| 52 MOCK_METHOD2(ShouldIgnoreNavigation, |
| 53 bool(content::WebContents* source, |
| 54 const NavigationParams& navigation_params)); |
| 55 }; |
| 56 |
| 57 // InterceptNavigationThrottleTest ------------------------------------ |
| 58 |
| 59 class InterceptNavigationThrottleTest |
| 60 : public content::RenderViewHostTestHarness { |
| 61 public: |
| 62 InterceptNavigationThrottleTest() |
| 63 : mock_callback_receiver_(new MockInterceptCallbackReceiver()) {} |
| 64 |
| 65 NavigationThrottle::ThrottleCheckResult |
| 66 SimulateWillStart(const GURL& url, const GURL& sanitized_url, bool is_post) { |
| 67 scoped_ptr<content::NavigationHandle> test_handle( |
| 68 content::NavigationHandleTester::CreateTestNavigationHandle( |
| 69 url, sanitized_url, true, web_contents())); |
| 70 test_handle->RegisterThrottleForTesting( |
| 71 scoped_ptr<NavigationThrottle>( |
| 72 new InterceptNavigationThrottle( |
| 73 web_contents(), test_handle.get(), |
| 74 base::Bind( |
| 75 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation, |
| 76 base::Unretained(mock_callback_receiver_.get())))) |
| 77 .Pass()); |
| 78 return content::NavigationHandleTester::For(test_handle.get()) |
| 79 ->SimulateWillStartRequest(is_post, content::Referrer(), false, |
| 80 ui::PAGE_TRANSITION_LINK, false); |
| 81 } |
| 82 |
| 83 NavigationThrottle::ThrottleCheckResult Simulate302() { |
| 84 scoped_ptr<content::NavigationHandle> test_handle( |
| 85 content::NavigationHandleTester::CreateTestNavigationHandle( |
| 86 GURL(kTestUrl), GURL(kTestUrl), true, web_contents())); |
| 87 test_handle->RegisterThrottleForTesting( |
| 88 scoped_ptr<NavigationThrottle>( |
| 89 new InterceptNavigationThrottle( |
| 90 web_contents(), test_handle.get(), |
| 91 base::Bind( |
| 92 &MockInterceptCallbackReceiver::ShouldIgnoreNavigation, |
| 93 base::Unretained(mock_callback_receiver_.get())))) |
| 94 .Pass()); |
| 95 content::NavigationHandleTester::For(test_handle.get()) |
| 96 ->SimulateWillStartRequest(true, content::Referrer(), false, |
| 97 ui::PAGE_TRANSITION_LINK, false); |
| 98 return content::NavigationHandleTester::For(test_handle.get()) |
| 99 ->SimulateWillRedirectRequest(GURL(kTestUrl), GURL(kTestUrl), false, |
| 100 GURL(), false); |
| 101 } |
| 102 |
| 103 scoped_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_; |
| 104 }; |
| 105 |
| 106 TEST_F(InterceptNavigationThrottleTest, |
| 107 RequestDeferredAndResumedIfNavigationNotIgnored) { |
| 108 ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _)) |
| 109 .WillByDefault(Return(false)); |
| 110 EXPECT_CALL( |
| 111 *mock_callback_receiver_, |
| 112 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); |
| 113 NavigationThrottle::ThrottleCheckResult result = |
| 114 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); |
| 115 |
| 116 EXPECT_EQ(NavigationThrottle::PROCEED, result); |
| 117 } |
| 118 |
| 119 TEST_F(InterceptNavigationThrottleTest, |
| 120 RequestDeferredAndCancelledIfNavigationIgnored) { |
| 121 ON_CALL(*mock_callback_receiver_, ShouldIgnoreNavigation(_, _)) |
| 122 .WillByDefault(Return(true)); |
| 123 EXPECT_CALL( |
| 124 *mock_callback_receiver_, |
| 125 ShouldIgnoreNavigation(web_contents(), NavigationParamsUrlIsTest())); |
| 126 NavigationThrottle::ThrottleCheckResult result = |
| 127 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); |
| 128 |
| 129 EXPECT_EQ(NavigationThrottle::CANCEL_AND_IGNORE, result); |
| 130 } |
| 131 |
| 132 TEST_F(InterceptNavigationThrottleTest, CallbackCalledWithFilteredUrl) { |
| 133 ON_CALL(*mock_callback_receiver_, |
| 134 ShouldIgnoreNavigation(_, NavigationParamsUrlIsSafe())) |
| 135 .WillByDefault(Return(false)); |
| 136 EXPECT_CALL(*mock_callback_receiver_, |
| 137 ShouldIgnoreNavigation(_, NavigationParamsUrlIsSafe())) |
| 138 .Times(1); |
| 139 NavigationThrottle::ThrottleCheckResult result = |
| 140 SimulateWillStart(GURL(kUnsafeTestUrl), GURL(), false); |
| 141 |
| 142 EXPECT_EQ(NavigationThrottle::PROCEED, result); |
| 143 } |
| 144 |
| 145 TEST_F(InterceptNavigationThrottleTest, CallbackIsPostFalseForGet) { |
| 146 EXPECT_CALL(*mock_callback_receiver_, |
| 147 ShouldIgnoreNavigation( |
| 148 _, AllOf(NavigationParamsUrlIsSafe(), |
| 149 Property(&NavigationParams::is_post, Eq(false))))) |
| 150 .WillOnce(Return(false)); |
| 151 |
| 152 NavigationThrottle::ThrottleCheckResult result = |
| 153 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), false); |
| 154 |
| 155 EXPECT_EQ(NavigationThrottle::PROCEED, result); |
| 156 } |
| 157 |
| 158 TEST_F(InterceptNavigationThrottleTest, CallbackIsPostTrueForPost) { |
| 159 EXPECT_CALL(*mock_callback_receiver_, |
| 160 ShouldIgnoreNavigation( |
| 161 _, AllOf(NavigationParamsUrlIsSafe(), |
| 162 Property(&NavigationParams::is_post, Eq(true))))) |
| 163 .WillOnce(Return(false)); |
| 164 NavigationThrottle::ThrottleCheckResult result = |
| 165 SimulateWillStart(GURL(kTestUrl), GURL(kTestUrl), true); |
| 166 |
| 167 EXPECT_EQ(NavigationThrottle::PROCEED, result); |
| 168 } |
| 169 |
| 170 TEST_F(InterceptNavigationThrottleTest, |
| 171 CallbackIsPostFalseForPostConvertedToGetBy302) { |
| 172 EXPECT_CALL(*mock_callback_receiver_, |
| 173 ShouldIgnoreNavigation( |
| 174 _, AllOf(NavigationParamsUrlIsSafe(), |
| 175 Property(&NavigationParams::is_post, Eq(true))))) |
| 176 .WillOnce(Return(false)); |
| 177 EXPECT_CALL(*mock_callback_receiver_, |
| 178 ShouldIgnoreNavigation( |
| 179 _, AllOf(NavigationParamsUrlIsSafe(), |
| 180 Property(&NavigationParams::is_post, Eq(false))))) |
| 181 .WillOnce(Return(false)); |
| 182 NavigationThrottle::ThrottleCheckResult result = Simulate302(); |
| 183 |
| 184 EXPECT_EQ(NavigationThrottle::PROCEED, result); |
| 185 } |
| 186 |
| 187 } // namespace navigation_interception |
OLD | NEW |