Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_pref_provider_unittest.cc
blob0409cc74f15ad3b6f382356d575c8c24d229ac94
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 EXPECT_TRUE(provider_->TestAllLocks());
48 private:
49 PrefProvider* provider_;
50 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread);
53 // A helper for observing an preference changes and testing whether
54 // |PrefProvider| holds a lock when the preferences change.
55 class DeadlockCheckerObserver {
56 public:
57 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or
58 // |provider|.
59 DeadlockCheckerObserver(PrefService* prefs, PrefProvider* provider)
60 : provider_(provider),
61 notification_received_(false) {
62 pref_change_registrar_.Init(prefs);
63 pref_change_registrar_.Add(
64 prefs::kContentSettingsPatternPairs,
65 base::Bind(
66 &DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged,
67 base::Unretained(this)));
69 virtual ~DeadlockCheckerObserver() {}
71 bool notification_received() const {
72 return notification_received_;
75 private:
76 void OnContentSettingsPatternPairsChanged() {
77 // Check whether |provider_| holds its lock. For this, we need a
78 // separate thread.
79 DeadlockCheckerThread thread(provider_);
80 base::PlatformThreadHandle handle;
81 ASSERT_TRUE(base::PlatformThread::Create(0, &thread, &handle));
82 base::PlatformThread::Join(handle);
83 notification_received_ = true;
86 PrefProvider* provider_;
87 PrefChangeRegistrar pref_change_registrar_;
88 bool notification_received_;
89 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver);
92 TEST(PrefProviderTest, Observer) {
93 TestingProfile profile;
94 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false);
96 ContentSettingsPattern pattern =
97 ContentSettingsPattern::FromString("[*.]example.com");
98 content_settings::MockObserver mock_observer;
99 EXPECT_CALL(mock_observer,
100 OnContentSettingChanged(pattern,
101 ContentSettingsPattern::Wildcard(),
102 CONTENT_SETTINGS_TYPE_IMAGES,
103 ""));
105 pref_content_settings_provider.AddObserver(&mock_observer);
107 pref_content_settings_provider.SetWebsiteSetting(
108 pattern,
109 ContentSettingsPattern::Wildcard(),
110 CONTENT_SETTINGS_TYPE_IMAGES,
111 std::string(),
112 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
114 pref_content_settings_provider.ShutdownOnUIThread();
117 // Test for regression in which the PrefProvider modified the user pref store
118 // of the OTR unintentionally: http://crbug.com/74466.
119 TEST(PrefProviderTest, Incognito) {
120 PersistentPrefStore* user_prefs = new TestingPrefStore();
121 OverlayUserPrefStore* otr_user_prefs =
122 new OverlayUserPrefStore(user_prefs);
124 PrefServiceMockFactory factory;
125 factory.set_user_prefs(make_scoped_refptr(user_prefs));
126 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
127 new user_prefs::PrefRegistrySyncable);
128 PrefServiceSyncable* regular_prefs =
129 factory.CreateSyncable(registry.get()).release();
131 chrome::RegisterUserProfilePrefs(registry.get());
133 PrefServiceMockFactory otr_factory;
134 otr_factory.set_user_prefs(make_scoped_refptr(otr_user_prefs));
135 scoped_refptr<user_prefs::PrefRegistrySyncable> otr_registry(
136 new user_prefs::PrefRegistrySyncable);
137 PrefServiceSyncable* otr_prefs =
138 otr_factory.CreateSyncable(otr_registry.get()).release();
140 chrome::RegisterUserProfilePrefs(otr_registry.get());
142 TestingProfile::Builder profile_builder;
143 profile_builder.SetPrefService(make_scoped_ptr(regular_prefs));
144 scoped_ptr<TestingProfile> profile = profile_builder.Build();
146 TestingProfile::Builder otr_profile_builder;
147 otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs));
148 otr_profile_builder.BuildIncognito(profile.get());
150 PrefProvider pref_content_settings_provider(regular_prefs, false);
151 PrefProvider pref_content_settings_provider_incognito(otr_prefs, true);
152 ContentSettingsPattern pattern =
153 ContentSettingsPattern::FromString("[*.]example.com");
154 pref_content_settings_provider.SetWebsiteSetting(
155 pattern,
156 pattern,
157 CONTENT_SETTINGS_TYPE_IMAGES,
158 std::string(),
159 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
161 GURL host("http://example.com/");
162 // The value should of course be visible in the regular PrefProvider.
163 EXPECT_EQ(CONTENT_SETTING_ALLOW,
164 GetContentSetting(&pref_content_settings_provider,
165 host,
166 host,
167 CONTENT_SETTINGS_TYPE_IMAGES,
168 std::string(),
169 false));
170 // And also in the OTR version.
171 EXPECT_EQ(CONTENT_SETTING_ALLOW,
172 GetContentSetting(&pref_content_settings_provider_incognito,
173 host,
174 host,
175 CONTENT_SETTINGS_TYPE_IMAGES,
176 std::string(),
177 false));
178 // But the value should not be overridden in the OTR user prefs accidentally.
179 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay(
180 prefs::kContentSettingsPatternPairs));
182 pref_content_settings_provider.ShutdownOnUIThread();
183 pref_content_settings_provider_incognito.ShutdownOnUIThread();
186 TEST(PrefProviderTest, GetContentSettingsValue) {
187 TestingProfile testing_profile;
188 PrefProvider provider(testing_profile.GetPrefs(), false);
190 GURL primary_url("http://example.com/");
191 ContentSettingsPattern primary_pattern =
192 ContentSettingsPattern::FromString("[*.]example.com");
194 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
195 GetContentSetting(&provider,
196 primary_url,
197 primary_url,
198 CONTENT_SETTINGS_TYPE_IMAGES,
199 std::string(),
200 false));
202 EXPECT_EQ(NULL,
203 GetContentSettingValue(&provider,
204 primary_url,
205 primary_url,
206 CONTENT_SETTINGS_TYPE_IMAGES,
207 std::string(),
208 false));
210 provider.SetWebsiteSetting(primary_pattern,
211 primary_pattern,
212 CONTENT_SETTINGS_TYPE_IMAGES,
213 std::string(),
214 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
215 EXPECT_EQ(CONTENT_SETTING_BLOCK,
216 GetContentSetting(&provider,
217 primary_url,
218 primary_url,
219 CONTENT_SETTINGS_TYPE_IMAGES,
220 std::string(),
221 false));
222 scoped_ptr<base::Value> value_ptr(
223 GetContentSettingValue(&provider,
224 primary_url,
225 primary_url,
226 CONTENT_SETTINGS_TYPE_IMAGES,
227 std::string(),
228 false));
229 int int_value = -1;
230 value_ptr->GetAsInteger(&int_value);
231 EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
233 provider.SetWebsiteSetting(primary_pattern,
234 primary_pattern,
235 CONTENT_SETTINGS_TYPE_IMAGES,
236 std::string(),
237 NULL);
238 EXPECT_EQ(NULL,
239 GetContentSettingValue(&provider,
240 primary_url,
241 primary_url,
242 CONTENT_SETTINGS_TYPE_IMAGES,
243 std::string(),
244 false));
245 provider.ShutdownOnUIThread();
248 TEST(PrefProviderTest, Patterns) {
249 TestingProfile testing_profile;
250 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
251 false);
253 GURL host1("http://example.com/");
254 GURL host2("http://www.example.com/");
255 GURL host3("http://example.org/");
256 GURL host4("file:///tmp/test.html");
257 ContentSettingsPattern pattern1 =
258 ContentSettingsPattern::FromString("[*.]example.com");
259 ContentSettingsPattern pattern2 =
260 ContentSettingsPattern::FromString("example.org");
261 ContentSettingsPattern pattern3 =
262 ContentSettingsPattern::FromString("file:///tmp/test.html");
264 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
265 GetContentSetting(&pref_content_settings_provider,
266 host1,
267 host1,
268 CONTENT_SETTINGS_TYPE_IMAGES,
269 std::string(),
270 false));
271 pref_content_settings_provider.SetWebsiteSetting(
272 pattern1,
273 pattern1,
274 CONTENT_SETTINGS_TYPE_IMAGES,
275 std::string(),
276 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
277 EXPECT_EQ(CONTENT_SETTING_BLOCK,
278 GetContentSetting(&pref_content_settings_provider,
279 host1,
280 host1,
281 CONTENT_SETTINGS_TYPE_IMAGES,
282 std::string(),
283 false));
284 EXPECT_EQ(CONTENT_SETTING_BLOCK,
285 GetContentSetting(&pref_content_settings_provider,
286 host2,
287 host2,
288 CONTENT_SETTINGS_TYPE_IMAGES,
289 std::string(),
290 false));
292 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
293 GetContentSetting(&pref_content_settings_provider,
294 host3,
295 host3,
296 CONTENT_SETTINGS_TYPE_IMAGES,
297 std::string(),
298 false));
299 pref_content_settings_provider.SetWebsiteSetting(
300 pattern2,
301 pattern2,
302 CONTENT_SETTINGS_TYPE_IMAGES,
303 std::string(),
304 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
305 EXPECT_EQ(CONTENT_SETTING_BLOCK,
306 GetContentSetting(&pref_content_settings_provider,
307 host3,
308 host3,
309 CONTENT_SETTINGS_TYPE_IMAGES,
310 std::string(),
311 false));
313 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
314 GetContentSetting(&pref_content_settings_provider,
315 host4,
316 host4,
317 CONTENT_SETTINGS_TYPE_IMAGES,
318 std::string(),
319 false));
320 pref_content_settings_provider.SetWebsiteSetting(
321 pattern3,
322 pattern3,
323 CONTENT_SETTINGS_TYPE_IMAGES,
324 std::string(),
325 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
326 EXPECT_EQ(CONTENT_SETTING_BLOCK,
327 GetContentSetting(&pref_content_settings_provider,
328 host4,
329 host4,
330 CONTENT_SETTINGS_TYPE_IMAGES,
331 std::string(),
332 false));
334 pref_content_settings_provider.ShutdownOnUIThread();
337 TEST(PrefProviderTest, ResourceIdentifier) {
338 TestingProfile testing_profile;
339 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
340 false);
342 GURL host("http://example.com/");
343 ContentSettingsPattern pattern =
344 ContentSettingsPattern::FromString("[*.]example.com");
345 std::string resource1("someplugin");
346 std::string resource2("otherplugin");
348 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
349 GetContentSetting(
350 &pref_content_settings_provider,
351 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
352 resource1, false));
353 pref_content_settings_provider.SetWebsiteSetting(
354 pattern,
355 pattern,
356 CONTENT_SETTINGS_TYPE_PLUGINS,
357 resource1,
358 new base::FundamentalValue(CONTENT_SETTING_BLOCK));
359 EXPECT_EQ(CONTENT_SETTING_BLOCK,
360 GetContentSetting(
361 &pref_content_settings_provider,
362 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
363 resource1, false));
364 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
365 GetContentSetting(
366 &pref_content_settings_provider,
367 host, host, CONTENT_SETTINGS_TYPE_PLUGINS,
368 resource2, false));
370 pref_content_settings_provider.ShutdownOnUIThread();
373 TEST(PrefProviderTest, AutoSubmitCertificateContentSetting) {
374 TestingProfile profile;
375 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService();
376 GURL primary_url("https://www.example.com");
377 GURL secondary_url("https://www.sample.com");
379 PrefProvider provider(prefs, false);
381 EXPECT_EQ(CONTENT_SETTING_DEFAULT,
382 GetContentSetting(
383 &provider,
384 primary_url,
385 primary_url,
386 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
387 std::string(),
388 false));
390 provider.SetWebsiteSetting(ContentSettingsPattern::FromURL(primary_url),
391 ContentSettingsPattern::Wildcard(),
392 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
393 std::string(),
394 new base::FundamentalValue(CONTENT_SETTING_ALLOW));
395 EXPECT_EQ(CONTENT_SETTING_ALLOW,
396 GetContentSetting(
397 &provider,
398 primary_url,
399 secondary_url,
400 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
401 std::string(),
402 false));
403 provider.ShutdownOnUIThread();
406 // http://crosbug.com/17760
407 TEST(PrefProviderTest, Deadlock) {
408 TestingPrefServiceSyncable prefs;
409 PrefProvider::RegisterProfilePrefs(prefs.registry());
411 // Chain of events: a preference changes, |PrefProvider| notices it, and reads
412 // and writes the preference. When the preference is written, a notification
413 // is sent, and this used to happen when |PrefProvider| was still holding its
414 // lock.
416 PrefProvider provider(&prefs, false);
417 DeadlockCheckerObserver observer(&prefs, &provider);
419 DictionaryPrefUpdate update(&prefs,
420 prefs::kContentSettingsPatternPairs);
421 base::DictionaryValue* mutable_settings = update.Get();
422 mutable_settings->SetWithoutPathExpansion("www.example.com,*",
423 new base::DictionaryValue());
425 EXPECT_TRUE(observer.notification_received());
427 provider.ShutdownOnUIThread();
430 TEST(PrefProviderTest, LastUsage) {
431 TestingProfile testing_profile;
432 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(),
433 false);
434 base::SimpleTestClock* test_clock = new base::SimpleTestClock;
435 test_clock->SetNow(base::Time::Now());
437 pref_content_settings_provider.SetClockForTesting(
438 scoped_ptr<base::Clock>(test_clock));
439 GURL host("http://example.com/");
440 ContentSettingsPattern pattern =
441 ContentSettingsPattern::FromString("[*.]example.com");
443 base::Time no_usage = pref_content_settings_provider.GetLastUsage(
444 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
445 EXPECT_EQ(no_usage.ToDoubleT(), 0);
447 pref_content_settings_provider.UpdateLastUsage(
448 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
449 base::Time first = pref_content_settings_provider.GetLastUsage(
450 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
452 test_clock->Advance(base::TimeDelta::FromSeconds(10));
454 pref_content_settings_provider.UpdateLastUsage(
455 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
456 base::Time second = pref_content_settings_provider.GetLastUsage(
457 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION);
459 base::TimeDelta delta = second - first;
460 EXPECT_EQ(delta.InSeconds(), 10);
462 pref_content_settings_provider.ShutdownOnUIThread();
466 // TODO(msramek): This tests the correct migration behavior between the old
467 // aggregate dictionary preferences for all content settings types and the new
468 // dictionary preferences for individual types. Remove this when the migration
469 // period is over.
470 TEST(PrefProviderTest, SyncingOldToNew) {
471 TestingPrefServiceSyncable prefs;
472 PrefProvider::RegisterProfilePrefs(prefs.registry());
473 PrefProvider provider(&prefs, false);
475 const std::string pattern = "google.com,*";
476 const std::string resource_id = "abcde12345";
477 base::DictionaryValue* exceptions = new base::DictionaryValue();
478 base::DictionaryValue* plugin_resources = new base::DictionaryValue();
480 // Add exceptions for images and app banner.
481 exceptions->SetIntegerWithoutPathExpansion(
482 GetTypeName(CONTENT_SETTINGS_TYPE_IMAGES), CONTENT_SETTING_ALLOW);
483 exceptions->SetIntegerWithoutPathExpansion(
484 GetTypeName(CONTENT_SETTINGS_TYPE_APP_BANNER), CONTENT_SETTING_ALLOW);
486 // Add a regular exception for plugins, then one with a resource identifier.
487 exceptions->SetIntegerWithoutPathExpansion(
488 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS), CONTENT_SETTING_ALLOW);
489 plugin_resources->SetIntegerWithoutPathExpansion(
490 resource_id, CONTENT_SETTING_BLOCK);
491 exceptions->SetWithoutPathExpansion(
492 "per_plugin", plugin_resources);
494 // Change the old dictionary preference and observe changes
495 // in the new preferences.
497 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs);
498 base::DictionaryValue* old_dictionary = update.Get();
499 old_dictionary->SetWithoutPathExpansion(pattern, exceptions);
502 // The images exception was synced.
504 DictionaryPrefUpdate update(
505 &prefs, prefs::kContentSettingsImagesPatternPairs);
506 const base::DictionaryValue* images_dictionary = update.Get();
508 EXPECT_EQ(1u, images_dictionary->size());
509 const base::DictionaryValue* images_exception;
510 EXPECT_TRUE(images_dictionary->GetDictionaryWithoutPathExpansion(
511 pattern, &images_exception));
513 // And it has a correct value.
514 int images_exception_value = CONTENT_SETTING_DEFAULT;
515 EXPECT_TRUE(images_exception->GetIntegerWithoutPathExpansion(
516 "setting", &images_exception_value));
517 EXPECT_EQ(CONTENT_SETTING_ALLOW, images_exception_value);
520 // The app banner exception was not synced.
522 DictionaryPrefUpdate update(
523 &prefs, prefs::kContentSettingsAppBannerPatternPairs);
524 const base::DictionaryValue* app_banner_dictionary = update.Get();
525 EXPECT_TRUE(app_banner_dictionary->empty());
528 // The plugins exception was synced, together with the resource identifiers.
530 DictionaryPrefUpdate update(
531 &prefs, prefs::kContentSettingsPluginsPatternPairs);
532 const base::DictionaryValue* plugins_dictionary = update.Get();
533 EXPECT_EQ(1u, plugins_dictionary->size());
535 const base::DictionaryValue* plugins_exception;
536 EXPECT_TRUE(plugins_dictionary->GetDictionaryWithoutPathExpansion(
537 pattern, &plugins_exception));
539 int plugins_exception_value = CONTENT_SETTING_DEFAULT;
540 EXPECT_TRUE(plugins_exception->GetIntegerWithoutPathExpansion(
541 "setting", &plugins_exception_value));
542 EXPECT_EQ(CONTENT_SETTING_ALLOW, plugins_exception_value);
544 int resource_exception_value = CONTENT_SETTING_DEFAULT;
545 const base::DictionaryValue* resource_exception;
546 EXPECT_TRUE(plugins_exception->GetDictionaryWithoutPathExpansion(
547 "per_resource", &resource_exception));
548 EXPECT_TRUE(resource_exception->GetIntegerWithoutPathExpansion(
549 resource_id, &resource_exception_value));
550 EXPECT_EQ(CONTENT_SETTING_BLOCK, resource_exception_value);
553 provider.ShutdownOnUIThread();
556 TEST(PrefProviderTest, SyncingNewToOld) {
557 TestingPrefServiceSyncable prefs;
558 PrefProvider::RegisterProfilePrefs(prefs.registry());
559 PrefProvider provider(&prefs, false);
561 const std::string pattern = "google.com,*";
562 const std::string resource_id = "abcde12345";
563 base::DictionaryValue block_exception;
564 block_exception.SetIntegerWithoutPathExpansion(
565 "setting", CONTENT_SETTING_BLOCK);
567 // Add a mouselock exception.
569 DictionaryPrefUpdate update(
570 &prefs, prefs::kContentSettingsMouseLockPatternPairs);
571 base::DictionaryValue* mouselock_dictionary = update.Get();
573 mouselock_dictionary->SetWithoutPathExpansion(
574 pattern, block_exception.DeepCopy());
577 // Add a microphone exception.
579 DictionaryPrefUpdate update(
580 &prefs, prefs::kContentSettingsMediaStreamMicPatternPairs);
581 base::DictionaryValue* microphone_dictionary = update.Get();
583 microphone_dictionary->SetWithoutPathExpansion(
584 pattern, block_exception.DeepCopy());
587 // Add a plugin exception with resource identifiers.
589 DictionaryPrefUpdate update(
590 &prefs, prefs::kContentSettingsPluginsPatternPairs);
591 base::DictionaryValue* plugins_dictionary = update.Get();
593 base::DictionaryValue* plugin_exception = block_exception.DeepCopy();
594 plugins_dictionary->SetWithoutPathExpansion(
595 pattern, plugin_exception);
597 base::DictionaryValue* resource_exception = new base::DictionaryValue();
598 resource_exception->SetIntegerWithoutPathExpansion(
599 resource_id, CONTENT_SETTING_ALLOW);
601 plugin_exception->SetWithoutPathExpansion(
602 "per_resource", resource_exception);
605 // Only the notifications and plugin exceptions should appear in the
606 // old dictionary. We should also have a resource identifiers section
607 // for plugins.
609 DictionaryPrefUpdate update(
610 &prefs, prefs::kContentSettingsPatternPairs);
611 const base::DictionaryValue* old_dictionary = update.Get();
613 const base::DictionaryValue* exception;
614 EXPECT_TRUE(old_dictionary->GetDictionaryWithoutPathExpansion(
615 pattern, &exception));
616 EXPECT_EQ(3u, exception->size());
617 EXPECT_FALSE(exception->HasKey(
618 GetTypeName(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)));
620 int mouselock_exception_value = CONTENT_SETTING_DEFAULT;
621 exception->GetIntegerWithoutPathExpansion(
622 GetTypeName(CONTENT_SETTINGS_TYPE_MOUSELOCK),
623 &mouselock_exception_value);
624 DCHECK_EQ(CONTENT_SETTING_BLOCK, mouselock_exception_value);
626 int plugins_exception_value = CONTENT_SETTING_DEFAULT;
627 exception->GetIntegerWithoutPathExpansion(
628 GetTypeName(CONTENT_SETTINGS_TYPE_PLUGINS),
629 &plugins_exception_value);
630 DCHECK_EQ(CONTENT_SETTING_BLOCK, plugins_exception_value);
632 int resource_exception_value = CONTENT_SETTING_DEFAULT;
633 const base::DictionaryValue* resource_values;
634 exception->GetDictionaryWithoutPathExpansion(
635 "per_plugin", &resource_values);
636 resource_values->GetIntegerWithoutPathExpansion(
637 resource_id, &resource_exception_value);
638 DCHECK_EQ(CONTENT_SETTING_ALLOW, resource_exception_value);
641 provider.ShutdownOnUIThread();
644 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
645 TEST(PrefProviderTest, PMIMigrateOnlyAllow) {
646 TestingPrefServiceSyncable prefs;
647 PrefProvider::RegisterProfilePrefs(prefs.registry());
649 const std::string pattern_1 = "google.com,*";
650 const std::string pattern_2 = "www.google.com,*";
651 base::DictionaryValue* exception_1 = new base::DictionaryValue();
652 base::DictionaryValue* exception_2 = new base::DictionaryValue();
654 // Add both an "allow" and "block" exception for PMI.
655 exception_1->SetIntegerWithoutPathExpansion(
656 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER),
657 CONTENT_SETTING_ALLOW);
658 exception_2->SetIntegerWithoutPathExpansion(
659 GetTypeName(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER),
660 CONTENT_SETTING_BLOCK);
662 // Change the old dictionary preference.
664 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs);
665 base::DictionaryValue* old_dictionary = update.Get();
666 old_dictionary->SetWithoutPathExpansion(pattern_1, exception_1);
667 old_dictionary->SetWithoutPathExpansion(pattern_2, exception_2);
670 // Create the PrefProvider. It should migrate the settings.
671 PrefProvider provider(&prefs, false);
673 // The "block" exception for PMI was migrated, but "allow" was not.
675 DictionaryPrefUpdate update(
676 &prefs, prefs::kContentSettingsProtectedMediaIdentifierPatternPairs);
677 const base::DictionaryValue* pmi_dictionary = update.Get();
678 EXPECT_FALSE(pmi_dictionary->HasKey(pattern_1));
679 EXPECT_TRUE(pmi_dictionary->HasKey(pattern_2));
682 provider.ShutdownOnUIThread();
684 #endif
686 TEST(PrefProviderTest, PrefsMigrateVerbatim) {
687 TestingPrefServiceSyncable prefs;
688 PrefProvider::RegisterProfilePrefs(prefs.registry());
690 const std::string pattern_1 = "google.com,*";
691 const std::string pattern_2 = "www.google.com,*";
692 base::DictionaryValue* exception_1 = new base::DictionaryValue();
693 base::DictionaryValue* exception_2 = new base::DictionaryValue();
694 scoped_ptr<base::DictionaryValue> old_dictionary;
696 // Add two exceptions.
697 exception_1->SetIntegerWithoutPathExpansion(
698 GetTypeName(CONTENT_SETTINGS_TYPE_COOKIES),
699 CONTENT_SETTING_ALLOW);
700 exception_2->SetIntegerWithoutPathExpansion(
701 GetTypeName(CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS),
702 CONTENT_SETTING_BLOCK);
704 // Change the old dictionary preference.
706 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs);
707 base::DictionaryValue* dictionary = update.Get();
708 dictionary->SetWithoutPathExpansion(pattern_1, exception_1);
709 dictionary->SetWithoutPathExpansion(pattern_2, exception_2);
710 old_dictionary.reset(dictionary->DeepCopy());
713 // Create the PrefProvider. It should copy the settings from the old
714 // preference to the new ones.
715 PrefProvider provider(&prefs, false);
717 // Force copying back from the new preferences to the old one.
719 DictionaryPrefUpdate update(
720 &prefs, prefs::kContentSettingsCookiesPatternPairs);
723 DictionaryPrefUpdate update(
724 &prefs, prefs::kContentSettingsAutomaticDownloadsPatternPairs);
727 // Test if the value after copying there and back is the same.
729 DictionaryPrefUpdate update(&prefs, prefs::kContentSettingsPatternPairs);
730 base::DictionaryValue* new_dictionary = update.Get();
731 EXPECT_TRUE(old_dictionary->Equals(new_dictionary));
734 provider.ShutdownOnUIThread();
737 } // namespace content_settings