| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | 5 #ifndef CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ |
| 6 #define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | 6 #define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/pref_registry.h" | 10 #include "base/prefs/pref_registry.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/prefs/testing_pref_store.h" | 12 #include "base/prefs/testing_pref_store.h" |
| 13 #include "chrome/browser/prefs/pref_service_syncable.h" | |
| 14 | 13 |
| 15 class PrefModelAssociator; | |
| 16 class PrefNotifierImpl; | 14 class PrefNotifierImpl; |
| 17 class PrefRegistrySimple; | 15 class PrefRegistrySimple; |
| 18 class PrefRegistrySyncable; | |
| 19 class TestingBrowserProcess; | |
| 20 class TestingPrefStore; | 16 class TestingPrefStore; |
| 21 | 17 |
| 22 // A PrefService subclass for testing. It operates totally in memory and | 18 // A PrefService subclass for testing. It operates totally in memory and |
| 23 // provides additional API for manipulating preferences at the different levels | 19 // provides additional API for manipulating preferences at the different levels |
| 24 // (managed, extension, user) conveniently. | 20 // (managed, extension, user) conveniently. |
| 25 // | 21 // |
| 26 // Use this via its specializations, TestingPrefServiceSimple and | 22 // Use this via its specializations, e.g. TestingPrefServiceSimple. |
| 27 // TestingPrefServiceSyncable. | |
| 28 template <class SuperPrefService> | 23 template <class SuperPrefService> |
| 29 class TestingPrefServiceBase : public SuperPrefService { | 24 class TestingPrefServiceBase : public SuperPrefService { |
| 30 public: | 25 public: |
| 31 virtual ~TestingPrefServiceBase(); | 26 virtual ~TestingPrefServiceBase(); |
| 32 | 27 |
| 33 // Read the value of a preference from the managed layer. Returns NULL if the | 28 // Read the value of a preference from the managed layer. Returns NULL if the |
| 34 // preference is not defined at the managed layer. | 29 // preference is not defined at the managed layer. |
| 35 const Value* GetManagedPref(const char* path) const; | 30 const Value* GetManagedPref(const char* path) const; |
| 36 | 31 |
| 37 // Set a preference on the managed layer and fire observers if the preference | 32 // Set a preference on the managed layer and fire observers if the preference |
| 38 // changed. Assumes ownership of |value|. | 33 // changed. Assumes ownership of |value|. |
| 39 void SetManagedPref(const char* path, Value* value); | 34 void SetManagedPref(const char* path, Value* value); |
| 40 | 35 |
| 41 // Clear the preference on the managed layer and fire observers if the | 36 // Clear the preference on the managed layer and fire observers if the |
| 42 // preference has been defined previously. | 37 // preference has been defined previously. |
| 43 void RemoveManagedPref(const char* path); | 38 void RemoveManagedPref(const char* path); |
| 44 | 39 |
| 45 // Similar to the above, but for user preferences. | 40 // Similar to the above, but for user preferences. |
| 46 const Value* GetUserPref(const char* path) const; | 41 const Value* GetUserPref(const char* path) const; |
| 47 void SetUserPref(const char* path, Value* value); | 42 void SetUserPref(const char* path, Value* value); |
| 48 void RemoveUserPref(const char* path); | 43 void RemoveUserPref(const char* path); |
| 49 | 44 |
| 50 // Similar to the above, but for recommended policy preferences. | 45 // Similar to the above, but for recommended policy preferences. |
| 51 const Value* GetRecommendedPref(const char* path) const; | 46 const Value* GetRecommendedPref(const char* path) const; |
| 52 void SetRecommendedPref(const char* path, Value* value); | 47 void SetRecommendedPref(const char* path, Value* value); |
| 53 void RemoveRecommendedPref(const char* path); | 48 void RemoveRecommendedPref(const char* path); |
| 54 | 49 |
| 50 // Do-nothing implementation for TestingPrefService. |
| 51 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} |
| 52 |
| 55 protected: | 53 protected: |
| 56 TestingPrefServiceBase( | 54 TestingPrefServiceBase( |
| 57 TestingPrefStore* managed_prefs, | 55 TestingPrefStore* managed_prefs, |
| 58 TestingPrefStore* user_prefs, | 56 TestingPrefStore* user_prefs, |
| 59 TestingPrefStore* recommended_prefs, | 57 TestingPrefStore* recommended_prefs, |
| 60 PrefRegistry* pref_registry, | 58 PrefRegistry* pref_registry, |
| 61 PrefNotifierImpl* pref_notifier); | 59 PrefNotifierImpl* pref_notifier); |
| 62 | 60 |
| 63 private: | 61 private: |
| 64 // Reads the value of the preference indicated by |path| from |pref_store|. | 62 // Reads the value of the preference indicated by |path| from |pref_store|. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 90 // an existing TestingPrefServiceSimple instance. On a production | 88 // an existing TestingPrefServiceSimple instance. On a production |
| 91 // PrefService you would do all registrations before constructing | 89 // PrefService you would do all registrations before constructing |
| 92 // it, passing it a PrefRegistry via its constructor (or via | 90 // it, passing it a PrefRegistry via its constructor (or via |
| 93 // e.g. PrefServiceBuilder). | 91 // e.g. PrefServiceBuilder). |
| 94 PrefRegistrySimple* registry(); | 92 PrefRegistrySimple* registry(); |
| 95 | 93 |
| 96 private: | 94 private: |
| 97 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); | 95 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); |
| 98 }; | 96 }; |
| 99 | 97 |
| 100 // Test version of PrefServiceSyncable. | |
| 101 class TestingPrefServiceSyncable | |
| 102 : public TestingPrefServiceBase<PrefServiceSyncable> { | |
| 103 public: | |
| 104 TestingPrefServiceSyncable(); | |
| 105 virtual ~TestingPrefServiceSyncable(); | |
| 106 | |
| 107 // This is provided as a convenience; on a production PrefService | |
| 108 // you would do all registrations before constructing it, passing it | |
| 109 // a PrefRegistry via its constructor (or via | |
| 110 // e.g. PrefServiceBuilder). | |
| 111 PrefRegistrySyncable* registry(); | |
| 112 | |
| 113 private: | |
| 114 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSyncable); | |
| 115 }; | |
| 116 | |
| 117 // Helper class to temporarily set up a |local_state| in the global | |
| 118 // TestingBrowserProcess (for most unit tests it's NULL). | |
| 119 class ScopedTestingLocalState { | |
| 120 public: | |
| 121 explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process); | |
| 122 ~ScopedTestingLocalState(); | |
| 123 | |
| 124 TestingPrefServiceSimple* Get() { | |
| 125 return &local_state_; | |
| 126 } | |
| 127 | |
| 128 private: | |
| 129 TestingBrowserProcess* browser_process_; | |
| 130 TestingPrefServiceSimple local_state_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState); | |
| 133 }; | |
| 134 | |
| 135 template<> | 98 template<> |
| 136 TestingPrefServiceBase<PrefService>::TestingPrefServiceBase( | 99 TestingPrefServiceBase<PrefService>::TestingPrefServiceBase( |
| 137 TestingPrefStore* managed_prefs, | 100 TestingPrefStore* managed_prefs, |
| 138 TestingPrefStore* user_prefs, | 101 TestingPrefStore* user_prefs, |
| 139 TestingPrefStore* recommended_prefs, | 102 TestingPrefStore* recommended_prefs, |
| 140 PrefRegistry* pref_registry, | 103 PrefRegistry* pref_registry, |
| 141 PrefNotifierImpl* pref_notifier); | 104 PrefNotifierImpl* pref_notifier); |
| 142 | 105 |
| 143 template<> | |
| 144 TestingPrefServiceBase<PrefServiceSyncable>::TestingPrefServiceBase( | |
| 145 TestingPrefStore* managed_prefs, | |
| 146 TestingPrefStore* user_prefs, | |
| 147 TestingPrefStore* recommended_prefs, | |
| 148 PrefRegistry* pref_registry, | |
| 149 PrefNotifierImpl* pref_notifier); | |
| 150 | |
| 151 template<class SuperPrefService> | 106 template<class SuperPrefService> |
| 152 TestingPrefServiceBase<SuperPrefService>::~TestingPrefServiceBase() { | 107 TestingPrefServiceBase<SuperPrefService>::~TestingPrefServiceBase() { |
| 153 } | 108 } |
| 154 | 109 |
| 155 template<class SuperPrefService> | 110 template<class SuperPrefService> |
| 156 const Value* TestingPrefServiceBase<SuperPrefService>::GetManagedPref( | 111 const Value* TestingPrefServiceBase<SuperPrefService>::GetManagedPref( |
| 157 const char* path) const { | 112 const char* path) const { |
| 158 return GetPref(managed_prefs_, path); | 113 return GetPref(managed_prefs_, path); |
| 159 } | 114 } |
| 160 | 115 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 pref_store->SetValue(path, value); | 174 pref_store->SetValue(path, value); |
| 220 } | 175 } |
| 221 | 176 |
| 222 template<class SuperPrefService> | 177 template<class SuperPrefService> |
| 223 void TestingPrefServiceBase<SuperPrefService>::RemovePref( | 178 void TestingPrefServiceBase<SuperPrefService>::RemovePref( |
| 224 TestingPrefStore* pref_store, const char* path) { | 179 TestingPrefStore* pref_store, const char* path) { |
| 225 pref_store->RemoveValue(path); | 180 pref_store->RemoveValue(path); |
| 226 } | 181 } |
| 227 | 182 |
| 228 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | 183 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ |
| OLD | NEW |