| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/content_settings.h" | 5 #include "chrome/common/content_settings.h" |
| 6 #include "chrome/common/render_messages.h" | 6 #include "chrome/common/render_messages.h" |
| 7 #include "chrome/renderer/content_settings_observer.h" | 7 #include "chrome/renderer/content_settings_observer.h" |
| 8 #include "chrome/test/base/chrome_render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "ipc/ipc_message_macros.h" | 10 #include "ipc/ipc_message_macros.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/web/WebView.h" | 13 #include "third_party/WebKit/public/web/WebView.h" |
| 14 | 14 |
| 15 using testing::_; | 15 using testing::_; |
| 16 using testing::DeleteArg; | 16 using testing::DeleteArg; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockContentSettingsObserver : public ContentSettingsObserver { | 20 class MockContentSettingsObserver : public ContentSettingsObserver { |
| 21 public: | 21 public: |
| 22 explicit MockContentSettingsObserver(content::RenderView* render_view); | 22 explicit MockContentSettingsObserver(content::RenderView* render_view); |
| 23 | 23 |
| 24 virtual bool Send(IPC::Message* message); | 24 virtual bool Send(IPC::Message* message); |
| 25 | 25 |
| 26 MOCK_METHOD2(OnContentBlocked, | 26 MOCK_METHOD1(OnContentBlocked, |
| 27 void(ContentSettingsType, const std::string&)); | 27 void(ContentSettingsType)); |
| 28 | 28 |
| 29 MOCK_METHOD5(OnAllowDOMStorage, | 29 MOCK_METHOD5(OnAllowDOMStorage, |
| 30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); | 30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
| 31 GURL image_url_; | 31 GURL image_url_; |
| 32 std::string image_origin_; | 32 std::string image_origin_; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 MockContentSettingsObserver::MockContentSettingsObserver( | 35 MockContentSettingsObserver::MockContentSettingsObserver( |
| 36 content::RenderView* render_view) | 36 content::RenderView* render_view) |
| 37 : ContentSettingsObserver(render_view), | 37 : ContentSettingsObserver(render_view), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 49 | 49 |
| 50 // Our super class deletes the message. | 50 // Our super class deletes the message. |
| 51 return RenderViewObserver::Send(message); | 51 return RenderViewObserver::Send(message); |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 TEST_F(ChromeRenderViewTest, DidBlockContentType) { | 56 TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
| 57 MockContentSettingsObserver observer(view_); | 57 MockContentSettingsObserver observer(view_); |
| 58 EXPECT_CALL(observer, | 58 EXPECT_CALL(observer, |
| 59 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 59 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES)); |
| 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 61 | 61 |
| 62 // Blocking the same content type a second time shouldn't send a notification. | 62 // Blocking the same content type a second time shouldn't send a notification. |
| 63 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 63 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 64 ::testing::Mock::VerifyAndClearExpectations(&observer); | 64 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 65 | |
| 66 // Blocking two different plugins should send two notifications. | |
| 67 std::string kFooPlugin = "foo"; | |
| 68 std::string kBarPlugin = "bar"; | |
| 69 EXPECT_CALL(observer, | |
| 70 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); | |
| 71 EXPECT_CALL(observer, | |
| 72 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); | |
| 73 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); | |
| 74 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); | |
| 75 } | 65 } |
| 76 | 66 |
| 77 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. | 67 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
| 78 // Fails due to http://crbug.com/104300 | 68 // Fails due to http://crbug.com/104300 |
| 79 TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) { | 69 TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) { |
| 80 // Load some HTML, so we have a valid security origin. | 70 // Load some HTML, so we have a valid security origin. |
| 81 LoadHTML("<html></html>"); | 71 LoadHTML("<html></html>"); |
| 82 MockContentSettingsObserver observer(view_); | 72 MockContentSettingsObserver observer(view_); |
| 83 ON_CALL(observer, | 73 ON_CALL(observer, |
| 84 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); | 74 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 image_setting_rules.push_back( | 181 image_setting_rules.push_back( |
| 192 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 182 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 193 ContentSettingsPattern::Wildcard(), | 183 ContentSettingsPattern::Wildcard(), |
| 194 CONTENT_SETTING_BLOCK, | 184 CONTENT_SETTING_BLOCK, |
| 195 std::string(), | 185 std::string(), |
| 196 false)); | 186 false)); |
| 197 | 187 |
| 198 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 188 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 199 observer->SetContentSettingRules(&content_setting_rules); | 189 observer->SetContentSettingRules(&content_setting_rules); |
| 200 EXPECT_CALL(mock_observer, | 190 EXPECT_CALL(mock_observer, |
| 201 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 191 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); |
| 202 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 192 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 203 true, mock_observer.image_url_)); | 193 true, mock_observer.image_url_)); |
| 204 ::testing::Mock::VerifyAndClearExpectations(&observer); | 194 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 205 | 195 |
| 206 // Create an exception which allows the image. | 196 // Create an exception which allows the image. |
| 207 image_setting_rules.insert( | 197 image_setting_rules.insert( |
| 208 image_setting_rules.begin(), | 198 image_setting_rules.begin(), |
| 209 ContentSettingPatternSource( | 199 ContentSettingPatternSource( |
| 210 ContentSettingsPattern::Wildcard(), | 200 ContentSettingsPattern::Wildcard(), |
| 211 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 201 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 212 CONTENT_SETTING_ALLOW, | 202 CONTENT_SETTING_ALLOW, |
| 213 std::string(), | 203 std::string(), |
| 214 false)); | 204 false)); |
| 215 | 205 |
| 216 EXPECT_CALL( | 206 EXPECT_CALL( |
| 217 mock_observer, | 207 mock_observer, |
| 218 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 208 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); |
| 219 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 209 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 220 mock_observer.image_url_)); | 210 mock_observer.image_url_)); |
| 221 ::testing::Mock::VerifyAndClearExpectations(&observer); | 211 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 222 } | 212 } |
| 223 | 213 |
| 224 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 214 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 225 MockContentSettingsObserver mock_observer(view_); | 215 MockContentSettingsObserver mock_observer(view_); |
| 226 | 216 |
| 227 // Load some HTML. | 217 // Load some HTML. |
| 228 LoadHTML("<html>Foo</html>"); | 218 LoadHTML("<html>Foo</html>"); |
| 229 | 219 |
| 230 // Set the default image blocking setting. | 220 // Set the default image blocking setting. |
| 231 RendererContentSettingRules content_setting_rules; | 221 RendererContentSettingRules content_setting_rules; |
| 232 ContentSettingsForOneType& image_setting_rules = | 222 ContentSettingsForOneType& image_setting_rules = |
| 233 content_setting_rules.image_rules; | 223 content_setting_rules.image_rules; |
| 234 image_setting_rules.push_back( | 224 image_setting_rules.push_back( |
| 235 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 225 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 236 ContentSettingsPattern::Wildcard(), | 226 ContentSettingsPattern::Wildcard(), |
| 237 CONTENT_SETTING_ALLOW, | 227 CONTENT_SETTING_ALLOW, |
| 238 std::string(), | 228 std::string(), |
| 239 false)); | 229 false)); |
| 240 | 230 |
| 241 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 231 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 242 observer->SetContentSettingRules(&content_setting_rules); | 232 observer->SetContentSettingRules(&content_setting_rules); |
| 243 EXPECT_CALL( | 233 EXPECT_CALL( |
| 244 mock_observer, | 234 mock_observer, |
| 245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 235 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); |
| 246 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 236 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 247 mock_observer.image_url_)); | 237 mock_observer.image_url_)); |
| 248 ::testing::Mock::VerifyAndClearExpectations(&observer); | 238 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 249 | 239 |
| 250 // Create an exception which blocks the image. | 240 // Create an exception which blocks the image. |
| 251 image_setting_rules.insert( | 241 image_setting_rules.insert( |
| 252 image_setting_rules.begin(), | 242 image_setting_rules.begin(), |
| 253 ContentSettingPatternSource( | 243 ContentSettingPatternSource( |
| 254 ContentSettingsPattern::Wildcard(), | 244 ContentSettingsPattern::Wildcard(), |
| 255 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 245 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 256 CONTENT_SETTING_BLOCK, | 246 CONTENT_SETTING_BLOCK, |
| 257 std::string(), | 247 std::string(), |
| 258 false)); | 248 false)); |
| 259 EXPECT_CALL(mock_observer, | 249 EXPECT_CALL(mock_observer, |
| 260 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 250 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); |
| 261 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 251 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 262 true, mock_observer.image_url_)); | 252 true, mock_observer.image_url_)); |
| 263 ::testing::Mock::VerifyAndClearExpectations(&observer); | 253 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 264 } | 254 } |
| 265 | 255 |
| 266 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { | 256 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 267 // Set the content settings for scripts. | 257 // Set the content settings for scripts. |
| 268 RendererContentSettingRules content_setting_rules; | 258 RendererContentSettingRules content_setting_rules; |
| 269 ContentSettingsForOneType& script_setting_rules = | 259 ContentSettingsForOneType& script_setting_rules = |
| 270 content_setting_rules.script_rules; | 260 content_setting_rules.script_rules; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 364 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 375 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 365 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 376 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 366 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 377 was_blocked = true; | 367 was_blocked = true; |
| 378 } | 368 } |
| 379 EXPECT_FALSE(was_blocked); | 369 EXPECT_FALSE(was_blocked); |
| 380 | 370 |
| 381 // Verify that images are allowed. | 371 // Verify that images are allowed. |
| 382 EXPECT_CALL( | 372 EXPECT_CALL( |
| 383 mock_observer, | 373 mock_observer, |
| 384 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 374 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); |
| 385 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 375 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 386 mock_observer.image_url_)); | 376 mock_observer.image_url_)); |
| 387 ::testing::Mock::VerifyAndClearExpectations(&observer); | 377 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 388 } | 378 } |
| OLD | NEW |