blob: 5a4fde43974b6aa0a113a2648609e1ac8b1f28bc [file] [log] [blame]
[email protected]74379bc52010-07-21 13:54:081// 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]ce1850e92010-10-15 08:40:587#include "chrome/browser/prefs/command_line_pref_store.h"
[email protected]37858e52010-08-26 00:22:028#include "chrome/browser/prefs/dummy_pref_store.h"
9#include "chrome/browser/prefs/pref_value_store.h"
[email protected]ce1850e92010-10-15 08:40:5810#include "chrome/browser/policy/configuration_policy_pref_store.h"
[email protected]74379bc52010-07-21 13:54:0811
[email protected]fa26b3d52010-08-06 08:51:5012TestingPrefService::TestingPrefValueStore::TestingPrefValueStore(
13 PrefStore* managed_prefs,
14 PrefStore* extension_prefs,
15 PrefStore* command_line_prefs,
16 PrefStore* user_prefs,
[email protected]c3b54f372010-09-14 08:25:0717 PrefStore* recommended_prefs,
18 PrefStore* default_prefs)
[email protected]fa26b3d52010-08-06 08:51:5019 : PrefValueStore(managed_prefs, extension_prefs, command_line_prefs,
[email protected]c3b54f372010-09-14 08:25:0720 user_prefs, recommended_prefs, default_prefs) {
[email protected]fa26b3d52010-08-06 08:51:5021}
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]74379bc52010-07-21 13:54:0825TestingPrefService::TestingPrefService()
[email protected]fa26b3d52010-08-06 08:51:5026 : PrefService(new TestingPrefValueStore(
[email protected]ce1850e92010-10-15 08:40:5827 managed_prefs_ = new DummyPrefStore(),
28 NULL,
29 NULL,
30 user_prefs_ = new DummyPrefStore(),
31 NULL,
32 default_prefs_ = new DummyPrefStore())) {
33}
34
35TestingPrefService::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
47PrefStore* TestingPrefService::CreateManagedPrefStore(
48 policy::ConfigurationPolicyProvider* provider) {
49 if (provider)
50 return new policy::ConfigurationPolicyPrefStore(provider);
51 return new DummyPrefStore();
52}
53
54PrefStore* TestingPrefService::CreateCommandLinePrefStore(
55 CommandLine* command_line) {
56 if (command_line)
57 return new CommandLinePrefStore(command_line);
58 return new DummyPrefStore();
[email protected]74379bc52010-07-21 13:54:0859}
60
[email protected]57ecc4b2010-08-11 03:02:5161const Value* TestingPrefService::GetManagedPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0862 return GetPref(managed_prefs_, path);
63}
64
[email protected]57ecc4b2010-08-11 03:02:5165void TestingPrefService::SetManagedPref(const char* path, Value* value) {
[email protected]74379bc52010-07-21 13:54:0866 SetPref(managed_prefs_, path, value);
67}
68
[email protected]57ecc4b2010-08-11 03:02:5169void TestingPrefService::RemoveManagedPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0870 RemovePref(managed_prefs_, path);
71}
72
[email protected]b6fd1fa72010-10-05 23:49:3173void TestingPrefService::SetManagedPrefWithoutNotification(const char* path,
74 Value* value) {
75 managed_prefs_->prefs()->Set(path, value);
76}
77
78void TestingPrefService::RemoveManagedPrefWithoutNotification(
79 const char* path) {
80 managed_prefs_->prefs()->Remove(path, NULL);
81}
82
[email protected]57ecc4b2010-08-11 03:02:5183const Value* TestingPrefService::GetUserPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0884 return GetPref(user_prefs_, path);
85}
86
[email protected]57ecc4b2010-08-11 03:02:5187void TestingPrefService::SetUserPref(const char* path, Value* value) {
[email protected]74379bc52010-07-21 13:54:0888 SetPref(user_prefs_, path, value);
89}
90
[email protected]57ecc4b2010-08-11 03:02:5191void TestingPrefService::RemoveUserPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0892 RemovePref(user_prefs_, path);
93}
94
95const Value* TestingPrefService::GetPref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:5196 const char* path) {
[email protected]74379bc52010-07-21 13:54:0897 Value* result;
98 return pref_store->prefs()->Get(path, &result) ? result : NULL;
99}
100
101void TestingPrefService::SetPref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:51102 const char* path,
[email protected]74379bc52010-07-21 13:54:08103 Value* value) {
104 pref_store->prefs()->Set(path, value);
[email protected]d81288a02010-08-18 07:25:50105 pref_notifier()->FireObservers(path);
[email protected]74379bc52010-07-21 13:54:08106}
107
108void TestingPrefService::RemovePref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:51109 const char* path) {
[email protected]74379bc52010-07-21 13:54:08110 pref_store->prefs()->Remove(path, NULL);
[email protected]d81288a02010-08-18 07:25:50111 pref_notifier()->FireObservers(path);
[email protected]74379bc52010-07-21 13:54:08112}