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

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: Move calculation to TemplateURLData 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/scoped_vector.h" 10 #include "base/memory/scoped_vector.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/google/core/browser/google_switches.h" 12 #include "components/google/core/browser/google_switches.h"
13 #include "components/pref_registry/testing_pref_service_syncable.h" 13 #include "components/pref_registry/testing_pref_service_syncable.h"
14 #include "components/search_engines/prepopulated_engines.h" 14 #include "components/search_engines/prepopulated_engines.h"
15 #include "components/search_engines/search_engines_pref_names.h" 15 #include "components/search_engines/search_engines_pref_names.h"
16 #include "components/search_engines/search_terms_data.h" 16 #include "components/search_engines/search_terms_data.h"
17 #include "components/search_engines/template_url.h" 17 #include "components/search_engines/template_url.h"
18 #include "components/search_engines/template_url_prepopulate_data.h" 18 #include "components/search_engines/template_url_prepopulate_data.h"
19 #include "components/search_engines/template_url_service.h" 19 #include "components/search_engines/template_url_service.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using base::ASCIIToUTF16; 22 using base::ASCIIToUTF16;
23 23
24 namespace { 24 namespace {
25 25
26 SearchEngineType GetEngineType(const std::string& url) { 26 SearchEngineType GetEngineType(const std::string& url) {
27 TemplateURLData data; 27 TemplateURLData data;
28 data.SetURL(url); 28 data.SetURL(url);
29 return TemplateURLPrepopulateData::GetEngineType(TemplateURL(data), 29 return TemplateURL(data).GetEngineType(SearchTermsData());
30 SearchTermsData());
31 } 30 }
32 31
33 std::string GetHostFromTemplateURLData(const TemplateURLData& data) { 32 std::string GetHostFromTemplateURLData(const TemplateURLData& data) {
34 return TemplateURL(data).url_ref().GetHost(SearchTermsData()); 33 return TemplateURL(data).url_ref().GetHost(SearchTermsData());
35 } 34 }
36 35
37 } // namespace 36 } // namespace
38 37
39 class TemplateURLPrepopulateDataTest : public testing::Test { 38 class TemplateURLPrepopulateDataTest : public testing::Test {
40 public: 39 public:
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 240 }
242 // Ensures the default URL is Google and has the optional fields filled. 241 // Ensures the default URL is Google and has the optional fields filled.
243 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name()); 242 EXPECT_EQ(ASCIIToUTF16("Google"), t_urls[default_index]->short_name());
244 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty()); 243 EXPECT_FALSE(t_urls[default_index]->suggestions_url.empty());
245 EXPECT_FALSE(t_urls[default_index]->instant_url.empty()); 244 EXPECT_FALSE(t_urls[default_index]->instant_url.empty());
246 EXPECT_FALSE(t_urls[default_index]->image_url.empty()); 245 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
247 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty()); 246 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
248 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty()); 247 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
249 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty()); 248 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
250 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, 249 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
251 TemplateURLPrepopulateData::GetEngineType( 250 TemplateURL(*t_urls[default_index]).GetEngineType(
252 TemplateURL(*t_urls[default_index]),
253 SearchTermsData())); 251 SearchTermsData()));
254 } 252 }
255 253
256 // Verifies that built-in search providers are processed correctly. 254 // Verifies that built-in search providers are processed correctly.
257 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) { 255 TEST_F(TemplateURLPrepopulateDataTest, ProvidersFromPrepopulated) {
258 // Use United States. 256 // Use United States.
259 prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S'); 257 prefs_.SetInteger(prefs::kCountryIDAtInstall, 'U'<<8|'S');
260 size_t default_index; 258 size_t default_index;
261 ScopedVector<TemplateURLData> t_urls = 259 ScopedVector<TemplateURLData> t_urls =
262 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_, 260 TemplateURLPrepopulateData::GetPrepopulatedEngines(&prefs_,
(...skipping 17 matching lines...) Expand all
280 EXPECT_FALSE(t_urls[default_index]->image_url.empty()); 278 EXPECT_FALSE(t_urls[default_index]->image_url.empty());
281 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty()); 279 EXPECT_FALSE(t_urls[default_index]->new_tab_url.empty());
282 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty()); 280 EXPECT_FALSE(t_urls[default_index]->contextual_search_url.empty());
283 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty()); 281 EXPECT_FALSE(t_urls[default_index]->image_url_post_params.empty());
284 // Expect at least 2 alternate_urls. 282 // Expect at least 2 alternate_urls.
285 // This caught a bug with static initialization of arrays, so leave this in. 283 // This caught a bug with static initialization of arrays, so leave this in.
286 EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u); 284 EXPECT_GT(t_urls[default_index]->alternate_urls.size(), 1u);
287 for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i) 285 for (size_t i = 0; i < t_urls[default_index]->alternate_urls.size(); ++i)
288 EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty()); 286 EXPECT_FALSE(t_urls[default_index]->alternate_urls[i].empty());
289 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, 287 EXPECT_EQ(SEARCH_ENGINE_GOOGLE,
290 TemplateURLPrepopulateData::GetEngineType( 288 TemplateURL(*t_urls[default_index]).GetEngineType(
291 TemplateURL(*t_urls[default_index]),
292 SearchTermsData())); 289 SearchTermsData()));
293 EXPECT_FALSE(t_urls[default_index]->search_terms_replacement_key.empty()); 290 EXPECT_FALSE(t_urls[default_index]->search_terms_replacement_key.empty());
294 } 291 }
295 292
296 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) { 293 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeBasic) {
297 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/")); 294 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://example.com/"));
298 EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/")); 295 EXPECT_EQ(SEARCH_ENGINE_ASK, GetEngineType("http://www.ask.com/"));
299 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/")); 296 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("http://search.atlas.cz/"));
300 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/")); 297 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType("http://www.google.com/"));
301 } 298 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url")); 340 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType("invalid:search:url"));
344 341
345 // URL that doesn't look Google-related, but matches a Google base URL 342 // URL that doesn't look Google-related, but matches a Google base URL
346 // specified on the command line. 343 // specified on the command line.
347 const std::string foo_url("http://www.foo.com/search?q={searchTerms}"); 344 const std::string foo_url("http://www.foo.com/search?q={searchTerms}");
348 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url)); 345 EXPECT_EQ(SEARCH_ENGINE_OTHER, GetEngineType(foo_url));
349 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( 346 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
350 switches::kGoogleBaseURL, "http://www.foo.com/"); 347 switches::kGoogleBaseURL, "http://www.foo.com/");
351 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url)); 348 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, GetEngineType(foo_url));
352 } 349 }
350
351 TEST_F(TemplateURLPrepopulateDataTest, GetEngineTypeForAllPrepopulatedEngines) {
352 using PrepopulatedEngine = TemplateURLPrepopulateData::PrepopulatedEngine;
353 const std::vector<const PrepopulatedEngine*> all_engines =
354 TemplateURLPrepopulateData::GetAllKnownPrepopulatedEngines();
355 for (const PrepopulatedEngine* engine : all_engines) {
356 std::unique_ptr<TemplateURLData> template_url_data =
357 TemplateURLPrepopulateData::MakeTemplateURLDataFromPrepopulatedEngine(
358 *engine);
359 EXPECT_EQ(engine->type,
360 template_url_data->GetEngineType(SearchTermsData()));
361 }
362 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698