OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> |
| 6 |
5 #include "chrome/common/content_settings.h" | 7 #include "chrome/common/content_settings.h" |
6 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
7 #include "chrome/renderer/content_settings_observer.h" | 9 #include "chrome/renderer/content_settings_observer.h" |
8 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
9 #include "content/common/view_messages.h" | 11 #include "content/common/view_messages.h" |
10 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
11 #include "ipc/ipc_message_macros.h" | 13 #include "ipc/ipc_message_macros.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
15 | 17 |
16 using testing::_; | 18 using testing::_; |
17 using testing::DeleteArg; | 19 using testing::DeleteArg; |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class MockContentSettingsObserver : public ContentSettingsObserver { | 23 class MockContentSettingsObserver : public ContentSettingsObserver { |
22 public: | 24 public: |
23 explicit MockContentSettingsObserver(content::RenderView* render_view); | 25 explicit MockContentSettingsObserver(content::RenderView* render_view); |
24 | 26 |
25 virtual bool Send(IPC::Message* message); | 27 virtual bool Send(IPC::Message* message); |
26 | 28 |
27 MOCK_METHOD2(OnContentBlocked, | 29 MOCK_METHOD2(OnContentBlocked, |
28 void(ContentSettingsType, const std::string&)); | 30 void(ContentSettingsType, const std::string&)); |
29 | 31 |
30 MOCK_METHOD5(OnAllowDOMStorage, | 32 MOCK_METHOD5(OnAllowDOMStorage, |
31 void(int, const GURL&, const GURL&, bool, IPC::Message*)); | 33 void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
| 34 std::vector<ContentSettingPatternSourceTuple> image_setting_rules_; |
| 35 GURL image_url_; |
| 36 std::string image_origin_; |
32 }; | 37 }; |
33 | 38 |
34 MockContentSettingsObserver::MockContentSettingsObserver( | 39 MockContentSettingsObserver::MockContentSettingsObserver( |
35 content::RenderView* render_view) | 40 content::RenderView* render_view) |
36 : ContentSettingsObserver(render_view) { | 41 : ContentSettingsObserver(render_view, &image_setting_rules_), |
| 42 image_url_("http://www.foo.com/image.jpg"), |
| 43 image_origin_("http://www.foo.com") { |
37 } | 44 } |
38 | 45 |
39 bool MockContentSettingsObserver::Send(IPC::Message* message) { | 46 bool MockContentSettingsObserver::Send(IPC::Message* message) { |
40 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) | 47 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
41 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) | 48 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) |
42 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, | 49 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, |
43 OnAllowDOMStorage) | 50 OnAllowDOMStorage) |
44 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) | 51 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
45 IPC_END_MESSAGE_MAP() | 52 IPC_END_MESSAGE_MAP() |
46 | 53 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Simulate a navigation within the page. | 170 // Simulate a navigation within the page. |
164 DidNavigateWithinPage(GetMainFrame(), true); | 171 DidNavigateWithinPage(GetMainFrame(), true); |
165 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 172 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
166 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 173 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
167 | 174 |
168 // Navigate to a different page. | 175 // Navigate to a different page. |
169 LoadHTML("<html>Bar</html>"); | 176 LoadHTML("<html>Bar</html>"); |
170 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 177 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
171 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 178 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
172 } | 179 } |
| 180 |
| 181 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
| 182 MockContentSettingsObserver mock_observer(view_); |
| 183 |
| 184 // Load some HTML. |
| 185 LoadHTML("<html>Foo</html>"); |
| 186 |
| 187 // Set the default image blocking setting. |
| 188 mock_observer.image_setting_rules_.push_back( |
| 189 ContentSettingPatternSourceTuple(ContentSettingsPattern::Wildcard(), |
| 190 ContentSettingsPattern::Wildcard(), |
| 191 CONTENT_SETTING_BLOCK, |
| 192 "", |
| 193 false)); |
| 194 |
| 195 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 196 EXPECT_CALL(mock_observer, |
| 197 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 198 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 199 true, mock_observer.image_url_)); |
| 200 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 201 |
| 202 // Create an exception which allows the image. |
| 203 mock_observer.image_setting_rules_.insert( |
| 204 mock_observer.image_setting_rules_.begin(), |
| 205 ContentSettingPatternSourceTuple( |
| 206 ContentSettingsPattern::Wildcard(), |
| 207 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 208 CONTENT_SETTING_ALLOW, |
| 209 "", |
| 210 false)); |
| 211 |
| 212 EXPECT_CALL( |
| 213 mock_observer, |
| 214 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 215 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 216 mock_observer.image_url_)); |
| 217 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 218 } |
| 219 |
| 220 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 221 MockContentSettingsObserver mock_observer(view_); |
| 222 |
| 223 // Load some HTML. |
| 224 LoadHTML("<html>Foo</html>"); |
| 225 |
| 226 // Set the default image blocking setting. |
| 227 mock_observer.image_setting_rules_.push_back( |
| 228 ContentSettingPatternSourceTuple(ContentSettingsPattern::Wildcard(), |
| 229 ContentSettingsPattern::Wildcard(), |
| 230 CONTENT_SETTING_ALLOW, |
| 231 "", |
| 232 false)); |
| 233 |
| 234 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 235 EXPECT_CALL( |
| 236 mock_observer, |
| 237 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 238 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 239 mock_observer.image_url_)); |
| 240 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 241 |
| 242 // Create an exception which blocks the image. |
| 243 mock_observer.image_setting_rules_.insert( |
| 244 mock_observer.image_setting_rules_.begin(), |
| 245 ContentSettingPatternSourceTuple( |
| 246 ContentSettingsPattern::Wildcard(), |
| 247 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 248 CONTENT_SETTING_BLOCK, |
| 249 "", |
| 250 false)); |
| 251 EXPECT_CALL(mock_observer, |
| 252 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 253 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 254 true, mock_observer.image_url_)); |
| 255 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 256 } |
OLD | NEW |