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.
4
5 #include "chrome/browser/sync/test/integration/preferences_helper.h"
6
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/test/integration/multi_client_status_change_checker.h"
12 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h"
15
16 using sync_datatype_helper::test;
17
18 namespace preferences_helper {
19
GetPrefs(int index)20 PrefService* GetPrefs(int index) {
21 return test()->GetProfile(index)->GetPrefs();
22 }
23
GetVerifierPrefs()24 PrefService* GetVerifierPrefs() {
25 return test()->verifier()->GetPrefs();
26 }
27
ChangeBooleanPref(int index,const char * pref_name)28 void ChangeBooleanPref(int index, const char* pref_name) {
29 bool new_value = !GetPrefs(index)->GetBoolean(pref_name);
30 GetPrefs(index)->SetBoolean(pref_name, new_value);
31 if (test()->use_verifier())
32 GetVerifierPrefs()->SetBoolean(pref_name, new_value);
33 }
34
ChangeIntegerPref(int index,const char * pref_name,int new_value)35 void ChangeIntegerPref(int index, const char* pref_name, int new_value) {
36 GetPrefs(index)->SetInteger(pref_name, new_value);
37 if (test()->use_verifier())
38 GetVerifierPrefs()->SetInteger(pref_name, new_value);
39 }
40
ChangeInt64Pref(int index,const char * pref_name,int64 new_value)41 void ChangeInt64Pref(int index, const char* pref_name, int64 new_value) {
42 GetPrefs(index)->SetInt64(pref_name, new_value);
43 if (test()->use_verifier())
44 GetVerifierPrefs()->SetInt64(pref_name, new_value);
45 }
46
ChangeDoublePref(int index,const char * pref_name,double new_value)47 void ChangeDoublePref(int index, const char* pref_name, double new_value) {
48 GetPrefs(index)->SetDouble(pref_name, new_value);
49 if (test()->use_verifier())
50 GetVerifierPrefs()->SetDouble(pref_name, new_value);
51 }
52
ChangeStringPref(int index,const char * pref_name,const std::string & new_value)53 void ChangeStringPref(int index,
54 const char* pref_name,
55 const std::string& new_value) {
56 GetPrefs(index)->SetString(pref_name, new_value);
57 if (test()->use_verifier())
58 GetVerifierPrefs()->SetString(pref_name, new_value);
59 }
60
AppendStringPref(int index,const char * pref_name,const std::string & append_value)61 void AppendStringPref(int index,
62 const char* pref_name,
63 const std::string& append_value) {
64 ChangeStringPref(index,
65 pref_name,
66 GetPrefs(index)->GetString(pref_name) + append_value);
67 }
68
ChangeFilePathPref(int index,const char * pref_name,const base::FilePath & new_value)69 void ChangeFilePathPref(int index,
70 const char* pref_name,
71 const base::FilePath& new_value) {
72 GetPrefs(index)->SetFilePath(pref_name, new_value);
73 if (test()->use_verifier())
74 GetVerifierPrefs()->SetFilePath(pref_name, new_value);
75 }
76
ChangeListPref(int index,const char * pref_name,const base::ListValue & new_value)77 void ChangeListPref(int index,
78 const char* pref_name,
79 const base::ListValue& new_value) {
80 {
81 ListPrefUpdate update(GetPrefs(index), pref_name);
82 base::ListValue* list = update.Get();
83 for (base::ListValue::const_iterator it = new_value.begin();
84 it != new_value.end();
85 ++it) {
86 list->Append((*it)->DeepCopy());
87 }
88 }
89
90 if (test()->use_verifier()) {
91 ListPrefUpdate update_verifier(GetVerifierPrefs(), pref_name);
92 base::ListValue* list_verifier = update_verifier.Get();
93 for (base::ListValue::const_iterator it = new_value.begin();
94 it != new_value.end();
95 ++it) {
96 list_verifier->Append((*it)->DeepCopy());
97 }
98 }
99 }
100
BooleanPrefMatches(const char * pref_name)101 bool BooleanPrefMatches(const char* pref_name) {
102 bool reference_value;
103 if (test()->use_verifier()) {
104 reference_value = GetVerifierPrefs()->GetBoolean(pref_name);
105 } else {
106 reference_value = GetPrefs(0)->GetBoolean(pref_name);
107 }
108 for (int i = 0; i < test()->num_clients(); ++i) {
109 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) {
110 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in"
111 << " profile " << i << ".";
112 return false;
113 }
114 }
115 return true;
116 }
117
IntegerPrefMatches(const char * pref_name)118 bool IntegerPrefMatches(const char* pref_name) {
119 int reference_value;
120 if (test()->use_verifier()) {
121 reference_value = GetVerifierPrefs()->GetInteger(pref_name);
122 } else {
123 reference_value = GetPrefs(0)->GetInteger(pref_name);
124 }
125 for (int i = 0; i < test()->num_clients(); ++i) {
126 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) {
127 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in"
128 << " profile " << i << ".";
129 return false;
130 }
131 }
132 return true;
133 }
134
Int64PrefMatches(const char * pref_name)135 bool Int64PrefMatches(const char* pref_name) {
136 int64 reference_value;
137 if (test()->use_verifier()) {
138 reference_value = GetVerifierPrefs()->GetInt64(pref_name);
139 } else {
140 reference_value = GetPrefs(0)->GetInt64(pref_name);
141 }
142 for (int i = 0; i < test()->num_clients(); ++i) {
143 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) {
144 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in"
145 << " profile " << i << ".";
146 return false;
147 }
148 }
149 return true;
150 }
151
DoublePrefMatches(const char * pref_name)152 bool DoublePrefMatches(const char* pref_name) {
153 double reference_value;
154 if (test()->use_verifier()) {
155 reference_value = GetVerifierPrefs()->GetDouble(pref_name);
156 } else {
157 reference_value = GetPrefs(0)->GetDouble(pref_name);
158 }
159 for (int i = 0; i < test()->num_clients(); ++i) {
160 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) {
161 LOG(ERROR) << "Double preference " << pref_name << " mismatched in"
162 << " profile " << i << ".";
163 return false;
164 }
165 }
166 return true;
167 }
168
StringPrefMatches(const char * pref_name)169 bool StringPrefMatches(const char* pref_name) {
170 std::string reference_value;
171 if (test()->use_verifier()) {
172 reference_value = GetVerifierPrefs()->GetString(pref_name);
173 } else {
174 reference_value = GetPrefs(0)->GetString(pref_name);
175 }
176 for (int i = 0; i < test()->num_clients(); ++i) {
177 if (reference_value != GetPrefs(i)->GetString(pref_name)) {
178 LOG(ERROR) << "String preference " << pref_name << " mismatched in"
179 << " profile " << i << ".";
180 return false;
181 }
182 }
183 return true;
184 }
185
FilePathPrefMatches(const char * pref_name)186 bool FilePathPrefMatches(const char* pref_name) {
187 base::FilePath reference_value;
188 if (test()->use_verifier()) {
189 reference_value = GetVerifierPrefs()->GetFilePath(pref_name);
190 } else {
191 reference_value = GetPrefs(0)->GetFilePath(pref_name);
192 }
193 for (int i = 0; i < test()->num_clients(); ++i) {
194 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) {
195 LOG(ERROR) << "base::FilePath preference " << pref_name
196 << " mismatched in" << " profile " << i << ".";
197 return false;
198 }
199 }
200 return true;
201 }
202
ListPrefMatches(const char * pref_name)203 bool ListPrefMatches(const char* pref_name) {
204 const base::ListValue* reference_value;
205 if (test()->use_verifier()) {
206 reference_value = GetVerifierPrefs()->GetList(pref_name);
207 } else {
208 reference_value = GetPrefs(0)->GetList(pref_name);
209 }
210 for (int i = 0; i < test()->num_clients(); ++i) {
211 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) {
212 LOG(ERROR) << "List preference " << pref_name << " mismatched in"
213 << " profile " << i << ".";
214 return false;
215 }
216 }
217 return true;
218 }
219
220
221 namespace {
222
223 // Helper class used in the implementation of AwaitListPrefMatches.
224 class ListPrefMatchStatusChecker : public MultiClientStatusChangeChecker {
225 public:
226 explicit ListPrefMatchStatusChecker(const char* pref_name);
227 virtual ~ListPrefMatchStatusChecker();
228
229 virtual bool IsExitConditionSatisfied() OVERRIDE;
230 virtual std::string GetDebugMessage() const OVERRIDE;
231 private:
232 const char* pref_name_;
233 };
234
ListPrefMatchStatusChecker(const char * pref_name)235 ListPrefMatchStatusChecker::ListPrefMatchStatusChecker(const char* pref_name)
236 : MultiClientStatusChangeChecker(
237 sync_datatype_helper::test()->GetSyncServices()),
238 pref_name_(pref_name) {}
239
~ListPrefMatchStatusChecker()240 ListPrefMatchStatusChecker::~ListPrefMatchStatusChecker() {}
241
IsExitConditionSatisfied()242 bool ListPrefMatchStatusChecker::IsExitConditionSatisfied() {
243 return ListPrefMatches(pref_name_);
244 }
245
GetDebugMessage() const246 std::string ListPrefMatchStatusChecker::GetDebugMessage() const {
247 return "Waiting for matching preferences";
248 }
249
250 } // namespace
251
AwaitListPrefMatches(const char * pref_name)252 bool AwaitListPrefMatches(const char* pref_name) {
253 ListPrefMatchStatusChecker checker(pref_name);
254 checker.Wait();
255 return !checker.TimedOut();
256 }
257
258 } // namespace preferences_helper
259