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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 public: | 21 public: |
22 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); | 22 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
23 | 23 |
24 virtual bool Send(IPC::Message* message); | 24 virtual bool Send(IPC::Message* message); |
25 | 25 |
26 MOCK_METHOD1(OnContentBlocked, | 26 MOCK_METHOD1(OnContentBlocked, |
27 void(ContentSettingsType)); | 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_, media_url_; |
32 std::string image_origin_; | 32 std::string image_origin_, media_origin_; |
33 }; | 33 }; |
34 | 34 |
35 MockContentSettingsObserver::MockContentSettingsObserver( | 35 MockContentSettingsObserver::MockContentSettingsObserver( |
36 content::RenderFrame* render_frame) | 36 content::RenderFrame* render_frame) |
37 : ContentSettingsObserver(render_frame, NULL), | 37 : ContentSettingsObserver(render_frame, NULL), |
38 image_url_("http://www.foo.com/image.jpg"), | 38 image_url_("http://www.foo.com/image.jpg"), |
39 image_origin_("http://www.foo.com") { | 39 media_url_("http://www.bar.com/video.webm"), |
| 40 image_origin_("http://www.foo.com"), |
| 41 media_origin_("http://www.bar.com") { |
40 } | 42 } |
41 | 43 |
42 bool MockContentSettingsObserver::Send(IPC::Message* message) { | 44 bool MockContentSettingsObserver::Send(IPC::Message* message) { |
43 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) | 45 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
44 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) | 46 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) |
45 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, | 47 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, |
46 OnAllowDOMStorage) | 48 OnAllowDOMStorage) |
47 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) | 49 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
48 IPC_END_MESSAGE_MAP() | 50 IPC_END_MESSAGE_MAP() |
49 | 51 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 246 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
245 CONTENT_SETTING_BLOCK, | 247 CONTENT_SETTING_BLOCK, |
246 std::string(), | 248 std::string(), |
247 false)); | 249 false)); |
248 EXPECT_CALL(mock_observer, | 250 EXPECT_CALL(mock_observer, |
249 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); | 251 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); |
250 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); | 252 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); |
251 ::testing::Mock::VerifyAndClearExpectations(&observer); | 253 ::testing::Mock::VerifyAndClearExpectations(&observer); |
252 } | 254 } |
253 | 255 |
| 256 TEST_F(ChromeRenderViewTest, MediaBlockedByDefault) { |
| 257 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| 258 |
| 259 // Load some HTML. |
| 260 LoadHTML("<html>Foo</html>"); |
| 261 |
| 262 // Set the default media blocking setting. |
| 263 RendererContentSettingRules content_setting_rules; |
| 264 ContentSettingsForOneType& media_setting_rules = |
| 265 content_setting_rules.media_rules; |
| 266 media_setting_rules.push_back( |
| 267 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 268 ContentSettingsPattern::Wildcard(), |
| 269 CONTENT_SETTING_BLOCK, |
| 270 std::string(), |
| 271 false)); |
| 272 |
| 273 ContentSettingsObserver* observer = |
| 274 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 275 observer->SetContentSettingRules(&content_setting_rules); |
| 276 EXPECT_CALL(mock_observer, |
| 277 OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIA)); |
| 278 EXPECT_FALSE(observer->allowMedia(mock_observer.media_url_)); |
| 279 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 280 |
| 281 // Create an exception which allows the video. |
| 282 media_setting_rules.insert( |
| 283 media_setting_rules.begin(), |
| 284 ContentSettingPatternSource( |
| 285 ContentSettingsPattern::Wildcard(), |
| 286 ContentSettingsPattern::FromString(mock_observer.media_origin_), |
| 287 CONTENT_SETTING_ALLOW, |
| 288 std::string(), |
| 289 false)); |
| 290 |
| 291 EXPECT_CALL( |
| 292 mock_observer, |
| 293 OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIA)).Times(0); |
| 294 EXPECT_TRUE(observer->allowMedia(mock_observer.media_url_)); |
| 295 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 296 } |
| 297 |
| 298 TEST_F(ChromeRenderViewTest, MediaAllowedByDefault) { |
| 299 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| 300 |
| 301 // Load some HTML. |
| 302 LoadHTML("<html>Foo</html>"); |
| 303 |
| 304 // Set the default media blocking setting. |
| 305 RendererContentSettingRules content_setting_rules; |
| 306 ContentSettingsForOneType& media_setting_rules = |
| 307 content_setting_rules.media_rules; |
| 308 media_setting_rules.push_back( |
| 309 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 310 ContentSettingsPattern::Wildcard(), |
| 311 CONTENT_SETTING_ALLOW, |
| 312 std::string(), |
| 313 false)); |
| 314 |
| 315 ContentSettingsObserver* observer = |
| 316 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 317 observer->SetContentSettingRules(&content_setting_rules); |
| 318 EXPECT_CALL( |
| 319 mock_observer, |
| 320 OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIA)).Times(0); |
| 321 EXPECT_TRUE(observer->allowMedia(mock_observer.media_url_)); |
| 322 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 323 |
| 324 // Create an exception which blocks the video. |
| 325 media_setting_rules.insert( |
| 326 media_setting_rules.begin(), |
| 327 ContentSettingPatternSource( |
| 328 ContentSettingsPattern::Wildcard(), |
| 329 ContentSettingsPattern::FromString(mock_observer.media_origin_), |
| 330 CONTENT_SETTING_BLOCK, |
| 331 std::string(), |
| 332 false)); |
| 333 EXPECT_CALL(mock_observer, |
| 334 OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIA)); |
| 335 EXPECT_FALSE(observer->allowMedia(mock_observer.media_url_)); |
| 336 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 337 } |
| 338 |
| 339 |
254 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { | 340 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
255 // Set the content settings for scripts. | 341 // Set the content settings for scripts. |
256 RendererContentSettingRules content_setting_rules; | 342 RendererContentSettingRules content_setting_rules; |
257 ContentSettingsForOneType& script_setting_rules = | 343 ContentSettingsForOneType& script_setting_rules = |
258 content_setting_rules.script_rules; | 344 content_setting_rules.script_rules; |
259 script_setting_rules.push_back( | 345 script_setting_rules.push_back( |
260 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 346 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
261 ContentSettingsPattern::Wildcard(), | 347 ContentSettingsPattern::Wildcard(), |
262 CONTENT_SETTING_BLOCK, | 348 CONTENT_SETTING_BLOCK, |
263 std::string(), | 349 std::string(), |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 // Block images. | 424 // Block images. |
339 ContentSettingsForOneType& image_setting_rules = | 425 ContentSettingsForOneType& image_setting_rules = |
340 content_setting_rules.image_rules; | 426 content_setting_rules.image_rules; |
341 image_setting_rules.push_back( | 427 image_setting_rules.push_back( |
342 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 428 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
343 ContentSettingsPattern::Wildcard(), | 429 ContentSettingsPattern::Wildcard(), |
344 CONTENT_SETTING_BLOCK, | 430 CONTENT_SETTING_BLOCK, |
345 std::string(), | 431 std::string(), |
346 false)); | 432 false)); |
347 | 433 |
| 434 // Block media. |
| 435 ContentSettingsForOneType& media_setting_rules = |
| 436 content_setting_rules.media_rules; |
| 437 media_setting_rules.push_back( |
| 438 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 439 ContentSettingsPattern::Wildcard(), |
| 440 CONTENT_SETTING_BLOCK, |
| 441 std::string(), |
| 442 false)); |
| 443 |
348 ContentSettingsObserver* observer = | 444 ContentSettingsObserver* observer = |
349 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 445 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
350 observer->SetContentSettingRules(&content_setting_rules); | 446 observer->SetContentSettingRules(&content_setting_rules); |
351 observer->OnSetAsInterstitial(); | 447 observer->OnSetAsInterstitial(); |
352 | 448 |
353 // Load a page which contains a script. | 449 // Load a page which contains a script. |
354 std::string html = "<html>" | 450 std::string html = "<html>" |
355 "<head>" | 451 "<head>" |
356 "<script src='data:foo'></script>" | 452 "<script src='data:foo'></script>" |
357 "</head>" | 453 "</head>" |
358 "<body>" | 454 "<body>" |
359 "</body>" | 455 "</body>" |
360 "</html>"; | 456 "</html>"; |
361 LoadHTML(html.c_str()); | 457 LoadHTML(html.c_str()); |
362 | 458 |
363 // Verify that the script was allowed. | 459 // Verify that the script was allowed. |
364 bool was_blocked = false; | 460 bool was_blocked = false; |
365 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 461 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
366 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 462 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
367 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 463 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
368 was_blocked = true; | 464 was_blocked = true; |
369 } | 465 } |
370 EXPECT_FALSE(was_blocked); | 466 EXPECT_FALSE(was_blocked); |
371 | 467 |
372 // Verify that images are allowed. | 468 // Verify that images and media are allowed. |
373 EXPECT_CALL( | 469 EXPECT_CALL( |
374 mock_observer, | 470 mock_observer, |
375 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); | 471 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); |
| 472 EXPECT_CALL( |
| 473 mock_observer, |
| 474 OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIA)).Times(0); |
376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 475 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 476 EXPECT_TRUE(observer->allowMedia(mock_observer.media_url_)); |
| 477 |
| 478 |
377 ::testing::Mock::VerifyAndClearExpectations(&observer); | 479 ::testing::Mock::VerifyAndClearExpectations(&observer); |
378 } | 480 } |
OLD | NEW |