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