OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager_factory.h" | 5 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager_factory.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
8 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
| 12 #include "base/test/simple_test_clock.h" |
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 14 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 15 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager.h" |
11 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
15 | 21 |
16 namespace { | 22 namespace { |
17 | 23 |
18 const char kActionsHistogram[] = "SubresourceFilter.Actions"; | 24 const char kActionsHistogram[] = "SubresourceFilter.Actions"; |
19 | 25 |
20 class SubresourceFilterContentSettingsManagerFactoryTest | 26 class SubresourceFilterContentSettingsManagerFactoryTest |
21 : public testing::Test { | 27 : public testing::Test { |
22 public: | 28 public: |
23 SubresourceFilterContentSettingsManagerFactoryTest() {} | 29 SubresourceFilterContentSettingsManagerFactoryTest() {} |
24 | 30 |
25 void SetUp() override { | 31 void SetUp() override { |
26 SubresourceFilterContentSettingsManagerFactory::EnsureForProfile( | 32 settings_manager_ = |
27 &testing_profile_); | 33 SubresourceFilterContentSettingsManagerFactory::EnsureForProfile( |
| 34 &testing_profile_); |
| 35 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 36 test_clock_ = test_clock.get(); |
| 37 settings_manager_->set_clock_for_testing(std::move(test_clock)); |
28 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 38 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
29 } | 39 } |
30 | 40 |
31 HostContentSettingsMap* GetSettingsMap() { | 41 HostContentSettingsMap* GetSettingsMap() { |
32 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); | 42 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); |
33 } | 43 } |
34 | 44 |
35 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 45 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
36 | 46 |
| 47 SubresourceFilterContentSettingsManager* settings_manager() { |
| 48 return settings_manager_; |
| 49 } |
| 50 |
| 51 ContentSetting GetContentSettingMatchingUrlExactly(const GURL& url) { |
| 52 ContentSettingsForOneType host_settings; |
| 53 GetSettingsMap()->GetSettingsForOneType( |
| 54 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 55 std::string(), &host_settings); |
| 56 GURL url_with_empty_path = url.GetWithEmptyPath(); |
| 57 for (const auto& it : host_settings) { |
| 58 // Need GURL conversion to get rid of unnecessary default ports. |
| 59 if (GURL(it.primary_pattern.ToString()) == url_with_empty_path) |
| 60 return it.setting; |
| 61 } |
| 62 return CONTENT_SETTING_DEFAULT; |
| 63 } |
| 64 |
| 65 base::SimpleTestClock* test_clock() { return test_clock_; } |
| 66 |
37 private: | 67 private: |
38 content::TestBrowserThreadBundle thread_bundle_; | 68 content::TestBrowserThreadBundle thread_bundle_; |
39 TestingProfile testing_profile_; | 69 TestingProfile testing_profile_; |
40 base::HistogramTester histogram_tester_; | 70 base::HistogramTester histogram_tester_; |
41 | 71 |
| 72 // Owned by the testing_profile_. |
| 73 SubresourceFilterContentSettingsManager* settings_manager_ = nullptr; |
| 74 |
| 75 // Owned by the settings_manager_. |
| 76 base::SimpleTestClock* test_clock_ = nullptr; |
| 77 |
42 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerFactoryTest); | 78 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerFactoryTest); |
43 }; | 79 }; |
44 | 80 |
45 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, IrrelevantSetting) { | 81 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, IrrelevantSetting) { |
46 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 82 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
47 CONTENT_SETTING_ALLOW); | 83 CONTENT_SETTING_ALLOW); |
48 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 84 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
49 } | 85 } |
50 | 86 |
51 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, DefaultSetting) { | 87 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, DefaultSetting) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 128 |
93 GetSettingsMap()->SetContentSettingCustomScope( | 129 GetSettingsMap()->SetContentSettingCustomScope( |
94 primary_pattern, secondary_pattern, | 130 primary_pattern, secondary_pattern, |
95 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), | 131 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
96 CONTENT_SETTING_ALLOW); | 132 CONTENT_SETTING_ALLOW); |
97 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); | 133 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); |
98 histogram_tester().ExpectBucketCount(kActionsHistogram, | 134 histogram_tester().ExpectBucketCount(kActionsHistogram, |
99 kActionContentSettingsWildcardUpdate, 2); | 135 kActionContentSettingsWildcardUpdate, 2); |
100 } | 136 } |
101 | 137 |
| 138 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, SmartUI) { |
| 139 if (!settings_manager()->should_use_smart_ui()) |
| 140 return; |
| 141 |
| 142 GURL url("https://example.test/"); |
| 143 GURL url2("https://example.test/path"); |
| 144 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 145 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 146 |
| 147 EXPECT_EQ(CONTENT_SETTING_DEFAULT, GetContentSettingMatchingUrlExactly(url)); |
| 148 settings_manager()->OnDidShowUI(url); |
| 149 |
| 150 // Should explicitly set this origin to Allow, but not log any metrics. |
| 151 EXPECT_EQ(CONTENT_SETTING_ALLOW, GetContentSettingMatchingUrlExactly(url)); |
| 152 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 153 kActionContentSettingsAllowed, 0); |
| 154 |
| 155 // Subsequent navigations to same-domains should not show UI. |
| 156 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| 157 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); |
| 158 |
| 159 // Showing the UI should trigger a forced content setting update, but no |
| 160 // metrics should be recorded. |
| 161 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 162 kActionContentSettingsAllowed, 0); |
| 163 |
| 164 // Fast forward the clock. |
| 165 test_clock()->Advance( |
| 166 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain); |
| 167 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 168 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); |
| 169 } |
| 170 |
| 171 // If the user manually sets a content setting to block the feature, the smart |
| 172 // UI should be reset. |
| 173 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, |
| 174 SmartUIWithOverride_Resets) { |
| 175 if (!settings_manager()->should_use_smart_ui()) |
| 176 return; |
| 177 |
| 178 GURL url("https://example.test/"); |
| 179 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 180 |
| 181 settings_manager()->OnDidShowUI(url); |
| 182 |
| 183 // Subsequent navigations to same-domains should not show UI. |
| 184 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); |
| 185 |
| 186 // The user changed their mind, make sure the feature is showing up in the |
| 187 // settings UI. i.e. the setting should be non-default. |
| 188 EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetContentSetting(url)); |
| 189 GetSettingsMap()->SetContentSettingDefaultScope( |
| 190 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| 191 CONTENT_SETTING_BLOCK); |
| 192 |
| 193 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 194 kActionContentSettingsBlocked, 1); |
| 195 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 196 kActionContentSettingsBlockedFromUI, 0); |
| 197 histogram_tester().ExpectBucketCount( |
| 198 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 1); |
| 199 |
| 200 // Smart UI should be reset. |
| 201 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 202 } |
| 203 |
| 204 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, |
| 205 OnlyLogUserInitiatedChanges) { |
| 206 GURL url("https://example.test/"); |
| 207 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); |
| 208 |
| 209 settings_manager()->OnDidShowUI(url); |
| 210 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| 211 |
| 212 // Simulate changing the setting via the infobar UI. |
| 213 settings_manager()->SetContentSetting(url, CONTENT_SETTING_BLOCK, |
| 214 true /* from_ui */); |
| 215 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 216 kActionContentSettingsBlocked, 1); |
| 217 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 218 kActionContentSettingsBlockedFromUI, 1); |
| 219 histogram_tester().ExpectBucketCount( |
| 220 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 0); |
| 221 |
| 222 GetSettingsMap()->SetContentSettingDefaultScope( |
| 223 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| 224 CONTENT_SETTING_ALLOW); |
| 225 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 226 kActionContentSettingsAllowed, 1); |
| 227 |
| 228 // Simulate de-activation of the site. Should not log any metrics. |
| 229 settings_manager()->ClearContentSetting(url); |
| 230 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 231 kActionContentSettingsWildcardUpdate, 0); |
| 232 } |
| 233 |
102 } // namespace | 234 } // namespace |
OLD | NEW |