Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_default_provider_unittest.cc
blob7a9b35e5d7c5ab4bf092f98839f3f43f9b01e0e8
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"
18 #include "url/gurl.h"
20 using ::testing::_;
22 class DefaultProviderTest : public testing::Test {
23 public:
24 DefaultProviderTest()
25 : provider_(profile_.GetPrefs(), false) {
27 ~DefaultProviderTest() override { provider_.ShutdownOnUIThread(); }
29 protected:
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_,
39 GURL(),
40 GURL(),
41 CONTENT_SETTINGS_TYPE_COOKIES,
42 std::string(),
43 false));
44 provider_.SetWebsiteSetting(
45 ContentSettingsPattern::Wildcard(),
46 ContentSettingsPattern::Wildcard(),
47 CONTENT_SETTINGS_TYPE_COOKIES,
48 std::string(),
49 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
50 EXPECT_EQ(CONTENT_SETTING_BLOCK,
51 GetContentSetting(&provider_,
52 GURL(),
53 GURL(),
54 CONTENT_SETTINGS_TYPE_COOKIES,
55 std::string(),
56 false));
58 EXPECT_EQ(CONTENT_SETTING_ASK,
59 GetContentSetting(&provider_,
60 GURL(),
61 GURL(),
62 CONTENT_SETTINGS_TYPE_GEOLOCATION,
63 std::string(),
64 false));
65 provider_.SetWebsiteSetting(
66 ContentSettingsPattern::Wildcard(),
67 ContentSettingsPattern::Wildcard(),
68 CONTENT_SETTINGS_TYPE_GEOLOCATION,
69 std::string(),
70 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
71 EXPECT_EQ(CONTENT_SETTING_BLOCK,
72 GetContentSetting(&provider_,
73 GURL(),
74 GURL(),
75 CONTENT_SETTINGS_TYPE_GEOLOCATION,
76 std::string(),
77 false));
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,
84 std::string(),
85 false));
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_,
95 primary_url,
96 secondary_url,
97 CONTENT_SETTINGS_TYPE_COOKIES,
98 std::string(),
99 false));
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,
106 std::string(),
107 value.get());
108 EXPECT_FALSE(owned);
109 EXPECT_EQ(CONTENT_SETTING_ALLOW,
110 GetContentSetting(&provider_,
111 primary_url,
112 secondary_url,
113 CONTENT_SETTINGS_TYPE_COOKIES,
114 std::string(),
115 false));
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,
128 std::string(),
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,
138 std::string(),
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,
154 std::string(),
155 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
156 EXPECT_EQ(CONTENT_SETTING_BLOCK,
157 GetContentSetting(&provider_,
158 GURL(),
159 GURL(),
160 CONTENT_SETTINGS_TYPE_COOKIES,
161 std::string(),
162 false));
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_,
172 GURL(),
173 GURL(),
174 CONTENT_SETTINGS_TYPE_COOKIES,
175 std::string(),
176 false));
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_,
181 GURL(),
182 GURL(),
183 CONTENT_SETTINGS_TYPE_COOKIES,
184 std::string(),
185 false));
188 TEST_F(DefaultProviderTest, OffTheRecord) {
189 content_settings::DefaultProvider otr_provider(profile_.GetPrefs(), true);
191 EXPECT_EQ(CONTENT_SETTING_ALLOW,
192 GetContentSetting(&provider_,
193 GURL(),
194 GURL(),
195 CONTENT_SETTINGS_TYPE_COOKIES,
196 std::string(),
197 false));
198 EXPECT_EQ(CONTENT_SETTING_ALLOW,
199 GetContentSetting(&otr_provider,
200 GURL(),
201 GURL(),
202 CONTENT_SETTINGS_TYPE_COOKIES,
203 std::string(),
204 true));
206 // Changing content settings on the main provider should also affect the
207 // incognito map.
208 provider_.SetWebsiteSetting(
209 ContentSettingsPattern::Wildcard(),
210 ContentSettingsPattern::Wildcard(),
211 CONTENT_SETTINGS_TYPE_COOKIES,
212 std::string(),
213 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
214 EXPECT_EQ(CONTENT_SETTING_BLOCK,
215 GetContentSetting(&provider_,
216 GURL(),
217 GURL(),
218 CONTENT_SETTINGS_TYPE_COOKIES,
219 std::string(),
220 false));
222 EXPECT_EQ(CONTENT_SETTING_BLOCK,
223 GetContentSetting(&otr_provider,
224 GURL(),
225 GURL(),
226 CONTENT_SETTINGS_TYPE_COOKIES,
227 std::string(),
228 true));
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,
237 std::string(),
238 value.get());
239 EXPECT_FALSE(owned);
240 EXPECT_EQ(CONTENT_SETTING_BLOCK,
241 GetContentSetting(&provider_,
242 GURL(),
243 GURL(),
244 CONTENT_SETTINGS_TYPE_COOKIES,
245 std::string(),
246 false));
248 EXPECT_EQ(CONTENT_SETTING_BLOCK,
249 GetContentSetting(&otr_provider,
250 GURL(),
251 GURL(),
252 CONTENT_SETTINGS_TYPE_COOKIES,
253 std::string(),
254 true));
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);
295 int js_setting;
296 bool has_cd_setting;
298 default_settings_dictionary->GetIntegerWithoutPathExpansion(
299 content_settings::GetTypeName(CONTENT_SETTINGS_TYPE_JAVASCRIPT),
300 &js_setting);
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);