| 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 #include "chrome/test/base/testing_pref_service.h" | 5 #include "chrome/test/base/testing_pref_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/prefs/default_pref_store.h" | 8 #include "base/prefs/default_pref_store.h" |
| 9 #include "base/prefs/testing_pref_store.h" | |
| 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 11 #include "chrome/browser/prefs/browser_prefs.h" | 9 #include "chrome/browser/prefs/browser_prefs.h" |
| 12 #include "chrome/browser/prefs/command_line_pref_store.h" | |
| 13 #include "chrome/browser/prefs/pref_model_associator.h" | |
| 14 #include "chrome/browser/prefs/pref_notifier_impl.h" | 10 #include "chrome/browser/prefs/pref_notifier_impl.h" |
| 15 #include "chrome/browser/prefs/pref_value_store.h" | 11 #include "chrome/browser/prefs/pref_value_store.h" |
| 16 #include "chrome/test/base/testing_browser_process.h" | 12 #include "chrome/test/base/testing_browser_process.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | |
| 19 | 14 |
| 20 namespace { | 15 namespace { |
| 21 | 16 |
| 22 // Do-nothing implementation for PrefService. | 17 // Do-nothing implementation for TestingPrefService. |
| 23 void HandleReadError(PersistentPrefStore::PrefReadError error) { | 18 void HandleReadError(PersistentPrefStore::PrefReadError error) { |
| 24 } | 19 } |
| 25 | 20 |
| 26 } // namespace | 21 } // namespace |
| 27 | 22 |
| 28 TestingPrefServiceBase::TestingPrefServiceBase( | 23 template<> |
| 24 TestingPrefServiceBase<PrefServiceSimple>::TestingPrefServiceBase( |
| 29 TestingPrefStore* managed_prefs, | 25 TestingPrefStore* managed_prefs, |
| 30 TestingPrefStore* user_prefs, | 26 TestingPrefStore* user_prefs, |
| 31 TestingPrefStore* recommended_prefs, | 27 TestingPrefStore* recommended_prefs, |
| 32 DefaultPrefStore* default_store, | 28 DefaultPrefStore* default_store, |
| 33 PrefModelAssociator* pref_sync_associator, | |
| 34 PrefNotifierImpl* pref_notifier) | 29 PrefNotifierImpl* pref_notifier) |
| 35 : PrefService(pref_notifier, | 30 : PrefServiceSimple(pref_notifier, |
| 36 new PrefValueStore( | 31 new PrefValueStore( |
| 37 managed_prefs, | 32 managed_prefs, |
| 38 NULL, | 33 NULL, |
| 39 NULL, | 34 NULL, |
| 40 user_prefs, | 35 user_prefs, |
| 41 recommended_prefs, | 36 recommended_prefs, |
| 42 default_store, | 37 default_store, |
| 43 pref_sync_associator, | 38 pref_notifier), |
| 44 pref_notifier), | 39 user_prefs, |
| 45 user_prefs, | 40 default_store, |
| 46 default_store, | 41 base::Bind(&HandleReadError), |
| 47 pref_sync_associator, | 42 false), |
| 48 base::Bind(&HandleReadError), | |
| 49 false), | |
| 50 managed_prefs_(managed_prefs), | 43 managed_prefs_(managed_prefs), |
| 51 user_prefs_(user_prefs), | 44 user_prefs_(user_prefs), |
| 52 recommended_prefs_(recommended_prefs) { | 45 recommended_prefs_(recommended_prefs) { |
| 53 } | 46 } |
| 54 | 47 |
| 55 TestingPrefServiceBase::~TestingPrefServiceBase() { | 48 template<> |
| 49 TestingPrefServiceBase<PrefServiceSyncable>::TestingPrefServiceBase( |
| 50 TestingPrefStore* managed_prefs, |
| 51 TestingPrefStore* user_prefs, |
| 52 TestingPrefStore* recommended_prefs, |
| 53 DefaultPrefStore* default_store, |
| 54 PrefNotifierImpl* pref_notifier) |
| 55 : PrefServiceSyncable(pref_notifier, |
| 56 new PrefValueStore( |
| 57 managed_prefs, |
| 58 NULL, |
| 59 NULL, |
| 60 user_prefs, |
| 61 recommended_prefs, |
| 62 default_store, |
| 63 pref_notifier), |
| 64 user_prefs, |
| 65 default_store, |
| 66 base::Bind(&HandleReadError), |
| 67 false), |
| 68 managed_prefs_(managed_prefs), |
| 69 user_prefs_(user_prefs), |
| 70 recommended_prefs_(recommended_prefs) { |
| 56 } | 71 } |
| 57 | 72 |
| 58 const Value* TestingPrefServiceBase::GetManagedPref(const char* path) const { | 73 TestingPrefServiceSimple::TestingPrefServiceSimple() |
| 59 return GetPref(managed_prefs_, path); | 74 : TestingPrefServiceBase<PrefServiceSimple>(new TestingPrefStore(), |
| 75 new TestingPrefStore(), |
| 76 new TestingPrefStore(), |
| 77 new DefaultPrefStore(), |
| 78 new PrefNotifierImpl()) { |
| 60 } | 79 } |
| 61 | 80 |
| 62 void TestingPrefServiceBase::SetManagedPref(const char* path, Value* value) { | 81 TestingPrefServiceSimple::~TestingPrefServiceSimple() { |
| 63 SetPref(managed_prefs_, path, value); | |
| 64 } | 82 } |
| 65 | 83 |
| 66 void TestingPrefServiceBase::RemoveManagedPref(const char* path) { | 84 TestingPrefServiceSyncable::TestingPrefServiceSyncable() |
| 67 RemovePref(managed_prefs_, path); | 85 : TestingPrefServiceBase<PrefServiceSyncable>(new TestingPrefStore(), |
| 86 new TestingPrefStore(), |
| 87 new TestingPrefStore(), |
| 88 new DefaultPrefStore(), |
| 89 new PrefNotifierImpl()) { |
| 68 } | 90 } |
| 69 | 91 |
| 70 const Value* TestingPrefServiceBase::GetUserPref(const char* path) const { | 92 TestingPrefServiceSyncable::~TestingPrefServiceSyncable() { |
| 71 return GetPref(user_prefs_, path); | |
| 72 } | |
| 73 | |
| 74 void TestingPrefServiceBase::SetUserPref(const char* path, Value* value) { | |
| 75 SetPref(user_prefs_, path, value); | |
| 76 } | |
| 77 | |
| 78 void TestingPrefServiceBase::RemoveUserPref(const char* path) { | |
| 79 RemovePref(user_prefs_, path); | |
| 80 } | |
| 81 | |
| 82 const Value* TestingPrefServiceBase::GetRecommendedPref( | |
| 83 const char* path) const { | |
| 84 return GetPref(recommended_prefs_, path); | |
| 85 } | |
| 86 | |
| 87 void TestingPrefServiceBase::SetRecommendedPref( | |
| 88 const char* path, Value* value) { | |
| 89 SetPref(recommended_prefs_, path, value); | |
| 90 } | |
| 91 | |
| 92 void TestingPrefServiceBase::RemoveRecommendedPref(const char* path) { | |
| 93 RemovePref(recommended_prefs_, path); | |
| 94 } | |
| 95 | |
| 96 const Value* TestingPrefServiceBase::GetPref(TestingPrefStore* pref_store, | |
| 97 const char* path) const { | |
| 98 const Value* res; | |
| 99 return pref_store->GetValue(path, &res) ? res : NULL; | |
| 100 } | |
| 101 | |
| 102 void TestingPrefServiceBase::SetPref(TestingPrefStore* pref_store, | |
| 103 const char* path, | |
| 104 Value* value) { | |
| 105 pref_store->SetValue(path, value); | |
| 106 } | |
| 107 | |
| 108 void TestingPrefServiceBase::RemovePref(TestingPrefStore* pref_store, | |
| 109 const char* path) { | |
| 110 pref_store->RemoveValue(path); | |
| 111 } | |
| 112 | |
| 113 TestingPrefService::TestingPrefService() | |
| 114 : TestingPrefServiceBase(new TestingPrefStore(), | |
| 115 new TestingPrefStore(), | |
| 116 new TestingPrefStore(), | |
| 117 new DefaultPrefStore(), | |
| 118 new PrefModelAssociator(), | |
| 119 new PrefNotifierImpl()) { | |
| 120 } | |
| 121 | |
| 122 TestingPrefService::~TestingPrefService() { | |
| 123 } | 93 } |
| 124 | 94 |
| 125 ScopedTestingLocalState::ScopedTestingLocalState( | 95 ScopedTestingLocalState::ScopedTestingLocalState( |
| 126 TestingBrowserProcess* browser_process) | 96 TestingBrowserProcess* browser_process) |
| 127 : browser_process_(browser_process) { | 97 : browser_process_(browser_process) { |
| 128 chrome::RegisterLocalState(&local_state_); | 98 chrome::RegisterLocalState(&local_state_); |
| 129 EXPECT_FALSE(browser_process->local_state()); | 99 EXPECT_FALSE(browser_process->local_state()); |
| 130 browser_process->SetLocalState(&local_state_); | 100 browser_process->SetLocalState(&local_state_); |
| 131 } | 101 } |
| 132 | 102 |
| 133 ScopedTestingLocalState::~ScopedTestingLocalState() { | 103 ScopedTestingLocalState::~ScopedTestingLocalState() { |
| 134 EXPECT_EQ(&local_state_, browser_process_->local_state()); | 104 EXPECT_EQ(&local_state_, browser_process_->local_state()); |
| 135 browser_process_->SetLocalState(NULL); | 105 browser_process_->SetLocalState(NULL); |
| 136 } | 106 } |
| OLD | NEW |