Chromium Code Reviews| Index: chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc |
| diff --git a/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc b/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc |
| index e588e89cf7d50041312470ceaa33352a71c0cab9..a46a6bb8f30d33141f7295af9b7d9212fc3c6026 100644 |
| --- a/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc |
| +++ b/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_unittest.cc |
| @@ -4,15 +4,29 @@ |
| #include "chrome/browser/subresource_filter/subresource_filter_content_settings_manager.h" |
| +#include <set> |
| +#include <utility> |
| + |
| #include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "base/task/cancelable_task_tracker.h" |
| #include "base/test/histogram_tester.h" |
| +#include "base/test/simple_test_clock.h" |
| +#include "base/time/time.h" |
| #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| +#include "chrome/browser/history/history_service_factory.h" |
| #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| +#include "chrome/browser/subresource_filter/subresource_filter_profile_context.h" |
| #include "chrome/browser/subresource_filter/subresource_filter_profile_context_factory.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/content_settings/core/browser/host_content_settings_map.h" |
| +#include "components/content_settings/core/common/content_settings.h" |
| +#include "components/history/core/browser/history_service.h" |
| +#include "components/history/core/test/history_service_test_util.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| namespace { |
| @@ -23,24 +37,71 @@ class SubresourceFilterContentSettingsManagerTest : public testing::Test { |
| SubresourceFilterContentSettingsManagerTest() {} |
| void SetUp() override { |
| - SubresourceFilterProfileContextFactory::EnsureForProfile(&testing_profile_); |
| + settings_manager_ = |
| + SubresourceFilterProfileContextFactory::GetForProfile(&testing_profile_) |
| + ->settings_manager(); |
| + auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| + test_clock_ = test_clock.get(); |
| + settings_manager_->set_clock_for_testing(std::move(test_clock)); |
| histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| } |
| HostContentSettingsMap* GetSettingsMap() { |
| - return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); |
| + return HostContentSettingsMapFactory::GetForProfile(profile()); |
| } |
| const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| + SubresourceFilterContentSettingsManager* settings_manager() { |
| + return settings_manager_; |
| + } |
| + |
| + TestingProfile* profile() { return &testing_profile_; } |
| + |
| + ContentSetting GetContentSettingMatchingUrlExactly(const GURL& url) { |
|
msramek
2017/05/08 16:40:26
nit: The name is a bit misleading, because no patt
Charlie Harrison
2017/05/08 20:57:21
Renamed to GetContentSettingMatchingUrlWithEmptyPa
|
| + ContentSettingsForOneType host_settings; |
| + GetSettingsMap()->GetSettingsForOneType( |
| + ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| + std::string(), &host_settings); |
| + GURL url_with_empty_path = url.GetWithEmptyPath(); |
| + for (const auto& it : host_settings) { |
| + // Need GURL conversion to get rid of unnecessary default ports. |
| + if (GURL(it.primary_pattern.ToString()) == url_with_empty_path) |
| + return it.setting; |
| + } |
| + return CONTENT_SETTING_DEFAULT; |
| + } |
| + |
| + base::SimpleTestClock* test_clock() { return test_clock_; } |
| + |
| private: |
| + base::ScopedTempDir scoped_dir_; |
| + |
| content::TestBrowserThreadBundle thread_bundle_; |
| TestingProfile testing_profile_; |
| base::HistogramTester histogram_tester_; |
| + // Owned by the testing_profile_. |
| + SubresourceFilterContentSettingsManager* settings_manager_ = nullptr; |
| + |
| + // Owned by the settings_manager_. |
| + base::SimpleTestClock* test_clock_ = nullptr; |
| + |
| DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerTest); |
| }; |
| +// It isn't very cheap to initialize the history service. Tests that need it can |
| +// use this harness. |
| +class SubresourceFilterContentSettingsManagerHistoryTest |
| + : public SubresourceFilterContentSettingsManagerTest { |
| + public: |
| + void SetUp() override { |
| + ASSERT_TRUE(profile()->CreateHistoryService(true /* delete_file */, |
| + false /* no_db */)); |
| + SubresourceFilterContentSettingsManagerTest::SetUp(); |
| + } |
| +}; |
| + |
| TEST_F(SubresourceFilterContentSettingsManagerTest, IrrelevantSetting) { |
| GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| CONTENT_SETTING_ALLOW); |
| @@ -98,4 +159,162 @@ TEST_F(SubresourceFilterContentSettingsManagerTest, WildcardUpdate) { |
| kActionContentSettingsWildcardUpdate, 2); |
| } |
| +TEST_F(SubresourceFilterContentSettingsManagerTest, SmartUI) { |
| + if (!settings_manager()->should_use_smart_ui()) |
| + return; |
| + |
| + GURL url("https://example.test/"); |
| + GURL url2("https://example.test/path"); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| + |
| + EXPECT_EQ(CONTENT_SETTING_DEFAULT, GetContentSettingMatchingUrlExactly(url)); |
| + settings_manager()->OnDidShowUI(url); |
| + |
| + // Subsequent navigations to same-domains should not show UI. |
|
msramek
2017/05/08 16:40:26
nit: Same domain or same origin? The website setti
Charlie Harrison
2017/05/08 20:57:21
Same origin, sorry for lack of precision.
|
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); |
| + |
| + // Showing the UI should trigger a forced content setting update, but no |
| + // metrics should be recorded. |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsAllowed, 0); |
| + |
| + // Fast forward the clock. |
| + test_clock()->Advance( |
| + SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| +} |
| + |
| +// If the user manually sets a content setting to block the feature, the smart |
| +// UI should be reset. |
| +TEST_F(SubresourceFilterContentSettingsManagerTest, |
| + SmartUIWithOverride_Resets) { |
| + if (!settings_manager()->should_use_smart_ui()) |
| + return; |
| + |
| + GURL url("https://example.test/"); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| + |
| + settings_manager()->OnDidShowUI(url); |
| + |
| + // Subsequent navigations to same-domains should not show UI. |
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| + |
| + // The user changed their mind, make sure the feature is showing up in the |
| + // settings UI. i.e. the setting should be non-default. |
| + EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetSitePermission(url)); |
| + GetSettingsMap()->SetContentSettingDefaultScope( |
| + url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| + CONTENT_SETTING_BLOCK); |
| + |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsBlocked, 1); |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsBlockedFromUI, 0); |
| + histogram_tester().ExpectBucketCount( |
| + kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 1); |
| + |
| + // Smart UI should be reset. |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| +} |
| + |
| +TEST_F(SubresourceFilterContentSettingsManagerTest, |
| + OnlyLogUserInitiatedChanges) { |
| + GURL url("https://example.test/"); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| + |
| + settings_manager()->OnDidShowUI(url); |
| + histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| + |
| + // Simulate changing the setting via the infobar UI. |
| + settings_manager()->SetSitePermission(url, CONTENT_SETTING_BLOCK, |
| + true /* from_ui */); |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsBlockedFromUI, 1); |
| + |
| + // The standard "Block" histograms are only triggered when blocking from the |
| + // settings UI, not our standard UI. |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsBlocked, 0); |
| + histogram_tester().ExpectBucketCount( |
| + kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 0); |
| + |
| + GetSettingsMap()->SetContentSettingDefaultScope( |
| + url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| + CONTENT_SETTING_ALLOW); |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsAllowed, 1); |
| + |
| + // Simulate de-activation of the site. Should not log any metrics. |
| + settings_manager()->SetSitePermission(url, CONTENT_SETTING_DEFAULT, |
| + false /* from_ui */); |
| + histogram_tester().ExpectBucketCount(kActionsHistogram, |
| + kActionContentSettingsWildcardUpdate, 0); |
| +} |
| + |
| +TEST_F(SubresourceFilterContentSettingsManagerHistoryTest, |
| + HistoryUrlDeleted_ClearsWebsiteSetting) { |
| + if (!settings_manager()->should_use_smart_ui()) |
| + return; |
| + |
| + auto* history_service = HistoryServiceFactory::GetForProfile( |
| + profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| + ASSERT_TRUE(history_service); |
| + |
| + // Ensure the website setting is set. |
| + GURL url("https://example.test"); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| + settings_manager()->OnDidShowUI(url); |
| + |
| + // Simulate adding the page to the history. |
| + history_service->AddPage(url, base::Time::Now(), history::SOURCE_BROWSED); |
|
msramek
2017/05/08 16:40:26
Please add one more URL, otherwise this test canno
|
| + history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| + |
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| + |
| + // Deleting the URL from history should clear the setting for this URL |
| + history_service->DeleteURL(url); |
| + history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| + |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| +} |
| + |
| +TEST_F(SubresourceFilterContentSettingsManagerHistoryTest, |
| + AllHistoryUrlDeleted_ClearsWebsiteSetting) { |
| + if (!settings_manager()->should_use_smart_ui()) |
| + return; |
| + |
| + auto* history_service = HistoryServiceFactory::GetForProfile( |
| + profile(), ServiceAccessType::EXPLICIT_ACCESS); |
| + ASSERT_TRUE(history_service); |
| + |
| + GURL url1("https://example.test"); |
| + GURL url2("https://example.test"); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1)); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| + settings_manager()->OnDidShowUI(url1); |
| + settings_manager()->OnDidShowUI(url2); |
| + |
| + // Simulate adding the pages to the history. |
| + history_service->AddPage(url1, base::Time::Now(), history::SOURCE_BROWSED); |
| + history_service->AddPage(url2, base::Time::Now(), history::SOURCE_BROWSED); |
| + history::BlockUntilHistoryProcessesPendingRequests(history_service); |
| + |
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url1)); |
| + EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); |
| + |
| + // Deleting all the URLs should clear everything. |
| + base::RunLoop run_loop; |
| + base::CancelableTaskTracker task_tracker; |
| + history_service->ExpireHistoryBetween(std::set<GURL>(), base::Time(), |
| + base::Time(), run_loop.QuitClosure(), |
| + &task_tracker); |
| + run_loop.Run(); |
| + |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url1)); |
| + EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| +} |
| + |
| } // namespace |