Commit 391eaa4e authored by Mikel Astiz's avatar Mikel Astiz Committed by Commit Bot

[sync] Refactor search engines' integration tests

This patch migrates the helper libraries to avoid using the concept of
a 'seed', which was hard to grasp, and instead migrates tests to more
directly use keywords.

Change-Id: Ibcdae092baade651daca059850ac31f276f47564
Bug: None
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2495461Reviewed-by: default avatarMaksim Moskvitin <mmoskvitin@google.com>
Commit-Queue: Mikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821819}
parent 5dcd99bd
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
#include <vector> #include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "base/guid.h"
#include "base/hash/sha1.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
...@@ -34,8 +38,8 @@ GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) { ...@@ -34,8 +38,8 @@ GUIDToTURLMap CreateGUIDToTURLMap(TemplateURLService* service) {
} }
std::string GetTURLInfoString(const TemplateURL& turl) { std::string GetTURLInfoString(const TemplateURL& turl) {
return "TemplateURL: shortname: " + base::UTF16ToASCII(turl.short_name()) + return "TemplateURL: shortname: " + base::UTF16ToUTF8(turl.short_name()) +
" keyword: " + base::UTF16ToASCII(turl.keyword()) + " keyword: " + base::UTF16ToUTF8(turl.keyword()) +
" url: " + turl.url(); " url: " + turl.url();
} }
...@@ -93,12 +97,6 @@ bool ServicesMatch(int profile_a, int profile_b, std::ostream* os) { ...@@ -93,12 +97,6 @@ bool ServicesMatch(int profile_a, int profile_b, std::ostream* os) {
return true; return true;
} }
// Convenience helper for consistently generating the same keyword for a given
// seed.
base::string16 CreateKeyword(int seed) {
return base::ASCIIToUTF16(base::StringPrintf("test%d", seed));
}
} // namespace } // namespace
namespace search_engines_helper { namespace search_engines_helper {
...@@ -163,110 +161,102 @@ bool AllServicesMatch(std::ostream* os) { ...@@ -163,110 +161,102 @@ bool AllServicesMatch(std::ostream* os) {
return true; return true;
} }
std::unique_ptr<TemplateURL> CreateTestTemplateURL(Profile* profile, int seed) { TemplateURLBuilder::TemplateURLBuilder(const std::string& keyword) {
return CreateTestTemplateURL(profile, seed, CreateKeyword(seed), data_.SetShortName(base::UTF8ToUTF16(keyword));
base::StringPrintf("0000-0000-0000-%04d", seed)); data_.SetKeyword(base::UTF8ToUTF16(keyword));
data_.SetURL(base::StringPrintf("http://www.test-%s.com/", keyword.c_str()));
data_.favicon_url = GURL("http://favicon.url");
data_.safe_for_autoreplace = true;
data_.date_created = base::Time::FromTimeT(100);
data_.last_modified = base::Time::FromTimeT(100);
data_.prepopulate_id = 999999;
// Produce a GUID deterministically from |keyword|.
std::string hex_encoded_hash = base::HexEncode(
base::SHA1HashSpan(base::as_bytes(base::make_span(keyword))));
hex_encoded_hash.resize(12);
data_.sync_guid =
base::StrCat({"12345678-0000-4000-8000-", hex_encoded_hash});
DCHECK(base::IsValidGUID(data_.sync_guid));
} }
std::unique_ptr<TemplateURL> CreateTestTemplateURL( TemplateURLBuilder::~TemplateURLBuilder() = default;
Profile* profile,
int seed,
const base::string16& keyword,
const std::string& sync_guid) {
return CreateTestTemplateURL(profile, seed, keyword,
base::StringPrintf("http://www.test%d.com/", seed), sync_guid);
}
std::unique_ptr<TemplateURL> CreateTestTemplateURL( std::unique_ptr<TemplateURL> TemplateURLBuilder::Build() {
Profile* profile, return std::make_unique<TemplateURL>(data_);
int seed,
const base::string16& keyword,
const std::string& url,
const std::string& sync_guid) {
TemplateURLData data;
data.SetShortName(CreateKeyword(seed));
data.SetKeyword(keyword);
data.SetURL(url);
data.favicon_url = GURL("http://favicon.url");
data.safe_for_autoreplace = true;
data.date_created = base::Time::FromTimeT(100);
data.last_modified = base::Time::FromTimeT(100);
data.prepopulate_id = 999999;
data.sync_guid = sync_guid;
return std::make_unique<TemplateURL>(data);
} }
void AddSearchEngine(int profile_index, int seed) { void AddSearchEngine(int profile_index, const std::string& keyword) {
Profile* profile = test()->GetProfile(profile_index); Profile* profile = test()->GetProfile(profile_index);
TemplateURLServiceFactory::GetForProfile(profile)->Add( TemplateURLBuilder builder(keyword);
CreateTestTemplateURL(profile, seed)); TemplateURLServiceFactory::GetForProfile(profile)->Add(builder.Build());
if (test()->UseVerifier()) if (test()->UseVerifier())
GetVerifierService()->Add(CreateTestTemplateURL(profile, seed)); GetVerifierService()->Add(builder.Build());
} }
void EditSearchEngine(int profile_index, void EditSearchEngine(int profile_index,
const base::string16& keyword, const std::string& keyword,
const base::string16& short_name, const base::string16& short_name,
const base::string16& new_keyword, const std::string& new_keyword,
const std::string& url) { const std::string& url) {
ASSERT_FALSE(url.empty()); ASSERT_FALSE(url.empty());
TemplateURLService* service = GetServiceForBrowserContext(profile_index); TemplateURLService* service = GetServiceForBrowserContext(profile_index);
TemplateURL* turl = service->GetTemplateURLForKeyword(keyword); TemplateURL* turl =
service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
EXPECT_TRUE(turl); EXPECT_TRUE(turl);
ASSERT_FALSE(new_keyword.empty()); ASSERT_FALSE(new_keyword.empty());
service->ResetTemplateURL(turl, short_name, new_keyword, url); service->ResetTemplateURL(turl, short_name, base::UTF8ToUTF16(new_keyword),
url);
// Make sure we do the same on the verifier. // Make sure we do the same on the verifier.
if (test()->UseVerifier()) { if (test()->UseVerifier()) {
TemplateURL* verifier_turl = TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
GetVerifierService()->GetTemplateURLForKeyword(keyword); base::UTF8ToUTF16(keyword));
EXPECT_TRUE(verifier_turl); EXPECT_TRUE(verifier_turl);
GetVerifierService()->ResetTemplateURL(verifier_turl, short_name, GetVerifierService()->ResetTemplateURL(verifier_turl, short_name,
new_keyword, url); base::UTF8ToUTF16(new_keyword), url);
} }
} }
void DeleteSearchEngineBySeed(int profile_index, int seed) { void DeleteSearchEngine(int profile_index, const std::string& keyword) {
TemplateURLService* service = GetServiceForBrowserContext(profile_index); TemplateURLService* service = GetServiceForBrowserContext(profile_index);
base::string16 keyword(CreateKeyword(seed)); TemplateURL* turl =
TemplateURL* turl = service->GetTemplateURLForKeyword(keyword); service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
EXPECT_TRUE(turl); EXPECT_TRUE(turl);
service->Remove(turl); service->Remove(turl);
// Make sure we do the same on the verifier. // Make sure we do the same on the verifier.
if (test()->UseVerifier()) { if (test()->UseVerifier()) {
TemplateURL* verifier_turl = TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
GetVerifierService()->GetTemplateURLForKeyword(keyword); base::UTF8ToUTF16(keyword));
EXPECT_TRUE(verifier_turl); EXPECT_TRUE(verifier_turl);
GetVerifierService()->Remove(verifier_turl); GetVerifierService()->Remove(verifier_turl);
} }
} }
void ChangeDefaultSearchProvider(int profile_index, int seed) { void ChangeDefaultSearchProvider(int profile_index,
const std::string& keyword) {
TemplateURLService* service = GetServiceForBrowserContext(profile_index); TemplateURLService* service = GetServiceForBrowserContext(profile_index);
TemplateURL* turl = service->GetTemplateURLForKeyword(CreateKeyword(seed)); TemplateURL* turl =
service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
ASSERT_TRUE(turl); ASSERT_TRUE(turl);
service->SetUserSelectedDefaultSearchProvider(turl); service->SetUserSelectedDefaultSearchProvider(turl);
if (test()->UseVerifier()) { if (test()->UseVerifier()) {
TemplateURL* verifier_turl = TemplateURL* verifier_turl = GetVerifierService()->GetTemplateURLForKeyword(
GetVerifierService()->GetTemplateURLForKeyword(CreateKeyword(seed)); base::UTF8ToUTF16(keyword));
ASSERT_TRUE(verifier_turl); ASSERT_TRUE(verifier_turl);
GetVerifierService()->SetUserSelectedDefaultSearchProvider(verifier_turl); GetVerifierService()->SetUserSelectedDefaultSearchProvider(verifier_turl);
} }
} }
bool HasSearchEngine(int profile_index, int seed) { bool HasSearchEngine(int profile_index, const std::string& keyword) {
return HasSearchEngineWithKeyword(profile_index, CreateKeyword(seed));
}
bool HasSearchEngineWithKeyword(int profile_index,
const base::string16& keyword) {
TemplateURLService* service = GetServiceForBrowserContext(profile_index); TemplateURLService* service = GetServiceForBrowserContext(profile_index);
TemplateURL* turl = service->GetTemplateURLForKeyword(keyword); TemplateURL* turl =
service->GetTemplateURLForKeyword(base::UTF8ToUTF16(keyword));
return turl != nullptr; return turl != nullptr;
} }
base::string16 GetDefaultSearchEngineKeyword(int profile_index) { std::string GetDefaultSearchEngineKeyword(int profile_index) {
TemplateURLService* service = GetServiceForBrowserContext(profile_index); TemplateURLService* service = GetServiceForBrowserContext(profile_index);
return service->GetDefaultSearchProvider()->keyword(); return base::UTF16ToUTF8(service->GetDefaultSearchProvider()->keyword());
} }
SearchEnginesMatchChecker::SearchEnginesMatchChecker() { SearchEnginesMatchChecker::SearchEnginesMatchChecker() {
...@@ -289,12 +279,10 @@ void SearchEnginesMatchChecker::OnTemplateURLServiceChanged() { ...@@ -289,12 +279,10 @@ void SearchEnginesMatchChecker::OnTemplateURLServiceChanged() {
CheckExitCondition(); CheckExitCondition();
} }
HasSearchEngineChecker::HasSearchEngineChecker(int profile_index, int seed)
: HasSearchEngineChecker(profile_index, CreateKeyword(seed)) {}
HasSearchEngineChecker::HasSearchEngineChecker(int profile_index, HasSearchEngineChecker::HasSearchEngineChecker(int profile_index,
const base::string16& keyword) const std::string& keyword)
: service_(GetServiceForBrowserContext(profile_index)), keyword_(keyword) { : service_(GetServiceForBrowserContext(profile_index)),
keyword_(base::UTF8ToUTF16(keyword)) {
observer_.Add(service_); observer_.Add(service_);
} }
......
...@@ -15,9 +15,7 @@ ...@@ -15,9 +15,7 @@
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "components/search_engines/template_url_service_observer.h" #include "components/search_engines/template_url_service_observer.h"
class Profile;
class TemplateURL; class TemplateURL;
class TemplateURLService;
using GUIDToTURLMap = std::map<std::string, const TemplateURL*>; using GUIDToTURLMap = std::map<std::string, const TemplateURL*>;
...@@ -37,52 +35,47 @@ bool ServiceMatchesVerifier(int profile_index); ...@@ -37,52 +35,47 @@ bool ServiceMatchesVerifier(int profile_index);
bool AllServicesMatch(); bool AllServicesMatch();
bool AllServicesMatch(std::ostream* os); bool AllServicesMatch(std::ostream* os);
// Create a TemplateURL with some test values based on |seed|. // Builder class that by default infers all fields from |keyword| and allows
std::unique_ptr<TemplateURL> CreateTestTemplateURL( // overriding those default values.
Profile* profile, class TemplateURLBuilder {
int seed, public:
const base::string16& keyword, explicit TemplateURLBuilder(const std::string& keyword);
const std::string& sync_guid); ~TemplateURLBuilder();
std::unique_ptr<TemplateURL> CreateTestTemplateURL(
Profile* profile, TemplateURLData* data() { return &data_; }
int seed, std::unique_ptr<TemplateURL> Build();
const base::string16& keyword,
const std::string& url, private:
const std::string& sync_guid); TemplateURLData data_;
};
// Add a search engine based on a seed to the service at index |profile_index|
// and the verifier if it is used. // Add a search engine based on a keyword to the service at index
void AddSearchEngine(int profile_index, int seed); // |profile_index| and the verifier if it is used.
void AddSearchEngine(int profile_index, const std::string& keyword);
// Retrieves a search engine from the service at index |profile_index| with // Retrieves a search engine from the service at index |profile_index| with
// original keyword |keyword| and changes its user-visible fields. Does the same // original keyword |keyword| and changes its user-visible fields. Does the same
// to the verifier, if it is used. // to the verifier, if it is used.
void EditSearchEngine(int profile_index, void EditSearchEngine(int profile_index,
const base::string16& keyword, const std::string& keyword,
const base::string16& short_name, const base::string16& short_name,
const base::string16& new_keyword, const std::string& new_keyword,
const std::string& url); const std::string& url);
// Deletes a search engine from the service at index |profile_index| which was // Deletes a search engine from the service at index |profile_index| with
// generated by seed |seed|. // |keyword|.
void DeleteSearchEngineBySeed(int profile_index, int seed); void DeleteSearchEngine(int profile_index, const std::string& keyword);
// Change the search engine generated with |seed| in service at index
// |profile_index| to be the new default. Does the same to the verifier, if it
// is used.
void ChangeDefaultSearchProvider(int profile_index, int seed);
// Returns true if the profile at |profile_index| has a search engine matching // Changes the search engine with |keyword| to be the new default for
// the search engine generated with |seed|. // |profile_index|. Does the same to the verifier, if it is used.
bool HasSearchEngine(int profile_index, int seed); void ChangeDefaultSearchProvider(int profile_index, const std::string& keyword);
// Returns true if the profile at |profile_index| has a search engine matching // Returns true if the profile at |profile_index| has a search engine matching
// |keyword|. // |keyword|.
bool HasSearchEngineWithKeyword(int profile_index, bool HasSearchEngine(int profile_index, const std::string& keyword);
const base::string16& keyword);
// Returns the keyword for the default search engine at |profile_index|. // Returns the keyword for the default search engine at |profile_index|.
base::string16 GetDefaultSearchEngineKeyword(int profile_index); std::string GetDefaultSearchEngineKeyword(int profile_index);
// Checker that blocks until all services have the same search engine data. // Checker that blocks until all services have the same search engine data.
class SearchEnginesMatchChecker : public StatusChangeChecker, class SearchEnginesMatchChecker : public StatusChangeChecker,
...@@ -103,12 +96,11 @@ class SearchEnginesMatchChecker : public StatusChangeChecker, ...@@ -103,12 +96,11 @@ class SearchEnginesMatchChecker : public StatusChangeChecker,
}; };
// Checker that blocks until |profile_index| has a search engine matching the // Checker that blocks until |profile_index| has a search engine matching the
// search engine generated with |seed| or |keyword|. // search engine generated with |keyword|.
class HasSearchEngineChecker : public StatusChangeChecker, class HasSearchEngineChecker : public StatusChangeChecker,
public TemplateURLServiceObserver { public TemplateURLServiceObserver {
public: public:
HasSearchEngineChecker(int profile_index, int seed); HasSearchEngineChecker(int profile_index, const std::string& keyword);
HasSearchEngineChecker(int profile_index, const base::string16& keyword);
~HasSearchEngineChecker() override; ~HasSearchEngineChecker() override;
// StatusChangeChecker overrides. // StatusChangeChecker overrides.
......
...@@ -40,7 +40,8 @@ class SingleClientSearchEnginesSyncTest : public SyncTest { ...@@ -40,7 +40,8 @@ class SingleClientSearchEnginesSyncTest : public SyncTest {
IN_PROC_BROWSER_TEST_F(SingleClientSearchEnginesSyncTest, Sanity) { IN_PROC_BROWSER_TEST_F(SingleClientSearchEnginesSyncTest, Sanity) {
ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0)); ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0));
search_engines_helper::AddSearchEngine(0, 0); search_engines_helper::AddSearchEngine(/*profile_index=*/0,
/*keyword=*/"test0");
ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait()); ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());
ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0)); ASSERT_TRUE(search_engines_helper::ServiceMatchesVerifier(0));
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment