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/testing_pref_service.h"
9 #include "chrome/browser/content_settings/content_settings_default_provider.h"
10 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
11 #include "chrome/browser/content_settings/content_settings_utils.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 using content::BrowserThread
;
21 class DefaultProviderTest
: public testing::Test
{
24 : ui_thread_(BrowserThread::UI
, &message_loop_
),
25 provider_(profile_
.GetPrefs(), false) {
27 virtual ~DefaultProviderTest() {
28 provider_
.ShutdownOnUIThread();
32 base::MessageLoop message_loop_
;
33 content::TestBrowserThread ui_thread_
;
34 TestingProfile profile_
;
35 content_settings::DefaultProvider provider_
;
38 TEST_F(DefaultProviderTest
, DefaultValues
) {
39 // Check setting defaults.
40 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
41 GetContentSetting(&provider_
,
44 CONTENT_SETTINGS_TYPE_COOKIES
,
47 provider_
.SetWebsiteSetting(
48 ContentSettingsPattern::Wildcard(),
49 ContentSettingsPattern::Wildcard(),
50 CONTENT_SETTINGS_TYPE_COOKIES
,
52 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
53 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
54 GetContentSetting(&provider_
,
57 CONTENT_SETTINGS_TYPE_COOKIES
,
61 EXPECT_EQ(CONTENT_SETTING_ASK
,
62 GetContentSetting(&provider_
,
65 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
68 provider_
.SetWebsiteSetting(
69 ContentSettingsPattern::Wildcard(),
70 ContentSettingsPattern::Wildcard(),
71 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
73 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
74 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
75 GetContentSetting(&provider_
,
78 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
82 scoped_ptr
<base::Value
> value(
83 GetContentSettingValue(&provider_
,
84 GURL("http://example.com/"),
85 GURL("http://example.com/"),
86 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
89 EXPECT_FALSE(value
.get());
92 TEST_F(DefaultProviderTest
, IgnoreNonDefaultSettings
) {
93 GURL
primary_url("http://www.google.com");
94 GURL
secondary_url("http://www.google.com");
96 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
97 GetContentSetting(&provider_
,
100 CONTENT_SETTINGS_TYPE_COOKIES
,
103 scoped_ptr
<base::Value
> value(
104 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
105 bool owned
= provider_
.SetWebsiteSetting(
106 ContentSettingsPattern::FromURL(primary_url
),
107 ContentSettingsPattern::FromURL(secondary_url
),
108 CONTENT_SETTINGS_TYPE_COOKIES
,
112 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
113 GetContentSetting(&provider_
,
116 CONTENT_SETTINGS_TYPE_COOKIES
,
121 TEST_F(DefaultProviderTest
, Observer
) {
122 content_settings::MockObserver mock_observer
;
123 EXPECT_CALL(mock_observer
,
124 OnContentSettingChanged(
125 _
, _
, CONTENT_SETTINGS_TYPE_IMAGES
, ""));
126 provider_
.AddObserver(&mock_observer
);
127 provider_
.SetWebsiteSetting(
128 ContentSettingsPattern::Wildcard(),
129 ContentSettingsPattern::Wildcard(),
130 CONTENT_SETTINGS_TYPE_IMAGES
,
132 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
134 EXPECT_CALL(mock_observer
,
135 OnContentSettingChanged(
136 _
, _
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, ""));
137 provider_
.SetWebsiteSetting(
138 ContentSettingsPattern::Wildcard(),
139 ContentSettingsPattern::Wildcard(),
140 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
142 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
146 TEST_F(DefaultProviderTest
, ObserveDefaultPref
) {
147 PrefService
* prefs
= profile_
.GetPrefs();
149 // Make a copy of the default pref value so we can reset it later.
150 scoped_ptr
<base::Value
> default_value(prefs
->FindPreference(
151 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
153 provider_
.SetWebsiteSetting(
154 ContentSettingsPattern::Wildcard(),
155 ContentSettingsPattern::Wildcard(),
156 CONTENT_SETTINGS_TYPE_COOKIES
,
158 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
159 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
160 GetContentSetting(&provider_
,
163 CONTENT_SETTINGS_TYPE_COOKIES
,
167 // Make a copy of the pref's new value so we can reset it later.
168 scoped_ptr
<base::Value
> new_value(prefs
->FindPreference(
169 prefs::kDefaultContentSettings
)->GetValue()->DeepCopy());
171 // Clearing the backing pref should also clear the internal cache.
172 prefs
->Set(prefs::kDefaultContentSettings
, *default_value
);
173 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
174 GetContentSetting(&provider_
,
177 CONTENT_SETTINGS_TYPE_COOKIES
,
180 // Reseting the pref to its previous value should update the cache.
181 prefs
->Set(prefs::kDefaultContentSettings
, *new_value
);
182 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
183 GetContentSetting(&provider_
,
186 CONTENT_SETTINGS_TYPE_COOKIES
,
191 TEST_F(DefaultProviderTest
, OffTheRecord
) {
192 content_settings::DefaultProvider
otr_provider(profile_
.GetPrefs(), true);
194 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
195 GetContentSetting(&provider_
,
198 CONTENT_SETTINGS_TYPE_COOKIES
,
201 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
202 GetContentSetting(&otr_provider
,
205 CONTENT_SETTINGS_TYPE_COOKIES
,
209 // Changing content settings on the main provider should also affect the
211 provider_
.SetWebsiteSetting(
212 ContentSettingsPattern::Wildcard(),
213 ContentSettingsPattern::Wildcard(),
214 CONTENT_SETTINGS_TYPE_COOKIES
,
216 base::Value::CreateIntegerValue(CONTENT_SETTING_BLOCK
));
217 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
218 GetContentSetting(&provider_
,
221 CONTENT_SETTINGS_TYPE_COOKIES
,
225 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
226 GetContentSetting(&otr_provider
,
229 CONTENT_SETTINGS_TYPE_COOKIES
,
233 // Changing content settings on the incognito provider should be ignored.
234 scoped_ptr
<base::Value
> value(
235 base::Value::CreateIntegerValue(CONTENT_SETTING_ALLOW
));
236 bool owned
= otr_provider
.SetWebsiteSetting(
237 ContentSettingsPattern::Wildcard(),
238 ContentSettingsPattern::Wildcard(),
239 CONTENT_SETTINGS_TYPE_COOKIES
,
243 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
244 GetContentSetting(&provider_
,
247 CONTENT_SETTINGS_TYPE_COOKIES
,
251 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
252 GetContentSetting(&otr_provider
,
255 CONTENT_SETTINGS_TYPE_COOKIES
,
258 otr_provider
.ShutdownOnUIThread();