| OLD | NEW |
| 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/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 14 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 15 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 16 #include "chrome/browser/autocomplete/autocomplete_input.h" | 16 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 17 #include "chrome/browser/autocomplete/autocomplete_match.h" | 17 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 18 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 19 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 20 #include "chrome/browser/autocomplete/history_url_provider.h" | 20 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 21 #include "chrome/browser/history/history_service.h" | 21 #include "chrome/browser/history/history_service.h" |
| 22 #include "chrome/browser/history/history_service_factory.h" | 22 #include "chrome/browser/history/history_service_factory.h" |
| 23 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 23 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
| 24 #include "chrome/browser/search/search.h" | 24 #include "chrome/browser/search/search.h" |
| 25 #include "chrome/browser/search_engines/template_url.h" | 25 #include "chrome/browser/search_engines/template_url.h" |
| 26 #include "chrome/browser/search_engines/template_url_service.h" | 26 #include "chrome/browser/search_engines/template_url_service.h" |
| 27 #include "chrome/browser/search_engines/template_url_service_factory.h" | 27 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 28 #include "chrome/common/instant_types.h" | |
| 29 #include "chrome/common/metrics/entropy_provider.h" | 28 #include "chrome/common/metrics/entropy_provider.h" |
| 30 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/test/base/testing_browser_process.h" | 30 #include "chrome/test/base/testing_browser_process.h" |
| 32 #include "chrome/test/base/testing_profile.h" | 31 #include "chrome/test/base/testing_profile.h" |
| 33 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
| 34 #include "net/url_request/test_url_fetcher_factory.h" | 33 #include "net/url_request/test_url_fetcher_factory.h" |
| 35 #include "net/url_request/url_request_status.h" | 34 #include "net/url_request/url_request_status.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 37 | 36 |
| 38 using content::BrowserThread; | 37 using content::BrowserThread; |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // URLFetchers runs. | 278 // URLFetchers runs. |
| 280 message_loop_.RunUntilIdle(); | 279 message_loop_.RunUntilIdle(); |
| 281 } | 280 } |
| 282 | 281 |
| 283 void SearchProviderTest::QueryForInputAndSetWYTMatch( | 282 void SearchProviderTest::QueryForInputAndSetWYTMatch( |
| 284 const string16& text, | 283 const string16& text, |
| 285 AutocompleteMatch* wyt_match) { | 284 AutocompleteMatch* wyt_match) { |
| 286 QueryForInput(text, false, false); | 285 QueryForInput(text, false, false); |
| 287 profile_.BlockUntilHistoryProcessesPendingRequests(); | 286 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 288 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | 287 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); |
| 289 EXPECT_NE(chrome::IsInstantExtendedAPIEnabled(), provider_->done()); | |
| 290 if (!wyt_match) | 288 if (!wyt_match) |
| 291 return; | 289 return; |
| 292 ASSERT_GE(provider_->matches().size(), 1u); | 290 ASSERT_GE(provider_->matches().size(), 1u); |
| 293 EXPECT_TRUE(FindMatchWithDestination(GURL( | 291 EXPECT_TRUE(FindMatchWithDestination(GURL( |
| 294 default_t_url_->url_ref().ReplaceSearchTerms( | 292 default_t_url_->url_ref().ReplaceSearchTerms( |
| 295 TemplateURLRef::SearchTermsArgs(text))), | 293 TemplateURLRef::SearchTermsArgs(text))), |
| 296 wyt_match)); | 294 wyt_match)); |
| 297 } | 295 } |
| 298 | 296 |
| 299 void SearchProviderTest::TearDown() { | 297 void SearchProviderTest::TearDown() { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 for (size_t i = 0; i < arraysize(inputs); ++i) { | 501 for (size_t i = 0; i < arraysize(inputs); ++i) { |
| 504 QueryForInput(ASCIIToUTF16(inputs[i]), false, false); | 502 QueryForInput(ASCIIToUTF16(inputs[i]), false, false); |
| 505 // Make sure the default providers suggest service was not queried. | 503 // Make sure the default providers suggest service was not queried. |
| 506 ASSERT_TRUE(test_factory_.GetFetcherByID( | 504 ASSERT_TRUE(test_factory_.GetFetcherByID( |
| 507 SearchProvider::kDefaultProviderURLFetcherID) == NULL); | 505 SearchProvider::kDefaultProviderURLFetcherID) == NULL); |
| 508 // Run till the history results complete. | 506 // Run till the history results complete. |
| 509 RunTillProviderDone(); | 507 RunTillProviderDone(); |
| 510 } | 508 } |
| 511 } | 509 } |
| 512 | 510 |
| 513 // Make sure FinalizeInstantQuery works. | |
| 514 TEST_F(SearchProviderTest, FinalizeInstantQuery) { | |
| 515 chrome::EnableInstantExtendedAPIForTesting(); | |
| 516 | |
| 517 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), | |
| 518 NULL)); | |
| 519 | |
| 520 // Tell the provider Instant is done. | |
| 521 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), | |
| 522 InstantSuggestion(ASCIIToUTF16("bar"), | |
| 523 INSTANT_COMPLETE_NOW, | |
| 524 INSTANT_SUGGESTION_SEARCH, | |
| 525 string16(), | |
| 526 kNoMatchIndex)); | |
| 527 | |
| 528 // The provider should now be done. | |
| 529 EXPECT_TRUE(provider_->done()); | |
| 530 | |
| 531 // There should be two matches, one for what you typed, the other for | |
| 532 // 'foobar'. | |
| 533 EXPECT_EQ(2u, provider_->matches().size()); | |
| 534 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( | |
| 535 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foobar")))); | |
| 536 AutocompleteMatch instant_match; | |
| 537 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | |
| 538 | |
| 539 // And the 'foobar' match should not have a description, it'll be set later. | |
| 540 EXPECT_TRUE(instant_match.description.empty()); | |
| 541 | |
| 542 // Make sure the what you typed match has no description. | |
| 543 AutocompleteMatch wyt_match; | |
| 544 EXPECT_TRUE(FindMatchWithDestination( | |
| 545 GURL(default_t_url_->url_ref().ReplaceSearchTerms( | |
| 546 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foo")))), | |
| 547 &wyt_match)); | |
| 548 EXPECT_TRUE(wyt_match.description.empty()); | |
| 549 | |
| 550 // Instant search suggestions are never inline autocompleted, so they should | |
| 551 // score less than the WYT match. | |
| 552 EXPECT_LT(instant_match.relevance, wyt_match.relevance); | |
| 553 } | |
| 554 | |
| 555 // Make sure FinalizeInstantQuery works with URL suggestions. | |
| 556 TEST_F(SearchProviderTest, FinalizeInstantURL) { | |
| 557 chrome::EnableInstantExtendedAPIForTesting(); | |
| 558 | |
| 559 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("ex"), | |
| 560 NULL)); | |
| 561 | |
| 562 // Tell the provider Instant is done. | |
| 563 provider_->FinalizeInstantQuery(ASCIIToUTF16("ex"), | |
| 564 InstantSuggestion( | |
| 565 ASCIIToUTF16("http://example.com/"), | |
| 566 INSTANT_COMPLETE_NOW, | |
| 567 INSTANT_SUGGESTION_URL, | |
| 568 string16(), | |
| 569 kNoMatchIndex)); | |
| 570 | |
| 571 // The provider should now be done. | |
| 572 EXPECT_TRUE(provider_->done()); | |
| 573 | |
| 574 // There should be two matches, one for what you typed, the other for | |
| 575 // "http://example.com/". | |
| 576 EXPECT_EQ(2u, provider_->matches().size()); | |
| 577 GURL instant_url("http://example.com"); | |
| 578 AutocompleteMatch instant_match; | |
| 579 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | |
| 580 | |
| 581 // The Instant match should not have a description, it'll be set later. | |
| 582 EXPECT_TRUE(instant_match.description.empty()); | |
| 583 | |
| 584 // Make sure the what you typed match has no description. | |
| 585 AutocompleteMatch wyt_match; | |
| 586 EXPECT_TRUE(FindMatchWithDestination( | |
| 587 GURL(default_t_url_->url_ref().ReplaceSearchTerms( | |
| 588 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("ex")))), | |
| 589 &wyt_match)); | |
| 590 EXPECT_TRUE(wyt_match.description.empty()); | |
| 591 | |
| 592 // The Instant URL should be more relevant. | |
| 593 EXPECT_GT(instant_match.relevance, wyt_match.relevance); | |
| 594 } | |
| 595 | |
| 596 // An Instant URL suggestion should behave the same way whether the input text | |
| 597 // is classified as UNKNOWN or as an URL. Otherwise if the user types | |
| 598 // "example.co" url-what-you-typed will displace the Instant suggestion for | |
| 599 // "example.com". | |
| 600 TEST_F(SearchProviderTest, FinalizeInstantURLWithURLText) { | |
| 601 chrome::EnableInstantExtendedAPIForTesting(); | |
| 602 | |
| 603 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch( | |
| 604 ASCIIToUTF16("example.co"), NULL)); | |
| 605 | |
| 606 // Tell the provider Instant is done. | |
| 607 provider_->FinalizeInstantQuery(ASCIIToUTF16("example.co"), | |
| 608 InstantSuggestion( | |
| 609 ASCIIToUTF16("http://example.com/"), | |
| 610 INSTANT_COMPLETE_NOW, | |
| 611 INSTANT_SUGGESTION_URL, | |
| 612 string16(), | |
| 613 kNoMatchIndex)); | |
| 614 | |
| 615 // The provider should now be done. | |
| 616 EXPECT_TRUE(provider_->done()); | |
| 617 | |
| 618 // There should be two matches, one for what you typed, the other for | |
| 619 // "http://example.com/". | |
| 620 EXPECT_EQ(2u, provider_->matches().size()); | |
| 621 GURL instant_url("http://example.com"); | |
| 622 AutocompleteMatch instant_match; | |
| 623 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | |
| 624 | |
| 625 // The Instant match should not have a description, it'll be set later. | |
| 626 EXPECT_TRUE(instant_match.description.empty()); | |
| 627 | |
| 628 // The Instant URL should be more relevant than a URL_WHAT_YOU_TYPED match. | |
| 629 EXPECT_GT(instant_match.relevance, | |
| 630 HistoryURLProvider::kScoreForWhatYouTypedResult); | |
| 631 } | |
| 632 | |
| 633 // Make sure that if FinalizeInstantQuery is invoked before suggest results | |
| 634 // return, the suggest text from FinalizeInstantQuery is remembered. | |
| 635 TEST_F(SearchProviderTest, RememberInstantQuery) { | |
| 636 chrome::EnableInstantExtendedAPIForTesting(); | |
| 637 | |
| 638 QueryForInput(ASCIIToUTF16("foo"), false, false); | |
| 639 | |
| 640 // Finalize the Instant query immediately. | |
| 641 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), | |
| 642 InstantSuggestion(ASCIIToUTF16("bar"), | |
| 643 INSTANT_COMPLETE_NOW, | |
| 644 INSTANT_SUGGESTION_SEARCH, | |
| 645 string16(), | |
| 646 kNoMatchIndex)); | |
| 647 | |
| 648 // There should be two matches, one for what you typed, the other for | |
| 649 // 'foobar'. | |
| 650 EXPECT_EQ(2u, provider_->matches().size()); | |
| 651 GURL instant_url(default_t_url_->url_ref().ReplaceSearchTerms( | |
| 652 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("foobar")))); | |
| 653 AutocompleteMatch instant_match; | |
| 654 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | |
| 655 | |
| 656 // Wait until history and the suggest query complete. | |
| 657 profile_.BlockUntilHistoryProcessesPendingRequests(); | |
| 658 ASSERT_NO_FATAL_FAILURE(FinishDefaultSuggestQuery()); | |
| 659 | |
| 660 // Provider should be done. | |
| 661 EXPECT_TRUE(provider_->done()); | |
| 662 | |
| 663 // There should be two matches, one for what you typed, the other for | |
| 664 // 'foobar'. | |
| 665 EXPECT_EQ(2u, provider_->matches().size()); | |
| 666 EXPECT_TRUE(FindMatchWithDestination(instant_url, &instant_match)); | |
| 667 | |
| 668 // And the 'foobar' match should not have a description, it'll be set later. | |
| 669 EXPECT_TRUE(instant_match.description.empty()); | |
| 670 } | |
| 671 | |
| 672 // Make sure that if trailing whitespace is added to the text supplied to | |
| 673 // AutocompleteInput the default suggest text is cleared. | |
| 674 TEST_F(SearchProviderTest, DifferingText) { | |
| 675 chrome::EnableInstantExtendedAPIForTesting(); | |
| 676 | |
| 677 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo"), | |
| 678 NULL)); | |
| 679 | |
| 680 // Finalize the Instant query immediately. | |
| 681 provider_->FinalizeInstantQuery(ASCIIToUTF16("foo"), | |
| 682 InstantSuggestion(ASCIIToUTF16("bar"), | |
| 683 INSTANT_COMPLETE_NOW, | |
| 684 INSTANT_SUGGESTION_SEARCH, | |
| 685 string16(), | |
| 686 kNoMatchIndex)); | |
| 687 | |
| 688 // Query with the same input text, but trailing whitespace. | |
| 689 AutocompleteMatch instant_match; | |
| 690 ASSERT_NO_FATAL_FAILURE(QueryForInputAndSetWYTMatch(ASCIIToUTF16("foo "), | |
| 691 &instant_match)); | |
| 692 | |
| 693 // There should only one match, for what you typed. | |
| 694 EXPECT_EQ(1u, provider_->matches().size()); | |
| 695 EXPECT_FALSE(instant_match.destination_url.is_empty()); | |
| 696 } | |
| 697 | |
| 698 TEST_F(SearchProviderTest, DontAutocompleteURLLikeTerms) { | 511 TEST_F(SearchProviderTest, DontAutocompleteURLLikeTerms) { |
| 699 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( | 512 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( |
| 700 &profile_, &AutocompleteClassifierFactory::BuildInstanceFor); | 513 &profile_, &AutocompleteClassifierFactory::BuildInstanceFor); |
| 701 GURL url = AddSearchToHistory(default_t_url_, | 514 GURL url = AddSearchToHistory(default_t_url_, |
| 702 ASCIIToUTF16("docs.google.com"), 1); | 515 ASCIIToUTF16("docs.google.com"), 1); |
| 703 | 516 |
| 704 // Add the term as a url. | 517 // Add the term as a url. |
| 705 HistoryServiceFactory::GetForProfile(&profile_, Profile::EXPLICIT_ACCESS)-> | 518 HistoryServiceFactory::GetForProfile(&profile_, Profile::EXPLICIT_ACCESS)-> |
| 706 AddPageWithDetails(GURL("http://docs.google.com"), string16(), 1, 1, | 519 AddPageWithDetails(GURL("http://docs.google.com"), string16(), 1, 1, |
| 707 base::Time::Now(), false, history::SOURCE_BROWSED); | 520 base::Time::Now(), false, history::SOURCE_BROWSED); |
| (...skipping 1451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2159 } else { | 1972 } else { |
| 2160 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion; | 1973 ASSERT_NE(sug_end, sug_it) << "Failed to find " << suggestion; |
| 2161 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion()); | 1974 EXPECT_EQ(ASCIIToUTF16(suggestion), sug_it->suggestion()); |
| 2162 ++sug_it; | 1975 ++sug_it; |
| 2163 } | 1976 } |
| 2164 } | 1977 } |
| 2165 EXPECT_EQ(sug_end, sug_it); | 1978 EXPECT_EQ(sug_end, sug_it); |
| 2166 EXPECT_EQ(nav_end, nav_it); | 1979 EXPECT_EQ(nav_end, nav_it); |
| 2167 } | 1980 } |
| 2168 } | 1981 } |
| OLD | NEW |