summaryrefslogtreecommitdiffstats
path: root/chrome/test/testing_pref_service.cc
blob: 5a4fde43974b6aa0a113a2648609e1ac8b1f28bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/testing_pref_service.h"

#include "chrome/browser/prefs/command_line_pref_store.h"
#include "chrome/browser/prefs/dummy_pref_store.h"
#include "chrome/browser/prefs/pref_value_store.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"

TestingPrefService::TestingPrefValueStore::TestingPrefValueStore(
    PrefStore* managed_prefs,
    PrefStore* extension_prefs,
    PrefStore* command_line_prefs,
    PrefStore* user_prefs,
    PrefStore* recommended_prefs,
    PrefStore* default_prefs)
    : PrefValueStore(managed_prefs, extension_prefs, command_line_prefs,
                     user_prefs, recommended_prefs, default_prefs) {
}

// TODO(pamg): Instantiate no PrefStores by default. Allow callers to specify
// which they want, and expand usage of this class to more unit tests.
TestingPrefService::TestingPrefService()
    : PrefService(new TestingPrefValueStore(
        managed_prefs_ = new DummyPrefStore(),
        NULL,
        NULL,
        user_prefs_ = new DummyPrefStore(),
        NULL,
        default_prefs_ = new DummyPrefStore())) {
}

TestingPrefService::TestingPrefService(
    policy::ConfigurationPolicyProvider* provider,
    CommandLine* command_line)
    : PrefService(new TestingPrefValueStore(
        managed_prefs_ = CreateManagedPrefStore(provider),
        NULL,
        CreateCommandLinePrefStore(command_line),
        user_prefs_ = new DummyPrefStore(),
        NULL,
        default_prefs_ = new DummyPrefStore())) {
}

PrefStore* TestingPrefService::CreateManagedPrefStore(
    policy::ConfigurationPolicyProvider* provider) {
  if (provider)
    return new policy::ConfigurationPolicyPrefStore(provider);
  return new DummyPrefStore();
}

PrefStore* TestingPrefService::CreateCommandLinePrefStore(
    CommandLine* command_line) {
  if (command_line)
    return new CommandLinePrefStore(command_line);
  return new DummyPrefStore();
}

const Value* TestingPrefService::GetManagedPref(const char* path) {
  return GetPref(managed_prefs_, path);
}

void TestingPrefService::SetManagedPref(const char* path, Value* value) {
  SetPref(managed_prefs_, path, value);
}

void TestingPrefService::RemoveManagedPref(const char* path) {
  RemovePref(managed_prefs_, path);
}

void TestingPrefService::SetManagedPrefWithoutNotification(const char* path,
                                                           Value* value) {
  managed_prefs_->prefs()->Set(path, value);
}

void TestingPrefService::RemoveManagedPrefWithoutNotification(
    const char* path) {
  managed_prefs_->prefs()->Remove(path, NULL);
}

const Value* TestingPrefService::GetUserPref(const char* path) {
  return GetPref(user_prefs_, path);
}

void TestingPrefService::SetUserPref(const char* path, Value* value) {
  SetPref(user_prefs_, path, value);
}

void TestingPrefService::RemoveUserPref(const char* path) {
  RemovePref(user_prefs_, path);
}

const Value* TestingPrefService::GetPref(PrefStore* pref_store,
                                         const char* path) {
  Value* result;
  return pref_store->prefs()->Get(path, &result) ? result : NULL;
}

void TestingPrefService::SetPref(PrefStore* pref_store,
                                 const char* path,
                                 Value* value) {
  pref_store->prefs()->Set(path, value);
  pref_notifier()->FireObservers(path);
}

void TestingPrefService::RemovePref(PrefStore* pref_store,
                                    const char* path) {
  pref_store->prefs()->Remove(path, NULL);
  pref_notifier()->FireObservers(path);
}