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 "testing/gtest/include/gtest/gtest.h"
21 class DefaultProviderTest
: public testing::Test
{
24 : provider_(profile_
.GetPrefs(), false) {
26 ~DefaultProviderTest() override
{ provider_
.ShutdownOnUIThread(); }
29 TestingProfile profile_
;
30 content_settings::DefaultProvider provider_
;
33 TEST_F(DefaultProviderTest
, DefaultValues
) {
34 // Check setting defaults.
35 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
36 GetContentSetting(&provider_
,
39 CONTENT_SETTINGS_TYPE_COOKIES
,
42 provider_
.SetWebsiteSetting(
43 ContentSettingsPattern::Wildcard(),
44 ContentSettingsPattern::Wildcard(),
45 CONTENT_SETTINGS_TYPE_COOKIES
,
47 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
48 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
49 GetContentSetting(&provider_
,
52 CONTENT_SETTINGS_TYPE_COOKIES
,
56 EXPECT_EQ(CONTENT_SETTING_ASK
,
57 GetContentSetting(&provider_
,
60 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
63 provider_
.SetWebsiteSetting(
64 ContentSettingsPattern::Wildcard(),
65 ContentSettingsPattern::Wildcard(),
66 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
68 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
69 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
70 GetContentSetting(&provider_
,
73 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
77 scoped_ptr
<base::Value
> value(
78 GetContentSettingValue(&provider_
,
79 GURL("http://example.com/"),
80 GURL("http://example.com/"),
81 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
84 EXPECT_FALSE(value
.get());
87 TEST_F(DefaultProviderTest
, IgnoreNonDefaultSettings
) {
88 GURL
primary_url("http://www.google.com");
89 GURL
secondary_url("http://www.google.com");
91 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
92 GetContentSetting(&provider_
,
95 CONTENT_SETTINGS_TYPE_COOKIES
,
98 scoped_ptr
<base::Value
> value(
99 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
100 bool owned
= provider_
.SetWebsiteSetting(
101 ContentSettingsPattern::FromURL(primary_url
),
102 ContentSettingsPattern::FromURL(secondary_url
),
103 CONTENT_SETTINGS_TYPE_COOKIES
,
107 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
108 GetContentSetting(&provider_
,
111 CONTENT_SETTINGS_TYPE_COOKIES
,
116 TEST_F(DefaultProviderTest
, Observer
) {
117 content_settings::MockObserver mock_observer
;
118 EXPECT_CALL(mock_observer
,
119 OnContentSettingChanged(
120 _
, _
, CONTENT_SETTINGS_TYPE_IMAGES
, ""));
121 provider_
.AddObserver(&mock_observer
);
122 provider_
.SetWebsiteSetting(
123 ContentSettingsPattern::Wildcard(),
124 ContentSettingsPattern::Wildcard(),
125 CONTENT_SETTINGS_TYPE_IMAGES
,
127 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
129 EXPECT_CALL(mock_observer
,
130 OnContentSettingChanged(
131 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
132 provider_
.SetWebsiteSetting(
133 ContentSettingsPattern::Wildcard(),
134 ContentSettingsPattern::Wildcard(),
135 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
137 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
141 TEST_F(DefaultProviderTest
, ObserveDefaultPref
) {
142 PrefService
* prefs
= profile_
.GetPrefs();
144 // Make a copy of the default pref value so we can reset it later.
145 scoped_ptr
<base::Value
> default_value(prefs
->FindPreference(
146 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
148 provider_
.SetWebsiteSetting(
149 ContentSettingsPattern::Wildcard(),
150 ContentSettingsPattern::Wildcard(),
151 CONTENT_SETTINGS_TYPE_COOKIES
,
153 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
154 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
155 GetContentSetting(&provider_
,
158 CONTENT_SETTINGS_TYPE_COOKIES
,
162 // Make a copy of the pref's new value so we can reset it later.
163 scoped_ptr
<base::Value
> new_value(prefs
->FindPreference(
164 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
166 // Clearing the backing pref should also clear the internal cache.
167 prefs
->Set(prefs::kDefaultContentSettings
, *default_value
);
168 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
169 GetContentSetting(&provider_
,
172 CONTENT_SETTINGS_TYPE_COOKIES
,
175 // Reseting the pref to its previous value should update the cache.
176 prefs
->Set(prefs::kDefaultContentSettings
, *new_value
);
177 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
178 GetContentSetting(&provider_
,
181 CONTENT_SETTINGS_TYPE_COOKIES
,
186 TEST_F(DefaultProviderTest
, OffTheRecord
) {
187 content_settings::DefaultProvider
otr_provider(profile_
.GetPrefs(), true);
189 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
190 GetContentSetting(&provider_
,
193 CONTENT_SETTINGS_TYPE_COOKIES
,
196 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
197 GetContentSetting(&otr_provider
,
200 CONTENT_SETTINGS_TYPE_COOKIES
,
204 // Changing content settings on the main provider should also affect the
206 provider_
.SetWebsiteSetting(
207 ContentSettingsPattern::Wildcard(),
208 ContentSettingsPattern::Wildcard(),
209 CONTENT_SETTINGS_TYPE_COOKIES
,
211 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
212 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
213 GetContentSetting(&provider_
,
216 CONTENT_SETTINGS_TYPE_COOKIES
,
220 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
221 GetContentSetting(&otr_provider
,
224 CONTENT_SETTINGS_TYPE_COOKIES
,
228 // Changing content settings on the incognito provider should be ignored.
229 scoped_ptr
<base::Value
> value(
230 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
231 bool owned
= otr_provider
.SetWebsiteSetting(
232 ContentSettingsPattern::Wildcard(),
233 ContentSettingsPattern::Wildcard(),
234 CONTENT_SETTINGS_TYPE_COOKIES
,
238 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
239 GetContentSetting(&provider_
,
242 CONTENT_SETTINGS_TYPE_COOKIES
,
246 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
247 GetContentSetting(&otr_provider
,
250 CONTENT_SETTINGS_TYPE_COOKIES
,
253 otr_provider
.ShutdownOnUIThread();
257 // TODO(msramek): The two tests below test syncing between old versions
258 // of Chrome using a dictionary pref and new versions using individual integer
259 // prefs for default content settings. Remove the tests together with
260 // the dictionary setting after two stable releases.
261 TEST_F(DefaultProviderTest
, SyncFromDictionaryToIndividualPreferences
) {
262 PrefService
* prefs
= profile_
.GetPrefs();
265 DictionaryPrefUpdate
update(prefs
, prefs::kDefaultContentSettings
);
266 base::DictionaryValue
* default_settings_dictionary
= update
.Get();
268 default_settings_dictionary
->SetWithoutPathExpansion(
269 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES
),
270 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
271 default_settings_dictionary
->SetWithoutPathExpansion(
272 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_GEOLOCATION
),
273 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
276 // Cookies should sync, but geolocation should not.
277 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(
278 prefs
->GetInteger(prefs::kDefaultCookiesSetting
)));
279 EXPECT_EQ(CONTENT_SETTING_ASK
, IntToContentSetting(
280 prefs
->GetInteger(prefs::kDefaultGeolocationSetting
)));
283 TEST_F(DefaultProviderTest
, SyncFromIndividualPreferencesToDictionary
) {
284 PrefService
* prefs
= profile_
.GetPrefs();
286 prefs
->SetInteger(prefs::kDefaultJavaScriptSetting
, CONTENT_SETTING_BLOCK
);
287 prefs
->SetInteger(prefs::kDefaultSSLCertDecisionsSetting
,
288 CONTENT_SETTING_BLOCK
);
290 // Javascript should sync, but cert decisions should not.
291 const base::DictionaryValue
* default_settings_dictionary
=
292 prefs
->GetDictionary(prefs::kDefaultContentSettings
);
296 default_settings_dictionary
->GetIntegerWithoutPathExpansion(
297 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_JAVASCRIPT
),
299 has_cd_setting
= default_settings_dictionary
->HasKey(
300 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS
));
302 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(js_setting
));
303 EXPECT_FALSE(has_cd_setting
);