OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 AutocompleteMatch* wyt_match) { | 193 AutocompleteMatch* wyt_match) { |
194 QueryForInput(text, false); | 194 QueryForInput(text, false); |
195 profile_.BlockUntilHistoryProcessesPendingRequests(); | 195 profile_.BlockUntilHistoryProcessesPendingRequests(); |
196 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 196 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
197 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled), | 197 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled), |
198 provider_->done()); | 198 provider_->done()); |
199 if (!wyt_match) | 199 if (!wyt_match) |
200 return; | 200 return; |
201 ASSERT_GE(provider_->matches().size(), 1u); | 201 ASSERT_GE(provider_->matches().size(), 1u); |
202 EXPECT_TRUE(FindMatchWithDestination(GURL( | 202 EXPECT_TRUE(FindMatchWithDestination(GURL( |
203 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, text, 0, | 203 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, text, |
204 string16())), wyt_match)); | 204 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), wyt_match)); |
205 } | 205 } |
206 | 206 |
207 void SearchProviderTest::TearDown() { | 207 void SearchProviderTest::TearDown() { |
208 message_loop_.RunAllPending(); | 208 message_loop_.RunAllPending(); |
209 | 209 |
210 // Shutdown the provider before the profile. | 210 // Shutdown the provider before the profile. |
211 provider_ = NULL; | 211 provider_ = NULL; |
212 } | 212 } |
213 | 213 |
214 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, | 214 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, |
215 string16 term, | 215 string16 term, |
216 int visit_count) { | 216 int visit_count) { |
217 HistoryService* history = | 217 HistoryService* history = |
218 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 218 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
219 GURL search(t_url->url()->ReplaceSearchTerms(*t_url, term, 0, string16())); | 219 GURL search(t_url->url()->ReplaceSearchTerms(*t_url, term, |
| 220 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
220 static base::Time last_added_time; | 221 static base::Time last_added_time; |
221 last_added_time = std::max(base::Time::Now(), | 222 last_added_time = std::max(base::Time::Now(), |
222 last_added_time + base::TimeDelta::FromMicroseconds(1)); | 223 last_added_time + base::TimeDelta::FromMicroseconds(1)); |
223 history->AddPageWithDetails(search, string16(), visit_count, visit_count, | 224 history->AddPageWithDetails(search, string16(), visit_count, visit_count, |
224 last_added_time, false, history::SOURCE_BROWSED); | 225 last_added_time, false, history::SOURCE_BROWSED); |
225 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); | 226 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); |
226 return search; | 227 return search; |
227 } | 228 } |
228 | 229 |
229 bool SearchProviderTest::FindMatchWithDestination(const GURL& url, | 230 bool SearchProviderTest::FindMatchWithDestination(const GURL& url, |
(...skipping 25 matching lines...) Expand all Loading... |
255 TEST_F(SearchProviderTest, QueryDefaultProvider) { | 256 TEST_F(SearchProviderTest, QueryDefaultProvider) { |
256 string16 term = term1_.substr(0, term1_.length() - 1); | 257 string16 term = term1_.substr(0, term1_.length() - 1); |
257 QueryForInput(term, false); | 258 QueryForInput(term, false); |
258 | 259 |
259 // Make sure the default providers suggest service was queried. | 260 // Make sure the default providers suggest service was queried. |
260 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 261 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
261 SearchProvider::kDefaultProviderURLFetcherID); | 262 SearchProvider::kDefaultProviderURLFetcherID); |
262 ASSERT_TRUE(fetcher); | 263 ASSERT_TRUE(fetcher); |
263 | 264 |
264 // And the URL matches what we expected. | 265 // And the URL matches what we expected. |
265 GURL expected_url = GURL(default_t_url_->suggestions_url()-> | 266 GURL expected_url(default_t_url_->suggestions_url()->ReplaceSearchTerms( |
266 ReplaceSearchTerms(*default_t_url_, term, 0, string16())); | 267 *default_t_url_, term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 268 string16())); |
267 ASSERT_TRUE(fetcher->GetOriginalURL() == expected_url); | 269 ASSERT_TRUE(fetcher->GetOriginalURL() == expected_url); |
268 | 270 |
269 // Tell the SearchProvider the suggest query is done. | 271 // Tell the SearchProvider the suggest query is done. |
270 fetcher->set_response_code(200); | 272 fetcher->set_response_code(200); |
271 fetcher->delegate()->OnURLFetchComplete(fetcher); | 273 fetcher->delegate()->OnURLFetchComplete(fetcher); |
272 fetcher = NULL; | 274 fetcher = NULL; |
273 | 275 |
274 // Run till the history results complete. | 276 // Run till the history results complete. |
275 RunTillProviderDone(); | 277 RunTillProviderDone(); |
276 | 278 |
277 // The SearchProvider is done. Make sure it has a result for the history | 279 // The SearchProvider is done. Make sure it has a result for the history |
278 // term term1. | 280 // term term1. |
279 AutocompleteMatch term1_match; | 281 AutocompleteMatch term1_match; |
280 EXPECT_TRUE(FindMatchWithDestination(term1_url_, &term1_match)); | 282 EXPECT_TRUE(FindMatchWithDestination(term1_url_, &term1_match)); |
281 // Term1 should not have a description, it's set later. | 283 // Term1 should not have a description, it's set later. |
282 EXPECT_TRUE(term1_match.description.empty()); | 284 EXPECT_TRUE(term1_match.description.empty()); |
283 | 285 |
284 AutocompleteMatch wyt_match; | 286 AutocompleteMatch wyt_match; |
285 EXPECT_TRUE(FindMatchWithDestination(GURL( | 287 EXPECT_TRUE(FindMatchWithDestination( |
286 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, term, 0, | 288 GURL(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, term, |
287 string16())), &wyt_match)); | 289 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), &wyt_match)); |
288 EXPECT_TRUE(wyt_match.description.empty()); | 290 EXPECT_TRUE(wyt_match.description.empty()); |
289 | 291 |
290 // The match for term1 should be more relevant than the what you typed result. | 292 // The match for term1 should be more relevant than the what you typed result. |
291 EXPECT_GT(term1_match.relevance, wyt_match.relevance); | 293 EXPECT_GT(term1_match.relevance, wyt_match.relevance); |
292 } | 294 } |
293 | 295 |
294 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { | 296 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { |
295 string16 term = term1_.substr(0, term1_.length() - 1); | 297 string16 term = term1_.substr(0, term1_.length() - 1); |
296 QueryForInput(term, true); | 298 QueryForInput(term, true); |
297 | 299 |
(...skipping 17 matching lines...) Expand all Loading... |
315 default_fetcher->set_response_code(200); | 317 default_fetcher->set_response_code(200); |
316 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); | 318 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); |
317 default_fetcher = NULL; | 319 default_fetcher = NULL; |
318 | 320 |
319 // Make sure the keyword providers suggest service was queried. | 321 // Make sure the keyword providers suggest service was queried. |
320 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( | 322 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( |
321 SearchProvider::kKeywordProviderURLFetcherID); | 323 SearchProvider::kKeywordProviderURLFetcherID); |
322 ASSERT_TRUE(keyword_fetcher); | 324 ASSERT_TRUE(keyword_fetcher); |
323 | 325 |
324 // And the URL matches what we expected. | 326 // And the URL matches what we expected. |
325 GURL expected_url = GURL(keyword_t_url_->suggestions_url()-> | 327 GURL expected_url(keyword_t_url_->suggestions_url()->ReplaceSearchTerms( |
326 ReplaceSearchTerms(*keyword_t_url_, term, 0, string16())); | 328 *keyword_t_url_, term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 329 string16())); |
327 ASSERT_TRUE(keyword_fetcher->GetOriginalURL() == expected_url); | 330 ASSERT_TRUE(keyword_fetcher->GetOriginalURL() == expected_url); |
328 | 331 |
329 // Tell the SearchProvider the keyword suggest query is done. | 332 // Tell the SearchProvider the keyword suggest query is done. |
330 keyword_fetcher->set_response_code(200); | 333 keyword_fetcher->set_response_code(200); |
331 keyword_fetcher->delegate()->OnURLFetchComplete(keyword_fetcher); | 334 keyword_fetcher->delegate()->OnURLFetchComplete(keyword_fetcher); |
332 keyword_fetcher = NULL; | 335 keyword_fetcher = NULL; |
333 | 336 |
334 // Run till the history results complete. | 337 // Run till the history results complete. |
335 RunTillProviderDone(); | 338 RunTillProviderDone(); |
336 | 339 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 | 387 |
385 // Tell the provider instant is done. | 388 // Tell the provider instant is done. |
386 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 389 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
387 | 390 |
388 // The provider should now be done. | 391 // The provider should now be done. |
389 EXPECT_TRUE(provider_->done()); | 392 EXPECT_TRUE(provider_->done()); |
390 | 393 |
391 // There should be two matches, one for what you typed, the other for | 394 // There should be two matches, one for what you typed, the other for |
392 // 'foobar'. | 395 // 'foobar'. |
393 EXPECT_EQ(2u, provider_->matches().size()); | 396 EXPECT_EQ(2u, provider_->matches().size()); |
394 GURL instant_url = GURL(default_t_url_->url()->ReplaceSearchTerms( | 397 GURL instant_url(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, |
395 *default_t_url_, ASCIIToUTF16("foobar"), 0, string16())); | 398 ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 399 string16())); |
396 AutocompleteMatch instant_match; | 400 AutocompleteMatch instant_match; |
397 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | 401 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); |
398 | 402 |
399 // And the 'foobar' match should not have a description, it'll be set later. | 403 // And the 'foobar' match should not have a description, it'll be set later. |
400 EXPECT_TRUE(instant_match.description.empty()); | 404 EXPECT_TRUE(instant_match.description.empty()); |
401 | 405 |
402 // Make sure the what you typed match has no description. | 406 // Make sure the what you typed match has no description. |
403 AutocompleteMatch wyt_match; | 407 AutocompleteMatch wyt_match; |
404 EXPECT_TRUE(FindMatchWithDestination(GURL( | 408 EXPECT_TRUE(FindMatchWithDestination( |
405 default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, | 409 GURL(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, |
406 ASCIIToUTF16("foo"), 0, string16())), &wyt_match)); | 410 ASCIIToUTF16("foo"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 411 string16())), &wyt_match)); |
407 EXPECT_TRUE(wyt_match.description.empty()); | 412 EXPECT_TRUE(wyt_match.description.empty()); |
408 | 413 |
409 // The instant search should be more relevant. | 414 // The instant search should be more relevant. |
410 EXPECT_GT(instant_match.relevance, wyt_match.relevance); | 415 EXPECT_GT(instant_match.relevance, wyt_match.relevance); |
411 } | 416 } |
412 | 417 |
413 // Make sure that if FinalizeInstantQuery is invoked before suggest results | 418 // Make sure that if FinalizeInstantQuery is invoked before suggest results |
414 // return, the suggest text from FinalizeInstantQuery is remembered. | 419 // return, the suggest text from FinalizeInstantQuery is remembered. |
415 TEST_F(SearchProviderTest, RememberInstantQuery) { | 420 TEST_F(SearchProviderTest, RememberInstantQuery) { |
416 PrefService* service = profile_.GetPrefs(); | 421 PrefService* service = profile_.GetPrefs(); |
417 service->SetBoolean(prefs::kInstantEnabled, true); | 422 service->SetBoolean(prefs::kInstantEnabled, true); |
418 | 423 |
419 QueryForInput(ASCIIToUTF16("foo"), false); | 424 QueryForInput(ASCIIToUTF16("foo"), false); |
420 | 425 |
421 // Finalize the instant query immediately. | 426 // Finalize the instant query immediately. |
422 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 427 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
423 | 428 |
424 // There should be two matches, one for what you typed, the other for | 429 // There should be two matches, one for what you typed, the other for |
425 // 'foobar'. | 430 // 'foobar'. |
426 EXPECT_EQ(2u, provider_->matches().size()); | 431 EXPECT_EQ(2u, provider_->matches().size()); |
427 GURL instant_url(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, | 432 GURL instant_url(default_t_url_->url()->ReplaceSearchTerms(*default_t_url_, |
428 ASCIIToUTF16("foobar"), 0, string16())); | 433 ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, |
| 434 string16())); |
429 AutocompleteMatch instant_match; | 435 AutocompleteMatch instant_match; |
430 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | 436 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); |
431 | 437 |
432 // Wait until history and the suggest query complete. | 438 // Wait until history and the suggest query complete. |
433 profile_.BlockUntilHistoryProcessesPendingRequests(); | 439 profile_.BlockUntilHistoryProcessesPendingRequests(); |
434 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 440 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
435 | 441 |
436 // Provider should be done. | 442 // Provider should be done. |
437 EXPECT_TRUE(provider_->done()); | 443 EXPECT_TRUE(provider_->done()); |
438 | 444 |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 fetcher = NULL; | 664 fetcher = NULL; |
659 | 665 |
660 // Run till the history results complete. | 666 // Run till the history results complete. |
661 RunTillProviderDone(); | 667 RunTillProviderDone(); |
662 | 668 |
663 // Make sure there is a match for 'a.com' and it doesn't have a template_url. | 669 // Make sure there is a match for 'a.com' and it doesn't have a template_url. |
664 AutocompleteMatch nav_match; | 670 AutocompleteMatch nav_match; |
665 EXPECT_TRUE(FindMatchWithDestination(GURL("http://a.com"), &nav_match)); | 671 EXPECT_TRUE(FindMatchWithDestination(GURL("http://a.com"), &nav_match)); |
666 EXPECT_FALSE(nav_match.template_url); | 672 EXPECT_FALSE(nav_match.template_url); |
667 } | 673 } |
OLD | NEW |