blob: 9cc532121d43f845950179f62a55ffe15efc84a6 [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]acd78969c2010-12-08 09:49:118#include "chrome/browser/prefs/testing_pref_store.h"
9#include "chrome/browser/prefs/pref_notifier.h"
[email protected]37858e52010-08-26 00:22:0210#include "chrome/browser/prefs/pref_value_store.h"
[email protected]ce1850e92010-10-15 08:40:5811#include "chrome/browser/policy/configuration_policy_pref_store.h"
[email protected]fa26b3d52010-08-06 08:51:5012
13// TODO(pamg): Instantiate no PrefStores by default. Allow callers to specify
14// which they want, and expand usage of this class to more unit tests.
[email protected]74379bc52010-07-21 13:54:0815TestingPrefService::TestingPrefService()
[email protected]acd78969c2010-12-08 09:49:1116 : PrefService(
17 managed_platform_prefs_ = new TestingPrefStore(),
18 device_management_prefs_ = new TestingPrefStore(),
19 NULL,
20 NULL,
21 user_prefs_ = new TestingPrefStore(),
22 NULL,
23 NULL) {
[email protected]ce1850e92010-10-15 08:40:5824}
25
26TestingPrefService::TestingPrefService(
[email protected]be1c6e922010-11-17 12:49:1727 policy::ConfigurationPolicyProvider* managed_platform_provider,
28 policy::ConfigurationPolicyProvider* device_management_provider,
[email protected]ce1850e92010-10-15 08:40:5829 CommandLine* command_line)
[email protected]acd78969c2010-12-08 09:49:1130 : PrefService(
[email protected]be1c6e922010-11-17 12:49:1731 managed_platform_prefs_ = CreatePolicyPrefStoreFromProvider(
32 managed_platform_provider),
33 device_management_prefs_ =
34 CreatePolicyPrefStoreFromProvider(device_management_provider),
[email protected]ce1850e92010-10-15 08:40:5835 NULL,
36 CreateCommandLinePrefStore(command_line),
[email protected]acd78969c2010-12-08 09:49:1137 user_prefs_ = new TestingPrefStore(),
[email protected]ce1850e92010-10-15 08:40:5838 NULL,
[email protected]acd78969c2010-12-08 09:49:1139 NULL) {
[email protected]ce1850e92010-10-15 08:40:5840}
41
[email protected]be1c6e922010-11-17 12:49:1742PrefStore* TestingPrefService::CreatePolicyPrefStoreFromProvider(
[email protected]ce1850e92010-10-15 08:40:5843 policy::ConfigurationPolicyProvider* provider) {
44 if (provider)
45 return new policy::ConfigurationPolicyPrefStore(provider);
[email protected]acd78969c2010-12-08 09:49:1146 return new TestingPrefStore();
[email protected]ce1850e92010-10-15 08:40:5847}
48
49PrefStore* TestingPrefService::CreateCommandLinePrefStore(
50 CommandLine* command_line) {
51 if (command_line)
52 return new CommandLinePrefStore(command_line);
[email protected]acd78969c2010-12-08 09:49:1153 return new TestingPrefStore();
[email protected]74379bc52010-07-21 13:54:0854}
55
[email protected]57ecc4b2010-08-11 03:02:5156const Value* TestingPrefService::GetManagedPref(const char* path) {
[email protected]be1c6e922010-11-17 12:49:1757 return GetPref(managed_platform_prefs_, path);
[email protected]74379bc52010-07-21 13:54:0858}
59
[email protected]57ecc4b2010-08-11 03:02:5160void TestingPrefService::SetManagedPref(const char* path, Value* value) {
[email protected]be1c6e922010-11-17 12:49:1761 SetPref(managed_platform_prefs_, path, value);
[email protected]74379bc52010-07-21 13:54:0862}
63
[email protected]57ecc4b2010-08-11 03:02:5164void TestingPrefService::RemoveManagedPref(const char* path) {
[email protected]be1c6e922010-11-17 12:49:1765 RemovePref(managed_platform_prefs_, path);
[email protected]74379bc52010-07-21 13:54:0866}
67
[email protected]b6fd1fa72010-10-05 23:49:3168void TestingPrefService::SetManagedPrefWithoutNotification(const char* path,
69 Value* value) {
[email protected]be1c6e922010-11-17 12:49:1770 managed_platform_prefs_->prefs()->Set(path, value);
[email protected]b6fd1fa72010-10-05 23:49:3171}
72
73void TestingPrefService::RemoveManagedPrefWithoutNotification(
74 const char* path) {
[email protected]be1c6e922010-11-17 12:49:1775 managed_platform_prefs_->prefs()->Remove(path, NULL);
[email protected]b6fd1fa72010-10-05 23:49:3176}
77
[email protected]57ecc4b2010-08-11 03:02:5178const Value* TestingPrefService::GetUserPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0879 return GetPref(user_prefs_, path);
80}
81
[email protected]57ecc4b2010-08-11 03:02:5182void TestingPrefService::SetUserPref(const char* path, Value* value) {
[email protected]74379bc52010-07-21 13:54:0883 SetPref(user_prefs_, path, value);
84}
85
[email protected]57ecc4b2010-08-11 03:02:5186void TestingPrefService::RemoveUserPref(const char* path) {
[email protected]74379bc52010-07-21 13:54:0887 RemovePref(user_prefs_, path);
88}
89
90const Value* TestingPrefService::GetPref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:5191 const char* path) {
[email protected]74379bc52010-07-21 13:54:0892 Value* result;
93 return pref_store->prefs()->Get(path, &result) ? result : NULL;
94}
95
96void TestingPrefService::SetPref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:5197 const char* path,
[email protected]74379bc52010-07-21 13:54:0898 Value* value) {
99 pref_store->prefs()->Set(path, value);
[email protected]acd78969c2010-12-08 09:49:11100 pref_notifier()->OnPreferenceChanged(path);
[email protected]74379bc52010-07-21 13:54:08101}
102
103void TestingPrefService::RemovePref(PrefStore* pref_store,
[email protected]57ecc4b2010-08-11 03:02:51104 const char* path) {
[email protected]74379bc52010-07-21 13:54:08105 pref_store->prefs()->Remove(path, NULL);
[email protected]acd78969c2010-12-08 09:49:11106 pref_notifier()->OnPreferenceChanged(path);
[email protected]74379bc52010-07-21 13:54:08107}