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