| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| (...skipping 3159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3170 if (suggestion == kNotApplicable) | 3170 if (suggestion == kNotApplicable) |
| 3171 break; | 3171 break; |
| 3172 if (cases[i].results[j].is_navigation_result) { | 3172 if (cases[i].results[j].is_navigation_result) { |
| 3173 provider_->default_results_.navigation_results.push_back( | 3173 provider_->default_results_.navigation_results.push_back( |
| 3174 SearchProvider::NavigationResult( | 3174 SearchProvider::NavigationResult( |
| 3175 *provider_.get(), GURL(suggestion), string16(), false, | 3175 *provider_.get(), GURL(suggestion), string16(), false, |
| 3176 cases[i].results[j].relevance, false)); | 3176 cases[i].results[j].relevance, false)); |
| 3177 } else { | 3177 } else { |
| 3178 provider_->default_results_.suggest_results.push_back( | 3178 provider_->default_results_.suggest_results.push_back( |
| 3179 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), | 3179 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), |
| 3180 string16(), std::string(), false, | 3180 string16(), std::string(), |
| 3181 std::string(), false, |
| 3181 cases[i].results[j].relevance, | 3182 cases[i].results[j].relevance, |
| 3182 false, false)); | 3183 false, false)); |
| 3183 } | 3184 } |
| 3184 } | 3185 } |
| 3185 | 3186 |
| 3186 provider_->input_ = AutocompleteInput( | 3187 provider_->input_ = AutocompleteInput( |
| 3187 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), | 3188 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), |
| 3188 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, | 3189 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, |
| 3189 AutocompleteInput::ALL_MATCHES); | 3190 AutocompleteInput::ALL_MATCHES); |
| 3190 provider_->RemoveAllStaleResults(); | 3191 provider_->RemoveAllStaleResults(); |
| (...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3647 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 3648 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
| 3648 } | 3649 } |
| 3649 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | 3650 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
| 3650 SCOPED_TRACE("and match: " + base::IntToString(j)); | 3651 SCOPED_TRACE("and match: " + base::IntToString(j)); |
| 3651 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | 3652 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
| 3652 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | 3653 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
| 3653 } | 3654 } |
| 3654 } | 3655 } |
| 3655 } | 3656 } |
| 3656 | 3657 |
| 3658 // Test that deletion url gets set on an AutocompleteMatch when available for a |
| 3659 // personalized query. |
| 3660 TEST_F(SearchProviderTest, ParseDeletionUrl) { |
| 3661 struct Match { |
| 3662 std::string contents; |
| 3663 std::string deletion_url; |
| 3664 AutocompleteMatchType::Type type; |
| 3665 }; |
| 3666 |
| 3667 const Match kEmptyMatch = { |
| 3668 kNotApplicable, "", AutocompleteMatchType::NUM_TYPES}; |
| 3669 |
| 3670 const char url[] = "https://www.google.com/complete/deleteitems" |
| 3671 "?delq=ab&client=chrome&deltok=xsrf123"; |
| 3672 |
| 3673 struct { |
| 3674 const std::string input_text; |
| 3675 const std::string response_json; |
| 3676 const Match matches[4]; |
| 3677 } cases[] = { |
| 3678 // Personalized query comes with a deletion url |
| 3679 { "a", |
| 3680 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3681 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3682 "\"google:suggestrelevance\":[1, 2]," |
| 3683 "\"google:suggestdetail\":[{\"du\":" |
| 3684 "\"https://www.google.com/complete/deleteitems?delq=ab&client=chrome" |
| 3685 "&deltok=xsrf123\"}, {}]}]", |
| 3686 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3687 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3688 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3689 kEmptyMatch, |
| 3690 }, |
| 3691 }, |
| 3692 // Personalized query, but no deletion url in response -- old server case |
| 3693 { "a", |
| 3694 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3695 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3696 "\"google:suggestrelevance\":[1, 2]," |
| 3697 "\"google:suggestdetail\":[{}, {}]}]", |
| 3698 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3699 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3700 { "ab", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3701 kEmptyMatch, |
| 3702 }, |
| 3703 }, |
| 3704 // Unexpected: non-personalized query comes with a deletion url, this |
| 3705 // should be ignored. |
| 3706 { "a", |
| 3707 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3708 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3709 "\"google:suggestrelevance\":[1, 2]," |
| 3710 "\"google:suggestdetail\":[{\"du\":\"" |
| 3711 "https://www.google.com/complete/deleteitems?delq=ab&" |
| 3712 "client=chrome&deltok=xsrf123\"}, {\"du\":\"" |
| 3713 "https://www.google.com/complete/deleteitems?delq=ab&" |
| 3714 "client=chrome&deltok=xsrf123\"}]}]", |
| 3715 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3716 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3717 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3718 kEmptyMatch, |
| 3719 }, |
| 3720 }, |
| 3721 }; |
| 3722 |
| 3723 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 3724 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); |
| 3725 |
| 3726 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3727 SearchProvider::kDefaultProviderURLFetcherID); |
| 3728 ASSERT_TRUE(fetcher); |
| 3729 fetcher->set_response_code(200); |
| 3730 fetcher->SetResponseString(cases[i].response_json); |
| 3731 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3732 |
| 3733 RunTillProviderDone(); |
| 3734 |
| 3735 const ACMatches& matches = provider_->matches(); |
| 3736 ASSERT_FALSE(matches.empty()); |
| 3737 |
| 3738 SCOPED_TRACE("for input with json = " + cases[i].response_json); |
| 3739 |
| 3740 for (size_t j = 0; j < matches.size(); ++j) { |
| 3741 const Match& match = cases[i].matches[j]; |
| 3742 SCOPED_TRACE(" and match index: " + base::IntToString(j)); |
| 3743 EXPECT_EQ(match.contents, UTF16ToUTF8(matches[j].contents)); |
| 3744 EXPECT_EQ(match.deletion_url, matches[j].GetAdditionalInfo( |
| 3745 "deletion_url")); |
| 3746 } |
| 3747 } |
| 3748 } |
| 3657 | 3749 |
| 3658 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { | 3750 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { |
| 3659 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); | 3751 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); |
| 3660 string16 term = term1_.substr(0, term1_.length() - 1); | 3752 string16 term = term1_.substr(0, term1_.length() - 1); |
| 3661 QueryForInput(term, true, false); | 3753 QueryForInput(term, true, false); |
| 3662 ASSERT_FALSE(provider_->matches().empty()); | 3754 ASSERT_FALSE(provider_->matches().empty()); |
| 3663 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3755 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 3664 provider_->matches()[0].type); | 3756 provider_->matches()[0].type); |
| 3665 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 3757 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
| 3666 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 3758 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3793 AutocompleteInput::OTHER, &profile_)); | 3885 AutocompleteInput::OTHER, &profile_)); |
| 3794 encrypted_types.Remove(syncer::SESSIONS); | 3886 encrypted_types.Remove(syncer::SESSIONS); |
| 3795 service->OnEncryptedTypesChanged(encrypted_types, false); | 3887 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3796 | 3888 |
| 3797 // Check that there were no side effects from previous tests. | 3889 // Check that there were no side effects from previous tests. |
| 3798 EXPECT_TRUE(SearchProvider::CanSendURL( | 3890 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3799 GURL("http://www.google.com/search"), | 3891 GURL("http://www.google.com/search"), |
| 3800 GURL("https://www.google.com/complete/search"), &google_template_url, | 3892 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3801 AutocompleteInput::OTHER, &profile_)); | 3893 AutocompleteInput::OTHER, &profile_)); |
| 3802 } | 3894 } |
| 3895 |
| 3896 TEST_F(SearchProviderTest, TestDeleteMatch_NoDeletionUrl) { |
| 3897 AutocompleteMatch m = AutocompleteMatch( |
| 3898 provider_, |
| 3899 640, |
| 3900 true, |
| 3901 AutocompleteMatchType::SEARCH_SUGGEST); |
| 3902 provider_->DeleteMatch(m); |
| 3903 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3904 SearchProvider::kDeletionURLFetcherID); |
| 3905 // Ensure that an autocomplete match without a deletion URL does not |
| 3906 // trigger a fetch. |
| 3907 ASSERT_FALSE(fetcher); |
| 3908 } |
| 3909 |
| 3910 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlSuccess) { |
| 3911 AutocompleteMatch m = AutocompleteMatch( |
| 3912 provider_, |
| 3913 640, |
| 3914 true, |
| 3915 AutocompleteMatchType::SEARCH_SUGGEST); |
| 3916 m.RecordAdditionalInfo(SearchProvider::kDeletionUrlKey, |
| 3917 "https://www.google.com/complete/deleteitem?q=foo"); |
| 3918 provider_->DeleteMatch(m); |
| 3919 // Set up a default fetcher with provided results. |
| 3920 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3921 SearchProvider::kDeletionURLFetcherID); |
| 3922 ASSERT_TRUE(fetcher); |
| 3923 fetcher->set_response_code(200); |
| 3924 fetcher->SetResponseString(""); |
| 3925 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3926 ASSERT_TRUE(provider_->deletion_handler_.get() == NULL); |
| 3927 } |
| 3928 |
| 3929 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlFailure) { |
| 3930 AutocompleteMatch m = AutocompleteMatch( |
| 3931 provider_, |
| 3932 640, |
| 3933 true, |
| 3934 AutocompleteMatchType::SEARCH_SUGGEST); |
| 3935 m.RecordAdditionalInfo(SearchProvider::kDeletionUrlKey, |
| 3936 "https://www.google.com/complete/deleteitem?q=foo"); |
| 3937 provider_->DeleteMatch(m); |
| 3938 // Set up a default fetcher with provided results. |
| 3939 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3940 SearchProvider::kDeletionURLFetcherID); |
| 3941 ASSERT_TRUE(fetcher); |
| 3942 fetcher->set_response_code(500); |
| 3943 fetcher->SetResponseString(""); |
| 3944 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3945 ASSERT_TRUE(provider_->deletion_handler_.get() == NULL); |
| 3946 } |
| OLD | NEW |