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 3305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3316 if (suggestion == kNotApplicable) | 3316 if (suggestion == kNotApplicable) |
3317 break; | 3317 break; |
3318 if (cases[i].results[j].is_navigation_result) { | 3318 if (cases[i].results[j].is_navigation_result) { |
3319 provider_->default_results_.navigation_results.push_back( | 3319 provider_->default_results_.navigation_results.push_back( |
3320 SearchProvider::NavigationResult( | 3320 SearchProvider::NavigationResult( |
3321 *provider_.get(), GURL(suggestion), string16(), false, | 3321 *provider_.get(), GURL(suggestion), string16(), false, |
3322 cases[i].results[j].relevance, false)); | 3322 cases[i].results[j].relevance, false)); |
3323 } else { | 3323 } else { |
3324 provider_->default_results_.suggest_results.push_back( | 3324 provider_->default_results_.suggest_results.push_back( |
3325 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), | 3325 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), |
3326 string16(), std::string(), false, | 3326 string16(), std::string(), |
3327 std::string(), false, | |
3327 cases[i].results[j].relevance, | 3328 cases[i].results[j].relevance, |
3328 false, false)); | 3329 false, false)); |
3329 } | 3330 } |
3330 } | 3331 } |
3331 | 3332 |
3332 provider_->input_ = AutocompleteInput( | 3333 provider_->input_ = AutocompleteInput( |
3333 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), | 3334 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), |
3334 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, | 3335 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, |
3335 AutocompleteInput::ALL_MATCHES); | 3336 AutocompleteInput::ALL_MATCHES); |
3336 provider_->RemoveAllStaleResults(); | 3337 provider_->RemoveAllStaleResults(); |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3797 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 3798 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
3798 } | 3799 } |
3799 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | 3800 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
3800 SCOPED_TRACE("and match: " + base::IntToString(j)); | 3801 SCOPED_TRACE("and match: " + base::IntToString(j)); |
3801 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | 3802 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
3802 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | 3803 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
3803 } | 3804 } |
3804 } | 3805 } |
3805 } | 3806 } |
3806 | 3807 |
3808 // Test that deletion url gets set on an AutocompleteMatch when available for a | |
3809 // personalized query. | |
3810 TEST_F(SearchProviderTest, ParseDeletionUrl) { | |
3811 struct Match { | |
3812 std::string contents; | |
3813 std::string deletion_url; | |
3814 AutocompleteMatchType::Type type; | |
3815 }; | |
3816 | |
3817 const Match kEmptyMatch = { | |
3818 kNotApplicable, "", AutocompleteMatchType::NUM_TYPES}; | |
3819 | |
3820 const char url[] = "https://www.google.com/complete/deleteitems" | |
3821 "?delq=ab&client=chrome&deltok=xsrf123"; | |
3822 | |
3823 struct { | |
3824 const std::string input_text; | |
3825 const std::string response_json; | |
3826 const Match matches[4]; | |
3827 } cases[] = { | |
3828 // Personalized query comes with a deletion url | |
3829 { "a", | |
3830 "[\"a\",[\"ab\", \"ac\"],[],[]," | |
3831 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," | |
3832 "\"google:suggestrelevance\":[1, 2]," | |
3833 "\"google:suggestdetail\":[{\"du\":" | |
3834 "\"https://www.google.com/complete/deleteitems?delq=ab&client=chrome" | |
3835 "&deltok=xsrf123\"}, {}]}]", | |
3836 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, | |
3837 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, | |
3838 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, | |
3839 kEmptyMatch, | |
3840 }, | |
3841 }, | |
3842 // Personalized query, but no deletion url in response -- old server case | |
3843 { "a", | |
3844 "[\"a\",[\"ab\", \"ac\"],[],[]," | |
3845 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," | |
3846 "\"google:suggestrelevance\":[1, 2]," | |
3847 "\"google:suggestdetail\":[{}, {}]}]", | |
3848 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, | |
3849 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, | |
3850 { "ab", "", AutocompleteMatchType::SEARCH_SUGGEST }, | |
3851 kEmptyMatch, | |
3852 }, | |
3853 }, | |
3854 // Unexpected: non-personalized query comes with a deletion url, this | |
3855 // should be ignored. | |
3856 { "a", | |
3857 "[\"a\",[\"ab\", \"ac\"],[],[]," | |
3858 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," | |
3859 "\"google:suggestrelevance\":[1, 2]," | |
3860 "\"google:suggestdetail\":[{\"du\":\"" | |
3861 "https://www.google.com/complete/deleteitems?delq=ab&" | |
3862 "client=chrome&deltok=xsrf123\"}, {\"du\":\"" | |
3863 "https://www.google.com/complete/deleteitems?delq=ab&" | |
3864 "client=chrome&deltok=xsrf123\"}]}]", | |
3865 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, | |
3866 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, | |
3867 { "ab", url, AutocompleteMatchType::SEARCH_SUGGEST }, | |
3868 kEmptyMatch, | |
3869 }, | |
3870 }, | |
3871 }; | |
3872 | |
3873 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { | |
3874 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); | |
3875 | |
3876 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | |
3877 SearchProvider::kDefaultProviderURLFetcherID); | |
3878 ASSERT_TRUE(fetcher); | |
3879 fetcher->set_response_code(200); | |
3880 fetcher->SetResponseString(cases[i].response_json); | |
3881 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
3882 | |
3883 RunTillProviderDone(); | |
3884 | |
3885 const ACMatches& matches = provider_->matches(); | |
3886 ASSERT_FALSE(matches.empty()); | |
3887 | |
3888 SCOPED_TRACE("for input with json = " + cases[i].response_json); | |
3889 | |
3890 for (size_t j = 0; j < matches.size(); ++j) { | |
3891 const Match& match = cases[i].matches[j]; | |
3892 SCOPED_TRACE(" and match index: " + base::IntToString(j)); | |
3893 EXPECT_EQ(match.contents, UTF16ToUTF8(matches[j].contents)); | |
3894 EXPECT_EQ(match.deletion_url, matches[j].GetAdditionalInfo( | |
3895 "deletion_url")); | |
3896 } | |
3897 } | |
3898 } | |
3807 | 3899 |
3808 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { | 3900 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { |
3809 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); | 3901 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); |
3810 string16 term = term1_.substr(0, term1_.length() - 1); | 3902 string16 term = term1_.substr(0, term1_.length() - 1); |
3811 QueryForInput(term, true, false); | 3903 QueryForInput(term, true, false); |
3812 ASSERT_FALSE(provider_->matches().empty()); | 3904 ASSERT_FALSE(provider_->matches().empty()); |
3813 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3905 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
3814 provider_->matches()[0].type); | 3906 provider_->matches()[0].type); |
3815 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 3907 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
3816 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 3908 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3943 AutocompleteInput::OTHER, &profile_)); | 4035 AutocompleteInput::OTHER, &profile_)); |
3944 encrypted_types.Remove(syncer::SESSIONS); | 4036 encrypted_types.Remove(syncer::SESSIONS); |
3945 service->OnEncryptedTypesChanged(encrypted_types, false); | 4037 service->OnEncryptedTypesChanged(encrypted_types, false); |
3946 | 4038 |
3947 // Check that there were no side effects from previous tests. | 4039 // Check that there were no side effects from previous tests. |
3948 EXPECT_TRUE(SearchProvider::CanSendURL( | 4040 EXPECT_TRUE(SearchProvider::CanSendURL( |
3949 GURL("http://www.google.com/search"), | 4041 GURL("http://www.google.com/search"), |
3950 GURL("https://www.google.com/complete/search"), &google_template_url, | 4042 GURL("https://www.google.com/complete/search"), &google_template_url, |
3951 AutocompleteInput::OTHER, &profile_)); | 4043 AutocompleteInput::OTHER, &profile_)); |
3952 } | 4044 } |
4045 | |
4046 TEST_F(SearchProviderTest, TestDeleteMatch_NoDeletionUrl) { | |
4047 AutocompleteMatch m = AutocompleteMatch( | |
Peter Kasting
2013/11/23 00:08:43
Nit: Always do "Class varname(...)" instead of "Cl
Maria
2013/11/26 02:36:27
Done.
| |
4048 provider_, | |
4049 640, | |
Peter Kasting
2013/11/23 00:08:43
Nit: Is the value here important? If not use 0 or
Maria
2013/11/26 02:36:27
Done.
| |
4050 true, | |
4051 AutocompleteMatchType::SEARCH_SUGGEST); | |
4052 provider_->DeleteMatch(m); | |
4053 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | |
4054 SearchProvider::kDeletionURLFetcherID); | |
4055 // Ensure that an autocomplete match without a deletion URL does not | |
4056 // trigger a fetch. | |
4057 ASSERT_FALSE(fetcher); | |
4058 } | |
4059 | |
4060 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlSuccess) { | |
4061 AutocompleteMatch m = AutocompleteMatch( | |
4062 provider_, | |
4063 640, | |
4064 true, | |
4065 AutocompleteMatchType::SEARCH_SUGGEST); | |
4066 m.RecordAdditionalInfo(SearchProvider::kDeletionUrlKey, | |
4067 "https://www.google.com/complete/deleteitem?q=foo"); | |
4068 provider_->DeleteMatch(m); | |
4069 // Set up a default fetcher with provided results. | |
4070 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | |
4071 SearchProvider::kDeletionURLFetcherID); | |
4072 ASSERT_TRUE(fetcher); | |
4073 fetcher->set_response_code(200); | |
4074 fetcher->SetResponseString(""); | |
Peter Kasting
2013/11/23 00:08:43
Nit: Is this line necessary at all? If it is, use
Maria
2013/11/26 02:36:27
Done.
| |
4075 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
4076 ASSERT_TRUE(provider_->deletion_handler_.get() == NULL); | |
Peter Kasting
2013/11/23 00:08:43
What is |deletion_handler_|? I don't see it in yo
Maria
2013/11/26 02:36:27
I would like to check whether my metric got record
Peter Kasting
2013/11/26 02:46:22
Nope. Sorry.
Peter Kasting
2013/11/27 21:58:18
OK, here's an idea. Subclass SearchProvider for t
| |
4077 } | |
4078 | |
4079 TEST_F(SearchProviderTest, TestDeleteMatch_HasDeletionUrlFailure) { | |
4080 AutocompleteMatch m = AutocompleteMatch( | |
4081 provider_, | |
4082 640, | |
4083 true, | |
4084 AutocompleteMatchType::SEARCH_SUGGEST); | |
4085 m.RecordAdditionalInfo(SearchProvider::kDeletionUrlKey, | |
4086 "https://www.google.com/complete/deleteitem?q=foo"); | |
4087 provider_->DeleteMatch(m); | |
4088 // Set up a default fetcher with provided results. | |
4089 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | |
4090 SearchProvider::kDeletionURLFetcherID); | |
4091 ASSERT_TRUE(fetcher); | |
4092 fetcher->set_response_code(500); | |
4093 fetcher->SetResponseString(""); | |
4094 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
4095 ASSERT_TRUE(provider_->deletion_handler_.get() == NULL); | |
4096 } | |
OLD | NEW |