| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_TEST_TESTING_PREF_SERVICE_H_ | |
| 6 #define CHROME_TEST_TESTING_PREF_SERVICE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "chrome/browser/prefs/pref_service.h" | |
| 11 | |
| 12 class TestingBrowserProcess; | |
| 13 class TestingPrefStore; | |
| 14 | |
| 15 // A PrefService subclass for testing. It operates totally in memory and | |
| 16 // provides additional API for manipulating preferences at the different levels | |
| 17 // (managed, extension, user) conveniently. | |
| 18 class TestingPrefServiceBase : public PrefService { | |
| 19 public: | |
| 20 virtual ~TestingPrefServiceBase(); | |
| 21 | |
| 22 // Read the value of a preference from the managed layer. Returns NULL if the | |
| 23 // preference is not defined at the managed layer. | |
| 24 const Value* GetManagedPref(const char* path) const; | |
| 25 | |
| 26 // Set a preference on the managed layer and fire observers if the preference | |
| 27 // changed. Assumes ownership of |value|. | |
| 28 void SetManagedPref(const char* path, Value* value); | |
| 29 | |
| 30 // Clear the preference on the managed layer and fire observers if the | |
| 31 // preference has been defined previously. | |
| 32 void RemoveManagedPref(const char* path); | |
| 33 | |
| 34 // Similar to the above, but for user preferences. | |
| 35 const Value* GetUserPref(const char* path) const; | |
| 36 void SetUserPref(const char* path, Value* value); | |
| 37 void RemoveUserPref(const char* path); | |
| 38 | |
| 39 // Similar to the above, but for recommended policy preferences. | |
| 40 const Value* GetRecommendedPref(const char* path) const; | |
| 41 void SetRecommendedPref(const char* path, Value* value); | |
| 42 void RemoveRecommendedPref(const char* path); | |
| 43 | |
| 44 protected: | |
| 45 TestingPrefServiceBase( | |
| 46 TestingPrefStore* managed_platform_prefs, | |
| 47 TestingPrefStore* user_prefs, | |
| 48 TestingPrefStore* recommended_platform_prefs); | |
| 49 | |
| 50 private: | |
| 51 // Reads the value of the preference indicated by |path| from |pref_store|. | |
| 52 // Returns NULL if the preference was not found. | |
| 53 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const; | |
| 54 | |
| 55 // Sets the value for |path| in |pref_store|. | |
| 56 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value); | |
| 57 | |
| 58 // Removes the preference identified by |path| from |pref_store|. | |
| 59 void RemovePref(TestingPrefStore* pref_store, const char* path); | |
| 60 | |
| 61 // Pointers to the pref stores our value store uses. | |
| 62 scoped_refptr<TestingPrefStore> managed_platform_prefs_; | |
| 63 scoped_refptr<TestingPrefStore> user_prefs_; | |
| 64 scoped_refptr<TestingPrefStore> recommended_platform_prefs_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); | |
| 67 }; | |
| 68 | |
| 69 // Class for simplified construction of TestPrefServiceBase objects. | |
| 70 class TestingPrefService : public TestingPrefServiceBase { | |
| 71 public: | |
| 72 TestingPrefService(); | |
| 73 virtual ~TestingPrefService(); | |
| 74 | |
| 75 private: | |
| 76 DISALLOW_COPY_AND_ASSIGN(TestingPrefService); | |
| 77 }; | |
| 78 | |
| 79 // Helper class to temporarily set up a |local_state| in the global | |
| 80 // TestingBrowserProcess (for most unit tests it's NULL). | |
| 81 class ScopedTestingLocalState { | |
| 82 public: | |
| 83 explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process); | |
| 84 ~ScopedTestingLocalState(); | |
| 85 | |
| 86 TestingPrefService* Get() { | |
| 87 return &local_state_; | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 TestingBrowserProcess* browser_process_; | |
| 92 TestingPrefService local_state_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState); | |
| 95 }; | |
| 96 | |
| 97 #endif // CHROME_TEST_TESTING_PREF_SERVICE_H_ | |
| OLD | NEW |