Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/content_settings/content_settings_pref_provider_unittest.cc

Issue 7484072: Migrate geolocation settings to host content settings map. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "chrome/browser/content_settings/content_settings_mock_observer.h" 9 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
10 #include "chrome/browser/prefs/browser_prefs.h" 10 #include "chrome/browser/prefs/browser_prefs.h"
11 #include "chrome/browser/prefs/default_pref_store.h" 11 #include "chrome/browser/prefs/default_pref_store.h"
12 #include "chrome/browser/prefs/incognito_user_pref_store.h" 12 #include "chrome/browser/prefs/incognito_user_pref_store.h"
13 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/prefs/pref_service_mock_builder.h" 14 #include "chrome/browser/prefs/pref_service_mock_builder.h"
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/prefs/scoped_user_pref_update.h"
16 #include "chrome/browser/prefs/testing_pref_store.h" 16 #include "chrome/browser/prefs/testing_pref_store.h"
17 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
19 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
20 #include "chrome/test/base/testing_pref_service.h" 20 #include "chrome/test/base/testing_pref_service.h"
21 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
22 #include "chrome/test/testing_browser_process_test.h" 22 #include "chrome/test/testing_browser_process_test.h"
23 #include "content/browser/browser_thread.h" 23 #include "content/browser/browser_thread.h"
24 #include "googleurl/src/gurl.h" 24 #include "googleurl/src/gurl.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 using ::testing::_; 27 using ::testing::_;
28 28
29 namespace {
30
31 void ExpectObsoleteGeolocationSetting(
32 const DictionaryValue& geo_settings_dictionary,
33 const GURL& primary_origin,
34 const GURL& secondary_origin,
35 ContentSetting expected_setting) {
36
37 DictionaryValue* one_origin_settings;
38 ASSERT_TRUE(geo_settings_dictionary.GetDictionaryWithoutPathExpansion(
39 std::string(primary_origin.spec()), &one_origin_settings));
40 int setting_value;
41 ASSERT_TRUE(one_origin_settings->GetIntegerWithoutPathExpansion(
42 std::string(secondary_origin.spec()), &setting_value));
43 EXPECT_EQ(expected_setting, setting_value);
44 }
45
46 } // namespace
47
29 namespace content_settings { 48 namespace content_settings {
30 49
31 class PrefDefaultProviderTest : public TestingBrowserProcessTest { 50 class PrefDefaultProviderTest : public TestingBrowserProcessTest {
32 public: 51 public:
33 PrefDefaultProviderTest() 52 PrefDefaultProviderTest()
34 : ui_thread_(BrowserThread::UI, &message_loop_), 53 : ui_thread_(BrowserThread::UI, &message_loop_),
35 provider_(profile_.GetPrefs(), false) { 54 provider_(profile_.GetPrefs(), false) {
36 } 55 }
37 ~PrefDefaultProviderTest() { 56 ~PrefDefaultProviderTest() {
38 provider_.ShutdownOnUIThread(); 57 provider_.ShutdownOnUIThread();
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( 413 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
395 GURL("http://www.example.com"), 414 GURL("http://www.example.com"),
396 GURL("http://www.example.com"), 415 GURL("http://www.example.com"),
397 CONTENT_SETTINGS_TYPE_IMAGES, 416 CONTENT_SETTINGS_TYPE_IMAGES,
398 "")); 417 ""));
399 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( 418 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
400 GURL("http://www.example.com"), 419 GURL("http://www.example.com"),
401 GURL("http://www.example.com"), 420 GURL("http://www.example.com"),
402 CONTENT_SETTINGS_TYPE_POPUPS, 421 CONTENT_SETTINGS_TYPE_POPUPS,
403 "")); 422 ""));
404 423 // Test if single pattern settings are properly migrated.
405 // Change obsolete preference. This can happen if a user has enabled sync 424 const_all_settings_dictionary = prefs->GetDictionary(
406 // while using an old version of chrome. 425 prefs::kContentSettingsPatternPairs);
407 { 426 EXPECT_EQ(1U, const_all_settings_dictionary->size());
408 DictionaryPrefUpdate update(prefs, prefs::kContentSettingsPatterns); 427 EXPECT_FALSE(const_all_settings_dictionary->HasKey(pattern.ToString()));
409 DictionaryValue* mutable_patterns = update.Get(); 428 EXPECT_TRUE(const_all_settings_dictionary->HasKey(
410 DictionaryValue* mutable_settings = NULL; 429 pattern.ToString() + "," +
411 std::string key = pattern.ToString(); 430 ContentSettingsPattern::Wildcard().ToString()));
412 mutable_patterns->GetDictionaryWithoutPathExpansion(key, &mutable_settings);
413 ASSERT_TRUE(mutable_settings != NULL) << "Dictionary has no key: " << key;
414 mutable_settings->SetInteger("javascript", CONTENT_SETTING_BLOCK);
415 }
416
417 // Test if the changed single pattern setting was migrated correctly.
418 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
419 GURL("http://www.example.com"),
420 GURL("http://www.foo.com"),
421 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
422 ""));
423 431
424 provider.ShutdownOnUIThread(); 432 provider.ShutdownOnUIThread();
425 } 433 }
426 434
427 TEST_F(PrefProviderTest, SyncObsoletePref) { 435 TEST_F(PrefProviderTest, SyncObsoletePref) {
428 TestingProfile profile; 436 TestingProfile profile;
429 PrefService* prefs = profile.GetPrefs(); 437 PrefService* prefs = profile.GetPrefs();
430 content_settings::PrefProvider provider(prefs, false); 438 content_settings::PrefProvider provider(prefs, false);
431 439
432 // Assert pre-condition. 440 // Assert pre-condition.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 &settings); 490 &settings);
483 ASSERT_TRUE(NULL != settings) << "Dictionary has no key: " 491 ASSERT_TRUE(NULL != settings) << "Dictionary has no key: "
484 << primary_pattern.ToString(); 492 << primary_pattern.ToString();
485 ASSERT_EQ(1U, settings->size()); 493 ASSERT_EQ(1U, settings->size());
486 settings->GetInteger("javascript", &setting_value); 494 settings->GetInteger("javascript", &setting_value);
487 EXPECT_EQ(setting_value, CONTENT_SETTING_ALLOW); 495 EXPECT_EQ(setting_value, CONTENT_SETTING_ALLOW);
488 496
489 provider.ShutdownOnUIThread(); 497 provider.ShutdownOnUIThread();
490 } 498 }
491 499
500 TEST_F(PrefProviderTest, MigrateObsoleteGeolocationPref) {
501 TestingProfile profile;
502 PrefService* prefs = profile.GetPrefs();
503 GURL secondary_url("http://www.foo.com");
504 GURL primary_url("http://www.bar.com");
505
506 // Set obsolete preference.
507 DictionaryValue* secondary_patterns_dictionary = new DictionaryValue();
508 secondary_patterns_dictionary->SetWithoutPathExpansion(
509 secondary_url.spec(),
510 Value::CreateIntegerValue(CONTENT_SETTING_BLOCK));
511 scoped_ptr<DictionaryValue> geolocation_settings_dictionary(
512 new DictionaryValue());
513 geolocation_settings_dictionary->SetWithoutPathExpansion(
514 primary_url.spec(), secondary_patterns_dictionary);
515 prefs->Set(prefs::kGeolocationContentSettings,
516 *geolocation_settings_dictionary);
517
518
519 content_settings::PrefProvider provider(prefs, false);
520
521 // Test if the migrated settings are loaded and available.
522 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting(
523 primary_url,
524 secondary_url,
525 CONTENT_SETTINGS_TYPE_GEOLOCATION,
526 ""));
527 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting(
528 GURL("http://www.example.com"),
529 secondary_url,
530 CONTENT_SETTINGS_TYPE_GEOLOCATION,
531 ""));
532 // Check if the settings where migrated correctly.
533 const DictionaryValue* const_all_settings_dictionary =
534 prefs->GetDictionary(prefs::kContentSettingsPatternPairs);
535 EXPECT_EQ(1U, const_all_settings_dictionary->size());
536 EXPECT_TRUE(const_all_settings_dictionary->HasKey(
537 ContentSettingsPattern::FromURLNoWildcard(primary_url).ToString() + "," +
538 ContentSettingsPattern::FromURLNoWildcard(secondary_url).ToString()));
539 // Check that geolocation settings were not synced to the obsolete content
540 // settings pattern preference.
541 const DictionaryValue* const_obsolete_patterns_dictionary =
542 prefs->GetDictionary(prefs::kContentSettingsPatterns);
543 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty());
544
545 // Change obsolete preference. This could be triggered by sync if sync is used
546 // with an old version of chrome.
547 secondary_patterns_dictionary = new DictionaryValue();
548 secondary_patterns_dictionary->SetWithoutPathExpansion(
549 secondary_url.spec(),
550 Value::CreateIntegerValue(CONTENT_SETTING_ALLOW));
551 geolocation_settings_dictionary.reset(new DictionaryValue());
552 geolocation_settings_dictionary->SetWithoutPathExpansion(
553 primary_url.spec(), secondary_patterns_dictionary);
554 prefs->Set(prefs::kGeolocationContentSettings,
555 *geolocation_settings_dictionary);
556
557 // Test if the changed obsolete preference was migrated correctly.
558 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting(
559 primary_url,
560 secondary_url,
561 CONTENT_SETTINGS_TYPE_GEOLOCATION,
562 ""));
563 // Check that geolocation settings were not synced to the obsolete content
564 // settings pattern preference.
565 const_obsolete_patterns_dictionary =
566 prefs->GetDictionary(prefs::kContentSettingsPatterns);
567 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty());
568
569 provider.ShutdownOnUIThread();
570 }
571
572 TEST_F(PrefProviderTest, SyncObsoleteGeolocationPref) {
573 TestingProfile profile;
574 PrefService* prefs = profile.GetPrefs();
575
576 content_settings::PrefProvider provider(prefs, false);
577
578 // Changing the preferences prefs::kContentSettingsPatternPairs.
579 ContentSettingsPattern primary_pattern=
580 ContentSettingsPattern::FromString("http://www.bar.com");
581 ContentSettingsPattern primary_pattern_2 =
582 ContentSettingsPattern::FromString("http://www.example.com");
583 ContentSettingsPattern secondary_pattern =
584 ContentSettingsPattern::FromString("http://www.foo.com");
585 scoped_ptr<DictionaryValue> settings_dictionary(new DictionaryValue());
586 settings_dictionary->SetInteger("geolocation", CONTENT_SETTING_BLOCK);
587 {
588 DictionaryPrefUpdate update(prefs,
589 prefs::kContentSettingsPatternPairs);
590 DictionaryValue* all_settings_dictionary = update.Get();
591 std::string key(
592 primary_pattern.ToString()+ "," +
593 secondary_pattern.ToString());
594 all_settings_dictionary->SetWithoutPathExpansion(
595 key, settings_dictionary->DeepCopy());
596
597 key = std::string(
598 primary_pattern_2.ToString()+ "," +
599 secondary_pattern.ToString());
600 all_settings_dictionary->SetWithoutPathExpansion(
601 key, settings_dictionary->DeepCopy());
602 }
603
604 // Test if the obsolete geolocation preference is kept in sync if the new
605 // preference is changed by a sync.
606 GURL primary_url("http://www.bar.com");
607 GURL primary_url_2("http://www.example.com");
608 GURL secondary_url("http://www.foo.com");
609
610 const DictionaryValue* geo_settings_dictionary =
611 prefs->GetDictionary(prefs::kGeolocationContentSettings);
612 EXPECT_EQ(2U, geo_settings_dictionary->size());
613 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary,
614 primary_url,
615 secondary_url,
616 CONTENT_SETTING_BLOCK);
617 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary,
618 primary_url_2,
619 secondary_url,
620 CONTENT_SETTING_BLOCK);
621
622 provider.ShutdownOnUIThread();
623 }
624
492 } // namespace content_settings 625 } // namespace content_settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698