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
, ObservePref
) {
144 PrefService
* prefs
= profile_
.GetPrefs();
146 provider_
.SetWebsiteSetting(
147 ContentSettingsPattern::Wildcard(),
148 ContentSettingsPattern::Wildcard(),
149 CONTENT_SETTINGS_TYPE_COOKIES
,
151 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
152 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
153 GetContentSetting(&provider_
,
156 CONTENT_SETTINGS_TYPE_COOKIES
,
160 // Clearing the backing pref should also clear the internal cache.
161 prefs
->ClearPref(prefs::kDefaultCookiesSetting
);
162 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
163 GetContentSetting(&provider_
,
166 CONTENT_SETTINGS_TYPE_COOKIES
,
169 // Reseting the pref to its previous value should update the cache.
170 prefs
->SetInteger(prefs::kDefaultCookiesSetting
, CONTENT_SETTING_BLOCK
);
171 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
172 GetContentSetting(&provider_
,
175 CONTENT_SETTINGS_TYPE_COOKIES
,
180 TEST_F(DefaultProviderTest
, OffTheRecord
) {
181 content_settings::DefaultProvider
otr_provider(profile_
.GetPrefs(), true);
183 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
184 GetContentSetting(&provider_
,
187 CONTENT_SETTINGS_TYPE_COOKIES
,
190 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
191 GetContentSetting(&otr_provider
,
194 CONTENT_SETTINGS_TYPE_COOKIES
,
198 // Changing content settings on the main provider should also affect the
200 provider_
.SetWebsiteSetting(
201 ContentSettingsPattern::Wildcard(),
202 ContentSettingsPattern::Wildcard(),
203 CONTENT_SETTINGS_TYPE_COOKIES
,
205 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
206 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
207 GetContentSetting(&provider_
,
210 CONTENT_SETTINGS_TYPE_COOKIES
,
214 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
215 GetContentSetting(&otr_provider
,
218 CONTENT_SETTINGS_TYPE_COOKIES
,
222 // Changing content settings on the incognito provider should be ignored.
223 scoped_ptr
<base::Value
> value(
224 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
225 bool owned
= otr_provider
.SetWebsiteSetting(
226 ContentSettingsPattern::Wildcard(),
227 ContentSettingsPattern::Wildcard(),
228 CONTENT_SETTINGS_TYPE_COOKIES
,
232 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
233 GetContentSetting(&provider_
,
236 CONTENT_SETTINGS_TYPE_COOKIES
,
240 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
241 GetContentSetting(&otr_provider
,
244 CONTENT_SETTINGS_TYPE_COOKIES
,
247 otr_provider
.ShutdownOnUIThread();