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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 AutocompleteMatch* wyt_match) { | 203 AutocompleteMatch* wyt_match) { |
204 QueryForInput(text, string16(), false); | 204 QueryForInput(text, string16(), false); |
205 profile_.BlockUntilHistoryProcessesPendingRequests(); | 205 profile_.BlockUntilHistoryProcessesPendingRequests(); |
206 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 206 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
207 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled), | 207 EXPECT_NE(profile_.GetPrefs()->GetBoolean(prefs::kInstantEnabled), |
208 provider_->done()); | 208 provider_->done()); |
209 if (!wyt_match) | 209 if (!wyt_match) |
210 return; | 210 return; |
211 ASSERT_GE(provider_->matches().size(), 1u); | 211 ASSERT_GE(provider_->matches().size(), 1u); |
212 EXPECT_TRUE(FindMatchWithDestination(GURL( | 212 EXPECT_TRUE(FindMatchWithDestination(GURL( |
213 default_t_url_->url_ref().ReplaceSearchTerms(text, | 213 default_t_url_->url_ref().ReplaceSearchTerms( |
214 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), wyt_match)); | 214 TemplateURLRef::SearchTermsArgs(text))), |
| 215 wyt_match)); |
215 } | 216 } |
216 | 217 |
217 void SearchProviderTest::TearDown() { | 218 void SearchProviderTest::TearDown() { |
218 message_loop_.RunAllPending(); | 219 message_loop_.RunAllPending(); |
219 | 220 |
220 // Shutdown the provider before the profile. | 221 // Shutdown the provider before the profile. |
221 provider_ = NULL; | 222 provider_ = NULL; |
222 } | 223 } |
223 | 224 |
224 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, | 225 GURL SearchProviderTest::AddSearchToHistory(TemplateURL* t_url, |
225 string16 term, | 226 string16 term, |
226 int visit_count) { | 227 int visit_count) { |
227 HistoryService* history = | 228 HistoryService* history = |
228 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); | 229 profile_.GetHistoryService(Profile::EXPLICIT_ACCESS); |
229 GURL search(t_url->url_ref().ReplaceSearchTerms(term, | 230 GURL search(t_url->url_ref().ReplaceSearchTerms( |
230 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 231 TemplateURLRef::SearchTermsArgs(term))); |
231 static base::Time last_added_time; | 232 static base::Time last_added_time; |
232 last_added_time = std::max(base::Time::Now(), | 233 last_added_time = std::max(base::Time::Now(), |
233 last_added_time + base::TimeDelta::FromMicroseconds(1)); | 234 last_added_time + base::TimeDelta::FromMicroseconds(1)); |
234 history->AddPageWithDetails(search, string16(), visit_count, visit_count, | 235 history->AddPageWithDetails(search, string16(), visit_count, visit_count, |
235 last_added_time, false, history::SOURCE_BROWSED); | 236 last_added_time, false, history::SOURCE_BROWSED); |
236 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); | 237 history->SetKeywordSearchTermsForURL(search, t_url->id(), term); |
237 return search; | 238 return search; |
238 } | 239 } |
239 | 240 |
240 bool SearchProviderTest::FindMatchWithContents(const string16& contents, | 241 bool SearchProviderTest::FindMatchWithContents(const string16& contents, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 string16 term = term1_.substr(0, term1_.length() - 1); | 280 string16 term = term1_.substr(0, term1_.length() - 1); |
280 QueryForInput(term, string16(), false); | 281 QueryForInput(term, string16(), false); |
281 | 282 |
282 // Make sure the default providers suggest service was queried. | 283 // Make sure the default providers suggest service was queried. |
283 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 284 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
284 SearchProvider::kDefaultProviderURLFetcherID); | 285 SearchProvider::kDefaultProviderURLFetcherID); |
285 ASSERT_TRUE(fetcher); | 286 ASSERT_TRUE(fetcher); |
286 | 287 |
287 // And the URL matches what we expected. | 288 // And the URL matches what we expected. |
288 GURL expected_url(default_t_url_->suggestions_url_ref().ReplaceSearchTerms( | 289 GURL expected_url(default_t_url_->suggestions_url_ref().ReplaceSearchTerms( |
289 term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 290 TemplateURLRef::SearchTermsArgs(term))); |
290 ASSERT_TRUE(fetcher->GetOriginalURL() == expected_url); | 291 ASSERT_TRUE(fetcher->GetOriginalURL() == expected_url); |
291 | 292 |
292 // Tell the SearchProvider the suggest query is done. | 293 // Tell the SearchProvider the suggest query is done. |
293 fetcher->set_response_code(200); | 294 fetcher->set_response_code(200); |
294 fetcher->delegate()->OnURLFetchComplete(fetcher); | 295 fetcher->delegate()->OnURLFetchComplete(fetcher); |
295 fetcher = NULL; | 296 fetcher = NULL; |
296 | 297 |
297 // Run till the history results complete. | 298 // Run till the history results complete. |
298 RunTillProviderDone(); | 299 RunTillProviderDone(); |
299 | 300 |
300 // The SearchProvider is done. Make sure it has a result for the history | 301 // The SearchProvider is done. Make sure it has a result for the history |
301 // term term1. | 302 // term term1. |
302 AutocompleteMatch term1_match; | 303 AutocompleteMatch term1_match; |
303 EXPECT_TRUE(FindMatchWithDestination(term1_url_, &term1_match)); | 304 EXPECT_TRUE(FindMatchWithDestination(term1_url_, &term1_match)); |
304 // Term1 should not have a description, it's set later. | 305 // Term1 should not have a description, it's set later. |
305 EXPECT_TRUE(term1_match.description.empty()); | 306 EXPECT_TRUE(term1_match.description.empty()); |
306 | 307 |
307 AutocompleteMatch wyt_match; | 308 AutocompleteMatch wyt_match; |
308 EXPECT_TRUE(FindMatchWithDestination( | 309 EXPECT_TRUE(FindMatchWithDestination( |
309 GURL(default_t_url_->url_ref().ReplaceSearchTerms(term, | 310 GURL(default_t_url_->url_ref().ReplaceSearchTerms( |
310 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), &wyt_match)); | 311 TemplateURLRef::SearchTermsArgs(term))), &wyt_match)); |
311 EXPECT_TRUE(wyt_match.description.empty()); | 312 EXPECT_TRUE(wyt_match.description.empty()); |
312 | 313 |
313 // The match for term1 should be more relevant than the what you typed result. | 314 // The match for term1 should be more relevant than the what you typed result. |
314 EXPECT_GT(term1_match.relevance, wyt_match.relevance); | 315 EXPECT_GT(term1_match.relevance, wyt_match.relevance); |
315 } | 316 } |
316 | 317 |
317 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { | 318 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { |
318 string16 term = term1_.substr(0, term1_.length() - 1); | 319 string16 term = term1_.substr(0, term1_.length() - 1); |
319 QueryForInput(term, string16(), true); | 320 QueryForInput(term, string16(), true); |
320 | 321 |
(...skipping 19 matching lines...) Expand all Loading... |
340 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); | 341 default_fetcher->delegate()->OnURLFetchComplete(default_fetcher); |
341 default_fetcher = NULL; | 342 default_fetcher = NULL; |
342 | 343 |
343 // Make sure the keyword providers suggest service was queried. | 344 // Make sure the keyword providers suggest service was queried. |
344 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( | 345 TestURLFetcher* keyword_fetcher = test_factory_.GetFetcherByID( |
345 SearchProvider::kKeywordProviderURLFetcherID); | 346 SearchProvider::kKeywordProviderURLFetcherID); |
346 ASSERT_TRUE(keyword_fetcher); | 347 ASSERT_TRUE(keyword_fetcher); |
347 | 348 |
348 // And the URL matches what we expected. | 349 // And the URL matches what we expected. |
349 GURL expected_url(keyword_t_url_->suggestions_url_ref().ReplaceSearchTerms( | 350 GURL expected_url(keyword_t_url_->suggestions_url_ref().ReplaceSearchTerms( |
350 term, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 351 TemplateURLRef::SearchTermsArgs(term))); |
351 ASSERT_TRUE(keyword_fetcher->GetOriginalURL() == expected_url); | 352 ASSERT_TRUE(keyword_fetcher->GetOriginalURL() == expected_url); |
352 | 353 |
353 // Tell the SearchProvider the keyword suggest query is done. | 354 // Tell the SearchProvider the keyword suggest query is done. |
354 keyword_fetcher->set_response_code(200); | 355 keyword_fetcher->set_response_code(200); |
355 keyword_fetcher->delegate()->OnURLFetchComplete(keyword_fetcher); | 356 keyword_fetcher->delegate()->OnURLFetchComplete(keyword_fetcher); |
356 keyword_fetcher = NULL; | 357 keyword_fetcher = NULL; |
357 | 358 |
358 // Run till the history results complete. | 359 // Run till the history results complete. |
359 RunTillProviderDone(); | 360 RunTillProviderDone(); |
360 | 361 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 // Tell the provider instant is done. | 410 // Tell the provider instant is done. |
410 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 411 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
411 | 412 |
412 // The provider should now be done. | 413 // The provider should now be done. |
413 EXPECT_TRUE(provider_->done()); | 414 EXPECT_TRUE(provider_->done()); |
414 | 415 |
415 // There should be two matches, one for what you typed, the other for | 416 // There should be two matches, one for what you typed, the other for |
416 // 'foobar'. | 417 // 'foobar'. |
417 EXPECT_EQ(2u, provider_->matches().size()); | 418 EXPECT_EQ(2u, provider_->matches().size()); |
418 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( | 419 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( |
419 ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, | 420 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foobar")))); |
420 string16())); | |
421 AutocompleteMatch instant_match; | 421 AutocompleteMatch instant_match; |
422 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | 422 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); |
423 | 423 |
424 // And the 'foobar' match should not have a description, it'll be set later. | 424 // And the 'foobar' match should not have a description, it'll be set later. |
425 EXPECT_TRUE(instant_match.description.empty()); | 425 EXPECT_TRUE(instant_match.description.empty()); |
426 | 426 |
427 // Make sure the what you typed match has no description. | 427 // Make sure the what you typed match has no description. |
428 AutocompleteMatch wyt_match; | 428 AutocompleteMatch wyt_match; |
429 EXPECT_TRUE(FindMatchWithDestination( | 429 EXPECT_TRUE(FindMatchWithDestination( |
430 GURL(default_t_url_->url_ref().ReplaceSearchTerms(ASCIIToUTF16("foo"), | 430 GURL(default_t_url_->url_ref().ReplaceSearchTerms( |
431 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), &wyt_match)); | 431 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foo")))), |
| 432 &wyt_match)); |
432 EXPECT_TRUE(wyt_match.description.empty()); | 433 EXPECT_TRUE(wyt_match.description.empty()); |
433 | 434 |
434 // The instant search should be more relevant. | 435 // The instant search should be more relevant. |
435 EXPECT_GT(instant_match.relevance, wyt_match.relevance); | 436 EXPECT_GT(instant_match.relevance, wyt_match.relevance); |
436 } | 437 } |
437 | 438 |
438 // Make sure that if FinalizeInstantQuery is invoked before suggest results | 439 // Make sure that if FinalizeInstantQuery is invoked before suggest results |
439 // return, the suggest text from FinalizeInstantQuery is remembered. | 440 // return, the suggest text from FinalizeInstantQuery is remembered. |
440 TEST_F(SearchProviderTest, RememberInstantQuery) { | 441 TEST_F(SearchProviderTest, RememberInstantQuery) { |
441 PrefService* service = profile_.GetPrefs(); | 442 PrefService* service = profile_.GetPrefs(); |
442 service->SetBoolean(prefs::kInstantEnabled, true); | 443 service->SetBoolean(prefs::kInstantEnabled, true); |
443 | 444 |
444 QueryForInput(ASCIIToUTF16("foo"), string16(), false); | 445 QueryForInput(ASCIIToUTF16("foo"), string16(), false); |
445 | 446 |
446 // Finalize the instant query immediately. | 447 // Finalize the instant query immediately. |
447 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); | 448 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), ASCIIToUTF16("bar")); |
448 | 449 |
449 // There should be two matches, one for what you typed, the other for | 450 // There should be two matches, one for what you typed, the other for |
450 // 'foobar'. | 451 // 'foobar'. |
451 EXPECT_EQ(2u, provider_->matches().size()); | 452 EXPECT_EQ(2u, provider_->matches().size()); |
452 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( | 453 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( |
453 ASCIIToUTF16("foobar"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, | 454 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foobar")))); |
454 string16())); | |
455 AutocompleteMatch instant_match; | 455 AutocompleteMatch instant_match; |
456 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | 456 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); |
457 | 457 |
458 // Wait until history and the suggest query complete. | 458 // Wait until history and the suggest query complete. |
459 profile_.BlockUntilHistoryProcessesPendingRequests(); | 459 profile_.BlockUntilHistoryProcessesPendingRequests(); |
460 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 460 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
461 | 461 |
462 // Provider should be done. | 462 // Provider should be done. |
463 EXPECT_TRUE(provider_->done()); | 463 EXPECT_TRUE(provider_->done()); |
464 | 464 |
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1215 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1215 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
1216 match.contents_class[0].style); | 1216 match.contents_class[0].style); |
1217 EXPECT_EQ(4U, match.contents_class[1].offset); | 1217 EXPECT_EQ(4U, match.contents_class[1].offset); |
1218 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | | 1218 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL | |
1219 AutocompleteMatch::ACMatchClassification::MATCH, | 1219 AutocompleteMatch::ACMatchClassification::MATCH, |
1220 match.contents_class[1].style); | 1220 match.contents_class[1].style); |
1221 EXPECT_EQ(5U, match.contents_class[2].offset); | 1221 EXPECT_EQ(5U, match.contents_class[2].offset); |
1222 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, | 1222 EXPECT_EQ(AutocompleteMatch::ACMatchClassification::URL, |
1223 match.contents_class[2].style); | 1223 match.contents_class[2].style); |
1224 } | 1224 } |
OLD | NEW |