Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_pref_provider_unittest.cc
blobf3811d71f772b9ba6e4955e0be877cac74e99184
1 // Copyright (c) 2012 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 "components/content_settings/core/browser/content_settings_pref_provider.h"
7 #include "base/auto_reset.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/default_pref_store.h"
12 #include "base/prefs/overlay_user_pref_store.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/prefs/scoped_user_pref_update.h"
16 #include "base/prefs/testing_pref_store.h"
17 #include "base/test/simple_test_clock.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/values.h"
20 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
21 #include "chrome/browser/prefs/browser_prefs.h"
22 #include "chrome/browser/prefs/pref_service_mock_factory.h"
23 #include "chrome/browser/prefs/pref_service_syncable.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/testing_pref_service_syncable.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "components/content_settings/core/browser/content_settings_pref.h"
30 #include "components/content_settings/core/browser/content_settings_utils.h"
31 #include "components/content_settings/core/test/content_settings_test_utils.h"
32 #include "components/pref_registry/pref_registry_syncable.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "url/gurl.h"
36 using ::testing::_;
38 namespace content_settings {
40 class DeadlockCheckerThread : public base::PlatformThread::Delegate {
41 public:
42 explicit DeadlockCheckerThread(PrefProvider* provider)
43 : provider_(provider) {}
45 void ThreadMain() override {
46 bool got_lock = provider_->content_settings_pref()->lock_.Try();
47 EXPECT_TRUE(got_lock);
48 if (got_lock)
49 provider_->content_settings_pref()->lock_.Release();
51 private:
52 PrefProvider* provider_;
53 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread);
56 // A helper for observing an preference changes and testing whether
57 // |PrefProvider| holds a lock when the preferences change.
58 class DeadlockCheckerObserver {
59 public:
60 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or
61 // |provider|.
62 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider)
63 : provider_(provider),
64 notification_received_(false) {
65 pref_change_registrar_.Init(prefs);
66 pref_change_registrar_.Add(
67 prefs::kContentSettingsPatternPairs,
68 base::Bind(
69 &DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged,
70 base::Unretained(this)));
72 virtual ~DeadlockCheckerObserver() {}
74 bool notification_received() const {
75 return notification_received_;
78 private:
79 void OnContentSettingsPatternPairsChanged() {
80 // Check whether |provider_| holds its lock. For this, we need a
81 // separate thread.
82 DeadlockCheckerThread thread(provider_);
83 base::PlatformThreadHandle handle;
84 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle));
85 base::PlatformThread::Join(handle);
86 notification_received_ = true;
89 PrefProvider* provider_;
90 PrefChangeRegistrar pref_change_registrar_;
91 bool notification_received_;
92 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver);
95 TEST(PrefProviderTest, Observer) {
96 TestingProfile profile;
97 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false);
99 ContentSettingsPattern pattern =
100 ContentSettingsPattern::FromString("[*.]example.com");
101 content_settings::MockObserver mock_observer;
102 EXPECT_CALL(mock_observer,
103 OnContentSettingChanged(pattern,
104 ContentSettingsPattern::Wildcard(),
105 CONTENT_SETTINGS_TYPE_IMAGES,
106 ""));
108 pref_content_settings_provider.AddObserver(&mock_observer);
110 pref_content_settings_provider.SetWebsiteSetting(
111 pattern,
112 ContentSettingsPattern::Wildcard(),
113 CONTENT_SETTINGS_TYPE_IMAGES,
114 std::string(),
115 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
117 pref_content_settings_provider.ShutdownOnUIThread();
120 // Test for regression in which the PrefProvider modified the user pref store
121 // of the OTR unintentionally: http://crbug.com/74466.
122 TEST(PrefProviderTest, Incognito) {
123 PersistentPrefStore* user_prefs = new TestingPrefStore();
124 OverlayUserPrefStore* otr_user_prefs =
125 new OverlayUserPrefStore(user_prefs);
127 PrefServiceMockFactory factory;
128 factory.set_user_prefs(make_scoped_refptr(user_prefs));
129 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
130 new user_prefs::PrefRegistrySyncable);
131 PrefServiceSyncable* regular_prefs =
132 factory.CreateSyncable(registry.get()).release();
134 chrome::RegisterUserProfilePrefs(registry.get());
136 PrefServiceMockFactory otr_factory;
137 otr_factory.set_user_prefs(make_scoped_refptr(otr_user_prefs));
138 scoped_refptr<user_prefs::PrefRegistrySyncable> otr_registry(
139 new user_prefs::PrefRegistrySyncable);
140 PrefServiceSyncable* otr_prefs =
141 otr_factory.CreateSyncable(otr_registry.get()).release();
143 chrome::RegisterUserProfilePrefs(otr_registry.get());
145 TestingProfile::Builder profile_builder;
146 profile_builder.SetPrefService(make_scoped_ptr(regular_prefs));
147 scoped_ptr<TestingProfile> profile = profile_builder.Build();
149 TestingProfile::Builder otr_profile_builder;
150 otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs));
151 otr_profile_builder.BuildIncognito(profile.get());
153 PrefProvider pref_content_settings_provider(regular_prefs, false);
154 PrefProvider pref_content_settings_provider_incognito(otr_prefs, true);
155 ContentSettingsPattern pattern =
156 ContentSettingsPattern::FromString("[*.]example.com");
157 pref_content_settings_provider.SetWebsiteSetting(
158 pattern,
159 pattern,
160 CONTENT_SETTINGS_TYPE_IMAGES,
161 std::string(),
162 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
164 GURL host("http://example.com/");
165 // The value should of course be visible in the regular PrefProvider.
166 EXPECT_EQ(CONTENT_SETTING_ALLOW,
167 GetContentSetting(&pref_content_settings_provider,
168 host,
169 host,
170 CONTENT_SETTINGS_TYPE_IMAGES,
171 std::string(),
172 false));
173 // And also in the OTR version.
174 EXPECT_EQ(CONTENT_SETTING_ALLOW,
175 GetContentSetting(&pref_content_settings_provider_incognito,
176 host,
177 host,
178 CONTENT_SETTINGS_TYPE_IMAGES,
179 std::string(),
180 false));
181 // But the value should not be overridden in the OTR user prefs accidentally.
182 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(
183 prefs::kContentSettingsPatternPairs));
185 pref_content_settings_provider.ShutdownOnUIThread();
186 pref_content_settings_provider_incognito.ShutdownOnUIThread();
189 TEST(PrefProviderTest, GetContentSettingsValue) {
190 TestingProfile testing_profile;
191 PrefProvider provider(testing_profile.GetPrefs(), false);
193 GURL primary_url("http://example.com/");
194 ContentSettingsPattern primary_pattern =
195 ContentSettingsPattern::FromString("[*.]example.com");
197 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
198 GetContentSetting(&provider,
199 primary_url,
200 primary_url,
201 CONTENT_SETTINGS_TYPE_IMAGES,
202 std::string(),
203 false));
205 EXPECT_EQ(NULL,
206 GetContentSettingValue(&provider,
207 primary_url,
208 primary_url,
209 CONTENT_SETTINGS_TYPE_IMAGES,
210 std::string(),
211 false));
213 provider.SetWebsiteSetting(primary_pattern,
214 primary_pattern,
215 CONTENT_SETTINGS_TYPE_IMAGES,
216 std::string(),
217 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
218 EXPECT_EQ(CONTENT_SETTING_BLOCK,
219 GetContentSetting(&provider,
220 primary_url,
221 primary_url,
222 CONTENT_SETTINGS_TYPE_IMAGES,
223 std::string(),
224 false));
225 scoped_ptr<base::Value> value_ptr(
226 GetContentSettingValue(&provider,
227 primary_url,
228 primary_url,
229 CONTENT_SETTINGS_TYPE_IMAGES,
230 std::string(),
231 false));
232 int int_value = -1;
233 value_ptr->GetAsInteger(&int_value);
234 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
236 provider.SetWebsiteSetting(primary_pattern,
237 primary_pattern,
238 CONTENT_SETTINGS_TYPE_IMAGES,
239 std::string(),
240 NULL);
241 EXPECT_EQ(NULL,
242 GetContentSettingValue(&provider,
243 primary_url,
244 primary_url,
245 CONTENT_SETTINGS_TYPE_IMAGES,
246 std::string(),
247 false));
248 provider.ShutdownOnUIThread();
251 TEST(PrefProviderTest, Patterns) {
252 TestingProfile testing_profile;
253 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
254 false);
256 GURL host1("http://example.com/");
257 GURL host2("http://www.example.com/");
258 GURL host3("http://example.org/");
259 GURL host4("file:///tmp/test.html");
260 ContentSettingsPattern pattern1 =
261 ContentSettingsPattern::FromString("[*.]example.com");
262 ContentSettingsPattern pattern2 =
263 ContentSettingsPattern::FromString("example.org");
264 ContentSettingsPattern pattern3 =
265 ContentSettingsPattern::FromString("file:///tmp/test.html");
267 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
268 GetContentSetting(&pref_content_settings_provider,
269 host1,
270 host1,
271 CONTENT_SETTINGS_TYPE_IMAGES,
272 std::string(),
273 false));
274 pref_content_settings_provider.SetWebsiteSetting(
275 pattern1,
276 pattern1,
277 CONTENT_SETTINGS_TYPE_IMAGES,
278 std::string(),
279 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
280 EXPECT_EQ(CONTENT_SETTING_BLOCK,
281 GetContentSetting(&pref_content_settings_provider,
282 host1,
283 host1,
284 CONTENT_SETTINGS_TYPE_IMAGES,
285 std::string(),
286 false));
287 EXPECT_EQ(CONTENT_SETTING_BLOCK,
288 GetContentSetting(&pref_content_settings_provider,
289 host2,
290 host2,
291 CONTENT_SETTINGS_TYPE_IMAGES,
292 std::string(),
293 false));
295 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
296 GetContentSetting(&pref_content_settings_provider,
297 host3,
298 host3,
299 CONTENT_SETTINGS_TYPE_IMAGES,
300 std::string(),
301 false));
302 pref_content_settings_provider.SetWebsiteSetting(
303 pattern2,
304 pattern2,
305 CONTENT_SETTINGS_TYPE_IMAGES,
306 std::string(),
307 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
308 EXPECT_EQ(CONTENT_SETTING_BLOCK,
309 GetContentSetting(&pref_content_settings_provider,
310 host3,
311 host3,
312 CONTENT_SETTINGS_TYPE_IMAGES,
313 std::string(),
314 false));
316 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
317 GetContentSetting(&pref_content_settings_provider,
318 host4,
319 host4,
320 CONTENT_SETTINGS_TYPE_IMAGES,
321 std::string(),
322 false));
323 pref_content_settings_provider.SetWebsiteSetting(
324 pattern3,
325 pattern3,
326 CONTENT_SETTINGS_TYPE_IMAGES,
327 std::string(),
328 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
329 EXPECT_EQ(CONTENT_SETTING_BLOCK,
330 GetContentSetting(&pref_content_settings_provider,
331 host4,
332 host4,
333 CONTENT_SETTINGS_TYPE_IMAGES,
334 std::string(),
335 false));
337 pref_content_settings_provider.ShutdownOnUIThread();
340 TEST(PrefProviderTest, ResourceIdentifier) {
341 TestingProfile testing_profile;
342 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
343 false);
345 GURL host("http://example.com/");
346 ContentSettingsPattern pattern =
347 ContentSettingsPattern::FromString("[*.]example.com");
348 std::string resource1("someplugin");
349 std::string resource2("otherplugin");
351 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
352 GetContentSetting(
353 &pref_content_settings_provider,
354 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
355 resource1, false));
356 pref_content_settings_provider.SetWebsiteSetting(
357 pattern,
358 pattern,
359 CONTENT_SETTINGS_TYPE_PLUGINS,
360 resource1,
361 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
362 EXPECT_EQ(CONTENT_SETTING_BLOCK,
363 GetContentSetting(
364 &pref_content_settings_provider,
365 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
366 resource1, false));
367 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
368 GetContentSetting(
369 &pref_content_settings_provider,
370 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
371 resource2, false));
373 pref_content_settings_provider.ShutdownOnUIThread();
376 TEST(PrefProviderTest, AutoSubmitCertificateContentSetting) {
377 TestingProfile profile;
378 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
379 GURL primary_url("https://www.example.com");
380 GURL secondary_url("https://www.sample.com");
382 PrefProvider provider(prefs, false);
384 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
385 GetContentSetting(
386 &provider,
387 primary_url,
388 primary_url,
389 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
390 std::string(),
391 false));
393 provider.SetWebsiteSetting(ContentSettingsPattern::FromURL(primary_url),
394 ContentSettingsPattern::Wildcard(),
395 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
396 std::string(),
397 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
398 EXPECT_EQ(CONTENT_SETTING_ALLOW,
399 GetContentSetting(
400 &provider,
401 primary_url,
402 secondary_url,
403 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
404 std::string(),
405 false));
406 provider.ShutdownOnUIThread();
409 // http://crosbug.com/17760
410 TEST(PrefProviderTest, Deadlock) {
411 TestingPrefServiceSyncable prefs;
412 PrefProvider::RegisterProfilePrefs(prefs.registry());
414 // Chain of events: a preference changes, |PrefProvider| notices it, and reads
415 // and writes the preference. When the preference is written, a notification
416 // is sent, and this used to happen when |PrefProvider| was still holding its
417 // lock.
419 PrefProvider provider(&prefs, false);
420 DeadlockCheckerObserver observer(&prefs, &provider);
422 DictionaryPrefUpdate update(&prefs,
423 prefs::kContentSettingsPatternPairs);
424 base::DictionaryValue* mutable_settings = update.Get();
425 mutable_settings->SetWithoutPathExpansion("www.example.com,*",
426 new base::DictionaryValue());
428 EXPECT_TRUE(observer.notification_received());
430 provider.ShutdownOnUIThread();
433 TEST(PrefProviderTest, LastUsage) {
434 TestingProfile testing_profile;
435 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
436 false);
437 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
438 test_clock->SetNow(base::Time::Now());
440 pref_content_settings_provider.SetClockForTesting(
441 scoped_ptr<base::Clock>(test_clock));
442 GURL host("http://example.com/");
443 ContentSettingsPattern pattern =
444 ContentSettingsPattern::FromString("[*.]example.com");
446 base::Time no_usage = pref_content_settings_provider.GetLastUsage(
447 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
448 EXPECT_EQ(no_usage.ToDoubleT(), 0);
450 pref_content_settings_provider.UpdateLastUsage(
451 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
452 base::Time first = pref_content_settings_provider.GetLastUsage(
453 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
455 test_clock->Advance(base::TimeDelta::FromSeconds(10));
457 pref_content_settings_provider.UpdateLastUsage(
458 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
459 base::Time second = pref_content_settings_provider.GetLastUsage(
460 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
462 base::TimeDelta delta = second - first;
463 EXPECT_EQ(delta.InSeconds(), 10);
465 pref_content_settings_provider.ShutdownOnUIThread();
468 } // namespace content_settings