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

Side by Side Diff: chrome/browser/autocomplete/search_provider_unittest.cc

Issue 17022004: Replace --google-base-suggest-url and --instant-url with --google-base-url. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 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 "chrome/browser/autocomplete/search_provider.h" 5 #include "chrome/browser/autocomplete/search_provider.h"
6 6
7 #include "base/command_line.h"
7 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
8 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
9 #include "base/run_loop.h" 10 #include "base/run_loop.h"
10 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "base/time.h" 13 #include "base/time.h"
13 #include "build/build_config.h" 14 #include "build/build_config.h"
14 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" 15 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
15 #include "chrome/browser/autocomplete/autocomplete_controller.h" 16 #include "chrome/browser/autocomplete/autocomplete_controller.h"
16 #include "chrome/browser/autocomplete/autocomplete_input.h" 17 #include "chrome/browser/autocomplete/autocomplete_input.h"
17 #include "chrome/browser/autocomplete/autocomplete_match.h" 18 #include "chrome/browser/autocomplete/autocomplete_match.h"
18 #include "chrome/browser/autocomplete/autocomplete_provider.h" 19 #include "chrome/browser/autocomplete/autocomplete_provider.h"
19 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" 20 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h"
20 #include "chrome/browser/autocomplete/history_url_provider.h" 21 #include "chrome/browser/autocomplete/history_url_provider.h"
21 #include "chrome/browser/history/history_service.h" 22 #include "chrome/browser/history/history_service.h"
22 #include "chrome/browser/history/history_service_factory.h" 23 #include "chrome/browser/history/history_service_factory.h"
23 #include "chrome/browser/omnibox/omnibox_field_trial.h" 24 #include "chrome/browser/omnibox/omnibox_field_trial.h"
24 #include "chrome/browser/search/search.h" 25 #include "chrome/browser/search/search.h"
25 #include "chrome/browser/search_engines/template_url.h" 26 #include "chrome/browser/search_engines/template_url.h"
26 #include "chrome/browser/search_engines/template_url_service.h" 27 #include "chrome/browser/search_engines/template_url_service.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h" 28 #include "chrome/browser/search_engines/template_url_service_factory.h"
29 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/instant_types.h" 30 #include "chrome/common/instant_types.h"
29 #include "chrome/common/metrics/entropy_provider.h" 31 #include "chrome/common/metrics/entropy_provider.h"
30 #include "chrome/common/pref_names.h" 32 #include "chrome/common/pref_names.h"
31 #include "chrome/test/base/testing_browser_process.h" 33 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h" 34 #include "chrome/test/base/testing_profile.h"
33 #include "content/public/test/test_browser_thread.h" 35 #include "content/public/test/test_browser_thread.h"
34 #include "net/url_request/test_url_fetcher_factory.h" 36 #include "net/url_request/test_url_fetcher_factory.h"
35 #include "net/url_request/url_request_status.h" 37 #include "net/url_request/url_request_status.h"
36 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
37 39
38 using content::BrowserThread; 40
41 // SearchProviderTest ---------------------------------------------------------
39 42
40 // The following environment is configured for these tests: 43 // The following environment is configured for these tests:
41 // . The TemplateURL default_t_url_ is set as the default provider. 44 // . The TemplateURL default_t_url_ is set as the default provider.
42 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This 45 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This
43 // TemplateURL has a valid suggest and search URL. 46 // TemplateURL has a valid suggest and search URL.
44 // . The URL created by using the search term term1_ with default_t_url_ is 47 // . The URL created by using the search term term1_ with default_t_url_ is
45 // added to history. 48 // added to history.
46 // . The URL created by using the search term keyword_term_ with keyword_t_url_ 49 // . The URL created by using the search term keyword_term_ with keyword_t_url_
47 // is added to history. 50 // is added to history.
48 // . test_factory_ is set as the URLFetcherFactory. 51 // . test_factory_ is set as the URLFetcherFactory.
49 class SearchProviderTest : public testing::Test, 52 class SearchProviderTest : public testing::Test,
50 public AutocompleteProviderListener { 53 public AutocompleteProviderListener {
51 public: 54 public:
52 SearchProviderTest()
53 : default_t_url_(NULL),
54 term1_(ASCIIToUTF16("term1")),
55 keyword_t_url_(NULL),
56 keyword_term_(ASCIIToUTF16("keyword")),
57 ui_thread_(BrowserThread::UI, &message_loop_),
58 io_thread_(BrowserThread::IO),
59 quit_when_done_(false) {
60 io_thread_.Start();
61 }
62
63 static void SetUpTestCase();
64
65 static void TearDownTestCase();
66
67 // See description above class for what this registers.
68 virtual void SetUp();
69
70 virtual void TearDown();
71
72 struct ResultInfo { 55 struct ResultInfo {
73 ResultInfo() : result_type(AutocompleteMatchType::NUM_TYPES) { 56 ResultInfo() : result_type(AutocompleteMatchType::NUM_TYPES) {
74 } 57 }
75 ResultInfo(GURL gurl, 58 ResultInfo(GURL gurl,
76 AutocompleteMatch::Type result_type, 59 AutocompleteMatch::Type result_type,
77 string16 fill_into_edit) 60 string16 fill_into_edit)
78 : gurl(gurl), 61 : gurl(gurl),
79 result_type(result_type), 62 result_type(result_type),
80 fill_into_edit(fill_into_edit) { 63 fill_into_edit(fill_into_edit) {
81 } 64 }
65
82 const GURL gurl; 66 const GURL gurl;
83 const AutocompleteMatch::Type result_type; 67 const AutocompleteMatch::Type result_type;
84 const string16 fill_into_edit; 68 const string16 fill_into_edit;
85 }; 69 };
70
86 struct TestData { 71 struct TestData {
87 const string16 input; 72 const string16 input;
88 const size_t num_results; 73 const size_t num_results;
89 const ResultInfo output[3]; 74 const ResultInfo output[3];
90 }; 75 };
91 76
77 SearchProviderTest()
78 : default_t_url_(NULL),
79 term1_(ASCIIToUTF16("term1")),
80 keyword_t_url_(NULL),
81 keyword_term_(ASCIIToUTF16("keyword")),
82 ui_thread_(content::BrowserThread::UI, &message_loop_),
83 io_thread_(content::BrowserThread::IO),
84 quit_when_done_(false) {
85 io_thread_.Start();
86 }
87
88 static void SetUpTestCase();
89 static void TearDownTestCase();
90
91 // See description above class for what this registers.
92 virtual void SetUp() OVERRIDE;
93 virtual void TearDown() OVERRIDE;
94
92 void RunTest(TestData* cases, int num_cases, bool prefer_keyword); 95 void RunTest(TestData* cases, int num_cases, bool prefer_keyword);
93 96
94 protected: 97 protected:
95 // Needed for AutocompleteFieldTrial::ActivateStaticTrials(); 98 // Needed for AutocompleteFieldTrial::ActivateStaticTrials();
96 static base::FieldTrialList* field_trial_list_; 99 static base::FieldTrialList* field_trial_list_;
97 100
98 // Default value used for testing. 101 // Default value used for testing.
99 static const std::string kNotApplicable; 102 static const std::string kNotApplicable;
100 103
101 // Adds a search for |term|, using the engine |t_url| to the history, and 104 // Adds a search for |term|, using the engine |t_url| to the history, and
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 scoped_refptr<SearchProvider> provider_; 162 scoped_refptr<SearchProvider> provider_;
160 163
161 // If true, OnProviderUpdate exits out of the current message loop. 164 // If true, OnProviderUpdate exits out of the current message loop.
162 bool quit_when_done_; 165 bool quit_when_done_;
163 166
164 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); 167 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest);
165 }; 168 };
166 169
167 // static 170 // static
168 base::FieldTrialList* SearchProviderTest::field_trial_list_ = NULL; 171 base::FieldTrialList* SearchProviderTest::field_trial_list_ = NULL;
169
170 // static
171 const std::string SearchProviderTest::kNotApplicable = "Not Applicable"; 172 const std::string SearchProviderTest::kNotApplicable = "Not Applicable";
172 173
173 // static 174 // static
174 void SearchProviderTest::SetUpTestCase() { 175 void SearchProviderTest::SetUpTestCase() {
175 // Set up Suggest experiments. 176 // Set up Suggest experiments.
176 field_trial_list_ = new base::FieldTrialList( 177 field_trial_list_ = new base::FieldTrialList(
177 new metrics::SHA1EntropyProvider("foo")); 178 new metrics::SHA1EntropyProvider("foo"));
178 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( 179 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
179 "AutocompleteDynamicTrial_0", "DefaultGroup"); 180 "AutocompleteDynamicTrial_0", "DefaultGroup");
180 trial->group(); 181 trial->group();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 231
231 // Keywords are updated by the InMemoryHistoryBackend only after the message 232 // Keywords are updated by the InMemoryHistoryBackend only after the message
232 // has been processed on the history thread. Block until history processes all 233 // has been processed on the history thread. Block until history processes all
233 // requests to ensure the InMemoryDatabase is the state we expect it. 234 // requests to ensure the InMemoryDatabase is the state we expect it.
234 profile_.BlockUntilHistoryProcessesPendingRequests(); 235 profile_.BlockUntilHistoryProcessesPendingRequests();
235 236
236 provider_ = new SearchProvider(this, &profile_); 237 provider_ = new SearchProvider(this, &profile_);
237 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; 238 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0;
238 } 239 }
239 240
241 void SearchProviderTest::TearDown() {
242 message_loop_.RunUntilIdle();
243
244 // Shutdown the provider before the profile.
245 provider_ = NULL;
246 }
247
248 void SearchProviderTest::RunTest(TestData* cases,
249 int num_cases,
250 bool prefer_keyword) {
251 ACMatches matches;
252 for (int i = 0; i < num_cases; ++i) {
253 AutocompleteInput input(cases[i].input, string16::npos, string16(), GURL(),
254 false, prefer_keyword, true,
255 AutocompleteInput::ALL_MATCHES);
256 provider_->Start(input, false);
257 matches = provider_->matches();
258 string16 diagnostic_details = ASCIIToUTF16("Input was: ") + cases[i].input +
259 ASCIIToUTF16("; prefer_keyword was: ") +
260 (prefer_keyword ? ASCIIToUTF16("true") : ASCIIToUTF16("false"));
261 EXPECT_EQ(cases[i].num_results, matches.size()) << diagnostic_details;
262 if (matches.size() == cases[i].num_results) {
263 for (size_t j = 0; j < cases[i].num_results; ++j) {
264 EXPECT_EQ(cases[i].output[j].gurl, matches[j].destination_url) <<
265 diagnostic_details;
266 EXPECT_EQ(cases[i].output[j].result_type, matches[j].type) <<
267 diagnostic_details;
268 EXPECT_EQ(cases[i].output[j].fill_into_edit,
269 matches[j].fill_into_edit) <<
270 diagnostic_details;
271 }
272 }
273 }
274 }
275
240 void SearchProviderTest::OnProviderUpdate(bool updated_matches) { 276 void SearchProviderTest::OnProviderUpdate(bool updated_matches) {
241 if (quit_when_done_ && provider_->done()) { 277 if (quit_when_done_ && provider_->done()) {
242 quit_when_done_ = false; 278 quit_when_done_ = false;
243 message_loop_.Quit(); 279 message_loop_.Quit();
244 } 280 }
245 } 281 }
246 282
247 net::TestURLFetcher* SearchProviderTest::WaitUntilURLFetcherIsReady( 283 net::TestURLFetcher* SearchProviderTest::WaitUntilURLFetcherIsReady(
248 int fetcher_id) { 284 int fetcher_id) {
249 net::TestURLFetcher* url_fetcher = test_factory_.GetFetcherByID(fetcher_id); 285 net::TestURLFetcher* url_fetcher = test_factory_.GetFetcherByID(fetcher_id);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 EXPECT_NE(chrome::IsInstantExtendedAPIEnabled(), provider_->done()); 325 EXPECT_NE(chrome::IsInstantExtendedAPIEnabled(), provider_->done());
290 if (!wyt_match) 326 if (!wyt_match)
291 return; 327 return;
292 ASSERT_GE(provider_->matches().size(), 1u); 328 ASSERT_GE(provider_->matches().size(), 1u);
293 EXPECT_TRUE(FindMatchWithDestination(GURL( 329 EXPECT_TRUE(FindMatchWithDestination(GURL(
294 default_t_url_->url_ref().ReplaceSearchTerms( 330 default_t_url_->url_ref().ReplaceSearchTerms(
295 TemplateURLRef::SearchTermsArgs(text))), 331 TemplateURLRef::SearchTermsArgs(text))),
296 wyt_match)); 332 wyt_match));
297 } 333 }
298 334
299 void SearchProviderTest::TearDown() {
300 message_loop_.RunUntilIdle();
301
302 // Shutdown the provider before the profile.
303 provider_ = NULL;
304 }
305
306 void SearchProviderTest::RunTest(TestData* cases,
307 int num_cases,
308 bool prefer_keyword) {
309 ACMatches matches;
310 for (int i = 0; i < num_cases; ++i) {
311 AutocompleteInput input(cases[i].input, string16::npos, string16(), GURL(),
312 false, prefer_keyword, true,
313 AutocompleteInput::ALL_MATCHES);
314 provider_->Start(input, false);
315 matches = provider_->matches();
316 string16 diagnostic_details = ASCIIToUTF16("Input was: ") + cases[i].input +
317 ASCIIToUTF16("; prefer_keyword was: ") +
318 (prefer_keyword ? ASCIIToUTF16("true") : ASCIIToUTF16("false"));
319 EXPECT_EQ(cases[i].num_results, matches.size()) << diagnostic_details;
320 if (matches.size() == cases[i].num_results) {
321 for (size_t j = 0; j < cases[i].num_results; ++j) {
322 EXPECT_EQ(cases[i].output[j].gurl, matches[j].destination_url) <<
323 diagnostic_details;
324 EXPECT_EQ(cases[i].output[j].result_type, matches[j].type) <<
325 diagnostic_details;
326 EXPECT_EQ(cases[i].output[j].fill_into_edit,
327 matches[j].fill_into_edit) <<
328 diagnostic_details;
329 }
330 }
331 }
332 }
333
334 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, 335 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url,
335 string16 term, 336 string16 term,
336 int visit_count) { 337 int visit_count) {
337 HistoryService* history = 338 HistoryService* history =
338 HistoryServiceFactory::GetForProfile(&profile_, 339 HistoryServiceFactory::GetForProfile(&profile_,
339 Profile::EXPLICIT_ACCESS); 340 Profile::EXPLICIT_ACCESS);
340 GURL search(t_url->url_ref().ReplaceSearchTerms( 341 GURL search(t_url->url_ref().ReplaceSearchTerms(
341 TemplateURLRef::SearchTermsArgs(term))); 342 TemplateURLRef::SearchTermsArgs(term)));
342 static base::Time last_added_time; 343 static base::Time last_added_time;
343 last_added_time = std::max(base::Time::Now(), 344 last_added_time = std::max(base::Time::Now(),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 void SearchProviderTest::FinishDefaultSuggestQuery() { 376 void SearchProviderTest::FinishDefaultSuggestQuery() {
376 net::TestURLFetcher* default_fetcher = WaitUntilURLFetcherIsReady( 377 net::TestURLFetcher* default_fetcher = WaitUntilURLFetcherIsReady(
377 SearchProvider::kDefaultProviderURLFetcherID); 378 SearchProvider::kDefaultProviderURLFetcherID);
378 ASSERT_TRUE(default_fetcher); 379 ASSERT_TRUE(default_fetcher);
379 380
380 // Tell the SearchProvider the default suggest query is done. 381 // Tell the SearchProvider the default suggest query is done.
381 default_fetcher->set_response_code(200); 382 default_fetcher->set_response_code(200);
382 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); 383 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher);
383 } 384 }
384 385
385 // Tests ----------------------------------------------------------------------- 386
387 // Actual Tests ---------------------------------------------------------------
386 388
387 // Make sure we query history for the default provider and a URLFetcher is 389 // Make sure we query history for the default provider and a URLFetcher is
388 // created for the default provider suggest results. 390 // created for the default provider suggest results.
389 TEST_F(SearchProviderTest, QueryDefaultProvider) { 391 TEST_F(SearchProviderTest, QueryDefaultProvider) {
390 string16 term = term1_.substr(0, term1_.length() - 1); 392 string16 term = term1_.substr(0, term1_.length() - 1);
391 QueryForInput(term, false, false); 393 QueryForInput(term, false, false);
392 394
393 // Make sure the default providers suggest service was queried. 395 // Make sure the default providers suggest service was queried.
394 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( 396 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(
395 SearchProvider::kDefaultProviderURLFetcherID); 397 SearchProvider::kDefaultProviderURLFetcherID);
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc. 975 // chrome/browser/extensions/api/omnibox/omnibox_apitest.cc.
974 }; 976 };
975 977
976 // Test not in keyword mode. 978 // Test not in keyword mode.
977 RunTest(cases, arraysize(cases), false); 979 RunTest(cases, arraysize(cases), false);
978 980
979 // Test in keyword mode. (Both modes should give the same result.) 981 // Test in keyword mode. (Both modes should give the same result.)
980 RunTest(cases, arraysize(cases), true); 982 RunTest(cases, arraysize(cases), true);
981 } 983 }
982 984
985 // Ensures command-line flags are reflected in the URLs the search provider
986 // generates.
987 TEST_F(SearchProviderTest, CommandLineOverrides) {
988 TemplateURLService* turl_model =
989 TemplateURLServiceFactory::GetForProfile(&profile_);
990
991 TemplateURLData data;
992 data.short_name = ASCIIToUTF16("default");
993 data.SetKeyword(data.short_name);
994 data.SetURL("{google:baseURL}{searchTerms}");
995 default_t_url_ = new TemplateURL(&profile_, data);
996 turl_model->Add(default_t_url_);
997 turl_model->SetDefaultSearchProvider(default_t_url_);
998
999 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL,
1000 "http://www.bar.com/");
1001 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
1002 switches::kExtraSearchQueryParams, "a=b");
1003
1004 TestData cases[] = {
1005 { ASCIIToUTF16("k a"), 2,
1006 { ResultInfo(GURL("http://keyword/a"),
1007 AutocompleteMatchType::SEARCH_OTHER_ENGINE,
1008 ASCIIToUTF16("k a")),
1009 ResultInfo(GURL("http://www.bar.com/k%20a?a=b"),
1010 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
1011 ASCIIToUTF16("k a")) } },
1012 };
1013
1014 RunTest(cases, arraysize(cases), false);
1015 }
1016
983 // Verifies Navsuggest results don't set a TemplateURL, which Instant relies on. 1017 // Verifies Navsuggest results don't set a TemplateURL, which Instant relies on.
984 // Also verifies that just the *first* navigational result is listed as a match 1018 // Also verifies that just the *first* navigational result is listed as a match
985 // if suggested relevance scores were not sent. 1019 // if suggested relevance scores were not sent.
986 TEST_F(SearchProviderTest, NavSuggestNoSuggestedRelevanceScores) { 1020 TEST_F(SearchProviderTest, NavSuggestNoSuggestedRelevanceScores) {
987 QueryForInput(ASCIIToUTF16("a.c"), false, false); 1021 QueryForInput(ASCIIToUTF16("a.c"), false, false);
988 1022
989 // Make sure the default providers suggest service was queried. 1023 // Make sure the default providers suggest service was queried.
990 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( 1024 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(
991 SearchProvider::kDefaultProviderURLFetcherID); 1025 SearchProvider::kDefaultProviderURLFetcherID);
992 ASSERT_TRUE(fetcher); 1026 ASSERT_TRUE(fetcher);
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after
2271 } else { 2305 } else {
2272 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion; 2306 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion;
2273 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion()); 2307 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion());
2274 ++sug_it; 2308 ++sug_it;
2275 } 2309 }
2276 } 2310 }
2277 EXPECT_EQ(sug_end, sug_it); 2311 EXPECT_EQ(sug_end, sug_it);
2278 EXPECT_EQ(nav_end, nav_it); 2312 EXPECT_EQ(nav_end, nav_it);
2279 } 2313 }
2280 } 2314 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/search_provider.cc ('k') | chrome/browser/autocomplete/zero_suggest_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698