Chromium Code Reviews| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
| 8 #include "chrome/renderer/content_settings_observer.h" | 8 #include "chrome/renderer/content_settings_observer.h" |
| 9 #include "chrome/test/base/chrome_render_view_test.h" | 9 #include "chrome/test/base/chrome_render_view_test.h" |
| 10 #include "components/content_settings/content/common/content_settings_messages.h " | 10 #include "components/content_settings/content/common/content_settings_messages.h " |
| 11 #include "components/content_settings/core/common/content_settings.h" | 11 #include "components/content_settings/core/common/content_settings.h" |
| 12 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
| 13 #include "ipc/ipc_message_macros.h" | 13 #include "ipc/ipc_message_macros.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/web/WebFrameContentDumper.h" | |
| 16 #include "third_party/WebKit/public/web/WebView.h" | 17 #include "third_party/WebKit/public/web/WebView.h" |
| 17 | 18 |
| 18 using testing::_; | 19 using testing::_; |
| 19 using testing::DeleteArg; | 20 using testing::DeleteArg; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class MockContentSettingsObserver : public ContentSettingsObserver { | 24 class MockContentSettingsObserver : public ContentSettingsObserver { |
| 24 public: | 25 public: |
| 25 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); | 26 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 // Verify that the script was not blocked. | 318 // Verify that the script was not blocked. |
| 318 bool was_blocked = false; | 319 bool was_blocked = false; |
| 319 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 320 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 320 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 321 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 321 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 322 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 322 was_blocked = true; | 323 was_blocked = true; |
| 323 } | 324 } |
| 324 EXPECT_FALSE(was_blocked); | 325 EXPECT_FALSE(was_blocked); |
| 325 } | 326 } |
| 326 | 327 |
| 328 // Regression test for crbug.com/232410: Load a page with JS blocked. Then, | |
| 329 // allow JS and reload the page. In each case, only one of noscript or script | |
| 330 // tags should be enabled, but never both. | |
| 331 TEST_F(ChromeRenderViewTest, ContentSettingsNoscriptTag) { | |
| 332 // 1. Block JavaScript. | |
| 333 RendererContentSettingRules content_setting_rules; | |
| 334 ContentSettingsForOneType& script_setting_rules = | |
| 335 content_setting_rules.script_rules; | |
| 336 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 337 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 338 CONTENT_SETTING_BLOCK, std::string(), false)); | |
| 339 | |
| 340 ContentSettingsObserver* observer = | |
| 341 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | |
| 342 observer->SetContentSettingRules(&content_setting_rules); | |
| 343 | |
| 344 // 2. Load a page which contains a noscript tag and a script tag. Note that | |
| 345 // the page doesn't have a body tag. | |
| 346 std::string html = | |
|
Lei Zhang
2016/03/29 00:13:45
Can this just be const char kHtml[] ?
meacer
2016/03/30 20:55:08
Done.
| |
| 347 "<html>" | |
| 348 "<noscript>JS_DISABLED</noscript>" | |
| 349 "<script>document.write('JS_ENABLED');</script>" | |
| 350 "</html>"; | |
| 351 LoadHTML(html.c_str()); | |
| 352 EXPECT_EQ("JS_DISABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText( | |
| 353 GetMainFrame(), 1024) | |
| 354 .utf8()); | |
| 355 | |
| 356 // 3. Allow JavaScript. | |
| 357 content_setting_rules.script_rules.clear(); | |
|
Lei Zhang
2016/03/29 00:13:45
Reuse |script_setting_rules| ?
meacer
2016/03/30 20:55:08
Done.
| |
| 358 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 359 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 360 CONTENT_SETTING_ALLOW, std::string(), false)); | |
| 361 observer->SetContentSettingRules(&content_setting_rules); | |
| 362 | |
| 363 // 4. Reload the page. | |
| 364 std::string url_str = "data:text/html;charset=utf-8,"; | |
| 365 url_str.append(html); | |
| 366 GURL url(url_str); | |
| 367 Reload(url); | |
| 368 EXPECT_EQ("JS_ENABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText( | |
| 369 GetMainFrame(), 1024) | |
| 370 .utf8()); | |
| 371 } | |
| 372 | |
| 373 // Checks that same page navigations don't update content settings for the page. | |
| 374 TEST_F(ChromeRenderViewTest, ContentSettingsSamePageNavigation) { | |
| 375 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | |
| 376 // Load a page which contains a script. | |
| 377 std::string html = | |
| 378 "<html>" | |
| 379 "<head>" | |
| 380 "<script src='data:foo'></script>" | |
| 381 "</head>" | |
| 382 "<body>" | |
| 383 "</body>" | |
| 384 "</html>"; | |
| 385 LoadHTML(html.c_str()); | |
| 386 | |
| 387 // Verify that the script was not blocked. | |
| 388 bool was_blocked = false; | |
| 389 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | |
| 390 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | |
| 391 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | |
| 392 was_blocked = true; | |
| 393 } | |
| 394 EXPECT_FALSE(was_blocked); | |
| 395 | |
| 396 // Block JavaScript. | |
| 397 RendererContentSettingRules content_setting_rules; | |
| 398 ContentSettingsForOneType& script_setting_rules = | |
| 399 content_setting_rules.script_rules; | |
| 400 script_setting_rules.push_back(ContentSettingPatternSource( | |
| 401 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), | |
| 402 CONTENT_SETTING_BLOCK, std::string(), false)); | |
| 403 | |
| 404 ContentSettingsObserver* observer = | |
| 405 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | |
| 406 observer->SetContentSettingRules(&content_setting_rules); | |
| 407 | |
| 408 // The page shouldn't see the change to script blocking setting after a | |
| 409 // same page navigation. | |
| 410 DidNavigateWithinPage(GetMainFrame(), true); | |
| 411 EXPECT_TRUE(observer->allowScript(true)); | |
| 412 } | |
| 413 | |
| 327 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { | 414 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { |
| 328 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | 415 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| 329 // Block scripts. | 416 // Block scripts. |
| 330 RendererContentSettingRules content_setting_rules; | 417 RendererContentSettingRules content_setting_rules; |
| 331 ContentSettingsForOneType& script_setting_rules = | 418 ContentSettingsForOneType& script_setting_rules = |
| 332 content_setting_rules.script_rules; | 419 content_setting_rules.script_rules; |
| 333 script_setting_rules.push_back( | 420 script_setting_rules.push_back( |
| 334 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 421 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 335 ContentSettingsPattern::Wildcard(), | 422 ContentSettingsPattern::Wildcard(), |
| 336 CONTENT_SETTING_BLOCK, | 423 CONTENT_SETTING_BLOCK, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 was_blocked = true; | 456 was_blocked = true; |
| 370 } | 457 } |
| 371 EXPECT_FALSE(was_blocked); | 458 EXPECT_FALSE(was_blocked); |
| 372 | 459 |
| 373 // Verify that images are allowed. | 460 // Verify that images are allowed. |
| 374 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, | 461 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 375 base::string16())).Times(0); | 462 base::string16())).Times(0); |
| 376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 463 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 377 ::testing::Mock::VerifyAndClearExpectations(&observer); | 464 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 378 } | 465 } |
| OLD | NEW |