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 2465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2476 if (suggestion == kNotApplicable) | 2476 if (suggestion == kNotApplicable) |
2477 break; | 2477 break; |
2478 if (cases[i].results[j].is_navigation_result) { | 2478 if (cases[i].results[j].is_navigation_result) { |
2479 provider_->default_results_.navigation_results.push_back( | 2479 provider_->default_results_.navigation_results.push_back( |
2480 SearchProvider::NavigationResult( | 2480 SearchProvider::NavigationResult( |
2481 *provider_.get(), GURL(suggestion), string16(), false, | 2481 *provider_.get(), GURL(suggestion), string16(), false, |
2482 cases[i].results[j].relevance, false)); | 2482 cases[i].results[j].relevance, false)); |
2483 } else { | 2483 } else { |
2484 provider_->default_results_.suggest_results.push_back( | 2484 provider_->default_results_.suggest_results.push_back( |
2485 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), | 2485 SearchProvider::SuggestResult(ASCIIToUTF16(suggestion), string16(), |
2486 string16(), std::string(), false, | 2486 string16(), std::string(), |
| 2487 std::string(), false, |
2487 cases[i].results[j].relevance, | 2488 cases[i].results[j].relevance, |
2488 false, false)); | 2489 false, false)); |
2489 } | 2490 } |
2490 } | 2491 } |
2491 | 2492 |
2492 provider_->input_ = AutocompleteInput( | 2493 provider_->input_ = AutocompleteInput( |
2493 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), | 2494 ASCIIToUTF16(cases[i].omnibox_input), string16::npos, string16(), |
2494 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, | 2495 GURL(), AutocompleteInput::INVALID_SPEC, false, false, true, |
2495 AutocompleteInput::ALL_MATCHES); | 2496 AutocompleteInput::ALL_MATCHES); |
2496 provider_->RemoveAllStaleResults(); | 2497 provider_->RemoveAllStaleResults(); |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2953 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 2954 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
2954 } | 2955 } |
2955 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { | 2956 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
2956 SCOPED_TRACE("and match: " + base::IntToString(j)); | 2957 SCOPED_TRACE("and match: " + base::IntToString(j)); |
2957 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); | 2958 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
2958 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); | 2959 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
2959 } | 2960 } |
2960 } | 2961 } |
2961 } | 2962 } |
2962 | 2963 |
| 2964 // Test that XSRF token gets set on an AutocompleteMatch when available for a |
| 2965 // personalized query. |
| 2966 TEST_F(SearchProviderTest, ParseXsrfToken) { |
| 2967 struct Match { |
| 2968 std::string contents; |
| 2969 std::string xsrf_token; |
| 2970 AutocompleteMatchType::Type type; |
| 2971 }; |
| 2972 |
| 2973 const Match kEmptyMatch = { |
| 2974 kNotApplicable, "", AutocompleteMatchType::NUM_TYPES}; |
| 2975 |
| 2976 struct { |
| 2977 const std::string input_text; |
| 2978 const std::string response_json; |
| 2979 const Match matches[4]; |
| 2980 } cases[] = { |
| 2981 // Personalized query comes with an XSRF token |
| 2982 { "a", |
| 2983 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 2984 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 2985 "\"google:suggestrelevance\":[1, 2]," |
| 2986 "\"google:suggestdetail\":[{\"x\":\"xsrf123\"}, {}]}]", |
| 2987 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 2988 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2989 { "ab", "xsrf123", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2990 kEmptyMatch, |
| 2991 }, |
| 2992 }, |
| 2993 // Personalized query, but no xsrf token in response -- old server case |
| 2994 { "a", |
| 2995 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 2996 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 2997 "\"google:suggestrelevance\":[1, 2]," |
| 2998 "\"google:suggestdetail\":[{}, {}]}]", |
| 2999 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3000 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3001 { "ab", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3002 kEmptyMatch, |
| 3003 }, |
| 3004 }, |
| 3005 // Unexpected: non-personalized query comes with an xsrf token, this |
| 3006 // should be ignored. |
| 3007 { "a", |
| 3008 "[\"a\",[\"ab\", \"ac\"],[],[]," |
| 3009 "{\"google:suggesttype\":[\"PERSONALIZED_QUERY\",\"QUERY\"]," |
| 3010 "\"google:suggestrelevance\":[1, 2]," |
| 3011 "\"google:suggestdetail\":[{\"x\":\"xsrf123\"}, {\"x\":\"xsrf\"}]}]", |
| 3012 { { "a", "", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 3013 { "ac", "", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3014 { "ab", "xsrf123", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 3015 kEmptyMatch, |
| 3016 }, |
| 3017 }, |
| 3018 }; |
| 3019 |
| 3020 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 3021 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); |
| 3022 |
| 3023 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3024 SearchProvider::kDefaultProviderURLFetcherID); |
| 3025 ASSERT_TRUE(fetcher); |
| 3026 fetcher->set_response_code(200); |
| 3027 fetcher->SetResponseString(cases[i].response_json); |
| 3028 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3029 |
| 3030 RunTillProviderDone(); |
| 3031 |
| 3032 const ACMatches& matches = provider_->matches(); |
| 3033 ASSERT_FALSE(matches.empty()); |
| 3034 |
| 3035 SCOPED_TRACE("for input with json = " + cases[i].response_json); |
| 3036 |
| 3037 for (size_t j = 0; j < matches.size(); ++j) { |
| 3038 const Match& match = cases[i].matches[j]; |
| 3039 SCOPED_TRACE(" and match index: " + base::IntToString(j)); |
| 3040 EXPECT_EQ(match.contents, UTF16ToUTF8(matches[j].contents)); |
| 3041 EXPECT_EQ(match.xsrf_token, matches[j].search_terms_args->xsrf_token); |
| 3042 } |
| 3043 } |
| 3044 } |
2963 | 3045 |
2964 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { | 3046 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { |
2965 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); | 3047 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); |
2966 string16 term = term1_.substr(0, term1_.length() - 1); | 3048 string16 term = term1_.substr(0, term1_.length() - 1); |
2967 QueryForInput(term, true, false); | 3049 QueryForInput(term, true, false); |
2968 ASSERT_FALSE(provider_->matches().empty()); | 3050 ASSERT_FALSE(provider_->matches().empty()); |
2969 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3051 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
2970 provider_->matches()[0].type); | 3052 provider_->matches()[0].type); |
2971 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 3053 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
2972 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 3054 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
2973 | 3055 |
2974 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); | 3056 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); |
2975 term = term1_.substr(0, term1_.length() - 1); | 3057 term = term1_.substr(0, term1_.length() - 1); |
2976 QueryForInput(term, true, false); | 3058 QueryForInput(term, true, false); |
2977 ASSERT_FALSE(provider_->matches().empty()); | 3059 ASSERT_FALSE(provider_->matches().empty()); |
2978 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 3060 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
2979 provider_->matches()[0].type); | 3061 provider_->matches()[0].type); |
2980 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 3062 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
2981 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 3063 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
2982 } | 3064 } |
OLD | NEW |