Chromium Code Reviews| Index: chrome/browser/subresource_filter/subresource_filter_browsertest.cc |
| diff --git a/chrome/browser/subresource_filter/subresource_filter_browsertest.cc b/chrome/browser/subresource_filter/subresource_filter_browsertest.cc |
| index 9aa2ddb77d7944964cc57c1930c8df379610699c..3184b8b9b5f1ac50206bd43aaf0d98bb00945510 100644 |
| --- a/chrome/browser/subresource_filter/subresource_filter_browsertest.cc |
| +++ b/chrome/browser/subresource_filter/subresource_filter_browsertest.cc |
| @@ -18,6 +18,7 @@ |
| #include "base/strings/string_util.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/test/histogram_tester.h" |
| +#include "base/test/simple_test_clock.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
| @@ -25,6 +26,8 @@ |
| #include "chrome/browser/safe_browsing/test_safe_browsing_service.h" |
| #include "chrome/browser/safe_browsing/v4_test_utils.h" |
| #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| +#include "chrome/browser/subresource_filter/subresource_filter_content_settings_manager.h" |
| +#include "chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory.h" |
| #include "chrome/browser/subresource_filter/test_ruleset_publisher.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_commands.h" |
| @@ -273,6 +276,13 @@ class SubresourceFilterBrowserTestImpl : public InProcessBrowserTest { |
| host_resolver()->AddRule("*", "127.0.0.1"); |
| content::SetupCrossSiteRedirector(embedded_test_server()); |
| ASSERT_TRUE(embedded_test_server()->Start()); |
| + |
| + settings_manager_ = |
| + SubresourceFilterContentSettingsManagerFactory::EnsureForProfile( |
| + browser()->profile()); |
| +#if defined(OS_ANDROID) |
| + EXPECT_TRUE(settings_manager->should_use_smart_ui()); |
| +#endif |
| } |
| virtual void SetUpActivationFeature() { |
| @@ -311,6 +321,10 @@ class SubresourceFilterBrowserTestImpl : public InProcessBrowserTest { |
| return browser()->tab_strip_model()->GetActiveWebContents(); |
| } |
| + SubresourceFilterContentSettingsManager* settings_manager() { |
| + return settings_manager_; |
| + } |
| + |
| content::RenderFrameHost* FindFrameByName(const std::string& name) { |
| for (content::RenderFrameHost* frame : web_contents()->GetAllFrames()) { |
| if (frame->GetFrameName() == name) |
| @@ -400,6 +414,9 @@ class SubresourceFilterBrowserTestImpl : public InProcessBrowserTest { |
| // Owned by the V4GetHashProtocolManager. |
| safe_browsing::TestV4GetHashProtocolManagerFactory* v4_get_hash_factory_; |
| + // Owned by the profile. |
| + SubresourceFilterContentSettingsManager* settings_manager_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(SubresourceFilterBrowserTestImpl); |
| }; |
| @@ -841,6 +858,8 @@ IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| ASSERT_NO_FATAL_FAILURE( |
| SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| GURL url(GetTestUrl(kTestFrameSetPath)); |
| + GURL a_url(embedded_test_server()->GetURL( |
| + "a.com", "/subresource_filter/frame_with_included_script.html")); |
| ConfigureAsPhishingURL(url); |
| base::HistogramTester tester; |
| ui_test_utils::NavigateToURL(browser(), url); |
| @@ -850,10 +869,12 @@ IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| EXPECT_FALSE(IsDynamicScriptElementLoaded(FindFrameByName("five"))); |
| tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| 1); |
| - // Check that bubble is shown for new navigation. |
| - ui_test_utils::NavigateToURL(browser(), url); |
| + // Check that bubble is shown for new navigation. Must be cross site to avoid |
| + // triggering smart UI on Android. |
| + ConfigureAsPhishingURL(a_url); |
| + ui_test_utils::NavigateToURL(browser(), a_url); |
| tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, kActionUIShown, |
| - 2); |
| + settings_manager()->should_use_smart_ui() ? 1 : 2); |
|
engedy
2017/04/12 14:02:50
I'm not sure I understand. If it's cross site, sho
Charlie Harrison
2017/04/12 17:53:44
Yeah, this is an error.
|
| } |
| IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| @@ -982,6 +1003,109 @@ IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| } |
| +IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| + SiteDeactivated_ContentSettingCleared) { |
| + HostContentSettingsMap* settings_map = |
| + HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| + ContentSettingsForOneType host_settings; |
| + settings_map->GetSettingsForOneType( |
| + ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| + std::string(), &host_settings); |
| + |
| + // Originally should just have a single default rule: * -> Allowed. |
| + EXPECT_EQ(1u, host_settings.size()); |
| + |
| + ASSERT_NO_FATAL_FAILURE( |
| + SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| + GURL url(GetTestUrl("subresource_filter/frame_with_included_script.html")); |
| + ConfigureAsPhishingURL(url); |
| + |
| + ui_test_utils::NavigateToURL(browser(), url); |
| + EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetContentSetting(url)); |
|
engedy
2017/04/12 14:02:50
nit: Can you please add an expectation that this i
Charlie Harrison
2017/04/12 17:53:44
Done, it's still just CONTENT_SETTING_ALLOW.
|
| + |
| + // Should have two rules now, one explicitly setting the origin of |url| to |
| + // Allow. |
| + settings_map->GetSettingsForOneType( |
| + ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| + std::string(), &host_settings); |
| + EXPECT_EQ(2u, host_settings.size()); |
| + |
| + // Configure a different URL as the phishing URL, which resets |url|. |
| + ConfigureAsPhishingURL(GURL("https://example.other/")); |
|
engedy
2017/04/12 14:02:50
Does this really work? I thought we would just hav
Charlie Harrison
2017/04/12 17:53:44
Yes this is how the test utils are currently desig
|
| + |
| + // Re-navigating to |url| should reset the content setting. |
| + ui_test_utils::NavigateToURL(browser(), url); |
| + settings_map->GetSettingsForOneType( |
| + ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| + std::string(), &host_settings); |
| + EXPECT_EQ(1u, host_settings.size()); |
| + EXPECT_TRUE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| +} |
| + |
| +// Test the "smart" UI, aka the logic to hide the UI on subsequent same-domain |
| +// navigations, until a certain time threshold has been reached. This is an |
| +// android-only feature. |
| +IN_PROC_BROWSER_TEST_F(SubresourceFilterBrowserTest, |
| + DoNotShowUIUntilThresholdReached) { |
| + ASSERT_NO_FATAL_FAILURE( |
| + SetRulesetToDisallowURLsWithPathSuffix("included_script.js")); |
| + GURL a_url(embedded_test_server()->GetURL( |
| + "a.com", "/subresource_filter/frame_with_included_script.html")); |
| + GURL b_url(embedded_test_server()->GetURL( |
| + "b.com", "/subresource_filter/frame_with_included_script.html")); |
| + // Test utils only support one blacklisted site at a time. |
| + // TODO(csharrison): Add support for more than one URL. |
| + ConfigureAsPhishingURL(a_url); |
| + |
| + // Cast is safe because this is the only type of client in non-unittest code. |
| + ChromeSubresourceFilterClient* client = |
| + static_cast<ChromeSubresourceFilterClient*>( |
| + ContentSubresourceFilterDriverFactory::FromWebContents(web_contents()) |
| + ->client()); |
| + auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| + base::SimpleTestClock* raw_clock = test_clock.get(); |
| + settings_manager()->set_clock_for_testing(std::move(test_clock)); |
| + |
| + base::HistogramTester histogram_tester; |
| + |
| + // First load should trigger the UI. |
| + ui_test_utils::NavigateToURL(browser(), a_url); |
| + EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| + EXPECT_TRUE(client->shown_for_navigation()); |
| + |
| + histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| + kActionUISuppressed, 0); |
| + |
| + // Second load should not trigger the UI, but should still filter content. |
| + ui_test_utils::NavigateToURL(browser(), a_url); |
| + EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| + |
| + bool use_smart_ui = settings_manager()->should_use_smart_ui(); |
| + EXPECT_EQ(client->shown_for_navigation(), !use_smart_ui); |
| + |
| + histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| + kActionUISuppressed, use_smart_ui ? 1 : 0); |
| + |
| + ConfigureAsPhishingURL(b_url); |
| + |
| + // Load to another domain should trigger the UI. |
| + ui_test_utils::NavigateToURL(browser(), b_url); |
| + EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| + EXPECT_TRUE(client->shown_for_navigation()); |
| + |
| + ConfigureAsPhishingURL(a_url); |
| + |
| + // Fast forward the clock, and a_url should trigger the UI again. |
| + raw_clock->Advance( |
| + SubresourceFilterContentSettingsManager::kUIShowThresholdTime); |
| + ui_test_utils::NavigateToURL(browser(), a_url); |
| + EXPECT_FALSE(WasParsedScriptElementLoaded(web_contents()->GetMainFrame())); |
| + EXPECT_TRUE(client->shown_for_navigation()); |
| + |
| + histogram_tester.ExpectBucketCount(kSubresourceFilterActionsHistogram, |
| + kActionUISuppressed, 1); |
|
engedy
2017/04/12 14:02:50
Don't we need to make this conditional on |use_sma
Charlie Harrison
2017/04/12 17:53:44
Yes, done. Sorry there were a bunch of mistakes, i
|
| +} |
| + |
| IN_PROC_BROWSER_TEST_P(SubresourceFilterWebSocketBrowserTest, BlockWebSocket) { |
| GURL url(GetTestUrl( |
| base::StringPrintf("subresource_filter/page_with_websocket.html?%s", |