| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 virtual void SetUp() { | 92 virtual void SetUp() { |
| 93 // Create TestingPrefStores. | 93 // Create TestingPrefStores. |
| 94 CreateManagedPlatformPrefs(); | 94 CreateManagedPlatformPrefs(); |
| 95 CreateDeviceManagementPrefs(); | 95 CreateDeviceManagementPrefs(); |
| 96 CreateExtensionPrefs(); | 96 CreateExtensionPrefs(); |
| 97 CreateCommandLinePrefs(); | 97 CreateCommandLinePrefs(); |
| 98 CreateUserPrefs(); | 98 CreateUserPrefs(); |
| 99 CreateRecommendedPrefs(); | 99 CreateRecommendedPrefs(); |
| 100 CreateDefaultPrefs(); | 100 CreateDefaultPrefs(); |
| 101 | 101 |
| 102 // Create a new pref-value-store. | 102 // Create a fresh PrefValueStore. |
| 103 pref_value_store_ = new PrefValueStore( | 103 pref_value_store_ = new PrefValueStore( |
| 104 managed_platform_pref_store_, | 104 managed_platform_pref_store_, |
| 105 device_management_pref_store_, | 105 device_management_pref_store_, |
| 106 extension_pref_store_, | 106 extension_pref_store_, |
| 107 command_line_pref_store_, | 107 command_line_pref_store_, |
| 108 user_pref_store_, | 108 user_pref_store_, |
| 109 recommended_pref_store_, | 109 recommended_pref_store_, |
| 110 default_pref_store_, | 110 default_pref_store_, |
| 111 &pref_notifier_, | 111 &pref_notifier_); |
| 112 NULL); | |
| 113 | 112 |
| 114 // Register prefs with the PrefValueStore. | 113 // Register prefs with the PrefValueStore. |
| 115 pref_value_store_->RegisterPreferenceType(prefs::kApplicationLocale, | 114 pref_value_store_->RegisterPreferenceType(prefs::kApplicationLocale, |
| 116 Value::TYPE_STRING); | 115 Value::TYPE_STRING); |
| 117 pref_value_store_->RegisterPreferenceType(prefs::kCurrentThemeID, | 116 pref_value_store_->RegisterPreferenceType(prefs::kCurrentThemeID, |
| 118 Value::TYPE_STRING); | 117 Value::TYPE_STRING); |
| 119 pref_value_store_->RegisterPreferenceType( | 118 pref_value_store_->RegisterPreferenceType( |
| 120 prefs::kDefaultSearchProviderName, | 119 prefs::kDefaultSearchProviderName, |
| 121 Value::TYPE_STRING); | 120 Value::TYPE_STRING); |
| 122 pref_value_store_->RegisterPreferenceType(prefs::kDeleteCache, | 121 pref_value_store_->RegisterPreferenceType(prefs::kDeleteCache, |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 // Test a preference from the default pref store. | 611 // Test a preference from the default pref store. |
| 613 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kDefaultPref)); | 612 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kDefaultPref)); |
| 614 EXPECT_TRUE( | 613 EXPECT_TRUE( |
| 615 pref_value_store_->PrefValueFromDefaultStore(prefs::kDefaultPref)); | 614 pref_value_store_->PrefValueFromDefaultStore(prefs::kDefaultPref)); |
| 616 | 615 |
| 617 // Test a preference for which the PrefValueStore does not contain a value. | 616 // Test a preference for which the PrefValueStore does not contain a value. |
| 618 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref)); | 617 ASSERT_FALSE(pref_value_store_->HasPrefPath(prefs::kMissingPref)); |
| 619 EXPECT_FALSE( | 618 EXPECT_FALSE( |
| 620 pref_value_store_->PrefValueFromDefaultStore(prefs::kMissingPref)); | 619 pref_value_store_->PrefValueFromDefaultStore(prefs::kMissingPref)); |
| 621 } | 620 } |
| 622 | |
| 623 // TODO(mnissler, danno): Clean this up when refactoring | |
| 624 // ConfigurationPolicyPrefStore. | |
| 625 class PrefValueStorePolicyRefreshTest : public testing::Test { | |
| 626 protected: | |
| 627 // Records preference changes. | |
| 628 class PrefChangeRecorder { | |
| 629 public: | |
| 630 void Record(const std::string& pref_name) { | |
| 631 changed_prefs_.insert(pref_name); | |
| 632 } | |
| 633 | |
| 634 void Clear() { | |
| 635 changed_prefs_.clear(); | |
| 636 } | |
| 637 | |
| 638 const std::set<std::string>& changed_prefs() { return changed_prefs_; } | |
| 639 | |
| 640 private: | |
| 641 std::set<std::string> changed_prefs_; | |
| 642 }; | |
| 643 | |
| 644 virtual void SetUp() { | |
| 645 using policy::ConfigurationPolicyPrefStore; | |
| 646 | |
| 647 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &loop_)), | |
| 648 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &loop_)), | |
| 649 policy_provider_.reset(new policy::DummyConfigurationPolicyProvider( | |
| 650 policy::ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList())); | |
| 651 | |
| 652 ConfigurationPolicyPrefStore* managed_store = | |
| 653 NewConfigurationPolicyPrefStore(); | |
| 654 managed_store->prefs()->SetString(prefs::kHomePage, | |
| 655 managed_platform_pref::kHomepageValue); | |
| 656 expected_differing_paths_.insert(prefs::kHomePage); | |
| 657 | |
| 658 ConfigurationPolicyPrefStore* device_management_store = | |
| 659 NewConfigurationPolicyPrefStore(); | |
| 660 device_management_store->prefs()->SetString( | |
| 661 prefs::kDefaultSearchProviderName, | |
| 662 device_management_pref::kSearchProviderNameValue); | |
| 663 expected_differing_paths_.insert("default_search_provider"); | |
| 664 expected_differing_paths_.insert(prefs::kDefaultSearchProviderName); | |
| 665 | |
| 666 ConfigurationPolicyPrefStore* recommended_store = | |
| 667 NewConfigurationPolicyPrefStore(); | |
| 668 recommended_store->prefs()->SetInteger(prefs::kStabilityLaunchCount, | |
| 669 recommended_pref::kStabilityLaunchCountValue); | |
| 670 recommended_store->prefs()->SetBoolean(prefs::kRecommendedPref, | |
| 671 recommended_pref::kRecommendedPrefValue); | |
| 672 | |
| 673 expected_differing_paths_.insert("this"); | |
| 674 expected_differing_paths_.insert("this.pref"); | |
| 675 expected_differing_paths_.insert(prefs::kRecommendedPref); | |
| 676 expected_differing_paths_.insert("user_experience_metrics"); | |
| 677 expected_differing_paths_.insert("user_experience_metrics.stability"); | |
| 678 expected_differing_paths_.insert(prefs::kStabilityLaunchCount); | |
| 679 | |
| 680 pref_value_store_ = new PrefValueStore( | |
| 681 managed_store, | |
| 682 device_management_store, | |
| 683 NULL, | |
| 684 NULL, | |
| 685 new TestingPrefStore(), | |
| 686 recommended_store, | |
| 687 NULL, | |
| 688 &pref_notifier_, | |
| 689 NULL); | |
| 690 } | |
| 691 | |
| 692 virtual void TearDown() { | |
| 693 loop_.RunAllPending(); | |
| 694 pref_value_store_ = NULL; | |
| 695 file_thread_.reset(); | |
| 696 ui_thread_.reset(); | |
| 697 } | |
| 698 | |
| 699 // Creates a new ConfigurationPolicyPrefStore for testing. | |
| 700 policy::ConfigurationPolicyPrefStore* NewConfigurationPolicyPrefStore() { | |
| 701 return new policy::ConfigurationPolicyPrefStore(policy_provider_.get()); | |
| 702 } | |
| 703 | |
| 704 // A vector of the preferences paths in policy PrefStores that are set at the | |
| 705 // beginning of a test. Can be modified by the test to track changes that it | |
| 706 // makes to the preferences stored in the managed and recommended PrefStores. | |
| 707 std::set<std::string> expected_differing_paths_; | |
| 708 | |
| 709 MessageLoop loop_; | |
| 710 MockPrefNotifier pref_notifier_; | |
| 711 scoped_refptr<PrefValueStore> pref_value_store_; | |
| 712 | |
| 713 private: | |
| 714 scoped_ptr<BrowserThread> ui_thread_; | |
| 715 scoped_ptr<BrowserThread> file_thread_; | |
| 716 | |
| 717 scoped_ptr<policy::DummyConfigurationPolicyProvider> policy_provider_; | |
| 718 }; | |
| 719 | |
| 720 TEST_F(PrefValueStorePolicyRefreshTest, TestPolicyRefresh) { | |
| 721 // pref_value_store_ is initialized by PrefValueStoreTest to have values in | |
| 722 // the managed platform, device management and recommended stores. By | |
| 723 // replacing them with dummy stores, all of the paths of the prefs originally | |
| 724 // in the managed platform, device management and recommended stores should | |
| 725 // change. | |
| 726 pref_value_store_->RefreshPolicyPrefs(); | |
| 727 | |
| 728 PrefChangeRecorder recorder; | |
| 729 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)) | |
| 730 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record)); | |
| 731 loop_.RunAllPending(); | |
| 732 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 733 } | |
| 734 | |
| 735 TEST_F(PrefValueStorePolicyRefreshTest, TestRefreshPolicyPrefsCompletion) { | |
| 736 using policy::ConfigurationPolicyPrefStore; | |
| 737 PrefChangeRecorder recorder; | |
| 738 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)) | |
| 739 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record)); | |
| 740 | |
| 741 // Test changed preferences in the managed platform store and removed | |
| 742 // preferences in the recommended store. In addition to "homepage", the other | |
| 743 // prefs that are set by default in the test class are removed by the | |
| 744 // DummyStore. | |
| 745 scoped_ptr<ConfigurationPolicyPrefStore> new_managed_platform_store( | |
| 746 NewConfigurationPolicyPrefStore()); | |
| 747 new_managed_platform_store->prefs()->SetString("homepage", | |
| 748 "some other changed homepage"); | |
| 749 | |
| 750 recorder.Clear(); | |
| 751 pref_value_store_->RefreshPolicyPrefsCompletion( | |
| 752 new_managed_platform_store.release(), | |
| 753 NewConfigurationPolicyPrefStore(), | |
| 754 NewConfigurationPolicyPrefStore()); | |
| 755 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 756 | |
| 757 // Test properties that have been removed from the managed platform store. | |
| 758 // Homepage is still set in managed prefs. | |
| 759 expected_differing_paths_.clear(); | |
| 760 expected_differing_paths_.insert(prefs::kHomePage); | |
| 761 | |
| 762 recorder.Clear(); | |
| 763 pref_value_store_->RefreshPolicyPrefsCompletion( | |
| 764 NewConfigurationPolicyPrefStore(), | |
| 765 NewConfigurationPolicyPrefStore(), | |
| 766 NewConfigurationPolicyPrefStore()); | |
| 767 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 768 | |
| 769 // Test properties that are added to the device management store. | |
| 770 expected_differing_paths_.clear(); | |
| 771 expected_differing_paths_.insert(prefs::kHomePage); | |
| 772 scoped_ptr<ConfigurationPolicyPrefStore> new_device_management_store( | |
| 773 NewConfigurationPolicyPrefStore()); | |
| 774 new_device_management_store->prefs()->SetString( | |
| 775 "homepage", "some other changed homepage"); | |
| 776 | |
| 777 recorder.Clear(); | |
| 778 pref_value_store_->RefreshPolicyPrefsCompletion( | |
| 779 NewConfigurationPolicyPrefStore(), | |
| 780 new_device_management_store.release(), | |
| 781 NewConfigurationPolicyPrefStore()); | |
| 782 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 783 | |
| 784 // Test properties that are added to the recommended store. | |
| 785 scoped_ptr<ConfigurationPolicyPrefStore> new_recommended_store( | |
| 786 NewConfigurationPolicyPrefStore()); | |
| 787 new_recommended_store->prefs()->SetString("homepage", | |
| 788 "some other changed homepage 2"); | |
| 789 expected_differing_paths_.clear(); | |
| 790 expected_differing_paths_.insert(prefs::kHomePage); | |
| 791 | |
| 792 recorder.Clear(); | |
| 793 pref_value_store_->RefreshPolicyPrefsCompletion( | |
| 794 NewConfigurationPolicyPrefStore(), | |
| 795 NewConfigurationPolicyPrefStore(), | |
| 796 new_recommended_store.release()); | |
| 797 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 798 | |
| 799 // Test adding a multi-key path. | |
| 800 new_managed_platform_store.reset(NewConfigurationPolicyPrefStore()); | |
| 801 new_managed_platform_store->prefs()->SetString("segment1.segment2", "value"); | |
| 802 expected_differing_paths_.clear(); | |
| 803 expected_differing_paths_.insert(prefs::kHomePage); | |
| 804 expected_differing_paths_.insert("segment1"); | |
| 805 expected_differing_paths_.insert("segment1.segment2"); | |
| 806 | |
| 807 recorder.Clear(); | |
| 808 pref_value_store_->RefreshPolicyPrefsCompletion( | |
| 809 new_managed_platform_store.release(), | |
| 810 NewConfigurationPolicyPrefStore(), | |
| 811 NewConfigurationPolicyPrefStore()); | |
| 812 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 813 } | |
| 814 | |
| 815 TEST_F(PrefValueStorePolicyRefreshTest, TestConcurrentPolicyRefresh) { | |
| 816 PrefChangeRecorder recorder; | |
| 817 EXPECT_CALL(pref_notifier_, OnPreferenceChanged(_)) | |
| 818 .WillRepeatedly(Invoke(&recorder, &PrefChangeRecorder::Record)); | |
| 819 | |
| 820 BrowserThread::PostTask( | |
| 821 BrowserThread::UI, FROM_HERE, | |
| 822 NewRunnableMethod( | |
| 823 pref_value_store_.get(), | |
| 824 &PrefValueStore::RefreshPolicyPrefs)); | |
| 825 | |
| 826 BrowserThread::PostTask( | |
| 827 BrowserThread::UI, FROM_HERE, | |
| 828 NewRunnableMethod( | |
| 829 pref_value_store_.get(), | |
| 830 &PrefValueStore::RefreshPolicyPrefs)); | |
| 831 | |
| 832 BrowserThread::PostTask( | |
| 833 BrowserThread::UI, FROM_HERE, | |
| 834 NewRunnableMethod( | |
| 835 pref_value_store_.get(), | |
| 836 &PrefValueStore::RefreshPolicyPrefs)); | |
| 837 | |
| 838 loop_.RunAllPending(); | |
| 839 EXPECT_EQ(expected_differing_paths_, recorder.changed_prefs()); | |
| 840 } | |
| OLD | NEW |