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/browser/website_settings_info.h"
16 #include "components/content_settings/core/browser/website_settings_registry.h"
17 #include "components/content_settings/core/test/content_settings_test_utils.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 class DefaultProviderTest
: public testing::Test
{
27 : provider_(profile_
.GetPrefs(), false) {
29 ~DefaultProviderTest() override
{ provider_
.ShutdownOnUIThread(); }
32 content::TestBrowserThreadBundle thread_bundle_
;
33 TestingProfile profile_
;
34 content_settings::DefaultProvider provider_
;
37 TEST_F(DefaultProviderTest
, DefaultValues
) {
38 // Check setting defaults.
39 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
40 GetContentSetting(&provider_
,
43 CONTENT_SETTINGS_TYPE_COOKIES
,
46 provider_
.SetWebsiteSetting(
47 ContentSettingsPattern::Wildcard(),
48 ContentSettingsPattern::Wildcard(),
49 CONTENT_SETTINGS_TYPE_COOKIES
,
51 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
52 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
53 GetContentSetting(&provider_
,
56 CONTENT_SETTINGS_TYPE_COOKIES
,
60 EXPECT_EQ(CONTENT_SETTING_ASK
,
61 GetContentSetting(&provider_
,
64 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
67 provider_
.SetWebsiteSetting(
68 ContentSettingsPattern::Wildcard(),
69 ContentSettingsPattern::Wildcard(),
70 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
72 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
73 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
74 GetContentSetting(&provider_
,
77 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
81 scoped_ptr
<base::Value
> value(
82 GetContentSettingValue(&provider_
,
83 GURL("http://example.com/"),
84 GURL("http://example.com/"),
85 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
88 EXPECT_FALSE(value
.get());
91 TEST_F(DefaultProviderTest
, IgnoreNonDefaultSettings
) {
92 GURL
primary_url("http://www.google.com");
93 GURL
secondary_url("http://www.google.com");
95 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
96 GetContentSetting(&provider_
,
99 CONTENT_SETTINGS_TYPE_COOKIES
,
102 scoped_ptr
<base::Value
> value(
103 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
104 bool owned
= provider_
.SetWebsiteSetting(
105 ContentSettingsPattern::FromURL(primary_url
),
106 ContentSettingsPattern::FromURL(secondary_url
),
107 CONTENT_SETTINGS_TYPE_COOKIES
,
111 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
112 GetContentSetting(&provider_
,
115 CONTENT_SETTINGS_TYPE_COOKIES
,
120 TEST_F(DefaultProviderTest
, Observer
) {
121 content_settings::MockObserver mock_observer
;
122 EXPECT_CALL(mock_observer
,
123 OnContentSettingChanged(
124 _
, _
, CONTENT_SETTINGS_TYPE_IMAGES
, ""));
125 provider_
.AddObserver(&mock_observer
);
126 provider_
.SetWebsiteSetting(
127 ContentSettingsPattern::Wildcard(),
128 ContentSettingsPattern::Wildcard(),
129 CONTENT_SETTINGS_TYPE_IMAGES
,
131 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
133 EXPECT_CALL(mock_observer
,
134 OnContentSettingChanged(
135 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
136 provider_
.SetWebsiteSetting(
137 ContentSettingsPattern::Wildcard(),
138 ContentSettingsPattern::Wildcard(),
139 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
141 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
145 TEST_F(DefaultProviderTest
, ObservePref
) {
146 PrefService
* prefs
= profile_
.GetPrefs();
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
,
161 const content_settings::WebsiteSettingsInfo
* info
=
162 content_settings::WebsiteSettingsRegistry::GetInstance()->Get(
163 CONTENT_SETTINGS_TYPE_COOKIES
);
164 // Clearing the backing pref should also clear the internal cache.
165 prefs
->ClearPref(info
->default_value_pref_name());
166 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
167 GetContentSetting(&provider_
,
170 CONTENT_SETTINGS_TYPE_COOKIES
,
173 // Reseting the pref to its previous value should update the cache.
174 prefs
->SetInteger(info
->default_value_pref_name(), CONTENT_SETTING_BLOCK
);
175 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
176 GetContentSetting(&provider_
,
179 CONTENT_SETTINGS_TYPE_COOKIES
,
184 TEST_F(DefaultProviderTest
, OffTheRecord
) {
185 content_settings::DefaultProvider
otr_provider(profile_
.GetPrefs(), true);
187 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
188 GetContentSetting(&provider_
,
191 CONTENT_SETTINGS_TYPE_COOKIES
,
194 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
195 GetContentSetting(&otr_provider
,
198 CONTENT_SETTINGS_TYPE_COOKIES
,
202 // Changing content settings on the main provider should also affect the
204 provider_
.SetWebsiteSetting(
205 ContentSettingsPattern::Wildcard(),
206 ContentSettingsPattern::Wildcard(),
207 CONTENT_SETTINGS_TYPE_COOKIES
,
209 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
210 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
211 GetContentSetting(&provider_
,
214 CONTENT_SETTINGS_TYPE_COOKIES
,
218 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
219 GetContentSetting(&otr_provider
,
222 CONTENT_SETTINGS_TYPE_COOKIES
,
226 // Changing content settings on the incognito provider should be ignored.
227 scoped_ptr
<base::Value
> value(
228 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
229 bool owned
= otr_provider
.SetWebsiteSetting(
230 ContentSettingsPattern::Wildcard(),
231 ContentSettingsPattern::Wildcard(),
232 CONTENT_SETTINGS_TYPE_COOKIES
,
236 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
237 GetContentSetting(&provider_
,
240 CONTENT_SETTINGS_TYPE_COOKIES
,
244 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
245 GetContentSetting(&otr_provider
,
248 CONTENT_SETTINGS_TYPE_COOKIES
,
251 otr_provider
.ShutdownOnUIThread();