1 // Copyright (c) 2011 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/content_settings/core/browser/content_settings_default_provider.h"
14 #include "components/content_settings/core/browser/content_settings_utils.h"
15 #include "components/content_settings/core/test/content_settings_test_utils.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class DefaultProviderTest
: public testing::Test
{
25 : provider_(profile_
.GetPrefs(), false) {
27 ~DefaultProviderTest() override
{ provider_
.ShutdownOnUIThread(); }
30 content::TestBrowserThreadBundle thread_bundle_
;
31 TestingProfile profile_
;
32 content_settings::DefaultProvider provider_
;
35 TEST_F(DefaultProviderTest
, DefaultValues
) {
36 // Check setting defaults.
37 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
38 GetContentSetting(&provider_
,
41 CONTENT_SETTINGS_TYPE_COOKIES
,
44 provider_
.SetWebsiteSetting(
45 ContentSettingsPattern::Wildcard(),
46 ContentSettingsPattern::Wildcard(),
47 CONTENT_SETTINGS_TYPE_COOKIES
,
49 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
50 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
51 GetContentSetting(&provider_
,
54 CONTENT_SETTINGS_TYPE_COOKIES
,
58 EXPECT_EQ(CONTENT_SETTING_ASK
,
59 GetContentSetting(&provider_
,
62 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
65 provider_
.SetWebsiteSetting(
66 ContentSettingsPattern::Wildcard(),
67 ContentSettingsPattern::Wildcard(),
68 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
70 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
71 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
72 GetContentSetting(&provider_
,
75 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
79 scoped_ptr
<base::Value
> value(
80 GetContentSettingValue(&provider_
,
81 GURL("http://example.com/"),
82 GURL("http://example.com/"),
83 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
86 EXPECT_FALSE(value
.get());
89 TEST_F(DefaultProviderTest
, IgnoreNonDefaultSettings
) {
90 GURL
primary_url("http://www.google.com");
91 GURL
secondary_url("http://www.google.com");
93 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
94 GetContentSetting(&provider_
,
97 CONTENT_SETTINGS_TYPE_COOKIES
,
100 scoped_ptr
<base::Value
> value(
101 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
102 bool owned
= provider_
.SetWebsiteSetting(
103 ContentSettingsPattern::FromURL(primary_url
),
104 ContentSettingsPattern::FromURL(secondary_url
),
105 CONTENT_SETTINGS_TYPE_COOKIES
,
109 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
110 GetContentSetting(&provider_
,
113 CONTENT_SETTINGS_TYPE_COOKIES
,
118 TEST_F(DefaultProviderTest
, Observer
) {
119 content_settings::MockObserver mock_observer
;
120 EXPECT_CALL(mock_observer
,
121 OnContentSettingChanged(
122 _
, _
, CONTENT_SETTINGS_TYPE_IMAGES
, ""));
123 provider_
.AddObserver(&mock_observer
);
124 provider_
.SetWebsiteSetting(
125 ContentSettingsPattern::Wildcard(),
126 ContentSettingsPattern::Wildcard(),
127 CONTENT_SETTINGS_TYPE_IMAGES
,
129 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
131 EXPECT_CALL(mock_observer
,
132 OnContentSettingChanged(
133 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
134 provider_
.SetWebsiteSetting(
135 ContentSettingsPattern::Wildcard(),
136 ContentSettingsPattern::Wildcard(),
137 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
139 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
143 TEST_F(DefaultProviderTest
, ObserveDefaultPref
) {
144 PrefService
* prefs
= profile_
.GetPrefs();
146 // Make a copy of the default pref value so we can reset it later.
147 scoped_ptr
<base::Value
> default_value(prefs
->FindPreference(
148 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
150 provider_
.SetWebsiteSetting(
151 ContentSettingsPattern::Wildcard(),
152 ContentSettingsPattern::Wildcard(),
153 CONTENT_SETTINGS_TYPE_COOKIES
,
155 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
156 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
157 GetContentSetting(&provider_
,
160 CONTENT_SETTINGS_TYPE_COOKIES
,
164 // Make a copy of the pref's new value so we can reset it later.
165 scoped_ptr
<base::Value
> new_value(prefs
->FindPreference(
166 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
168 // Clearing the backing pref should also clear the internal cache.
169 prefs
->Set(prefs::kDefaultContentSettings
, *default_value
);
170 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
171 GetContentSetting(&provider_
,
174 CONTENT_SETTINGS_TYPE_COOKIES
,
177 // Reseting the pref to its previous value should update the cache.
178 prefs
->Set(prefs::kDefaultContentSettings
, *new_value
);
179 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
180 GetContentSetting(&provider_
,
183 CONTENT_SETTINGS_TYPE_COOKIES
,
188 TEST_F(DefaultProviderTest
, OffTheRecord
) {
189 content_settings::DefaultProvider
otr_provider(profile_
.GetPrefs(), true);
191 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
192 GetContentSetting(&provider_
,
195 CONTENT_SETTINGS_TYPE_COOKIES
,
198 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
199 GetContentSetting(&otr_provider
,
202 CONTENT_SETTINGS_TYPE_COOKIES
,
206 // Changing content settings on the main provider should also affect the
208 provider_
.SetWebsiteSetting(
209 ContentSettingsPattern::Wildcard(),
210 ContentSettingsPattern::Wildcard(),
211 CONTENT_SETTINGS_TYPE_COOKIES
,
213 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
214 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
215 GetContentSetting(&provider_
,
218 CONTENT_SETTINGS_TYPE_COOKIES
,
222 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
223 GetContentSetting(&otr_provider
,
226 CONTENT_SETTINGS_TYPE_COOKIES
,
230 // Changing content settings on the incognito provider should be ignored.
231 scoped_ptr
<base::Value
> value(
232 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
233 bool owned
= otr_provider
.SetWebsiteSetting(
234 ContentSettingsPattern::Wildcard(),
235 ContentSettingsPattern::Wildcard(),
236 CONTENT_SETTINGS_TYPE_COOKIES
,
240 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
241 GetContentSetting(&provider_
,
244 CONTENT_SETTINGS_TYPE_COOKIES
,
248 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
249 GetContentSetting(&otr_provider
,
252 CONTENT_SETTINGS_TYPE_COOKIES
,
255 otr_provider
.ShutdownOnUIThread();
259 // TODO(msramek): The two tests below test syncing between old versions
260 // of Chrome using a dictionary pref and new versions using individual integer
261 // prefs for default content settings. Remove the tests together with
262 // the dictionary setting after two stable releases.
263 TEST_F(DefaultProviderTest
, SyncFromDictionaryToIndividualPreferences
) {
264 PrefService
* prefs
= profile_
.GetPrefs();
267 DictionaryPrefUpdate
update(prefs
, prefs::kDefaultContentSettings
);
268 base::DictionaryValue
* default_settings_dictionary
= update
.Get();
270 default_settings_dictionary
->SetWithoutPathExpansion(
271 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES
),
272 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
273 default_settings_dictionary
->SetWithoutPathExpansion(
274 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_GEOLOCATION
),
275 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
278 // Cookies should sync, but geolocation should not.
279 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(
280 prefs
->GetInteger(prefs::kDefaultCookiesSetting
)));
281 EXPECT_EQ(CONTENT_SETTING_ASK
, IntToContentSetting(
282 prefs
->GetInteger(prefs::kDefaultGeolocationSetting
)));
285 TEST_F(DefaultProviderTest
, SyncFromIndividualPreferencesToDictionary
) {
286 PrefService
* prefs
= profile_
.GetPrefs();
288 prefs
->SetInteger(prefs::kDefaultJavaScriptSetting
, CONTENT_SETTING_BLOCK
);
289 prefs
->SetInteger(prefs::kDefaultSSLCertDecisionsSetting
,
290 CONTENT_SETTING_BLOCK
);
292 // Javascript should sync, but cert decisions should not.
293 const base::DictionaryValue
* default_settings_dictionary
=
294 prefs
->GetDictionary(prefs::kDefaultContentSettings
);
298 default_settings_dictionary
->GetIntegerWithoutPathExpansion(
299 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_JAVASCRIPT
),
301 has_cd_setting
= default_settings_dictionary
->HasKey(
302 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS
));
304 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(js_setting
));
305 EXPECT_FALSE(has_cd_setting
);