OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/live_sync/preferences_helper.h" | 5 #include "chrome/test/live_sync/preferences_helper.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 9 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/sync/profile_sync_service_harness.h" | 11 #include "chrome/browser/sync/profile_sync_service_harness.h" |
12 #include "chrome/test/live_sync/live_sync_test.h" | 12 #include "chrome/test/live_sync/live_sync_test.h" |
| 13 #include "chrome/test/live_sync/sync_datatype_helper.h" |
13 | 14 |
14 PreferencesHelper::PreferencesHelper() {} | 15 using sync_datatype_helper::test; |
15 | 16 |
16 PreferencesHelper::~PreferencesHelper() {} | 17 namespace preferences_helper { |
17 | 18 |
18 // static | 19 PrefService* GetPrefs(int index) { |
19 PrefService* PreferencesHelper::GetPrefs(int index) { | |
20 return test()->GetProfile(index)->GetPrefs(); | 20 return test()->GetProfile(index)->GetPrefs(); |
21 } | 21 } |
22 | 22 |
23 // static | 23 PrefService* GetVerifierPrefs() { |
24 PrefService* PreferencesHelper::GetVerifierPrefs() { | |
25 return test()->verifier()->GetPrefs(); | 24 return test()->verifier()->GetPrefs(); |
26 } | 25 } |
27 | 26 |
28 // static | 27 void ChangeBooleanPref(int index, const char* pref_name) { |
29 void PreferencesHelper::ChangeBooleanPref(int index, const char* pref_name) { | |
30 bool new_value = !GetPrefs(index)->GetBoolean(pref_name); | 28 bool new_value = !GetPrefs(index)->GetBoolean(pref_name); |
31 GetPrefs(index)->SetBoolean(pref_name, new_value); | 29 GetPrefs(index)->SetBoolean(pref_name, new_value); |
32 if (test()->use_verifier()) | 30 if (test()->use_verifier()) |
33 GetVerifierPrefs()->SetBoolean(pref_name, new_value); | 31 GetVerifierPrefs()->SetBoolean(pref_name, new_value); |
34 } | 32 } |
35 | 33 |
36 // static | 34 void ChangeIntegerPref(int index, const char* pref_name, int new_value) { |
37 void PreferencesHelper::ChangeIntegerPref(int index, | |
38 const char* pref_name, | |
39 int new_value) { | |
40 GetPrefs(index)->SetInteger(pref_name, new_value); | 35 GetPrefs(index)->SetInteger(pref_name, new_value); |
41 if (test()->use_verifier()) | 36 if (test()->use_verifier()) |
42 GetVerifierPrefs()->SetInteger(pref_name, new_value); | 37 GetVerifierPrefs()->SetInteger(pref_name, new_value); |
43 } | 38 } |
44 | 39 |
45 // static | 40 void ChangeDoublePref(int index, const char* pref_name, double new_value) { |
46 void PreferencesHelper::ChangeDoublePref(int index, | |
47 const char* pref_name, | |
48 double new_value) { | |
49 GetPrefs(index)->SetDouble(pref_name, new_value); | 41 GetPrefs(index)->SetDouble(pref_name, new_value); |
50 if (test()->use_verifier()) | 42 if (test()->use_verifier()) |
51 GetVerifierPrefs()->SetDouble(pref_name, new_value); | 43 GetVerifierPrefs()->SetDouble(pref_name, new_value); |
52 } | 44 } |
53 | 45 |
54 // static | 46 void ChangeStringPref(int index, |
55 void PreferencesHelper::ChangeStringPref(int index, | 47 const char* pref_name, |
56 const char* pref_name, | 48 const std::string& new_value) { |
57 const std::string& new_value) { | |
58 GetPrefs(index)->SetString(pref_name, new_value); | 49 GetPrefs(index)->SetString(pref_name, new_value); |
59 if (test()->use_verifier()) | 50 if (test()->use_verifier()) |
60 GetVerifierPrefs()->SetString(pref_name, new_value); | 51 GetVerifierPrefs()->SetString(pref_name, new_value); |
61 } | 52 } |
62 | 53 |
63 // static | 54 void AppendStringPref(int index, |
64 void PreferencesHelper::AppendStringPref(int index, | 55 const char* pref_name, |
65 const char* pref_name, | 56 const std::string& append_value) { |
66 const std::string& append_value) { | |
67 ChangeStringPref(index, | 57 ChangeStringPref(index, |
68 pref_name, | 58 pref_name, |
69 GetPrefs(index)->GetString(pref_name) + append_value); | 59 GetPrefs(index)->GetString(pref_name) + append_value); |
70 } | 60 } |
71 | 61 |
72 // static | 62 void ChangeFilePathPref(int index, |
73 void PreferencesHelper::ChangeFilePathPref(int index, | 63 const char* pref_name, |
74 const char* pref_name, | 64 const FilePath& new_value) { |
75 const FilePath& new_value) { | |
76 GetPrefs(index)->SetFilePath(pref_name, new_value); | 65 GetPrefs(index)->SetFilePath(pref_name, new_value); |
77 if (test()->use_verifier()) | 66 if (test()->use_verifier()) |
78 GetVerifierPrefs()->SetFilePath(pref_name, new_value); | 67 GetVerifierPrefs()->SetFilePath(pref_name, new_value); |
79 } | 68 } |
80 | 69 |
81 // static | 70 void ChangeListPref(int index, |
82 void PreferencesHelper::ChangeListPref(int index, | 71 const char* pref_name, |
83 const char* pref_name, | 72 const ListValue& new_value) { |
84 const ListValue& new_value) { | |
85 { | 73 { |
86 ListPrefUpdate update(GetPrefs(index), pref_name); | 74 ListPrefUpdate update(GetPrefs(index), pref_name); |
87 ListValue* list = update.Get(); | 75 ListValue* list = update.Get(); |
88 for (ListValue::const_iterator it = new_value.begin(); | 76 for (ListValue::const_iterator it = new_value.begin(); |
89 it != new_value.end(); | 77 it != new_value.end(); |
90 ++it) { | 78 ++it) { |
91 list->Append((*it)->DeepCopy()); | 79 list->Append((*it)->DeepCopy()); |
92 } | 80 } |
93 } | 81 } |
94 | 82 |
95 if (test()->use_verifier()) { | 83 if (test()->use_verifier()) { |
96 ListPrefUpdate update_verifier(GetVerifierPrefs(), pref_name); | 84 ListPrefUpdate update_verifier(GetVerifierPrefs(), pref_name); |
97 ListValue* list_verifier = update_verifier.Get(); | 85 ListValue* list_verifier = update_verifier.Get(); |
98 for (ListValue::const_iterator it = new_value.begin(); | 86 for (ListValue::const_iterator it = new_value.begin(); |
99 it != new_value.end(); | 87 it != new_value.end(); |
100 ++it) { | 88 ++it) { |
101 list_verifier->Append((*it)->DeepCopy()); | 89 list_verifier->Append((*it)->DeepCopy()); |
102 } | 90 } |
103 } | 91 } |
104 } | 92 } |
105 | 93 |
106 // static | 94 bool BooleanPrefMatches(const char* pref_name) { |
107 bool PreferencesHelper::BooleanPrefMatches(const char* pref_name) { | |
108 bool reference_value; | 95 bool reference_value; |
109 if (test()->use_verifier()) { | 96 if (test()->use_verifier()) { |
110 reference_value = GetVerifierPrefs()->GetBoolean(pref_name); | 97 reference_value = GetVerifierPrefs()->GetBoolean(pref_name); |
111 } else { | 98 } else { |
112 reference_value = GetPrefs(0)->GetBoolean(pref_name); | 99 reference_value = GetPrefs(0)->GetBoolean(pref_name); |
113 } | 100 } |
114 for (int i = 0; i < test()->num_clients(); ++i) { | 101 for (int i = 0; i < test()->num_clients(); ++i) { |
115 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) { | 102 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) { |
116 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in" | 103 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in" |
117 << " profile " << i << "."; | 104 << " profile " << i << "."; |
118 return false; | 105 return false; |
119 } | 106 } |
120 } | 107 } |
121 return true; | 108 return true; |
122 } | 109 } |
123 | 110 |
124 // static | 111 bool IntegerPrefMatches(const char* pref_name) { |
125 bool PreferencesHelper::IntegerPrefMatches(const char* pref_name) { | |
126 int reference_value; | 112 int reference_value; |
127 if (test()->use_verifier()) { | 113 if (test()->use_verifier()) { |
128 reference_value = GetVerifierPrefs()->GetInteger(pref_name); | 114 reference_value = GetVerifierPrefs()->GetInteger(pref_name); |
129 } else { | 115 } else { |
130 reference_value = GetPrefs(0)->GetInteger(pref_name); | 116 reference_value = GetPrefs(0)->GetInteger(pref_name); |
131 } | 117 } |
132 for (int i = 0; i < test()->num_clients(); ++i) { | 118 for (int i = 0; i < test()->num_clients(); ++i) { |
133 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) { | 119 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) { |
134 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" | 120 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" |
135 << " profile " << i << "."; | 121 << " profile " << i << "."; |
136 return false; | 122 return false; |
137 } | 123 } |
138 } | 124 } |
139 return true; | 125 return true; |
140 } | 126 } |
141 | 127 |
142 // static | 128 bool DoublePrefMatches(const char* pref_name) { |
143 bool PreferencesHelper::DoublePrefMatches(const char* pref_name) { | |
144 double reference_value; | 129 double reference_value; |
145 if (test()->use_verifier()) { | 130 if (test()->use_verifier()) { |
146 reference_value = GetVerifierPrefs()->GetDouble(pref_name); | 131 reference_value = GetVerifierPrefs()->GetDouble(pref_name); |
147 } else { | 132 } else { |
148 reference_value = GetPrefs(0)->GetDouble(pref_name); | 133 reference_value = GetPrefs(0)->GetDouble(pref_name); |
149 } | 134 } |
150 for (int i = 0; i < test()->num_clients(); ++i) { | 135 for (int i = 0; i < test()->num_clients(); ++i) { |
151 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) { | 136 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) { |
152 LOG(ERROR) << "Double preference " << pref_name << " mismatched in" | 137 LOG(ERROR) << "Double preference " << pref_name << " mismatched in" |
153 << " profile " << i << "."; | 138 << " profile " << i << "."; |
154 return false; | 139 return false; |
155 } | 140 } |
156 } | 141 } |
157 return true; | 142 return true; |
158 } | 143 } |
159 | 144 |
160 // static | 145 bool StringPrefMatches(const char* pref_name) { |
161 bool PreferencesHelper::StringPrefMatches(const char* pref_name) { | |
162 std::string reference_value; | 146 std::string reference_value; |
163 if (test()->use_verifier()) { | 147 if (test()->use_verifier()) { |
164 reference_value = GetVerifierPrefs()->GetString(pref_name); | 148 reference_value = GetVerifierPrefs()->GetString(pref_name); |
165 } else { | 149 } else { |
166 reference_value = GetPrefs(0)->GetString(pref_name); | 150 reference_value = GetPrefs(0)->GetString(pref_name); |
167 } | 151 } |
168 for (int i = 0; i < test()->num_clients(); ++i) { | 152 for (int i = 0; i < test()->num_clients(); ++i) { |
169 if (reference_value != GetPrefs(i)->GetString(pref_name)) { | 153 if (reference_value != GetPrefs(i)->GetString(pref_name)) { |
170 LOG(ERROR) << "String preference " << pref_name << " mismatched in" | 154 LOG(ERROR) << "String preference " << pref_name << " mismatched in" |
171 << " profile " << i << "."; | 155 << " profile " << i << "."; |
172 return false; | 156 return false; |
173 } | 157 } |
174 } | 158 } |
175 return true; | 159 return true; |
176 } | 160 } |
177 | 161 |
178 // static | 162 bool FilePathPrefMatches(const char* pref_name) { |
179 bool PreferencesHelper::FilePathPrefMatches(const char* pref_name) { | |
180 FilePath reference_value; | 163 FilePath reference_value; |
181 if (test()->use_verifier()) { | 164 if (test()->use_verifier()) { |
182 reference_value = GetVerifierPrefs()->GetFilePath(pref_name); | 165 reference_value = GetVerifierPrefs()->GetFilePath(pref_name); |
183 } else { | 166 } else { |
184 reference_value = GetPrefs(0)->GetFilePath(pref_name); | 167 reference_value = GetPrefs(0)->GetFilePath(pref_name); |
185 } | 168 } |
186 for (int i = 0; i < test()->num_clients(); ++i) { | 169 for (int i = 0; i < test()->num_clients(); ++i) { |
187 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) { | 170 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) { |
188 LOG(ERROR) << "FilePath preference " << pref_name << " mismatched in" | 171 LOG(ERROR) << "FilePath preference " << pref_name << " mismatched in" |
189 << " profile " << i << "."; | 172 << " profile " << i << "."; |
190 return false; | 173 return false; |
191 } | 174 } |
192 } | 175 } |
193 return true; | 176 return true; |
194 } | 177 } |
195 | 178 |
196 // static | 179 bool ListPrefMatches(const char* pref_name) { |
197 bool PreferencesHelper::ListPrefMatches(const char* pref_name) { | |
198 const ListValue* reference_value; | 180 const ListValue* reference_value; |
199 if (test()->use_verifier()) { | 181 if (test()->use_verifier()) { |
200 reference_value = GetVerifierPrefs()->GetList(pref_name); | 182 reference_value = GetVerifierPrefs()->GetList(pref_name); |
201 } else { | 183 } else { |
202 reference_value = GetPrefs(0)->GetList(pref_name); | 184 reference_value = GetPrefs(0)->GetList(pref_name); |
203 } | 185 } |
204 for (int i = 0; i < test()->num_clients(); ++i) { | 186 for (int i = 0; i < test()->num_clients(); ++i) { |
205 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) { | 187 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) { |
206 LOG(ERROR) << "List preference " << pref_name << " mismatched in" | 188 LOG(ERROR) << "List preference " << pref_name << " mismatched in" |
207 << " profile " << i << "."; | 189 << " profile " << i << "."; |
208 return false; | 190 return false; |
209 } | 191 } |
210 } | 192 } |
211 return true; | 193 return true; |
212 } | 194 } |
213 | 195 |
214 // static | 196 bool EnableEncryption(int index) { |
215 bool PreferencesHelper::EnableEncryption(int index) { | |
216 return test()->GetClient(index)->EnableEncryptionForType( | 197 return test()->GetClient(index)->EnableEncryptionForType( |
217 syncable::PREFERENCES); | 198 syncable::PREFERENCES); |
218 } | 199 } |
219 | 200 |
220 // static | 201 bool IsEncrypted(int index) { |
221 bool PreferencesHelper::IsEncrypted(int index) { | |
222 return test()->GetClient(index)->IsTypeEncrypted(syncable::PREFERENCES); | 202 return test()->GetClient(index)->IsTypeEncrypted(syncable::PREFERENCES); |
223 } | 203 } |
| 204 |
| 205 } // namespace preferences_helper |
OLD | NEW |