| Index: chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory_unittest.cc
|
| diff --git a/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory_unittest.cc b/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory_unittest.cc
|
| index 39f683b327f88c13b7b23449010f08aa543914e8..3216429df3773db63019247d2ad6040e7c0a7d08 100644
|
| --- a/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory_unittest.cc
|
| +++ b/chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory_unittest.cc
|
| @@ -4,12 +4,18 @@
|
|
|
| #include "chrome/browser/subresource_filter/subresource_filter_content_settings_manager_factory.h"
|
|
|
| +#include <utility>
|
| +
|
| #include "base/macros.h"
|
| +#include "base/memory/ptr_util.h"
|
| #include "base/test/histogram_tester.h"
|
| +#include "base/test/simple_test_clock.h"
|
| #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
|
| #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h"
|
| +#include "chrome/browser/subresource_filter/subresource_filter_content_settings_manager.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 "content/public/test/test_browser_thread_bundle.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| @@ -23,8 +29,12 @@ class SubresourceFilterContentSettingsManagerFactoryTest
|
| SubresourceFilterContentSettingsManagerFactoryTest() {}
|
|
|
| void SetUp() override {
|
| - SubresourceFilterContentSettingsManagerFactory::EnsureForProfile(
|
| - &testing_profile_);
|
| + settings_manager_ =
|
| + SubresourceFilterContentSettingsManagerFactory::EnsureForProfile(
|
| + &testing_profile_);
|
| + 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);
|
| }
|
|
|
| @@ -34,11 +44,37 @@ class SubresourceFilterContentSettingsManagerFactoryTest
|
|
|
| const base::HistogramTester& histogram_tester() { return histogram_tester_; }
|
|
|
| + SubresourceFilterContentSettingsManager* settings_manager() {
|
| + return settings_manager_;
|
| + }
|
| +
|
| + ContentSetting GetContentSettingMatchingUrlExactly(const GURL& url) {
|
| + 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:
|
| 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(SubresourceFilterContentSettingsManagerFactoryTest);
|
| };
|
|
|
| @@ -99,4 +135,100 @@ TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, WildcardUpdate) {
|
| kActionContentSettingsWildcardUpdate, 2);
|
| }
|
|
|
| +TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, 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);
|
| +
|
| + // Should explicitly set this origin to Allow, but not log any metrics.
|
| + EXPECT_EQ(CONTENT_SETTING_ALLOW, GetContentSettingMatchingUrlExactly(url));
|
| + histogram_tester().ExpectBucketCount(kActionsHistogram,
|
| + kActionContentSettingsAllowed, 0);
|
| +
|
| + // Subsequent navigations to same-domains should not show UI.
|
| + 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(SubresourceFilterContentSettingsManagerFactoryTest,
|
| + 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()->GetContentSetting(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(SubresourceFilterContentSettingsManagerFactoryTest,
|
| + 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()->SetContentSetting(url, CONTENT_SETTING_BLOCK,
|
| + true /* from_ui */);
|
| + histogram_tester().ExpectBucketCount(kActionsHistogram,
|
| + kActionContentSettingsBlocked, 1);
|
| + histogram_tester().ExpectBucketCount(kActionsHistogram,
|
| + kActionContentSettingsBlockedFromUI, 1);
|
| + 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()->ClearContentSetting(url);
|
| + histogram_tester().ExpectBucketCount(kActionsHistogram,
|
| + kActionContentSettingsWildcardUpdate, 0);
|
| +}
|
| +
|
| } // namespace
|
|
|