[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/test/testing_pref_service.h" |
| 6 | |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame^] | 7 | #include "chrome/browser/prefs/command_line_pref_store.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 8 | #include "chrome/browser/prefs/dummy_pref_store.h" |
| 9 | #include "chrome/browser/prefs/pref_value_store.h" |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame^] | 10 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 11 | |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 12 | TestingPrefService::TestingPrefValueStore::TestingPrefValueStore( |
| 13 | PrefStore* managed_prefs, |
| 14 | PrefStore* extension_prefs, |
| 15 | PrefStore* command_line_prefs, |
| 16 | PrefStore* user_prefs, |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 17 | PrefStore* recommended_prefs, |
| 18 | PrefStore* default_prefs) |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 19 | : PrefValueStore(managed_prefs, extension_prefs, command_line_prefs, |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 20 | user_prefs, recommended_prefs, default_prefs) { |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // TODO(pamg): Instantiate no PrefStores by default. Allow callers to specify |
| 24 | // which they want, and expand usage of this class to more unit tests. |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 25 | TestingPrefService::TestingPrefService() |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 26 | : PrefService(new TestingPrefValueStore( |
[email protected] | ce1850e9 | 2010-10-15 08:40:58 | [diff] [blame^] | 27 | managed_prefs_ = new DummyPrefStore(), |
| 28 | NULL, |
| 29 | NULL, |
| 30 | user_prefs_ = new DummyPrefStore(), |
| 31 | NULL, |
| 32 | default_prefs_ = new DummyPrefStore())) { |
| 33 | } |
| 34 | |
| 35 | TestingPrefService::TestingPrefService( |
| 36 | policy::ConfigurationPolicyProvider* provider, |
| 37 | CommandLine* command_line) |
| 38 | : PrefService(new TestingPrefValueStore( |
| 39 | managed_prefs_ = CreateManagedPrefStore(provider), |
| 40 | NULL, |
| 41 | CreateCommandLinePrefStore(command_line), |
| 42 | user_prefs_ = new DummyPrefStore(), |
| 43 | NULL, |
| 44 | default_prefs_ = new DummyPrefStore())) { |
| 45 | } |
| 46 | |
| 47 | PrefStore* TestingPrefService::CreateManagedPrefStore( |
| 48 | policy::ConfigurationPolicyProvider* provider) { |
| 49 | if (provider) |
| 50 | return new policy::ConfigurationPolicyPrefStore(provider); |
| 51 | return new DummyPrefStore(); |
| 52 | } |
| 53 | |
| 54 | PrefStore* TestingPrefService::CreateCommandLinePrefStore( |
| 55 | CommandLine* command_line) { |
| 56 | if (command_line) |
| 57 | return new CommandLinePrefStore(command_line); |
| 58 | return new DummyPrefStore(); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 61 | const Value* TestingPrefService::GetManagedPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 62 | return GetPref(managed_prefs_, path); |
| 63 | } |
| 64 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 65 | void TestingPrefService::SetManagedPref(const char* path, Value* value) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 66 | SetPref(managed_prefs_, path, value); |
| 67 | } |
| 68 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 69 | void TestingPrefService::RemoveManagedPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 70 | RemovePref(managed_prefs_, path); |
| 71 | } |
| 72 | |
[email protected] | b6fd1fa7 | 2010-10-05 23:49:31 | [diff] [blame] | 73 | void TestingPrefService::SetManagedPrefWithoutNotification(const char* path, |
| 74 | Value* value) { |
| 75 | managed_prefs_->prefs()->Set(path, value); |
| 76 | } |
| 77 | |
| 78 | void TestingPrefService::RemoveManagedPrefWithoutNotification( |
| 79 | const char* path) { |
| 80 | managed_prefs_->prefs()->Remove(path, NULL); |
| 81 | } |
| 82 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 83 | const Value* TestingPrefService::GetUserPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 84 | return GetPref(user_prefs_, path); |
| 85 | } |
| 86 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 87 | void TestingPrefService::SetUserPref(const char* path, Value* value) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 88 | SetPref(user_prefs_, path, value); |
| 89 | } |
| 90 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 91 | void TestingPrefService::RemoveUserPref(const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 92 | RemovePref(user_prefs_, path); |
| 93 | } |
| 94 | |
| 95 | const Value* TestingPrefService::GetPref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 96 | const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 97 | Value* result; |
| 98 | return pref_store->prefs()->Get(path, &result) ? result : NULL; |
| 99 | } |
| 100 | |
| 101 | void TestingPrefService::SetPref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 102 | const char* path, |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 103 | Value* value) { |
| 104 | pref_store->prefs()->Set(path, value); |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 105 | pref_notifier()->FireObservers(path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void TestingPrefService::RemovePref(PrefStore* pref_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 109 | const char* path) { |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 110 | pref_store->prefs()->Remove(path, NULL); |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 111 | pref_notifier()->FireObservers(path); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 112 | } |