#include "components/search_engines/template_url_service.h"
#include <stddef.h>
#include <memory>
#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/search_engines/prepopulated_engines.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_utils.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service_client.h"
#include "components/search_engines/template_url_service_observer.h"
#include "components/search_engines/template_url_service_test_util.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
class TemplateURLServiceUnitTest : public TemplateURLServiceUnitTestBase { … };
TEST_F(TemplateURLServiceUnitTest, SessionToken) { … }
TEST_F(TemplateURLServiceUnitTest, GenerateSearchURL) { … }
TEST_F(TemplateURLServiceUnitTest, ExtractSearchMetadata) { … }
#if BUILDFLAG(IS_ANDROID)
class TemplateURLServiceWithDatabaseUnitTest
: public LoadedTemplateURLServiceUnitTestBase {
protected:
const std::u16string kOldPlayEngineKeyword = u"old_keyword";
const std::u16string kNewPlayEngineKeyword = u"new_keyword";
TemplateURLData CreatePlayAPITemplateURLData(const std::u16string& keyword) {
return TemplateURLService::CreatePlayAPITemplateURLData(
keyword, keyword,
base::StringPrintf("https://%s.com/q={searchTerms}",
base::UTF16ToUTF8(keyword).c_str()));
}
TemplateURL* AddPlayApiEngineLegacy(const std::u16string& keyword,
bool set_as_default) {
TemplateURL* template_url = template_url_service().Add(
std::make_unique<TemplateURL>(CreatePlayAPITemplateURLData(keyword)));
CHECK(template_url);
CHECK(template_url->created_from_play_api());
CHECK_EQ(template_url,
template_url_service().GetTemplateURLForKeyword(keyword));
if (set_as_default) {
template_url_service().SetUserSelectedDefaultSearchProvider(template_url);
CHECK_EQ(template_url_service().GetDefaultSearchProvider()->keyword(),
keyword);
} else {
CHECK_EQ(template_url_service().GetDefaultSearchProvider()->keyword(),
TemplateURLPrepopulateData::google.keyword);
}
return template_url;
}
};
TEST_F(TemplateURLServiceWithDatabaseUnitTest, ResetPlayAPISearchEngine) {
const size_t initial_turl_count =
template_url_service().GetTemplateURLs().size();
AddPlayApiEngineLegacy(kOldPlayEngineKeyword,
false);
EXPECT_EQ(template_url_service().GetTemplateURLs().size(),
initial_turl_count + 1);
const auto new_play_engine_data =
CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
EXPECT_TRUE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
EXPECT_FALSE(
template_url_service().GetTemplateURLForKeyword(kOldPlayEngineKeyword));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
EXPECT_EQ(template_url_service().GetTemplateURLs().size(),
initial_turl_count + 1);
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_OldIsDefault) {
AddPlayApiEngineLegacy(kOldPlayEngineKeyword, true);
const auto new_play_engine_data =
CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
EXPECT_TRUE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
EXPECT_FALSE(
template_url_service().GetTemplateURLForKeyword(kOldPlayEngineKeyword));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_NewIsPrepopulated) {
const std::u16string overriden_keyword =
TemplateURLPrepopulateData::bing.keyword;
TemplateURL* old_prepopulated_engine =
template_url_service().GetTemplateURLForKeyword(overriden_keyword);
ASSERT_TRUE(old_prepopulated_engine);
const std::string new_engine_url_from_play =
"https://new.chromium?q={searchTerms}";
const std::u16string new_engine_name_from_play = u"New Engine From Play";
const auto new_engine_data = TemplateURLService::CreatePlayAPITemplateURLData(
overriden_keyword, new_engine_name_from_play, new_engine_url_from_play);
EXPECT_TRUE(template_url_service().ResetPlayAPISearchEngine(new_engine_data));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(overriden_keyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_EQ(new_play_engine->prepopulate_id(), 0);
EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
EXPECT_EQ(new_play_engine->url(), new_engine_url_from_play);
EXPECT_EQ(new_play_engine->short_name(), new_engine_name_from_play);
ASSERT_THAT(
GetTemplateURLsMatchingKeyword(overriden_keyword),
testing::UnorderedElementsAre(old_prepopulated_engine, new_play_engine));
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_OldIsPrepopulated) {
const std::u16string overriden_keyword =
TemplateURLPrepopulateData::bing.keyword;
TemplateURL* old_prepopulated_engine =
template_url_service().GetTemplateURLForKeyword(overriden_keyword);
ASSERT_TRUE(old_prepopulated_engine);
TemplateURL* old_play_engine =
AddPlayApiEngineLegacy(overriden_keyword,
true);
ASSERT_THAT(
GetTemplateURLsMatchingKeyword(overriden_keyword),
testing::UnorderedElementsAre(old_prepopulated_engine, old_play_engine));
ASSERT_EQ(old_play_engine,
template_url_service().GetTemplateURLForKeyword(overriden_keyword));
const auto new_play_engine_data =
CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
EXPECT_TRUE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
EXPECT_THAT(GetTemplateURLsMatchingKeyword(overriden_keyword),
testing::UnorderedElementsAre(old_prepopulated_engine));
EXPECT_EQ(old_prepopulated_engine,
template_url_service().GetTemplateURLForKeyword(overriden_keyword));
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_NewHasSameKeywordAsOld) {
const std::u16string play_engine_keyword = u"play_keyword";
AddPlayApiEngineLegacy(play_engine_keyword, false);
const std::string new_engine_url_from_play =
"https://play.chromium?q={searchTerms}&is_new=true";
const std::u16string new_engine_name_from_play = u"Play Engine New";
const auto new_play_engine_data =
TemplateURLService::CreatePlayAPITemplateURLData(
play_engine_keyword, new_engine_name_from_play,
new_engine_url_from_play);
EXPECT_TRUE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(play_engine_keyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_EQ(new_play_engine, template_url_service().GetDefaultSearchProvider());
EXPECT_THAT(GetTemplateURLsMatchingKeyword(play_engine_keyword),
testing::Contains(new_play_engine));
EXPECT_EQ(new_play_engine->url(), new_engine_url_from_play);
EXPECT_EQ(new_play_engine->short_name(), new_engine_name_from_play);
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_DseSetByPolicy) {
TemplateURLData policy_engine_data;
policy_engine_data.SetShortName(u"Chromium.org Search");
policy_engine_data.SetKeyword(u"cr_search");
policy_engine_data.SetURL("https://search.chromium.org?q={searchTerms}");
template_url_service().ApplyDefaultSearchChangeForTesting(
&policy_engine_data, DefaultSearchManager::FROM_POLICY);
const TemplateURL* policy_engine =
template_url_service().GetDefaultSearchProvider();
ASSERT_EQ(policy_engine->keyword(), policy_engine_data.keyword());
ASSERT_EQ(policy_engine->created_by_policy(),
TemplateURLData::CreatedByPolicy::kDefaultSearchProvider);
const auto new_play_engine_data =
CreatePlayAPITemplateURLData(kNewPlayEngineKeyword);
EXPECT_TRUE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
auto* new_play_engine =
template_url_service().GetTemplateURLForKeyword(kNewPlayEngineKeyword);
EXPECT_TRUE(new_play_engine);
EXPECT_TRUE(new_play_engine->created_from_play_api());
EXPECT_NE(new_play_engine, template_url_service().GetDefaultSearchProvider());
EXPECT_EQ(policy_engine, template_url_service().GetDefaultSearchProvider());
}
TEST_F(TemplateURLServiceWithDatabaseUnitTest,
ResetPlayAPISearchEngine_NewHasSameKeywordAsPolicyEngine) {
TemplateURLData policy_engine_data;
policy_engine_data.SetShortName(u"Chromium.org Search");
policy_engine_data.SetKeyword(u"cr_search");
policy_engine_data.SetURL("https://search.chromium.org?q={searchTerms}");
template_url_service().ApplyDefaultSearchChangeForTesting(
&policy_engine_data, DefaultSearchManager::FROM_POLICY);
const TemplateURL* policy_engine =
template_url_service().GetDefaultSearchProvider();
ASSERT_EQ(policy_engine->keyword(), policy_engine_data.keyword());
ASSERT_EQ(policy_engine->created_by_policy(),
TemplateURLData::CreatedByPolicy::kDefaultSearchProvider);
const auto new_play_engine_data =
TemplateURLService::CreatePlayAPITemplateURLData(
policy_engine_data.keyword(), u"title2",
"https://example2.com/q={searchTerms}");
EXPECT_FALSE(
template_url_service().ResetPlayAPISearchEngine(new_play_engine_data));
EXPECT_THAT(GetTemplateURLsMatchingKeyword(policy_engine_data.keyword()),
testing::UnorderedElementsAre(policy_engine));
EXPECT_EQ(policy_engine, template_url_service().GetDefaultSearchProvider());
}
#endif