OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/sync/test/integration/search_engines_helper.h" | 5 #include "chrome/browser/sync/test/integration/search_engines_helper.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/search_engines/template_url.h" | 14 #include "chrome/browser/search_engines/template_url.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
17 #include "chrome/browser/sync/profile_sync_service_harness.h" | 17 #include "chrome/browser/sync/profile_sync_service_harness.h" |
18 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 18 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
19 #include "chrome/browser/sync/test/integration/sync_test.h" | 19 #include "chrome/browser/sync/test/integration/sync_test.h" |
20 | 20 |
21 using sync_datatype_helper::test; | 21 namespace { |
22 | |
23 namespace search_engines_helper { | |
24 | |
25 TemplateURLService* GetServiceForProfile(int index) { | |
26 return TemplateURLServiceFactory::GetForProfile(test()->GetProfile(index)); | |
27 } | |
28 | |
29 TemplateURLService* GetVerifierService() { | |
30 return TemplateURLServiceFactory::GetForProfile(test()->verifier()); | |
31 } | |
32 | 22 |
33 GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) { | 23 GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) { |
34 CHECK(service); | 24 CHECK(service); |
35 | 25 |
36 GUIDToTURLMap map; | 26 GUIDToTURLMap map; |
37 TemplateURLService::TemplateURLVector turls = service->GetTemplateURLs(); | 27 TemplateURLService::TemplateURLVector turls = service->GetTemplateURLs(); |
38 for (TemplateURLService::TemplateURLVector::iterator it = turls.begin(); | 28 for (TemplateURLService::TemplateURLVector::iterator it = turls.begin(); |
39 it != turls.end(); ++it) { | 29 it != turls.end(); ++it) { |
40 CHECK(*it); | 30 CHECK(*it); |
41 CHECK(map.find((*it)->sync_guid()) == map.end()); | 31 CHECK(map.find((*it)->sync_guid()) == map.end()); |
(...skipping 18 matching lines...) Expand all Loading... | |
60 | 50 |
61 // Print some useful debug info. | 51 // Print some useful debug info. |
62 if (!result) { | 52 if (!result) { |
63 LOG(ERROR) << "TemplateURLs did not match: " << GetTURLInfoString(turl1) | 53 LOG(ERROR) << "TemplateURLs did not match: " << GetTURLInfoString(turl1) |
64 << " vs " << GetTURLInfoString(turl2); | 54 << " vs " << GetTURLInfoString(turl2); |
65 } | 55 } |
66 | 56 |
67 return result; | 57 return result; |
68 } | 58 } |
69 | 59 |
70 bool ServiceMatchesVerifier(int profile) { | |
71 TemplateURLService* verifier = GetVerifierService(); | |
72 TemplateURLService* other = GetServiceForProfile(profile); | |
73 | |
74 CHECK(verifier); | |
75 CHECK(other); | |
76 | |
77 TemplateURLService::TemplateURLVector verifier_turls = | |
78 verifier->GetTemplateURLs(); | |
79 if (verifier_turls.size() != other->GetTemplateURLs().size()) { | |
80 LOG(ERROR) << "Verifier and other service have a different count of TURLs: " | |
81 << verifier_turls.size() << " vs " | |
82 << other->GetTemplateURLs().size() << " respectively."; | |
83 return false; | |
84 } | |
85 | |
86 for (size_t i = 0; i < verifier_turls.size(); ++i) { | |
87 const TemplateURL* verifier_turl = verifier_turls.at(i); | |
88 CHECK(verifier_turl); | |
89 const TemplateURL* other_turl = other->GetTemplateURLForKeyword( | |
90 verifier_turl->keyword()); | |
91 | |
92 if (!other_turl) { | |
93 LOG(ERROR) << "The other service did not contain a TURL with keyword: " | |
94 << verifier_turl->keyword(); | |
95 return false; | |
96 } | |
97 if (!TURLsMatch(verifier_turl, other_turl)) | |
98 return false; | |
99 } | |
100 | |
101 return true; | |
102 } | |
103 | |
104 bool ServicesMatch(int profile_a, int profile_b) { | 60 bool ServicesMatch(int profile_a, int profile_b) { |
105 TemplateURLService* service_a = GetServiceForProfile(profile_a); | 61 TemplateURLService* service_a = |
106 TemplateURLService* service_b = GetServiceForProfile(profile_b); | 62 search_engines_helper::GetServiceForProfile(profile_a); |
63 TemplateURLService* service_b = | |
64 search_engines_helper::GetServiceForProfile(profile_b); | |
107 CHECK(service_a); | 65 CHECK(service_a); |
108 CHECK(service_b); | 66 CHECK(service_b); |
109 | 67 |
110 // Services that have synced should have identical TURLs, including the GUIDs. | 68 // Services that have synced should have identical TURLs, including the GUIDs. |
111 // Make sure we compare those fields in addition to the user-visible fields. | 69 // Make sure we compare those fields in addition to the user-visible fields. |
112 GUIDToTURLMap a_turls = CreateGUIDToTURLMap(service_a); | 70 GUIDToTURLMap a_turls = CreateGUIDToTURLMap(service_a); |
113 GUIDToTURLMap b_turls = CreateGUIDToTURLMap(service_b); | 71 GUIDToTURLMap b_turls = CreateGUIDToTURLMap(service_b); |
114 | 72 |
115 if (a_turls.size() != b_turls.size()) { | 73 if (a_turls.size() != b_turls.size()) { |
116 LOG(ERROR) << "Service a and b do not match in size: " << a_turls.size() | 74 LOG(ERROR) << "Service a and b do not match in size: " << a_turls.size() |
(...skipping 21 matching lines...) Expand all Loading... | |
138 << default_b->keyword(); | 96 << default_b->keyword(); |
139 return false; | 97 return false; |
140 } else { | 98 } else { |
141 LOG(INFO) << "A had default with URL: " << default_a->url() | 99 LOG(INFO) << "A had default with URL: " << default_a->url() |
142 << " and keyword: " << default_a->keyword(); | 100 << " and keyword: " << default_a->keyword(); |
143 } | 101 } |
144 | 102 |
145 return true; | 103 return true; |
146 } | 104 } |
147 | 105 |
106 // Convenience helper for consistently generating the same keyword for a given | |
107 // seed. | |
108 string16 CreateKeyword(int seed) { | |
109 return ASCIIToUTF16(base::StringPrintf("test%d", seed)); | |
110 } | |
111 | |
112 } // namespace | |
113 | |
114 namespace search_engines_helper { | |
115 | |
116 TemplateURLService* GetServiceForProfile(int profile_index) { | |
117 return TemplateURLServiceFactory::GetForProfile( | |
118 sync_datatype_helper::test()->GetProfile(profile_index)); | |
119 } | |
120 | |
121 TemplateURLService* GetVerifierService() { | |
122 return TemplateURLServiceFactory::GetForProfile( | |
123 sync_datatype_helper::test()->verifier()); | |
124 } | |
125 | |
126 bool ServiceMatchesVerifier(int profile_index) { | |
127 TemplateURLService* verifier = GetVerifierService(); | |
128 TemplateURLService* other = GetServiceForProfile(profile_index); | |
129 | |
130 CHECK(verifier); | |
131 CHECK(other); | |
132 | |
133 TemplateURLService::TemplateURLVector verifier_turls = | |
134 verifier->GetTemplateURLs(); | |
135 if (verifier_turls.size() != other->GetTemplateURLs().size()) { | |
136 LOG(ERROR) << "Verifier and other service have a different count of TURLs: " | |
137 << verifier_turls.size() << " vs " | |
138 << other->GetTemplateURLs().size() << " respectively."; | |
139 return false; | |
140 } | |
141 | |
142 for (size_t i = 0; i < verifier_turls.size(); ++i) { | |
143 const TemplateURL* verifier_turl = verifier_turls.at(i); | |
144 CHECK(verifier_turl); | |
145 const TemplateURL* other_turl = other->GetTemplateURLForKeyword( | |
146 verifier_turl->keyword()); | |
147 | |
148 if (!other_turl) { | |
149 LOG(ERROR) << "The other service did not contain a TURL with keyword: " | |
150 << verifier_turl->keyword(); | |
151 return false; | |
152 } | |
153 if (!TURLsMatch(verifier_turl, other_turl)) | |
154 return false; | |
155 } | |
156 | |
157 return true; | |
158 } | |
159 | |
148 bool AllServicesMatch() { | 160 bool AllServicesMatch() { |
149 // Use 0 as the baseline. | 161 // Use 0 as the baseline. |
150 if (test()->use_verifier() && !ServiceMatchesVerifier(0)) { | 162 if (sync_datatype_helper::test()->use_verifier() && |
163 !ServiceMatchesVerifier(0)) { | |
151 LOG(ERROR) << "TemplateURLService 0 does not match verifier."; | 164 LOG(ERROR) << "TemplateURLService 0 does not match verifier."; |
152 return false; | 165 return false; |
153 } | 166 } |
154 | 167 |
155 for (int it = 1; it < test()->num_clients(); ++it) { | 168 for (int it = 1; it < sync_datatype_helper::test()->num_clients(); ++it) { |
156 if (!ServicesMatch(0, it)) { | 169 if (!ServicesMatch(0, it)) { |
157 LOG(ERROR) << "TemplateURLService " << it << " does not match with " | 170 LOG(ERROR) << "TemplateURLService " << it << " does not match with " |
158 << "service 0."; | 171 << "service 0."; |
159 return false; | 172 return false; |
160 } | 173 } |
161 } | 174 } |
162 return true; | 175 return true; |
163 } | 176 } |
164 | 177 |
165 // Convenience helper for consistently generating the same keyword for a given | 178 TemplateURL* CreateTestTemplateURL(Profile* profile, int seed) { |
166 // seed. | 179 return CreateTestTemplateURL(profile, seed, CreateKeyword(seed), |
167 string16 CreateKeyword(int seed) { | |
168 return ASCIIToUTF16(base::StringPrintf("test%d", seed)); | |
169 } | |
170 | |
171 TemplateURL* CreateTestTemplateURL(int seed) { | |
172 return CreateTestTemplateURL(seed, CreateKeyword(seed), | |
173 base::StringPrintf("0000-0000-0000-%04d", seed)); | 180 base::StringPrintf("0000-0000-0000-%04d", seed)); |
174 } | 181 } |
175 | 182 |
176 TemplateURL* CreateTestTemplateURL(int seed, | 183 TemplateURL* CreateTestTemplateURL(Profile* profile, |
184 int seed, | |
177 const string16& keyword, | 185 const string16& keyword, |
178 const std::string& sync_guid) { | 186 const std::string& sync_guid) { |
179 return CreateTestTemplateURL(seed, keyword, | 187 return CreateTestTemplateURL(profile, seed, keyword, |
180 base::StringPrintf("http://www.test%d.com/", seed), sync_guid); | 188 base::StringPrintf("http://www.test%d.com/", seed), sync_guid); |
181 } | 189 } |
182 | 190 |
183 TemplateURL* CreateTestTemplateURL(int seed, | 191 TemplateURL* CreateTestTemplateURL(Profile* profile, |
192 int seed, | |
184 const string16& keyword, | 193 const string16& keyword, |
185 const std::string& url, | 194 const std::string& url, |
186 const std::string& sync_guid) { | 195 const std::string& sync_guid) { |
187 TemplateURLData data; | 196 TemplateURLData data; |
188 data.short_name = CreateKeyword(seed); | 197 data.short_name = CreateKeyword(seed); |
189 data.SetKeyword(keyword); | 198 data.SetKeyword(keyword); |
190 data.SetURL(url); | 199 data.SetURL(url); |
191 data.favicon_url = GURL("http://favicon.url"); | 200 data.favicon_url = GURL("http://favicon.url"); |
192 data.safe_for_autoreplace = true; | 201 data.safe_for_autoreplace = true; |
193 data.date_created = base::Time::FromTimeT(100); | 202 data.date_created = base::Time::FromTimeT(100); |
194 data.last_modified = base::Time::FromTimeT(100); | 203 data.last_modified = base::Time::FromTimeT(100); |
195 data.prepopulate_id = 999999; | 204 data.prepopulate_id = 999999; |
196 data.sync_guid = sync_guid; | 205 data.sync_guid = sync_guid; |
197 return new TemplateURL(data); | 206 return new TemplateURL(profile, data); |
198 } | 207 } |
199 | 208 |
200 void AddSearchEngine(int profile, int seed) { | 209 void AddSearchEngine(int profile_index, int seed) { |
201 GetServiceForProfile(profile)->Add(CreateTestTemplateURL(seed)); | 210 Profile* profile = sync_datatype_helper::test()->GetProfile(profile_index); |
202 if (test()->use_verifier()) | 211 TemplateURLServiceFactory::GetForProfile(profile)->Add( |
203 GetVerifierService()->Add(CreateTestTemplateURL(seed)); | 212 CreateTestTemplateURL(profile, seed)); |
213 if (sync_datatype_helper::test()->use_verifier()) | |
214 GetVerifierService()->Add(CreateTestTemplateURL(profile, seed)); | |
204 } | 215 } |
205 | 216 |
206 void EditSearchEngine(int profile, | 217 void EditSearchEngine(int profile_index, |
207 const string16& keyword, | 218 const string16& keyword, |
208 const string16& short_name, | 219 const string16& short_name, |
209 const string16& new_keyword, | 220 const string16& new_keyword, |
210 const std::string& url) { | 221 const std::string& url) { |
211 DCHECK(!url.empty()); | 222 DCHECK(!url.empty()); |
212 const TemplateURL* turl = | 223 TemplateURLService* service = GetServiceForProfile(profile_index); |
213 GetServiceForProfile(profile)->GetTemplateURLForKeyword(keyword); | 224 const TemplateURL* turl = service->GetTemplateURLForKeyword(keyword); |
214 EXPECT_TRUE(turl); | 225 EXPECT_TRUE(turl); |
215 ASSERT_FALSE(new_keyword.empty()); | 226 ASSERT_FALSE(new_keyword.empty()); |
216 GetServiceForProfile(profile)->ResetTemplateURL(turl, short_name, new_keyword, | 227 service->ResetTemplateURL(turl, short_name, new_keyword, url); |
217 url); | |
218 // Make sure we do the same on the verifier. | 228 // Make sure we do the same on the verifier. |
219 if (test()->use_verifier()) { | 229 if (sync_datatype_helper::test()->use_verifier()) { |
220 const TemplateURL* verifier_turl = | 230 const TemplateURL* verifier_turl = |
221 GetVerifierService()->GetTemplateURLForKeyword(keyword); | 231 GetVerifierService()->GetTemplateURLForKeyword(keyword); |
222 EXPECT_TRUE(verifier_turl); | 232 EXPECT_TRUE(verifier_turl); |
223 GetVerifierService()->ResetTemplateURL(verifier_turl, short_name, | 233 GetVerifierService()->ResetTemplateURL(verifier_turl, short_name, |
224 new_keyword, url); | 234 new_keyword, url); |
225 } | 235 } |
226 } | 236 } |
227 | 237 |
228 void DeleteSearchEngineByKeyword(int profile, const string16& keyword) { | 238 void DeleteSearchEngineBySeed(int profile_index, int seed) { |
229 const TemplateURL* turl = | 239 TemplateURLService* service = GetServiceForProfile(profile_index); |
230 GetServiceForProfile(profile)->GetTemplateURLForKeyword(keyword); | 240 string16 keyword(CreateKeyword(seed)); |
241 const TemplateURL* turl = service->GetTemplateURLForKeyword(keyword); | |
231 EXPECT_TRUE(turl); | 242 EXPECT_TRUE(turl); |
232 GetServiceForProfile(profile)->Remove(turl); | 243 service->Remove(turl); |
233 // Make sure we do the same on the verifier. | 244 // Make sure we do the same on the verifier. |
234 if (test()->use_verifier()) { | 245 if (sync_datatype_helper::test()->use_verifier()) { |
235 const TemplateURL* verifier_turl = | 246 const TemplateURL* verifier_turl = |
236 GetVerifierService()->GetTemplateURLForKeyword(keyword); | 247 GetVerifierService()->GetTemplateURLForKeyword(keyword); |
237 EXPECT_TRUE(verifier_turl); | 248 EXPECT_TRUE(verifier_turl); |
238 GetVerifierService()->Remove(verifier_turl); | 249 GetVerifierService()->Remove(verifier_turl); |
239 } | 250 } |
240 } | 251 } |
241 | 252 |
242 void DeleteSearchEngineBySeed(int profile, int seed) { | 253 void ChangeDefaultSearchProvider(int profile_index, int seed) { |
243 DeleteSearchEngineByKeyword(profile, CreateKeyword(seed)); | 254 TemplateURLService* service = GetServiceForProfile(profile_index); |
244 } | |
245 | |
246 void ChangeDefaultSearchProvider(int profile, int seed) { | |
247 TemplateURLService* service = GetServiceForProfile(profile); | |
248 ASSERT_TRUE(service); | 255 ASSERT_TRUE(service); |
249 const TemplateURL* turl = | 256 const TemplateURL* turl = |
250 service->GetTemplateURLForKeyword(CreateKeyword(seed)); | 257 service->GetTemplateURLForKeyword(CreateKeyword(seed)); |
251 ASSERT_TRUE(turl); | 258 ASSERT_TRUE(turl); |
252 service->SetDefaultSearchProvider(turl); | 259 service->SetDefaultSearchProvider(turl); |
253 if (test()->use_verifier()) { | 260 if (sync_datatype_helper::test()->use_verifier()) { |
Andrew T Wilson (Slow)
2012/04/23 16:36:36
This is fine to land, but does it really help read
Peter Kasting
2012/04/23 18:14:26
I'm not sure, honestly.
The reason I changed this
| |
254 const TemplateURL* verifier_turl = | 261 const TemplateURL* verifier_turl = |
255 GetVerifierService()->GetTemplateURLForKeyword(CreateKeyword(seed)); | 262 GetVerifierService()->GetTemplateURLForKeyword(CreateKeyword(seed)); |
256 ASSERT_TRUE(verifier_turl); | 263 ASSERT_TRUE(verifier_turl); |
257 GetVerifierService()->SetDefaultSearchProvider(verifier_turl); | 264 GetVerifierService()->SetDefaultSearchProvider(verifier_turl); |
258 } | 265 } |
259 } | 266 } |
260 | 267 |
261 } // namespace search_engines_helper | 268 } // namespace search_engines_helper |
OLD | NEW |