| 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 3336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3347 if (suggestion == kNotApplicable) | 3347 if (suggestion == kNotApplicable) |
| 3348 break; | 3348 break; |
| 3349 if (cases[i].results[j].is_navigation_result) { | 3349 if (cases[i].results[j].is_navigation_result) { |
| 3350 provider_->default_results_.navigation_results.push_back( | 3350 provider_->default_results_.navigation_results.push_back( |
| 3351 SearchProvider::NavigationResult( | 3351 SearchProvider::NavigationResult( |
| 3352 *provider_.get(), GURL(suggestion), string16(), false, | 3352 *provider_.get(), GURL(suggestion), string16(), false, |
| 3353 cases[i].results[j].relevance, false)); | 3353 cases[i].results[j].relevance, false)); |
| 3354 } else { | 3354 } else { |
| 3355 provider_->default_results_.suggest_results.push_back( | 3355 provider_->default_results_.suggest_results.push_back( |
| 3356 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), | 3356 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), |
| 3357 string16(), std::string(), false, | 3357 string16(), std::string(), |
| 3358 std::string(), false, |
| 3358 cases[i].results[j].relevance, | 3359 cases[i].results[j].relevance, |
| 3359 false, false)); | 3360 false, false)); |
| 3360 } | 3361 } |
| 3361 } | 3362 } |
| 3362 | 3363 |
| 3363 provider_->input_ = AutocompleteInput( | 3364 provider_->input_ = AutocompleteInput( |
| 3364 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), | 3365 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), |
| 3365 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, | 3366 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, |
| 3366 AutocompleteInput::ALL_MATCHES); | 3367 AutocompleteInput::ALL_MATCHES); |
| 3367 provider_->RemoveAllStaleResults(); | 3368 provider_->RemoveAllStaleResults(); |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3828 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 3829 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
| 3829 } | 3830 } |
| 3830 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | 3831 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
| 3831 SCOPED_TRACE("and match: " + base::IntToString(j)); | 3832 SCOPED_TRACE("and match: " + base::IntToString(j)); |
| 3832 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | 3833 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
| 3833 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | 3834 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
| 3834 } | 3835 } |
| 3835 } | 3836 } |
| 3836 } | 3837 } |
| 3837 | 3838 |
| 3839 // Test that deletion url gets set on an AutocompleteMatch when available for a |
| 3840 // personalized query. |
| 3841 TEST_F(SearchProviderTest, ParseDeletionUrl) { |
| 3842 struct Match { |
| 3843 std::string contents; |
| 3844 std::string deletion_url; |
| 3845 AutocompleteMatchType::Type type; |
| 3846 }; |
| 3847 |
| 3848 const Match kEmptyMatch = { |
| 3849 kNotApplicable, "", AutocompleteMatchType::NUM_TYPES}; |
| 3850 |
| 3851 const char url[] = "https://www.google.com/complete/deleteitems" |
| 3852 "?delq=ab&client=chrome&deltok=xsrf123"; |
| 3853 |
| 3854 struct { |
| 3855 const std::string input_text; |
| 3856 const std::string response_json; |
| 3857 const Match matches[4]; |
| 3858 } cases[] = { |
| 3859 // Personalized query comes with a deletion url |
| 3860 { "a", |
| 3861 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3862 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3863 "\"google:suggestrelevance\":[1, 2]," |
| 3864 "\"google:suggestdetail\":[{\"du\":" |
| 3865 "\"https://www.google.com/complete/deleteitems?delq=ab&client=chrome" |
| 3866 "&deltok=xsrf123\"}, {}]}]", |
| 3867 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3868 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3869 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3870 kEmptyMatch, |
| 3871 }, |
| 3872 }, |
| 3873 // Personalized query, but no deletion url in response -- old server case |
| 3874 { "a", |
| 3875 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3876 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3877 "\"google:suggestrelevance\":[1, 2]," |
| 3878 "\"google:suggestdetail\":[{}, {}]}]", |
| 3879 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3880 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3881 { "ab", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3882 kEmptyMatch, |
| 3883 }, |
| 3884 }, |
| 3885 // Unexpected: non-personalized query comes with a deletion url, this |
| 3886 // should be ignored. |
| 3887 { "a", |
| 3888 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3889 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3890 "\"google:suggestrelevance\":[1, 2]," |
| 3891 "\"google:suggestdetail\":[{\"du\":\"" |
| 3892 "https://www.google.com/complete/deleteitems?delq=ab&" |
| 3893 "client=chrome&deltok=xsrf123\"}, {\"du\":\"" |
| 3894 "https://www.google.com/complete/deleteitems?delq=ab&" |
| 3895 "client=chrome&deltok=xsrf123\"}]}]", |
| 3896 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3897 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3898 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3899 kEmptyMatch, |
| 3900 }, |
| 3901 }, |
| 3902 }; |
| 3903 |
| 3904 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 3905 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); |
| 3906 |
| 3907 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3908 SearchProvider::kDefaultProviderURLFetcherID); |
| 3909 ASSERT_TRUE(fetcher); |
| 3910 fetcher->set_response_code(200); |
| 3911 fetcher->SetResponseString(cases[i].response_json); |
| 3912 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3913 |
| 3914 RunTillProviderDone(); |
| 3915 |
| 3916 const ACMatches& matches = provider_->matches(); |
| 3917 ASSERT_FALSE(matches.empty()); |
| 3918 |
| 3919 SCOPED_TRACE("for input with json = " + cases[i].response_json); |
| 3920 |
| 3921 for (size_t j = 0; j < matches.size(); ++j) { |
| 3922 const Match& match = cases[i].matches[j]; |
| 3923 SCOPED_TRACE(" and match index: " + base::IntToString(j)); |
| 3924 EXPECT_EQ(match.contents, UTF16ToUTF8(matches[j].contents)); |
| 3925 EXPECT_EQ(match.deletion_url, matches[j].GetAdditionalInfo( |
| 3926 "deletion_url")); |
| 3927 } |
| 3928 } |
| 3929 } |
| 3838 | 3930 |
| 3839 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { | 3931 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { |
| 3840 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); | 3932 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); |
| 3841 string16 term = term1_.substr(0, term1_.length() - 1); | 3933 string16 term = term1_.substr(0, term1_.length() - 1); |
| 3842 QueryForInput(term, true, false); | 3934 QueryForInput(term, true, false); |
| 3843 ASSERT_FALSE(provider_->matches().empty()); | 3935 ASSERT_FALSE(provider_->matches().empty()); |
| 3844 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3936 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
| 3845 provider_->matches()[0].type); | 3937 provider_->matches()[0].type); |
| 3846 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 3938 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
| 3847 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 3939 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3974 AutocompleteInput::OTHER, &profile_)); | 4066 AutocompleteInput::OTHER, &profile_)); |
| 3975 encrypted_types.Remove(syncer::SESSIONS); | 4067 encrypted_types.Remove(syncer::SESSIONS); |
| 3976 service->OnEncryptedTypesChanged(encrypted_types, false); | 4068 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3977 | 4069 |
| 3978 // Check that there were no side effects from previous tests. | 4070 // Check that there were no side effects from previous tests. |
| 3979 EXPECT_TRUE(SearchProvider::CanSendURL( | 4071 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3980 GURL("http://www.google.com/search"), | 4072 GURL("http://www.google.com/search"), |
| 3981 GURL("https://www.google.com/complete/search"), &google_template_url, | 4073 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3982 AutocompleteInput::OTHER, &profile_)); | 4074 AutocompleteInput::OTHER, &profile_)); |
| 3983 } | 4075 } |
| 4076 |
| 4077 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlSuccess) { |
| 4078 AutocompleteMatch match( |
| 4079 provider_, |
| 4080 0, |
| 4081 true, |
| 4082 AutocompleteMatchType::SEARCH_SUGGEST); |
| 4083 match.RecordAdditionalInfo( |
| 4084 SearchProvider::kDeletionUrlKey, |
| 4085 "https://www.google.com/complete/deleteitem?q=foo"); |
| 4086 provider_->matches_.push_back(match); |
| 4087 provider_->DeleteMatch(match); |
| 4088 ASSERT_FALSE(provider_->deletion_handlers_.empty()); |
| 4089 ASSERT_TRUE(provider_->matches_.empty()); |
| 4090 // Set up a default fetcher with provided results. |
| 4091 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 4092 SearchProvider::kDeletionURLFetcherID); |
| 4093 ASSERT_TRUE(fetcher); |
| 4094 fetcher->set_response_code(200); |
| 4095 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 4096 ASSERT_TRUE(provider_->deletion_handlers_.empty()); |
| 4097 } |
| 4098 |
| 4099 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlFailure) { |
| 4100 AutocompleteMatch match( |
| 4101 provider_, |
| 4102 0, |
| 4103 true, |
| 4104 AutocompleteMatchType::SEARCH_SUGGEST); |
| 4105 match.RecordAdditionalInfo( |
| 4106 SearchProvider::kDeletionUrlKey, |
| 4107 "https://www.google.com/complete/deleteitem?q=foo"); |
| 4108 provider_->matches_.push_back(match); |
| 4109 provider_->DeleteMatch(match); |
| 4110 ASSERT_FALSE(provider_->deletion_handlers_.empty()); |
| 4111 ASSERT_TRUE(provider_->matches_.empty()); |
| 4112 // Set up a default fetcher with provided results. |
| 4113 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 4114 SearchProvider::kDeletionURLFetcherID); |
| 4115 ASSERT_TRUE(fetcher); |
| 4116 fetcher->set_response_code(500); |
| 4117 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 4118 ASSERT_TRUE(provider_->deletion_handlers_.empty()); |
| 4119 } |
| OLD | NEW |