1 // Copyright (c) 2011 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.
5 #include "chrome/browser/sync/test/integration/preferences_helper.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/profile_sync_service_harness.h"
12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
13 #include "chrome/browser/sync/test/integration/sync_test.h"
15 using sync_datatype_helper::test;
17 namespace preferences_helper {
19 PrefService* GetPrefs(int index) {
20 return test()->GetProfile(index)->GetPrefs();
23 PrefService* GetVerifierPrefs() {
24 return test()->verifier()->GetPrefs();
27 void ChangeBooleanPref(int index, const char* pref_name) {
28 bool new_value = !GetPrefs(index)->GetBoolean(pref_name);
29 GetPrefs(index)->SetBoolean(pref_name, new_value);
30 if (test()->use_verifier())
31 GetVerifierPrefs()->SetBoolean(pref_name, new_value);
34 void ChangeIntegerPref(int index, const char* pref_name, int new_value) {
35 GetPrefs(index)->SetInteger(pref_name, new_value);
36 if (test()->use_verifier())
37 GetVerifierPrefs()->SetInteger(pref_name, new_value);
40 void ChangeInt64Pref(int index, const char* pref_name, int64 new_value) {
41 GetPrefs(index)->SetInt64(pref_name, new_value);
42 if (test()->use_verifier())
43 GetVerifierPrefs()->SetInt64(pref_name, new_value);
46 void ChangeDoublePref(int index, const char* pref_name, double new_value) {
47 GetPrefs(index)->SetDouble(pref_name, new_value);
48 if (test()->use_verifier())
49 GetVerifierPrefs()->SetDouble(pref_name, new_value);
52 void ChangeStringPref(int index,
53 const char* pref_name,
54 const std::string& new_value) {
55 GetPrefs(index)->SetString(pref_name, new_value);
56 if (test()->use_verifier())
57 GetVerifierPrefs()->SetString(pref_name, new_value);
60 void AppendStringPref(int index,
61 const char* pref_name,
62 const std::string& append_value) {
63 ChangeStringPref(index,
65 GetPrefs(index)->GetString(pref_name) + append_value);
68 void ChangeFilePathPref(int index,
69 const char* pref_name,
70 const base::FilePath& new_value) {
71 GetPrefs(index)->SetFilePath(pref_name, new_value);
72 if (test()->use_verifier())
73 GetVerifierPrefs()->SetFilePath(pref_name, new_value);
76 void ChangeListPref(int index,
77 const char* pref_name,
78 const ListValue& new_value) {
80 ListPrefUpdate update(GetPrefs(index), pref_name);
81 ListValue* list = update.Get();
82 for (ListValue::const_iterator it = new_value.begin();
83 it != new_value.end();
85 list->Append((*it)->DeepCopy());
89 if (test()->use_verifier()) {
90 ListPrefUpdate update_verifier(GetVerifierPrefs(), pref_name);
91 ListValue* list_verifier = update_verifier.Get();
92 for (ListValue::const_iterator it = new_value.begin();
93 it != new_value.end();
95 list_verifier->Append((*it)->DeepCopy());
100 bool BooleanPrefMatches(const char* pref_name) {
101 bool reference_value;
102 if (test()->use_verifier()) {
103 reference_value = GetVerifierPrefs()->GetBoolean(pref_name);
105 reference_value = GetPrefs(0)->GetBoolean(pref_name);
107 for (int i = 0; i < test()->num_clients(); ++i) {
108 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) {
109 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in"
110 << " profile " << i << ".";
117 bool IntegerPrefMatches(const char* pref_name) {
119 if (test()->use_verifier()) {
120 reference_value = GetVerifierPrefs()->GetInteger(pref_name);
122 reference_value = GetPrefs(0)->GetInteger(pref_name);
124 for (int i = 0; i < test()->num_clients(); ++i) {
125 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) {
126 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in"
127 << " profile " << i << ".";
134 bool Int64PrefMatches(const char* pref_name) {
135 int64 reference_value;
136 if (test()->use_verifier()) {
137 reference_value = GetVerifierPrefs()->GetInt64(pref_name);
139 reference_value = GetPrefs(0)->GetInt64(pref_name);
141 for (int i = 0; i < test()->num_clients(); ++i) {
142 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) {
143 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in"
144 << " profile " << i << ".";
151 bool DoublePrefMatches(const char* pref_name) {
152 double reference_value;
153 if (test()->use_verifier()) {
154 reference_value = GetVerifierPrefs()->GetDouble(pref_name);
156 reference_value = GetPrefs(0)->GetDouble(pref_name);
158 for (int i = 0; i < test()->num_clients(); ++i) {
159 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) {
160 LOG(ERROR) << "Double preference " << pref_name << " mismatched in"
161 << " profile " << i << ".";
168 bool StringPrefMatches(const char* pref_name) {
169 std::string reference_value;
170 if (test()->use_verifier()) {
171 reference_value = GetVerifierPrefs()->GetString(pref_name);
173 reference_value = GetPrefs(0)->GetString(pref_name);
175 for (int i = 0; i < test()->num_clients(); ++i) {
176 if (reference_value != GetPrefs(i)->GetString(pref_name)) {
177 LOG(ERROR) << "String preference " << pref_name << " mismatched in"
178 << " profile " << i << ".";
185 bool FilePathPrefMatches(const char* pref_name) {
186 base::FilePath reference_value;
187 if (test()->use_verifier()) {
188 reference_value = GetVerifierPrefs()->GetFilePath(pref_name);
190 reference_value = GetPrefs(0)->GetFilePath(pref_name);
192 for (int i = 0; i < test()->num_clients(); ++i) {
193 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) {
194 LOG(ERROR) << "base::FilePath preference " << pref_name
195 << " mismatched in" << " profile " << i << ".";
202 bool ListPrefMatches(const char* pref_name) {
203 const ListValue* reference_value;
204 if (test()->use_verifier()) {
205 reference_value = GetVerifierPrefs()->GetList(pref_name);
207 reference_value = GetPrefs(0)->GetList(pref_name);
209 for (int i = 0; i < test()->num_clients(); ++i) {
210 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) {
211 LOG(ERROR) << "List preference " << pref_name << " mismatched in"
212 << " profile " << i << ".";
219 } // namespace preferences_helper