template_url_prepopulate_data_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // Copyright 2014 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.
  4. #include "components/search_engines/template_url_prepopulate_data.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/command_line.h"
  9. #include "base/containers/contains.h"
  10. #include "base/files/scoped_temp_dir.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/values.h"
  13. #include "components/country_codes/country_codes.h"
  14. #include "components/google/core/common/google_switches.h"
  15. #include "components/search_engines/prepopulated_engines.h"
  16. #include "components/search_engines/search_engines_pref_names.h"
  17. #include "components/search_engines/search_terms_data.h"
  18. #include "components/search_engines/template_url.h"
  19. #include "components/search_engines/template_url_data_util.h"
  20. #include "components/search_engines/template_url_service.h"
  21. #include "components/search_engines/testing_search_terms_data.h"
  22. #include "components/sync_preferences/testing_pref_service_syncable.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. using base::ASCIIToUTF16;
  25. namespace {
  26. SearchEngineType GetEngineType(const std::string& url) {
  27. TemplateURLData data;
  28. data.SetURL(url);
  29. return TemplateURL(data).GetEngineType(SearchTermsData());
  30. }
  31. std::string GetHostFromTemplateURLData(const TemplateURLData& data) {
  32. return TemplateURL(data).url_ref().GetHost(SearchTermsData());
  33. }
  34. } // namespace
  35. class TemplateURLPrepopulateDataTest : public testing::Test {
  36. public:
  37. void SetUp() override {
  38. TemplateURLPrepopulateData::RegisterProfilePrefs(prefs_.registry());
  39. }
  40. protected:
  41. sync_preferences::TestingPrefServiceSyncable prefs_;
  42. };
  43. // Verifies the set of prepopulate data doesn't contain entries with duplicate
  44. // ids.
  45. TEST_F(TemplateURLPrepopulateDataTest, UniqueIDs) {
  46. const int kCountryIds[] = {
  47. 'A'<<8|'D', 'A'<<8|'E', 'A'<<8|'F', 'A'<<8|'G', 'A'<<8|'I',
  48. 'A'<<8|'L', 'A'<<8|'M', 'A'<<8|'N', 'A'<<8|'O', 'A'<<8|'Q',
  49. 'A'<<8|'R', 'A'<<8|'S', 'A'<<8|'T', 'A'<<8|'U', 'A'<<8|'W',
  50. 'A'<<8|'X', 'A'<<8|'Z', 'B'<<8|'A', 'B'<<8|'B', 'B'<<8|'D',
  51. 'B'<<8|'E', 'B'<<8|'F', 'B'<<8|'G', 'B'<<8|'H', 'B'<<8|'I',
  52. 'B'<<8|'J', 'B'<<8|'M', 'B'<<8|'N', 'B'<<8|'O', 'B'<<8|'R',
  53. 'B'<<8|'S', 'B'<<8|'T', 'B'<<8|'V', 'B'<<8|'W', 'B'<<8|'Y',
  54. 'B'<<8|'Z', 'C'<<8|'A', 'C'<<8|'C', 'C'<<8|'D', 'C'<<8|'F',
  55. 'C'<<8|'G', 'C'<<8|'H', 'C'<<8|'I', 'C'<<8|'K', 'C'<<8|'L',
  56. 'C'<<8|'M', 'C'<<8|'N', 'C'<<8|'O', 'C'<<8|'R', 'C'<<8|'U',
  57. 'C'<<8|'V', 'C'<<8|'X', 'C'<<8|'Y', 'C'<<8|'Z', 'D'<<8|'E',
  58. 'D'<<8|'J', 'D'<<8|'K', 'D'<<8|'M', 'D'<<8|'O', 'D'<<8|'Z',
  59. 'E'<<8|'C', 'E'<<8|'E', 'E'<<8|'G', 'E'<<8|'R', 'E'<<8|'S',
  60. 'E'<<8|'T', 'F'<<8|'I', 'F'<<8|'J', 'F'<<8|'K', 'F'<<8|'M',
  61. 'F'<<8|'O', 'F'<<8|'R', 'G'<<8|'A', 'G'<<8|'B', 'G'<<8|'D',
  62. 'G'<<8|'E', 'G'<<8|'F', 'G'<<8|'G', 'G'<<8|'H', 'G'<<8|'I',
  63. 'G'<<8|'L', 'G'<<8|'M', 'G'<<8|'N', 'G'<<8|'P', 'G'<<8|'Q',
  64. 'G'<<8|'R', 'G'<<8|'S', 'G'<<8|'T', 'G'<<8|'U', 'G'<<8|'W',
  65. 'G'<<8|'Y', 'H'<<8|'K', 'H'<<8|'M', 'H'<<8|'N', 'H'<<8|'R',
  66. 'H'<<8|'T', 'H'<<8|'U', 'I'<<8|'D', 'I'<<8|'E', 'I'<<8|'L',
  67. 'I'<<8|'M', 'I'<<8|'N', 'I'<<8|'O', 'I'<<8|'P', 'I'<<8|'Q',
  68. 'I'<<8|'R', 'I'<<8|'S', 'I'<<8|'T', 'J'<<8|'E', 'J'<<8|'M',
  69. 'J'<<8|'O', 'J'<<8|'P', 'K'<<8|'E', 'K'<<8|'G', 'K'<<8|'H',
  70. 'K'<<8|'I', 'K'<<8|'M', 'K'<<8|'N', 'K'<<8|'P', 'K'<<8|'R',
  71. 'K'<<8|'W', 'K'<<8|'Y', 'K'<<8|'Z', 'L'<<8|'A', 'L'<<8|'B',
  72. 'L'<<8|'C', 'L'<<8|'I', 'L'<<8|'K', 'L'<<8|'R', 'L'<<8|'S',
  73. 'L'<<8|'T', 'L'<<8|'U', 'L'<<8|'V', 'L'<<8|'Y', 'M'<<8|'A',
  74. 'M'<<8|'C', 'M'<<8|'D', 'M'<<8|'E', 'M'<<8|'G', 'M'<<8|'H',
  75. 'M'<<8|'K', 'M'<<8|'L', 'M'<<8|'M', 'M'<<8|'N', 'M'<<8|'O',
  76. 'M'<<8|'P', 'M'<<8|'Q', 'M'<<8|'R', 'M'<<8|'S', 'M'<<8|'T',
  77. 'M'<<8|'U', 'M'<<8|'V', 'M'<<8|'W', 'M'<<8|'X', 'M'<<8|'Y',
  78. 'M'<<8|'Z', 'N'<<8|'A', 'N'<<8|'C', 'N'<<8|'E', 'N'<<8|'F',
  79. 'N'<<8|'G', 'N'<<8|'I', 'N'<<8|'L', 'N'<<8|'O', 'N'<<8|'P',
  80. 'N'<<8|'R', 'N'<<8|'U', 'N'<<8|'Z', 'O'<<8|'M', 'P'<<8|'A',
  81. 'P'<<8|'E', 'P'<<8|'F', 'P'<<8|'G', 'P'<<8|'H', 'P'<<8|'K',
  82. 'P'<<8|'L', 'P'<<8|'M', 'P'<<8|'N', 'P'<<8|'R', 'P'<<8|'S',
  83. 'P'<<8|'T', 'P'<<8|'W', 'P'<<8|'Y', 'Q'<<8|'A', 'R'<<8|'E',
  84. 'R'<<8|'O', 'R'<<8|'S', 'R'<<8|'U', 'R'<<8|'W', 'S'<<8|'A',
  85. 'S'<<8|'B', 'S'<<8|'C', 'S'<<8|'D', 'S'<<8|'E', 'S'<<8|'G',
  86. 'S'<<8|'H', 'S'<<8|'I', 'S'<<8|'J', 'S'<<8|'K', 'S'<<8|'L',
  87. 'S'<<8|'M', 'S'<<8|'N', 'S'<<8|'O', 'S'<<8|'R', 'S'<<8|'T',
  88. 'S'<<8|'V', 'S'<<8|'Y', 'S'<<8|'Z', 'T'<<8|'C', 'T'<<8|'D',
  89. 'T'<<8|'F', 'T'<<8|'G', 'T'<<8|'H', 'T'<<8|'J', 'T'<<8|'K',
  90. 'T'<<8|'L', 'T'<<8|'M', 'T'<<8|'N', 'T'<<8|'O', 'T'<<8|'R',
  91. 'T'<<8|'T', 'T'<<8|'V', 'T'<<8|'W', 'T'<<8|'Z', 'U'<<8|'A',
  92. 'U'<<8|'G', 'U'<<8|'M', 'U'<<8|'S', 'U'<<8|'Y', 'U'<<8|'Z',
  93. 'V'<<8|'A', 'V'<<8|'C', 'V'<<8|'E', 'V'<<8|'G', 'V'<<8|'I',
  94. 'V'<<8|'N', 'V'<<8|'U', 'W'<<8|'F', 'W'<<8|'S', 'Y'<<8|'E',
  95. 'Y'<<8|'T', 'Z'<<8|'A', 'Z'<<8|'M', 'Z'<<8|'W', -1 };
  96. for (size_t i = 0; i < std::size(kCountryIds); ++i) {
  97. prefs_.SetInteger(country_codes::kCountryIDAtInstall, kCountryIds[i]);
  98. std::vector<std::unique_ptr<TemplateURLData>> urls =
  99. TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, nullptr);
  100. std::set<int> unique_ids;
  101. for (size_t turl_i = 0; turl_i < urls.size(); ++turl_i) {
  102. ASSERT_TRUE(unique_ids.find(urls[turl_i]->prepopulate_id) ==
  103. unique_ids.end());
  104. unique_ids.insert(urls[turl_i]->prepopulate_id);
  105. }
  106. }
  107. }
  108. // Verifies that default search providers from the preferences file
  109. // override the built-in ones.
  110. TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrefs) {
  111. prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion,
  112. std::make_unique<base::Value>(1));
  113. base::Value overrides(base::Value::Type::LIST);
  114. base::Value entry(base::Value::Type::DICTIONARY);
  115. // Set only the minimal required settings for a search provider configuration.
  116. entry.SetStringKey("name", "foo");
  117. entry.SetStringKey("keyword", "fook");
  118. entry.SetStringKey("search_url", "http://foo.com/s?q={searchTerms}");
  119. entry.SetStringKey("favicon_url", "http://foi.com/favicon.ico");
  120. entry.SetStringKey("encoding", "UTF-8");
  121. entry.SetIntKey("id", 1001);
  122. overrides.Append(entry.Clone());
  123. prefs_.SetUserPref(prefs::kSearchProviderOverrides,
  124. base::Value::ToUniquePtrValue(std::move(overrides)));
  125. int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
  126. EXPECT_EQ(1, version);
  127. size_t default_index;
  128. std::vector<std::unique_ptr<TemplateURLData>> t_urls =
  129. TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
  130. &default_index);
  131. ASSERT_EQ(1u, t_urls.size());
  132. EXPECT_EQ(u"foo", t_urls[0]->short_name());
  133. EXPECT_EQ(u"fook", t_urls[0]->keyword());
  134. EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0]));
  135. EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host());
  136. EXPECT_EQ(1u, t_urls[0]->input_encodings.size());
  137. EXPECT_EQ(1001, t_urls[0]->prepopulate_id);
  138. EXPECT_TRUE(t_urls[0]->suggestions_url.empty());
  139. EXPECT_EQ(0u, t_urls[0]->alternate_urls.size());
  140. EXPECT_TRUE(t_urls[0]->safe_for_autoreplace);
  141. EXPECT_TRUE(t_urls[0]->date_created.is_null());
  142. EXPECT_TRUE(t_urls[0]->last_modified.is_null());
  143. // Test the optional settings too.
  144. entry.SetStringKey("suggest_url", "http://foo.com/suggest?q={searchTerms}");
  145. base::Value alternate_urls(base::Value::Type::LIST);
  146. alternate_urls.Append("http://foo.com/alternate?q={searchTerms}");
  147. entry.SetKey("alternate_urls", std::move(alternate_urls));
  148. overrides = base::Value(base::Value::Type::LIST);
  149. overrides.Append(entry.Clone());
  150. prefs_.SetUserPref(prefs::kSearchProviderOverrides,
  151. base::Value::ToUniquePtrValue(std::move(overrides)));
  152. t_urls = TemplateURLPrepopulateData::GetPrepopulatedEngines(
  153. &prefs_, &default_index);
  154. ASSERT_EQ(1u, t_urls.size());
  155. EXPECT_EQ(u"foo", t_urls[0]->short_name());
  156. EXPECT_EQ(u"fook", t_urls[0]->keyword());
  157. EXPECT_EQ("foo.com", GetHostFromTemplateURLData(*t_urls[0]));
  158. EXPECT_EQ("foi.com", t_urls[0]->favicon_url.host());
  159. EXPECT_EQ(1u, t_urls[0]->input_encodings.size());
  160. EXPECT_EQ(1001, t_urls[0]->prepopulate_id);
  161. EXPECT_EQ("http://foo.com/suggest?q={searchTerms}",
  162. t_urls[0]->suggestions_url);
  163. ASSERT_EQ(1u, t_urls[0]->alternate_urls.size());
  164. EXPECT_EQ("http://foo.com/alternate?q={searchTerms}",
  165. t_urls[0]->alternate_urls[0]);
  166. // Test that subsequent providers are loaded even if an intermediate
  167. // provider has an incomplete configuration.
  168. overrides = base::Value(base::Value::Type::LIST);
  169. overrides.Append(entry.Clone());
  170. entry.SetIntKey("id", 1002);
  171. entry.SetStringKey("name", "bar");
  172. entry.SetStringKey("keyword", "bark");
  173. entry.SetStringKey("encoding", std::string());
  174. overrides.Append(entry.Clone());
  175. entry.SetIntKey("id", 1003);
  176. entry.SetStringKey("name", "baz");
  177. entry.SetStringKey("keyword", "bazk");
  178. entry.SetStringKey("encoding", "UTF-8");
  179. overrides.Append(entry.Clone());
  180. prefs_.SetUserPref(prefs::kSearchProviderOverrides,
  181. base::Value::ToUniquePtrValue(std::move(overrides)));
  182. t_urls =
  183. TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
  184. &default_index);
  185. EXPECT_EQ(2u, t_urls.size());
  186. }
  187. TEST_F(TemplateURLPrepopulateDataTest, ClearProvidersFromPrefs) {
  188. prefs_.SetUserPref(prefs::kSearchProviderOverridesVersion,
  189. std::make_unique<base::Value>(1));
  190. base::Value overrides(base::Value::Type::LIST);
  191. base::Value entry(base::Value::Type::DICTIONARY);
  192. // Set only the minimal required settings for a search provider configuration.
  193. entry.SetStringKey("name", "foo");
  194. entry.SetStringKey("keyword", "fook");
  195. entry.SetStringKey("search_url", "http://foo.com/s?q={searchTerms}");
  196. entry.SetStringKey("favicon_url", "http://foi.com/favicon.ico");
  197. entry.SetStringKey("encoding", "UTF-8");
  198. entry.SetIntKey("id", 1001);
  199. overrides.Append(std::move(entry));
  200. prefs_.SetUserPref(prefs::kSearchProviderOverrides,
  201. base::Value::ToUniquePtrValue(std::move(overrides)));
  202. int version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
  203. EXPECT_EQ(1, version);
  204. // This call removes the above search engine.
  205. TemplateURLPrepopulateData::ClearPrepopulatedEnginesInPrefs(&prefs_);
  206. version = TemplateURLPrepopulateData::GetDataVersion(&prefs_);
  207. EXPECT_EQ(TemplateURLPrepopulateData::kCurrentDataVersion, version);
  208. size_t default_index;
  209. std::vector<std::unique_ptr<TemplateURLData>> t_urls =
  210. TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
  211. &default_index);
  212. ASSERT_FALSE(t_urls.empty());
  213. for (size_t i = 0; i < t_urls.size(); ++i) {
  214. EXPECT_NE(u"foo", t_urls[i]->short_name());
  215. EXPECT_NE(u"fook", t_urls[i]->keyword());
  216. EXPECT_NE("foi.com", t_urls[i]->favicon_url.host());
  217. EXPECT_NE("foo.com", GetHostFromTemplateURLData(*t_urls[i]));
  218. EXPECT_NE(1001, t_urls[i]->prepopulate_id);
  219. }
  220. // Ensures the default URL is Google and has the optional fields filled.
  221. EXPECT_EQ(u"Google", t_urls[default_index]->short_name());
  222. EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
  223. EXPECT_FALSE(t_urls[default_index]->image_url.empty());
  224. EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
  225. EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
  226. EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
  227. TemplateURL(*t_urls[default_index]).GetEngineType(
  228. SearchTermsData()));
  229. }
  230. // Verifies that built-in search providers are processed correctly.
  231. TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) {
  232. // Use United States.
  233. prefs_.SetInteger(country_codes::kCountryIDAtInstall, 'U' << 8 | 'S');
  234. size_t default_index;
  235. std::vector<std::unique_ptr<TemplateURLData>> t_urls =
  236. TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
  237. &default_index);
  238. // Ensure all the URLs have the required fields populated.
  239. ASSERT_FALSE(t_urls.empty());
  240. for (size_t i = 0; i < t_urls.size(); ++i) {
  241. ASSERT_FALSE(t_urls[i]->short_name().empty());
  242. ASSERT_FALSE(t_urls[i]->keyword().empty());
  243. ASSERT_FALSE(t_urls[i]->favicon_url.host().empty());
  244. ASSERT_FALSE(GetHostFromTemplateURLData(*t_urls[i]).empty());
  245. ASSERT_FALSE(t_urls[i]->input_encodings.empty());
  246. EXPECT_GT(t_urls[i]->prepopulate_id, 0);
  247. EXPECT_TRUE(t_urls[0]->safe_for_autoreplace);
  248. EXPECT_TRUE(t_urls[0]->date_created.is_null());
  249. EXPECT_TRUE(t_urls[0]->last_modified.is_null());
  250. }
  251. // Ensures the default URL is Google and has the optional fields filled.
  252. EXPECT_EQ(u"Google", t_urls[default_index]->short_name());
  253. EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
  254. EXPECT_FALSE(t_urls[default_index]->image_url.empty());
  255. EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
  256. EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
  257. // Expect at least 2 alternate_urls.
  258. // This caught a bug with static initialization of arrays, so leave this in.
  259. EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u);
  260. for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i)
  261. EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty());
  262. EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
  263. TemplateURL(*t_urls[default_index]).GetEngineType(
  264. SearchTermsData()));
  265. }
  266. TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) {
  267. EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/"));
  268. EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/"));
  269. EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/"));
  270. EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/"));
  271. }
  272. TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeAdvanced) {
  273. // Google URLs in different forms.
  274. const char* kGoogleURLs[] = {
  275. // Original with google:baseURL:
  276. "{google:baseURL}search?q={searchTerms}&{google:RLZ}"
  277. "{google:originalQueryForSuggestion}{google:searchFieldtrialParameter}"
  278. "sourceid=chrome&ie={inputEncoding}",
  279. // Custom with google.com and reordered query params:
  280. "http://google.com/search?{google:RLZ}{google:originalQueryForSuggestion}"
  281. "{google:searchFieldtrialParameter}"
  282. "sourceid=chrome&ie={inputEncoding}&q={searchTerms}",
  283. // Custom with a country TLD and almost no query params:
  284. "http://www.google.ru/search?q={searchTerms}"
  285. };
  286. for (size_t i = 0; i < std::size(kGoogleURLs); ++i) {
  287. EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(kGoogleURLs[i]));
  288. }
  289. // Non-Google URLs.
  290. const char* kYahooURLs[] = {
  291. ("http://search.yahoo.com/search?"
  292. "ei={inputEncoding}&fr=crmas&p={searchTerms}"),
  293. "http://search.yahoo.com/search?p={searchTerms}",
  294. // Aggressively match types by checking just TLD+1.
  295. "http://someothersite.yahoo.com/",
  296. };
  297. for (size_t i = 0; i < std::size(kYahooURLs); ++i) {
  298. EXPECT_EQ(SEARCH_ENGINE_YAHOO, GetEngineType(kYahooURLs[i]));
  299. }
  300. // URLs for engines not present in country-specific lists.
  301. EXPECT_EQ(SEARCH_ENGINE_NIGMA,
  302. GetEngineType("http://nigma.ru/?s={searchTerms}&arg1=value1"));
  303. // Also test matching against alternate URLs (and TLD+1 matching).
  304. EXPECT_EQ(SEARCH_ENGINE_SOFTONIC,
  305. GetEngineType("http://test.softonic.com.br/?{searchTerms}"));
  306. // Search URL for which no prepopulated search provider exists.
  307. EXPECT_EQ(SEARCH_ENGINE_OTHER,
  308. GetEngineType("http://example.net/search?q={searchTerms}"));
  309. EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url"));
  310. // URL that doesn't look Google-related, but matches a Google base URL
  311. // specified on the command line.
  312. const std::string foo_url("http://www.foo.com/search?q={searchTerms}");
  313. EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url));
  314. base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
  315. switches::kGoogleBaseURL, "http://www.foo.com/");
  316. EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url));
  317. }
  318. TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeForAllPrepopulatedEngines) {
  319. using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
  320. const std::vector<const PrepopulatedEngine*> all_engines =
  321. TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
  322. for (const PrepopulatedEngine* engine : all_engines) {
  323. std::unique_ptr<TemplateURLData> data =
  324. TemplateURLDataFromPrepopulatedEngine(*engine);
  325. EXPECT_EQ(engine->type,
  326. TemplateURL(*data).GetEngineType(SearchTermsData()));
  327. }
  328. }
  329. TEST_F(TemplateURLPrepopulateDataTest, CheckSearchURLDetection) {
  330. using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
  331. const std::vector<const PrepopulatedEngine*> all_engines =
  332. TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
  333. for (const PrepopulatedEngine* engine : all_engines) {
  334. std::unique_ptr<TemplateURLData> data =
  335. TemplateURLDataFromPrepopulatedEngine(*engine);
  336. TemplateURL t_url(*data);
  337. SearchTermsData search_data;
  338. // Test that search term is successfully extracted from generated search
  339. // url.
  340. GURL search_url = t_url.GenerateSearchURL(search_data);
  341. EXPECT_TRUE(t_url.IsSearchURL(search_url, search_data))
  342. << "Search url is incorrectly detected for " << search_url;
  343. }
  344. }
  345. namespace {
  346. void CheckTemplateUrlRefIsCryptographic(const TemplateURLRef& url_ref) {
  347. TestingSearchTermsData search_terms_data("https://www.google.com/");
  348. if (!url_ref.IsValid(search_terms_data)) {
  349. ADD_FAILURE() << url_ref.GetURL();
  350. return;
  351. }
  352. // Double parentheses around the string16 constructor to prevent the compiler
  353. // from parsing it as a function declaration.
  354. TemplateURLRef::SearchTermsArgs search_term_args((std::u16string()));
  355. GURL url(url_ref.ReplaceSearchTerms(search_term_args, search_terms_data));
  356. EXPECT_TRUE(url.is_empty() || url.SchemeIsCryptographic()) << url;
  357. }
  358. } // namespace
  359. TEST_F(TemplateURLPrepopulateDataTest, HttpsUrls) {
  360. // Search engines that don't use HTTPS URLs.
  361. // Since Chrome and the Internet are trying to transition from HTTP to HTTPS,
  362. // please get approval from a PM before entering new HTTP exceptions here.
  363. std::set<int> exceptions{
  364. 4, 6, 16, 17, 21, 27, 35, 36, 43, 44, 45, 50, 54, 55, 56, 60, 61,
  365. 62, 63, 64, 65, 66, 68, 70, 74, 75, 76, 77, 78, 79, 80, 81, 85, 90,
  366. };
  367. using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
  368. const std::vector<const PrepopulatedEngine*> all_engines =
  369. TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
  370. for (const PrepopulatedEngine* engine : all_engines) {
  371. std::unique_ptr<TemplateURLData> data =
  372. TemplateURLDataFromPrepopulatedEngine(*engine);
  373. if (base::Contains(exceptions, data->prepopulate_id))
  374. continue;
  375. GURL logo_url = data->logo_url;
  376. EXPECT_TRUE(logo_url.is_empty() || logo_url.SchemeIsCryptographic())
  377. << logo_url;
  378. GURL doodle_url = data->doodle_url;
  379. EXPECT_TRUE(doodle_url.is_empty() || doodle_url.SchemeIsCryptographic())
  380. << doodle_url;
  381. EXPECT_TRUE(logo_url.is_empty() || doodle_url.is_empty())
  382. << "Only one of logo_url or doodle_url should be set.";
  383. GURL favicon_url = data->favicon_url;
  384. EXPECT_TRUE(favicon_url.is_empty() || favicon_url.SchemeIsCryptographic())
  385. << favicon_url;
  386. TemplateURL template_url(*data);
  387. // Intentionally don't check alternate URLs, because those are only used
  388. // for matching.
  389. CheckTemplateUrlRefIsCryptographic(template_url.url_ref());
  390. CheckTemplateUrlRefIsCryptographic(template_url.suggestions_url_ref());
  391. CheckTemplateUrlRefIsCryptographic(template_url.image_url_ref());
  392. CheckTemplateUrlRefIsCryptographic(template_url.new_tab_url_ref());
  393. CheckTemplateUrlRefIsCryptographic(
  394. template_url.contextual_search_url_ref());
  395. }
  396. }
  397. TEST_F(TemplateURLPrepopulateDataTest, FindGoogleIndex) {
  398. constexpr int kGoogleId = 1;
  399. size_t index;
  400. std::vector<std::unique_ptr<TemplateURLData>> urls;
  401. // Google is first in US, so confirm index 0.
  402. prefs_.SetInteger(country_codes::kCountryIDAtInstall, 'U' << 8 | 'S');
  403. urls = TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, &index);
  404. EXPECT_EQ(index, size_t{0});
  405. EXPECT_EQ(urls[index]->prepopulate_id, kGoogleId);
  406. // Google is not first in CN; confirm it is found at index > 0.
  407. // If Google ever does reach top in China, this test will need to be adjusted:
  408. // check template_url_prepopulate_data.cc reference orders (engines_CN, etc.)
  409. // to find a suitable country and index.
  410. prefs_.SetInteger(country_codes::kCountryIDAtInstall, 'C' << 8 | 'N');
  411. urls = TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, &index);
  412. EXPECT_GT(index, size_t{0});
  413. EXPECT_LT(index, urls.size());
  414. EXPECT_EQ(urls[index]->prepopulate_id, kGoogleId);
  415. }