15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/test/integration/search_engines_helper.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
12eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sync/test/integration/sync_test.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using sync_datatype_helper::test;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(service);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GUIDToTURLMap map;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService::TemplateURLVector turls = service->GetTemplateURLs();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (TemplateURLService::TemplateURLVector::iterator it = turls.begin();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it != turls.end(); ++it) {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(*it);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(map.find((*it)->sync_guid()) == map.end());
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    map[(*it)->sync_guid()] = *it;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return map;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetTURLInfoString(const TemplateURL* turl) {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(turl);
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return "TemplateURL: shortname: " + base::UTF16ToASCII(turl->short_name()) +
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      " keyword: " + base::UTF16ToASCII(turl->keyword()) + " url: " +
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      turl->url();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TURLsMatch(const TemplateURL* turl1, const TemplateURL* turl2) {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(turl1);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(turl2);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool result = (turl1->url() == turl2->url()) &&
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (turl1->keyword() == turl2->keyword()) &&
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (turl1->short_name() == turl2->short_name());
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Print some useful debug info.
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!result) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "TemplateURLs did not match: " << GetTURLInfoString(turl1)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << " vs " << GetTURLInfoString(turl2);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ServicesMatch(int profile_a, int profile_b) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* service_a =
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      search_engines_helper::GetServiceForBrowserContext(profile_a);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* service_b =
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      search_engines_helper::GetServiceForBrowserContext(profile_b);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(service_a);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(service_b);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Services that have synced should have identical TURLs, including the GUIDs.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure we compare those fields in addition to the user-visible fields.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GUIDToTURLMap a_turls = CreateGUIDToTURLMap(service_a);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GUIDToTURLMap b_turls = CreateGUIDToTURLMap(service_b);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (a_turls.size() != b_turls.size()) {
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Service a and b do not match in size: " << a_turls.size()
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << " vs " << b_turls.size() << " respectively.";
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (GUIDToTURLMap::iterator it = a_turls.begin();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it != a_turls.end(); ++it) {
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (b_turls.find(it->first) == b_turls.end()) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "TURL GUID from a not found in b's TURLs: " << it->first;
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!TURLsMatch(b_turls[it->first], it->second))
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURL* default_a = service_a->GetDefaultSearchProvider();
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const TemplateURL* default_b = service_b->GetDefaultSearchProvider();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(default_a);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(default_b);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!TURLsMatch(default_a, default_b)) {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Default search providers do not match: A's default: "
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << default_a->keyword() << " B's default: "
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << default_b->keyword();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(INFO) << "A had default with URL: " << default_a->url()
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              << " and keyword: " << default_a->keyword();
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Convenience helper for consistently generating the same keyword for a given
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// seed.
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::string16 CreateKeyword(int seed) {
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return base::ASCIIToUTF16(base::StringPrintf("test%d", seed));
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace search_engines_helper {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)TemplateURLService* GetServiceForBrowserContext(int profile_index) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TemplateURLServiceFactory::GetForProfile(
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      test()->GetProfile(profile_index));
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURLService* GetVerifierService() {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TemplateURLServiceFactory::GetForProfile(test()->verifier());
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ServiceMatchesVerifier(int profile_index) {
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* verifier = GetVerifierService();
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  TemplateURLService* other = GetServiceForBrowserContext(profile_index);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(verifier);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(other);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService::TemplateURLVector verifier_turls =
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      verifier->GetTemplateURLs();
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (verifier_turls.size() != other->GetTemplateURLs().size()) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "Verifier and other service have a different count of TURLs: "
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << verifier_turls.size() << " vs "
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               << other->GetTemplateURLs().size() << " respectively.";
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < verifier_turls.size(); ++i) {
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TemplateURL* verifier_turl = verifier_turls.at(i);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(verifier_turl);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TemplateURL* other_turl = other->GetTemplateURLForKeyword(
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        verifier_turl->keyword());
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!other_turl) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "The other service did not contain a TURL with keyword: "
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << verifier_turl->keyword();
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!TURLsMatch(verifier_turl, other_turl))
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool AllServicesMatch() {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use 0 as the baseline.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test()->use_verifier() && !ServiceMatchesVerifier(0)) {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(ERROR) << "TemplateURLService 0 does not match verifier.";
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int it = 1; it < test()->num_clients(); ++it) {
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!ServicesMatch(0, it)) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(ERROR) << "TemplateURLService " << it << " does not match with "
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 << "service 0.";
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURL* CreateTestTemplateURL(Profile* profile, int seed) {
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return CreateTestTemplateURL(profile, seed, CreateKeyword(seed),
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               base::StringPrintf("0000-0000-0000-%04d", seed));
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURL* CreateTestTemplateURL(Profile* profile,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int seed,
186a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                   const base::string16& keyword,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::string& sync_guid) {
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return CreateTestTemplateURL(profile, seed, keyword,
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::StringPrintf("http://www.test%d.com/", seed), sync_guid);
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TemplateURL* CreateTestTemplateURL(Profile* profile,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int seed,
194a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                   const base::string16& keyword,
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::string& url,
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::string& sync_guid) {
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLData data;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.short_name = CreateKeyword(seed);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.SetKeyword(keyword);
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.SetURL(url);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.favicon_url = GURL("http://favicon.url");
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.safe_for_autoreplace = true;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.date_created = base::Time::FromTimeT(100);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.last_modified = base::Time::FromTimeT(100);
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.prepopulate_id = 999999;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  data.sync_guid = sync_guid;
207f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return new TemplateURL(data);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AddSearchEngine(int profile_index, int seed) {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile = test()->GetProfile(profile_index);
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLServiceFactory::GetForProfile(profile)->Add(
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CreateTestTemplateURL(profile, seed));
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test()->use_verifier())
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetVerifierService()->Add(CreateTestTemplateURL(profile, seed));
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void EditSearchEngine(int profile_index,
219a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      const base::string16& keyword,
220a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      const base::string16& short_name,
221a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                      const base::string16& new_keyword,
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const std::string& url) {
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!url.empty());
22490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* turl = service->GetTemplateURLForKeyword(keyword);
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(turl);
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_FALSE(new_keyword.empty());
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  service->ResetTemplateURL(turl, short_name, new_keyword, url);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure we do the same on the verifier.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test()->use_verifier()) {
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TemplateURL* verifier_turl =
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GetVerifierService()->GetTemplateURLForKeyword(keyword);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(verifier_turl);
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetVerifierService()->ResetTemplateURL(verifier_turl, short_name,
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           new_keyword, url);
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void DeleteSearchEngineBySeed(int profile_index, int seed) {
24090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
241a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 keyword(CreateKeyword(seed));
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* turl = service->GetTemplateURLForKeyword(keyword);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(turl);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  service->Remove(turl);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Make sure we do the same on the verifier.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test()->use_verifier()) {
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TemplateURL* verifier_turl =
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GetVerifierService()->GetTemplateURLForKeyword(keyword);
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(verifier_turl);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetVerifierService()->Remove(verifier_turl);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ChangeDefaultSearchProvider(int profile_index, int seed) {
25590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  TemplateURLService* service = GetServiceForBrowserContext(profile_index);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(service);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURL* turl = service->GetTemplateURLForKeyword(CreateKeyword(seed));
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ASSERT_TRUE(turl);
2595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  service->SetUserSelectedDefaultSearchProvider(turl);
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (test()->use_verifier()) {
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TemplateURL* verifier_turl =
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GetVerifierService()->GetTemplateURLForKeyword(CreateKeyword(seed));
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(verifier_turl);
2645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    GetVerifierService()->SetUserSelectedDefaultSearchProvider(verifier_turl);
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace search_engines_helper
269