Chromium Code Reviews| 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 "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/test/histogram_tester.h" | 8 #include "base/test/histogram_tester.h" |
| 9 #include "base/test/simple_test_clock.h" | |
| 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 11 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 12 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ manager.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 14 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 const char kActionsHistogram[] = "SubresourceFilter.Actions"; | 20 const char kActionsHistogram[] = "SubresourceFilter.Actions"; |
| 19 | 21 |
| 20 class SubresourceFilterContentSettingsManagerFactoryTest | 22 class SubresourceFilterContentSettingsManagerFactoryTest |
| 21 : public testing::Test { | 23 : public testing::Test { |
| 22 public: | 24 public: |
| 23 SubresourceFilterContentSettingsManagerFactoryTest() {} | 25 SubresourceFilterContentSettingsManagerFactoryTest() {} |
| 24 | 26 |
| 25 void SetUp() override { | 27 void SetUp() override { |
| 26 SubresourceFilterContentSettingsManagerFactory::EnsureForProfile( | 28 settings_manager_ = |
| 27 &testing_profile_); | 29 SubresourceFilterContentSettingsManagerFactory::EnsureForProfile( |
| 30 &testing_profile_); | |
| 31 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); | |
|
engedy
2017/04/12 14:02:51
#include "base/memory/ptr_util.h"
Charlie Harrison
2017/04/12 17:53:45
Done.
| |
| 32 test_clock_ = test_clock.get(); | |
| 33 settings_manager_->set_clock_for_testing(std::move(test_clock)); | |
|
engedy
2017/04/12 14:02:51
#include <utility>
Charlie Harrison
2017/04/12 17:53:45
Done.
| |
| 28 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 34 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| 29 } | 35 } |
| 30 | 36 |
| 31 HostContentSettingsMap* GetSettingsMap() { | 37 HostContentSettingsMap* GetSettingsMap() { |
| 32 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); | 38 return HostContentSettingsMapFactory::GetForProfile(&testing_profile_); |
| 33 } | 39 } |
| 34 | 40 |
| 35 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 41 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| 36 | 42 |
| 43 SubresourceFilterContentSettingsManager* settings_manager() { | |
| 44 return settings_manager_; | |
| 45 } | |
| 46 | |
| 47 base::SimpleTestClock* test_clock() { return test_clock_; } | |
| 48 | |
| 37 private: | 49 private: |
| 38 content::TestBrowserThreadBundle thread_bundle_; | 50 content::TestBrowserThreadBundle thread_bundle_; |
| 39 TestingProfile testing_profile_; | 51 TestingProfile testing_profile_; |
| 40 base::HistogramTester histogram_tester_; | 52 base::HistogramTester histogram_tester_; |
| 41 | 53 |
| 54 // Owned by the testing_profile_. | |
| 55 SubresourceFilterContentSettingsManager* settings_manager_ = nullptr; | |
| 56 | |
| 57 // Owned by the settings_manager_. | |
| 58 base::SimpleTestClock* test_clock_ = nullptr; | |
| 59 | |
| 42 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerFactoryTest); | 60 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsManagerFactoryTest); |
| 43 }; | 61 }; |
| 44 | 62 |
| 45 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, IrrelevantSetting) { | 63 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, IrrelevantSetting) { |
| 46 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 64 GetSettingsMap()->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 47 CONTENT_SETTING_ALLOW); | 65 CONTENT_SETTING_ALLOW); |
| 48 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); | 66 histogram_tester().ExpectTotalCount(kActionsHistogram, 0); |
| 49 } | 67 } |
| 50 | 68 |
| 51 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, DefaultSetting) { | 69 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, DefaultSetting) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 110 |
| 93 GetSettingsMap()->SetContentSettingCustomScope( | 111 GetSettingsMap()->SetContentSettingCustomScope( |
| 94 primary_pattern, secondary_pattern, | 112 primary_pattern, secondary_pattern, |
| 95 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), | 113 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, std::string(), |
| 96 CONTENT_SETTING_ALLOW); | 114 CONTENT_SETTING_ALLOW); |
| 97 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); | 115 histogram_tester().ExpectTotalCount(kActionsHistogram, 2); |
| 98 histogram_tester().ExpectBucketCount(kActionsHistogram, | 116 histogram_tester().ExpectBucketCount(kActionsHistogram, |
| 99 kActionContentSettingsWildcardUpdate, 2); | 117 kActionContentSettingsWildcardUpdate, 2); |
| 100 } | 118 } |
| 101 | 119 |
| 120 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, SmartUI) { | |
| 121 if (!settings_manager()->should_use_smart_ui()) | |
| 122 return; | |
| 123 | |
| 124 GURL url("https://example.test/"); | |
| 125 GURL url2("https://example.test/path"); | |
| 126 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); | |
| 127 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); | |
| 128 | |
| 129 settings_manager()->OnDidShowUI(url); | |
| 130 | |
| 131 // Subsequent navigations to same-domains should not show UI. | |
| 132 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); | |
| 133 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url2)); | |
| 134 | |
| 135 // Showing the UI should trigger a forced content setting update, but no | |
|
engedy
2017/04/12 14:02:51
nit: Let's enforce the first part of this sentence
Charlie Harrison
2017/04/12 17:53:45
Done.
| |
| 136 // metrics should be recorded. | |
| 137 histogram_tester().ExpectBucketCount(kActionsHistogram, | |
| 138 kActionContentSettingsAllowed, 0); | |
| 139 | |
| 140 // Fast forward the clock. | |
| 141 test_clock()->Advance( | |
| 142 SubresourceFilterContentSettingsManager::kUIShowThresholdTime); | |
| 143 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); | |
| 144 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url2)); | |
| 145 } | |
| 146 | |
| 147 // If the user manually sets a content setting to block the feature, the smart | |
| 148 // UI should be reset. | |
| 149 TEST_F(SubresourceFilterContentSettingsManagerFactoryTest, | |
| 150 SmartUIWithOverride_Resets) { | |
| 151 if (!settings_manager()->should_use_smart_ui()) | |
| 152 return; | |
| 153 | |
| 154 GURL url("https://example.test/"); | |
| 155 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); | |
| 156 | |
| 157 settings_manager()->OnDidShowUI(url); | |
| 158 | |
| 159 // Subsequent navigations to same-domains should not show UI. | |
| 160 EXPECT_FALSE(settings_manager()->ShouldShowUIForSite(url)); | |
| 161 | |
| 162 // The user changed their mind, make sure the feature is showing up in the | |
| 163 // settings UI. i.e. the setting should be non-default. | |
| 164 EXPECT_EQ(CONTENT_SETTING_ALLOW, settings_manager()->GetContentSetting(url)); | |
| 165 settings_manager()->SetContentSetting(url, CONTENT_SETTING_BLOCK, | |
| 166 true /* log_metrics */); | |
| 167 | |
| 168 histogram_tester().ExpectBucketCount(kActionsHistogram, | |
| 169 kActionContentSettingsBlocked, 1); | |
| 170 histogram_tester().ExpectBucketCount( | |
| 171 kActionsHistogram, kActionContentSettingsBlockedWhileUISuppressed, 1); | |
| 172 | |
| 173 // Smart UI should be reset. | |
| 174 EXPECT_TRUE(settings_manager()->ShouldShowUIForSite(url)); | |
| 175 } | |
| 176 | |
| 102 } // namespace | 177 } // namespace |
| OLD | NEW |