Chromium Code Reviews| Index: chrome/renderer/content_settings_observer_browsertest.cc |
| diff --git a/chrome/renderer/content_settings_observer_browsertest.cc b/chrome/renderer/content_settings_observer_browsertest.cc |
| index d57647d7f56cd9e51d711fe62060a5b9a332d137..61aa9d578e0b2967ca196e2e90f7f7c41c643f36 100644 |
| --- a/chrome/renderer/content_settings_observer_browsertest.cc |
| +++ b/chrome/renderer/content_settings_observer_browsertest.cc |
| @@ -13,6 +13,7 @@ |
| #include "ipc/ipc_message_macros.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/WebKit/public/web/WebFrameContentDumper.h" |
| #include "third_party/WebKit/public/web/WebView.h" |
| using testing::_; |
| @@ -324,6 +325,92 @@ TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| EXPECT_FALSE(was_blocked); |
| } |
| +// Regression test for crbug.com/232410: Load a page with JS blocked. Then, |
| +// allow JS and reload the page. In each case, only one of noscript or script |
| +// tags should be enabled, but never both. |
| +TEST_F(ChromeRenderViewTest, ContentSettingsNoscriptTag) { |
| + // 1. Block JavaScript. |
| + RendererContentSettingRules content_setting_rules; |
| + ContentSettingsForOneType& script_setting_rules = |
| + content_setting_rules.script_rules; |
| + script_setting_rules.push_back(ContentSettingPatternSource( |
| + ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), |
| + CONTENT_SETTING_BLOCK, std::string(), false)); |
| + |
| + ContentSettingsObserver* observer = |
| + ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| + observer->SetContentSettingRules(&content_setting_rules); |
| + |
| + // 2. Load a page which contains a noscript tag and a script tag. Note that |
| + // the page doesn't have a body tag. |
| + 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.
|
| + "<html>" |
| + "<noscript>JS_DISABLED</noscript>" |
| + "<script>document.write('JS_ENABLED');</script>" |
| + "</html>"; |
| + LoadHTML(html.c_str()); |
| + EXPECT_EQ("JS_DISABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText( |
| + GetMainFrame(), 1024) |
| + .utf8()); |
| + |
| + // 3. Allow JavaScript. |
| + 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.
|
| + script_setting_rules.push_back(ContentSettingPatternSource( |
| + ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), |
| + CONTENT_SETTING_ALLOW, std::string(), false)); |
| + observer->SetContentSettingRules(&content_setting_rules); |
| + |
| + // 4. Reload the page. |
| + std::string url_str = "data:text/html;charset=utf-8,"; |
| + url_str.append(html); |
| + GURL url(url_str); |
| + Reload(url); |
| + EXPECT_EQ("JS_ENABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText( |
| + GetMainFrame(), 1024) |
| + .utf8()); |
| +} |
| + |
| +// Checks that same page navigations don't update content settings for the page. |
| +TEST_F(ChromeRenderViewTest, ContentSettingsSamePageNavigation) { |
| + MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| + // Load a page which contains a script. |
| + std::string html = |
| + "<html>" |
| + "<head>" |
| + "<script src='data:foo'></script>" |
| + "</head>" |
| + "<body>" |
| + "</body>" |
| + "</html>"; |
| + LoadHTML(html.c_str()); |
| + |
| + // Verify that the script was not blocked. |
| + bool was_blocked = false; |
| + for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| + const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| + if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| + was_blocked = true; |
| + } |
| + EXPECT_FALSE(was_blocked); |
| + |
| + // Block JavaScript. |
| + RendererContentSettingRules content_setting_rules; |
| + ContentSettingsForOneType& script_setting_rules = |
| + content_setting_rules.script_rules; |
| + script_setting_rules.push_back(ContentSettingPatternSource( |
| + ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(), |
| + CONTENT_SETTING_BLOCK, std::string(), false)); |
| + |
| + ContentSettingsObserver* observer = |
| + ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| + observer->SetContentSettingRules(&content_setting_rules); |
| + |
| + // The page shouldn't see the change to script blocking setting after a |
| + // same page navigation. |
| + DidNavigateWithinPage(GetMainFrame(), true); |
| + EXPECT_TRUE(observer->allowScript(true)); |
| +} |
| + |
| TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { |
| MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| // Block scripts. |