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 2850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2861 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents)); | 2861 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents)); |
2862 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched, | 2862 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched, |
2863 SearchProvider::ShouldPrefetch(matches[j])); | 2863 SearchProvider::ShouldPrefetch(matches[j])); |
2864 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); | 2864 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
2865 EXPECT_EQ(cases[i].matches[j].from_keyword, | 2865 EXPECT_EQ(cases[i].matches[j].from_keyword, |
2866 matches[j].keyword == ASCIIToUTF16("k")); | 2866 matches[j].keyword == ASCIIToUTF16("k")); |
2867 } | 2867 } |
2868 } | 2868 } |
2869 } | 2869 } |
2870 | 2870 |
| 2871 // A basic test that verifies that the XSSI guarded JSON response is parsed |
| 2872 // correctly. |
| 2873 TEST_F(SearchProviderTest, XSSIGuardedJSONParsing) { |
| 2874 struct Match { |
| 2875 std::string contents; |
| 2876 AutocompleteMatchType::Type type; |
| 2877 }; |
| 2878 const Match kEmptyMatch = { kNotApplicable, |
| 2879 AutocompleteMatchType::NUM_TYPES}; |
| 2880 |
| 2881 struct { |
| 2882 const std::string input_text; |
| 2883 const std::string default_provider_response_json; |
| 2884 const Match matches[4]; |
| 2885 } cases[] = { |
| 2886 // No XSSI guard. |
| 2887 { "a", |
| 2888 "[\"a\",[\"b\", \"c\"],[],[]," |
| 2889 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"]}]", |
| 2890 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 2891 { "c", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2892 { "b", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2893 kEmptyMatch, |
| 2894 }, |
| 2895 }, |
| 2896 // Standard XSSI guard - )]}'\n. |
| 2897 { "a", |
| 2898 ")]}'\n[\"a\",[\"b\", \"c\"],[],[]," |
| 2899 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"]}]", |
| 2900 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 2901 { "c", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2902 { "b", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2903 kEmptyMatch, |
| 2904 }, |
| 2905 }, |
| 2906 // Modified XSSI guard - contains "[". |
| 2907 { "a", |
| 2908 ")]}'\n[)\"[\"a\",[\"b\", \"c\"],[],[]," |
| 2909 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"]}]", |
| 2910 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED }, |
| 2911 { "c", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2912 { "b", AutocompleteMatchType::SEARCH_SUGGEST }, |
| 2913 kEmptyMatch, |
| 2914 }, |
| 2915 }, |
| 2916 }; |
| 2917 |
| 2918 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) { |
| 2919 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false); |
| 2920 |
| 2921 // Set up a default fetcher with provided results. |
| 2922 net::TestURLFetcher* fetcher = |
| 2923 test_factory_.GetFetcherByID( |
| 2924 SearchProvider::kDefaultProviderURLFetcherID); |
| 2925 ASSERT_TRUE(fetcher); |
| 2926 fetcher->set_response_code(200); |
| 2927 fetcher->SetResponseString(cases[i].default_provider_response_json); |
| 2928 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 2929 |
| 2930 RunTillProviderDone(); |
| 2931 |
| 2932 const ACMatches& matches = provider_->matches(); |
| 2933 // The top match must inline and score as highly as calculated verbatim. |
| 2934 ASSERT_FALSE(matches.empty()); |
| 2935 EXPECT_GE(matches[0].relevance, 1300); |
| 2936 |
| 2937 SCOPED_TRACE("for case: " + base::IntToString(i)); |
| 2938 size_t j = 0; |
| 2939 // Ensure that the returned matches equal the expectations. |
| 2940 for (; j < matches.size(); ++j) { |
| 2941 SCOPED_TRACE("and match: " + base::IntToString(j)); |
| 2942 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents)); |
| 2943 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); |
| 2944 } |
| 2945 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) { |
| 2946 SCOPED_TRACE("and match: " + base::IntToString(j)); |
| 2947 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable); |
| 2948 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES); |
| 2949 } |
| 2950 } |
| 2951 } |
| 2952 |
| 2953 |
2871 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { | 2954 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { |
2872 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); | 2955 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); |
2873 string16 term = term1_.substr(0, term1_.length() - 1); | 2956 string16 term = term1_.substr(0, term1_.length() - 1); |
2874 QueryForInput(term, true, false); | 2957 QueryForInput(term, true, false); |
2875 ASSERT_FALSE(provider_->matches().empty()); | 2958 ASSERT_FALSE(provider_->matches().empty()); |
2876 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 2959 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
2877 provider_->matches()[0].type); | 2960 provider_->matches()[0].type); |
2878 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 2961 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
2879 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 2962 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
2880 | 2963 |
2881 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); | 2964 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); |
2882 term = term1_.substr(0, term1_.length() - 1); | 2965 term = term1_.substr(0, term1_.length() - 1); |
2883 QueryForInput(term, true, false); | 2966 QueryForInput(term, true, false); |
2884 ASSERT_FALSE(provider_->matches().empty()); | 2967 ASSERT_FALSE(provider_->matches().empty()); |
2885 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, | 2968 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, |
2886 provider_->matches()[0].type); | 2969 provider_->matches()[0].type); |
2887 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); | 2970 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); |
2888 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); | 2971 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); |
2889 } | 2972 } |
OLD | NEW |