| 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/prefs/testing_pref_store.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 10 | 11 |
| 11 class DefaultPrefStore; | 12 class DefaultPrefStore; |
| 12 class PrefModelAssociator; | 13 class PrefModelAssociator; |
| 13 class PrefNotifierImpl; | 14 class PrefNotifierImpl; |
| 14 class TestingBrowserProcess; | 15 class TestingBrowserProcess; |
| 15 class TestingPrefStore; | 16 class TestingPrefStore; |
| 16 | 17 |
| 17 // A PrefService subclass for testing. It operates totally in memory and | 18 // A PrefService subclass for testing. It operates totally in memory and |
| 18 // provides additional API for manipulating preferences at the different levels | 19 // provides additional API for manipulating preferences at the different levels |
| 19 // (managed, extension, user) conveniently. | 20 // (managed, extension, user) conveniently. |
| 20 class TestingPrefServiceBase : public PrefService { | 21 // |
| 22 // Use this via its specializations, TestingPrefServiceSimple and |
| 23 // TestingPrefServiceSyncable. |
| 24 template <class SuperPrefService> |
| 25 class TestingPrefServiceBase : public SuperPrefService { |
| 21 public: | 26 public: |
| 22 virtual ~TestingPrefServiceBase(); | 27 virtual ~TestingPrefServiceBase(); |
| 23 | 28 |
| 24 // Read the value of a preference from the managed layer. Returns NULL if the | 29 // Read the value of a preference from the managed layer. Returns NULL if the |
| 25 // preference is not defined at the managed layer. | 30 // preference is not defined at the managed layer. |
| 26 const Value* GetManagedPref(const char* path) const; | 31 const Value* GetManagedPref(const char* path) const; |
| 27 | 32 |
| 28 // Set a preference on the managed layer and fire observers if the preference | 33 // Set a preference on the managed layer and fire observers if the preference |
| 29 // changed. Assumes ownership of |value|. | 34 // changed. Assumes ownership of |value|. |
| 30 void SetManagedPref(const char* path, Value* value); | 35 void SetManagedPref(const char* path, Value* value); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 42 const Value* GetRecommendedPref(const char* path) const; | 47 const Value* GetRecommendedPref(const char* path) const; |
| 43 void SetRecommendedPref(const char* path, Value* value); | 48 void SetRecommendedPref(const char* path, Value* value); |
| 44 void RemoveRecommendedPref(const char* path); | 49 void RemoveRecommendedPref(const char* path); |
| 45 | 50 |
| 46 protected: | 51 protected: |
| 47 TestingPrefServiceBase( | 52 TestingPrefServiceBase( |
| 48 TestingPrefStore* managed_prefs, | 53 TestingPrefStore* managed_prefs, |
| 49 TestingPrefStore* user_prefs, | 54 TestingPrefStore* user_prefs, |
| 50 TestingPrefStore* recommended_prefs, | 55 TestingPrefStore* recommended_prefs, |
| 51 DefaultPrefStore* default_store, | 56 DefaultPrefStore* default_store, |
| 52 PrefModelAssociator* pref_sync_associator, | |
| 53 PrefNotifierImpl* pref_notifier); | 57 PrefNotifierImpl* pref_notifier); |
| 54 | 58 |
| 55 private: | 59 private: |
| 56 // Reads the value of the preference indicated by |path| from |pref_store|. | 60 // Reads the value of the preference indicated by |path| from |pref_store|. |
| 57 // Returns NULL if the preference was not found. | 61 // Returns NULL if the preference was not found. |
| 58 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const; | 62 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const; |
| 59 | 63 |
| 60 // Sets the value for |path| in |pref_store|. | 64 // Sets the value for |path| in |pref_store|. |
| 61 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value); | 65 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value); |
| 62 | 66 |
| 63 // Removes the preference identified by |path| from |pref_store|. | 67 // Removes the preference identified by |path| from |pref_store|. |
| 64 void RemovePref(TestingPrefStore* pref_store, const char* path); | 68 void RemovePref(TestingPrefStore* pref_store, const char* path); |
| 65 | 69 |
| 66 // Pointers to the pref stores our value store uses. | 70 // Pointers to the pref stores our value store uses. |
| 67 scoped_refptr<TestingPrefStore> managed_prefs_; | 71 scoped_refptr<TestingPrefStore> managed_prefs_; |
| 68 scoped_refptr<TestingPrefStore> user_prefs_; | 72 scoped_refptr<TestingPrefStore> user_prefs_; |
| 69 scoped_refptr<TestingPrefStore> recommended_prefs_; | 73 scoped_refptr<TestingPrefStore> recommended_prefs_; |
| 70 | 74 |
| 71 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); | 75 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); |
| 72 }; | 76 }; |
| 73 | 77 |
| 74 // Class for simplified construction of TestPrefServiceBase objects. | 78 // Test version of PrefServiceSimple. |
| 75 class TestingPrefService : public TestingPrefServiceBase { | 79 class TestingPrefServiceSimple |
| 80 : public TestingPrefServiceBase<PrefServiceSimple> { |
| 76 public: | 81 public: |
| 77 TestingPrefService(); | 82 TestingPrefServiceSimple(); |
| 78 virtual ~TestingPrefService(); | 83 virtual ~TestingPrefServiceSimple(); |
| 79 | 84 |
| 80 private: | 85 private: |
| 81 DISALLOW_COPY_AND_ASSIGN(TestingPrefService); | 86 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); |
| 87 }; |
| 88 |
| 89 // Test version of PrefServiceSyncable. |
| 90 class TestingPrefServiceSyncable |
| 91 : public TestingPrefServiceBase<PrefServiceSyncable> { |
| 92 public: |
| 93 TestingPrefServiceSyncable(); |
| 94 virtual ~TestingPrefServiceSyncable(); |
| 95 |
| 96 private: |
| 97 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSyncable); |
| 82 }; | 98 }; |
| 83 | 99 |
| 84 // Helper class to temporarily set up a |local_state| in the global | 100 // Helper class to temporarily set up a |local_state| in the global |
| 85 // TestingBrowserProcess (for most unit tests it's NULL). | 101 // TestingBrowserProcess (for most unit tests it's NULL). |
| 86 class ScopedTestingLocalState { | 102 class ScopedTestingLocalState { |
| 87 public: | 103 public: |
| 88 explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process); | 104 explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process); |
| 89 ~ScopedTestingLocalState(); | 105 ~ScopedTestingLocalState(); |
| 90 | 106 |
| 91 TestingPrefService* Get() { | 107 TestingPrefServiceSimple* Get() { |
| 92 return &local_state_; | 108 return &local_state_; |
| 93 } | 109 } |
| 94 | 110 |
| 95 private: | 111 private: |
| 96 TestingBrowserProcess* browser_process_; | 112 TestingBrowserProcess* browser_process_; |
| 97 TestingPrefService local_state_; | 113 TestingPrefServiceSimple local_state_; |
| 98 | 114 |
| 99 DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState); | 115 DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState); |
| 100 }; | 116 }; |
| 101 | 117 |
| 118 template<> |
| 119 TestingPrefServiceBase<PrefServiceSimple>::TestingPrefServiceBase( |
| 120 TestingPrefStore* managed_prefs, |
| 121 TestingPrefStore* user_prefs, |
| 122 TestingPrefStore* recommended_prefs, |
| 123 DefaultPrefStore* default_store, |
| 124 PrefNotifierImpl* pref_notifier); |
| 125 |
| 126 template<> |
| 127 TestingPrefServiceBase<PrefServiceSyncable>::TestingPrefServiceBase( |
| 128 TestingPrefStore* managed_prefs, |
| 129 TestingPrefStore* user_prefs, |
| 130 TestingPrefStore* recommended_prefs, |
| 131 DefaultPrefStore* default_store, |
| 132 PrefNotifierImpl* pref_notifier); |
| 133 |
| 134 template<class SuperPrefService> |
| 135 TestingPrefServiceBase<SuperPrefService>::~TestingPrefServiceBase() { |
| 136 } |
| 137 |
| 138 template<class SuperPrefService> |
| 139 const Value* TestingPrefServiceBase<SuperPrefService>::GetManagedPref( |
| 140 const char* path) const { |
| 141 return GetPref(managed_prefs_, path); |
| 142 } |
| 143 |
| 144 template<class SuperPrefService> |
| 145 void TestingPrefServiceBase<SuperPrefService>::SetManagedPref( |
| 146 const char* path, Value* value) { |
| 147 SetPref(managed_prefs_, path, value); |
| 148 } |
| 149 |
| 150 template<class SuperPrefService> |
| 151 void TestingPrefServiceBase<SuperPrefService>::RemoveManagedPref( |
| 152 const char* path) { |
| 153 RemovePref(managed_prefs_, path); |
| 154 } |
| 155 |
| 156 template<class SuperPrefService> |
| 157 const Value* TestingPrefServiceBase<SuperPrefService>::GetUserPref( |
| 158 const char* path) const { |
| 159 return GetPref(user_prefs_, path); |
| 160 } |
| 161 |
| 162 template<class SuperPrefService> |
| 163 void TestingPrefServiceBase<SuperPrefService>::SetUserPref( |
| 164 const char* path, Value* value) { |
| 165 SetPref(user_prefs_, path, value); |
| 166 } |
| 167 |
| 168 template<class SuperPrefService> |
| 169 void TestingPrefServiceBase<SuperPrefService>::RemoveUserPref( |
| 170 const char* path) { |
| 171 RemovePref(user_prefs_, path); |
| 172 } |
| 173 |
| 174 template<class SuperPrefService> |
| 175 const Value* TestingPrefServiceBase<SuperPrefService>::GetRecommendedPref( |
| 176 const char* path) const { |
| 177 return GetPref(recommended_prefs_, path); |
| 178 } |
| 179 |
| 180 template<class SuperPrefService> |
| 181 void TestingPrefServiceBase<SuperPrefService>::SetRecommendedPref( |
| 182 const char* path, Value* value) { |
| 183 SetPref(recommended_prefs_, path, value); |
| 184 } |
| 185 |
| 186 template<class SuperPrefService> |
| 187 void TestingPrefServiceBase<SuperPrefService>::RemoveRecommendedPref( |
| 188 const char* path) { |
| 189 RemovePref(recommended_prefs_, path); |
| 190 } |
| 191 |
| 192 template<class SuperPrefService> |
| 193 const Value* TestingPrefServiceBase<SuperPrefService>::GetPref( |
| 194 TestingPrefStore* pref_store, const char* path) const { |
| 195 const Value* res; |
| 196 return pref_store->GetValue(path, &res) ? res : NULL; |
| 197 } |
| 198 |
| 199 template<class SuperPrefService> |
| 200 void TestingPrefServiceBase<SuperPrefService>::SetPref( |
| 201 TestingPrefStore* pref_store, const char* path, Value* value) { |
| 202 pref_store->SetValue(path, value); |
| 203 } |
| 204 |
| 205 template<class SuperPrefService> |
| 206 void TestingPrefServiceBase<SuperPrefService>::RemovePref( |
| 207 TestingPrefStore* pref_store, const char* path) { |
| 208 pref_store->RemoveValue(path); |
| 209 } |
| 210 |
| 102 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | 211 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ |
| OLD | NEW |