| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/browsing_data/core/counters/site_settings_counter.h" |
| 6 |
| 7 #include "base/test/scoped_feature_list.h" |
| 8 #include "base/test/simple_test_clock.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 10 #include "chrome/common/chrome_features.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "components/browsing_data/core/browsing_data_utils.h" |
| 13 #include "components/browsing_data/core/pref_names.h" |
| 14 #include "components/prefs/pref_service.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 class SiteSettingsCounterTest : public testing::Test { |
| 21 public: |
| 22 SiteSettingsCounterTest() { |
| 23 base::test::ScopedFeatureList feature_list; |
| 24 // Enable tabsInCbd to activate timestamp recording for content-settings. |
| 25 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 26 profile_ = base::MakeUnique<TestingProfile>(); |
| 27 map_ = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 28 } |
| 29 |
| 30 Profile* profile() { return profile_.get(); } |
| 31 |
| 32 HostContentSettingsMap* map() { return map_.get(); } |
| 33 |
| 34 void SetSiteSettingsDeletionPref(bool value) { |
| 35 profile()->GetPrefs()->SetBoolean(browsing_data::prefs::kDeleteSiteSettings, |
| 36 value); |
| 37 } |
| 38 |
| 39 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
| 40 profile()->GetPrefs()->SetInteger(browsing_data::prefs::kDeleteTimePeriod, |
| 41 static_cast<int>(period)); |
| 42 } |
| 43 |
| 44 browsing_data::BrowsingDataCounter::ResultInt GetResult() { |
| 45 DCHECK(finished_); |
| 46 return result_; |
| 47 } |
| 48 |
| 49 void Callback( |
| 50 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 51 DCHECK(result->Finished()); |
| 52 finished_ = result->Finished(); |
| 53 |
| 54 result_ = static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( |
| 55 result.get()) |
| 56 ->Value(); |
| 57 } |
| 58 |
| 59 private: |
| 60 content::TestBrowserThreadBundle thread_bundle_; |
| 61 std::unique_ptr<TestingProfile> profile_; |
| 62 |
| 63 scoped_refptr<HostContentSettingsMap> map_; |
| 64 bool finished_; |
| 65 browsing_data::BrowsingDataCounter::ResultInt result_; |
| 66 }; |
| 67 |
| 68 // Tests that the counter correctly counts each setting. |
| 69 TEST_F(SiteSettingsCounterTest, Count) { |
| 70 map()->SetContentSettingDefaultScope( |
| 71 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 72 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 73 map()->SetContentSettingDefaultScope( |
| 74 GURL("http://maps.google.com"), GURL("http://maps.google.com"), |
| 75 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW); |
| 76 |
| 77 browsing_data::SiteSettingsCounter counter(map()); |
| 78 counter.Init( |
| 79 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 80 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 81 counter.Restart(); |
| 82 |
| 83 EXPECT_EQ(2, GetResult()); |
| 84 } |
| 85 |
| 86 // Test that the counter counts correctly when using a time period. |
| 87 TEST_F(SiteSettingsCounterTest, CountWithTimePeriod) { |
| 88 auto test_clock = base::MakeUnique<base::SimpleTestClock>(); |
| 89 base::SimpleTestClock* clock = test_clock.get(); |
| 90 map()->SetClockForTesting(std::move(test_clock)); |
| 91 |
| 92 // Create a setting at Now()-90min. |
| 93 clock->SetNow(base::Time::Now() - base::TimeDelta::FromMinutes(90)); |
| 94 map()->SetContentSettingDefaultScope( |
| 95 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 96 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 97 |
| 98 // Create a setting at Now()-30min. |
| 99 clock->SetNow(base::Time::Now() - base::TimeDelta::FromMinutes(30)); |
| 100 map()->SetContentSettingDefaultScope( |
| 101 GURL("http://maps.google.com"), GURL("http://maps.google.com"), |
| 102 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW); |
| 103 |
| 104 clock->SetNow(base::Time::Now()); |
| 105 browsing_data::SiteSettingsCounter counter(map()); |
| 106 counter.Init( |
| 107 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 108 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 109 // Only one of the settings was created in the last hour. |
| 110 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR); |
| 111 EXPECT_EQ(1, GetResult()); |
| 112 // Both settings were created during the last day. |
| 113 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_DAY); |
| 114 EXPECT_EQ(2, GetResult()); |
| 115 } |
| 116 |
| 117 // Tests that the counter doesn't count website settings |
| 118 TEST_F(SiteSettingsCounterTest, OnlyCountContentSettings) { |
| 119 map()->SetContentSettingDefaultScope( |
| 120 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 121 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 122 map()->SetWebsiteSettingDefaultScope( |
| 123 GURL("http://maps.google.com"), GURL(), |
| 124 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
| 125 base::MakeUnique<base::DictionaryValue>()); |
| 126 |
| 127 browsing_data::SiteSettingsCounter counter(map()); |
| 128 counter.Init( |
| 129 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 130 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 131 counter.Restart(); |
| 132 |
| 133 EXPECT_EQ(1, GetResult()); |
| 134 } |
| 135 |
| 136 // Tests that the counter counts settings with the same pattern only |
| 137 // once. |
| 138 TEST_F(SiteSettingsCounterTest, OnlyCountPatternOnce) { |
| 139 map()->SetContentSettingDefaultScope( |
| 140 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 141 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 142 map()->SetContentSettingDefaultScope( |
| 143 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 144 CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string(), CONTENT_SETTING_ALLOW); |
| 145 |
| 146 browsing_data::SiteSettingsCounter counter(map()); |
| 147 counter.Init( |
| 148 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 149 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 150 counter.Restart(); |
| 151 |
| 152 EXPECT_EQ(1, GetResult()); |
| 153 } |
| 154 |
| 155 // Tests that the counter starts counting automatically when the deletion |
| 156 // pref changes to true. |
| 157 TEST_F(SiteSettingsCounterTest, PrefChanged) { |
| 158 SetSiteSettingsDeletionPref(false); |
| 159 map()->SetContentSettingDefaultScope( |
| 160 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 161 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 162 |
| 163 browsing_data::SiteSettingsCounter counter(map()); |
| 164 counter.Init( |
| 165 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 166 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 167 SetSiteSettingsDeletionPref(true); |
| 168 EXPECT_EQ(1, GetResult()); |
| 169 } |
| 170 |
| 171 // Tests that changing the deletion period restarts the counting. |
| 172 TEST_F(SiteSettingsCounterTest, PeriodChanged) { |
| 173 map()->SetContentSettingDefaultScope( |
| 174 GURL("http://www.google.com"), GURL("http://www.google.com"), |
| 175 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); |
| 176 |
| 177 browsing_data::SiteSettingsCounter counter(map()); |
| 178 counter.Init( |
| 179 profile()->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED, |
| 180 base::Bind(&SiteSettingsCounterTest::Callback, base::Unretained(this))); |
| 181 |
| 182 SetDeletionPeriodPref(browsing_data::TimePeriod::LAST_HOUR); |
| 183 EXPECT_EQ(1, GetResult()); |
| 184 } |
| 185 |
| 186 } // namespace |
| OLD | NEW |