Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(762)

Side by Side Diff: components/search_engines/template_url_prepopulate_data_unittest.cc

Issue 1983773002: Cache SearchEngineType of TemplateURL (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-extracting-terms-from-template-url
Patch Set: Fix typo Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/search_engines/template_url_prepopulate_data.h" 5 #include "components/search_engines/template_url_prepopulate_data.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 12 matching lines...) Expand all
23 #include "components/search_engines/template_url_service.h" 23 #include "components/search_engines/template_url_service.h"
24 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 25
26 using base::ASCIIToUTF16; 26 using base::ASCIIToUTF16;
27 27
28 namespace { 28 namespace {
29 29
30 SearchEngineType GetEngineType(const std::string& url) { 30 SearchEngineType GetEngineType(const std::string& url) {
31 TemplateURLData data; 31 TemplateURLData data;
32 data.SetURL(url); 32 data.SetURL(url);
33 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(data), 33 return TemplateURL(data).GetEngineType(SearchTermsData());
34 SearchTermsData());
35 } 34 }
36 35
37 std::string GetHostFromTemplateURLData(const TemplateURLData& data) { 36 std::string GetHostFromTemplateURLData(const TemplateURLData& data) {
38 return TemplateURL(data).url_ref().GetHost(SearchTermsData()); 37 return TemplateURL(data).url_ref().GetHost(SearchTermsData());
39 } 38 }
40 39
41 } // namespace 40 } // namespace
42 41
43 class TemplateURLPrepopulateDataTest : public testing::Test { 42 class TemplateURLPrepopulateDataTest : public testing::Test {
44 public: 43 public:
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 244 }
246 // Ensures the default URL is Google and has the optional fields filled. 245 // Ensures the default URL is Google and has the optional fields filled.
247 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name()); 246 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name());
248 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty()); 247 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
249 EXPECT_FALSE(t_urls[default_index]->instant_url.empty()); 248 EXPECT_FALSE(t_urls[default_index]->instant_url.empty());
250 EXPECT_FALSE(t_urls[default_index]->image_url.empty()); 249 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
251 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty()); 250 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
252 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty()); 251 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
253 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty()); 252 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
254 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, 253 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
255 TemplateURLPrepopulateData::GetEngineType( 254 TemplateURL(*t_urls[default_index]).GetEngineType(
256 TemplateURL(*t_urls[default_index]),
257 SearchTermsData())); 255 SearchTermsData()));
258 } 256 }
259 257
260 // Verifies that built-in search providers are processed correctly. 258 // Verifies that built-in search providers are processed correctly.
261 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { 259 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) {
262 // Use United States. 260 // Use United States.
263 prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S'); 261 prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S');
264 size_t default_index; 262 size_t default_index;
265 ScopedVector<TemplateURLData> t_urls = 263 ScopedVector<TemplateURLData> t_urls =
266 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, 264 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
(...skipping 17 matching lines...) Expand all
284 EXPECT_FALSE(t_urls[default_index]->image_url.empty()); 282 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
285 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty()); 283 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
286 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty()); 284 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
287 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty()); 285 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
288 // Expect at least 2 alternate_urls. 286 // Expect at least 2 alternate_urls.
289 // This caught a bug with static initialization of arrays, so leave this in. 287 // This caught a bug with static initialization of arrays, so leave this in.
290 EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u); 288 EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u);
291 for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i) 289 for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i)
292 EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty()); 290 EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty());
293 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, 291 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
294 TemplateURLPrepopulateData::GetEngineType( 292 TemplateURL(*t_urls[default_index]).GetEngineType(
295 TemplateURL(*t_urls[default_index]),
296 SearchTermsData())); 293 SearchTermsData()));
297 EXPECT_FALSE(t_urls[default_index]->search_terms_replacement_key.empty()); 294 EXPECT_FALSE(t_urls[default_index]->search_terms_replacement_key.empty());
298 } 295 }
299 296
300 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) { 297 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) {
301 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/")); 298 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/"));
302 EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/")); 299 EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/"));
303 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/")); 300 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/"));
304 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/")); 301 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/"));
305 } 302 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url")); 344 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url"));
348 345
349 // URL that doesn't look Google-related, but matches a Google base URL 346 // URL that doesn't look Google-related, but matches a Google base URL
350 // specified on the command line. 347 // specified on the command line.
351 const std::string foo_url("http://www.foo.com/search?q={searchTerms}"); 348 const std::string foo_url("http://www.foo.com/search?q={searchTerms}");
352 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url)); 349 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url));
353 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 350 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
354 switches::kGoogleBaseURL, "http://www.foo.com/"); 351 switches::kGoogleBaseURL, "http://www.foo.com/");
355 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url)); 352 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url));
356 } 353 }
354
355 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeForAllPrepopulatedEngines) {
356 using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
357 const std::vector<const PrepopulatedEngine*> all_engines =
358 TemplateURLPrepopulateData::GetAllPrepopulatedEngines();
359 for (const PrepopulatedEngine* engine : all_engines) {
360 std::unique_ptr<TemplateURLData> data =
361 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine(
362 *engine);
363 EXPECT_EQ(engine->type,
364 TemplateURL(*data).GetEngineType(SearchTermsData()));
365 }
366 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698