Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / search_engines_helper.cc
blobb10485193dc42387254b34a3a36224aa934065eb
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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/test/integration/search_engines_helper.h"
7 #include <vector>
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/sync/test/integration/await_match_status_change_checker.h"
16 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
17 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
18 #include "chrome/browser/sync/test/integration/sync_test.h"
19 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_service.h"
22 using sync_datatype_helper::test;
24 namespace {
26 GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) {
27 CHECK(service);
29 GUIDToTURLMap map;
30 TemplateURLService::TemplateURLVector turls = service->GetTemplateURLs();
31 for (TemplateURLService::TemplateURLVector::iterator it = turls.begin();
32 it != turls.end(); ++it) {
33 CHECK(*it);
34 CHECK(map.find((*it)->sync_guid()) == map.end());
35 map[(*it)->sync_guid()] = *it;
38 return map;
41 std::string GetTURLInfoString(const TemplateURL* turl) {
42 DCHECK(turl);
43 return "TemplateURL: shortname: " + base::UTF16ToASCII(turl->short_name()) +
44 " keyword: " + base::UTF16ToASCII(turl->keyword()) + " url: " +
45 turl->url();
48 bool TURLsMatch(const TemplateURL* turl1, const TemplateURL* turl2) {
49 CHECK(turl1);
50 CHECK(turl2);
51 bool result = (turl1->url() == turl2->url()) &&
52 (turl1->keyword() == turl2->keyword()) &&
53 (turl1->short_name() == turl2->short_name());
55 // Print some useful debug info.
56 if (!result) {
57 DVLOG(1) << "TemplateURLs did not match: " << GetTURLInfoString(turl1)
58 << " vs " << GetTURLInfoString(turl2);
61 return result;
64 bool ServicesMatch(int profile_a, int profile_b) {
65 TemplateURLService* service_a =
66 search_engines_helper::GetServiceForBrowserContext(profile_a);
67 TemplateURLService* service_b =
68 search_engines_helper::GetServiceForBrowserContext(profile_b);
69 CHECK(service_a);
70 CHECK(service_b);
72 // Services that have synced should have identical TURLs, including the GUIDs.
73 // Make sure we compare those fields in addition to the user-visible fields.
74 GUIDToTURLMap a_turls = CreateGUIDToTURLMap(service_a);
75 GUIDToTURLMap b_turls = CreateGUIDToTURLMap(service_b);
77 if (a_turls.size() != b_turls.size()) {
78 DVLOG(1) << "Service a and b do not match in size: " << a_turls.size()
79 << " vs " << b_turls.size() << " respectively.";
80 return false;
83 for (GUIDToTURLMap::iterator it = a_turls.begin();
84 it != a_turls.end(); ++it) {
85 if (b_turls.find(it->first) == b_turls.end()) {
86 DVLOG(1) << "TURL GUID from a not found in b's TURLs: " << it->first;
87 return false;
89 if (!TURLsMatch(b_turls[it->first], it->second))
90 return false;
93 const TemplateURL* default_a = service_a->GetDefaultSearchProvider();
94 const TemplateURL* default_b = service_b->GetDefaultSearchProvider();
95 CHECK(default_a);
96 CHECK(default_b);
97 if (!TURLsMatch(default_a, default_b)) {
98 DVLOG(1) << "Default search providers do not match: A's default: "
99 << default_a->keyword()
100 << " B's default: " << default_b->keyword();
101 return false;
102 } else {
103 DVLOG(1) << "A had default with URL: " << default_a->url()
104 << " and keyword: " << default_a->keyword();
107 return true;
110 // Convenience helper for consistently generating the same keyword for a given
111 // seed.
112 base::string16 CreateKeyword(int seed) {
113 return base::ASCIIToUTF16(base::StringPrintf("test%d", seed));
116 } // namespace
118 namespace search_engines_helper {
120 TemplateURLService* GetServiceForBrowserContext(int profile_index) {
121 return TemplateURLServiceFactory::GetForProfile(
122 test()->GetProfile(profile_index));
125 TemplateURLService* GetVerifierService() {
126 return TemplateURLServiceFactory::GetForProfile(test()->verifier());
129 bool ServiceMatchesVerifier(int profile_index) {
130 TemplateURLService* verifier = GetVerifierService();
131 TemplateURLService* other = GetServiceForBrowserContext(profile_index);
133 CHECK(verifier);
134 CHECK(other);
136 TemplateURLService::TemplateURLVector verifier_turls =
137 verifier->GetTemplateURLs();
138 if (verifier_turls.size() != other->GetTemplateURLs().size()) {
139 DVLOG(1) << "Verifier and other service have a different count of TURLs: "
140 << verifier_turls.size() << " vs "
141 << other->GetTemplateURLs().size() << " respectively.";
142 return false;
145 for (size_t i = 0; i < verifier_turls.size(); ++i) {
146 const TemplateURL* verifier_turl = verifier_turls.at(i);
147 CHECK(verifier_turl);
148 const TemplateURL* other_turl = other->GetTemplateURLForKeyword(
149 verifier_turl->keyword());
151 if (!other_turl) {
152 DVLOG(1) << "The other service did not contain a TURL with keyword: "
153 << verifier_turl->keyword();
154 return false;
156 if (!TURLsMatch(verifier_turl, other_turl))
157 return false;
160 return true;
163 bool AwaitAllServicesMatch() {
164 AwaitMatchStatusChangeChecker checker(base::Bind(AllServicesMatch),
165 "All search engines match");
166 checker.Wait();
167 return !checker.TimedOut();
170 bool AllServicesMatch() {
171 // Use 0 as the baseline.
172 if (test()->use_verifier() && !ServiceMatchesVerifier(0)) {
173 DVLOG(1) << "TemplateURLService 0 does not match verifier.";
174 return false;
177 for (int it = 1; it < test()->num_clients(); ++it) {
178 if (!ServicesMatch(0, it)) {
179 DVLOG(1) << "TemplateURLService " << it << " does not match with "
180 << "service 0.";
181 return false;
184 return true;
187 TemplateURL* CreateTestTemplateURL(Profile* profile, int seed) {
188 return CreateTestTemplateURL(profile, seed, CreateKeyword(seed),
189 base::StringPrintf("0000-0000-0000-%04d", seed));
192 TemplateURL* CreateTestTemplateURL(Profile* profile,
193 int seed,
194 const base::string16& keyword,
195 const std::string& sync_guid) {
196 return CreateTestTemplateURL(profile, seed, keyword,
197 base::StringPrintf("http://www.test%d.com/", seed), sync_guid);
200 TemplateURL* CreateTestTemplateURL(Profile* profile,
201 int seed,
202 const base::string16& keyword,
203 const std::string& url,
204 const std::string& sync_guid) {
205 TemplateURLData data;
206 data.SetShortName(CreateKeyword(seed));
207 data.SetKeyword(keyword);
208 data.SetURL(url);
209 data.favicon_url = GURL("http://favicon.url");
210 data.safe_for_autoreplace = true;
211 data.date_created = base::Time::FromTimeT(100);
212 data.last_modified = base::Time::FromTimeT(100);
213 data.prepopulate_id = 999999;
214 data.sync_guid = sync_guid;
215 return new TemplateURL(data);
218 void AddSearchEngine(int profile_index, int seed) {
219 Profile* profile = test()->GetProfile(profile_index);
220 TemplateURLServiceFactory::GetForProfile(profile)->Add(
221 CreateTestTemplateURL(profile, seed));
222 if (test()->use_verifier())
223 GetVerifierService()->Add(CreateTestTemplateURL(profile, seed));
226 void EditSearchEngine(int profile_index,
227 const base::string16& keyword,
228 const base::string16& short_name,
229 const base::string16& new_keyword,
230 const std::string& url) {
231 DCHECK(!url.empty());
232 TemplateURLService* service = GetServiceForBrowserContext(profile_index);
233 TemplateURL* turl = service->GetTemplateURLForKeyword(keyword);
234 EXPECT_TRUE(turl);
235 ASSERT_FALSE(new_keyword.empty());
236 service->ResetTemplateURL(turl, short_name, new_keyword, url);
237 // Make sure we do the same on the verifier.
238 if (test()->use_verifier()) {
239 TemplateURL* verifier_turl =
240 GetVerifierService()->GetTemplateURLForKeyword(keyword);
241 EXPECT_TRUE(verifier_turl);
242 GetVerifierService()->ResetTemplateURL(verifier_turl, short_name,
243 new_keyword, url);
247 void DeleteSearchEngineBySeed(int profile_index, int seed) {
248 TemplateURLService* service = GetServiceForBrowserContext(profile_index);
249 base::string16 keyword(CreateKeyword(seed));
250 TemplateURL* turl = service->GetTemplateURLForKeyword(keyword);
251 EXPECT_TRUE(turl);
252 service->Remove(turl);
253 // Make sure we do the same on the verifier.
254 if (test()->use_verifier()) {
255 TemplateURL* verifier_turl =
256 GetVerifierService()->GetTemplateURLForKeyword(keyword);
257 EXPECT_TRUE(verifier_turl);
258 GetVerifierService()->Remove(verifier_turl);
262 void ChangeDefaultSearchProvider(int profile_index, int seed) {
263 TemplateURLService* service = GetServiceForBrowserContext(profile_index);
264 ASSERT_TRUE(service);
265 TemplateURL* turl = service->GetTemplateURLForKeyword(CreateKeyword(seed));
266 ASSERT_TRUE(turl);
267 service->SetUserSelectedDefaultSearchProvider(turl);
268 if (test()->use_verifier()) {
269 TemplateURL* verifier_turl =
270 GetVerifierService()->GetTemplateURLForKeyword(CreateKeyword(seed));
271 ASSERT_TRUE(verifier_turl);
272 GetVerifierService()->SetUserSelectedDefaultSearchProvider(verifier_turl);
276 bool HasSearchEngine(int profile_index, int seed) {
277 TemplateURLService* service = GetServiceForBrowserContext(profile_index);
278 base::string16 keyword(CreateKeyword(seed));
279 TemplateURL* turl = service->GetTemplateURLForKeyword(keyword);
280 return turl != nullptr;
283 } // namespace search_engines_helper