Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(355)

Side by Side Diff: chrome/browser/autocomplete/search_provider_unittest.cc

Issue 45863006: Parse out XSSI guards in Suggestion JSON response (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, reload, retry Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/autocomplete/search_provider.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 void QueryForInputAndSetWYTMatch(const string16& text, 130 void QueryForInputAndSetWYTMatch(const string16& text,
131 AutocompleteMatch* wyt_match); 131 AutocompleteMatch* wyt_match);
132 132
133 // Notifies the URLFetcher for the suggest query corresponding to the default 133 // Notifies the URLFetcher for the suggest query corresponding to the default
134 // search provider that it's done. 134 // search provider that it's done.
135 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE. 135 // Be sure and wrap calls to this in ASSERT_NO_FATAL_FAILURE.
136 void FinishDefaultSuggestQuery(); 136 void FinishDefaultSuggestQuery();
137 137
138 void ResetFieldTrialList(); 138 void ResetFieldTrialList();
139 139
140 void ClearAllResults();
141
140 // See description above class for details of these fields. 142 // See description above class for details of these fields.
141 TemplateURL* default_t_url_; 143 TemplateURL* default_t_url_;
142 const string16 term1_; 144 const string16 term1_;
143 GURL term1_url_; 145 GURL term1_url_;
144 TemplateURL* keyword_t_url_; 146 TemplateURL* keyword_t_url_;
145 const string16 keyword_term_; 147 const string16 keyword_term_;
146 GURL keyword_url_; 148 GURL keyword_url_;
147 149
148 content::TestBrowserThreadBundle thread_bundle_; 150 content::TestBrowserThreadBundle thread_bundle_;
149 151
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 // a DCHECK. 360 // a DCHECK.
359 field_trial_list_.reset(); 361 field_trial_list_.reset();
360 field_trial_list_.reset(new base::FieldTrialList( 362 field_trial_list_.reset(new base::FieldTrialList(
361 new metrics::SHA1EntropyProvider("foo"))); 363 new metrics::SHA1EntropyProvider("foo")));
362 chrome_variations::testing::ClearAllVariationParams(); 364 chrome_variations::testing::ClearAllVariationParams();
363 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( 365 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
364 "AutocompleteDynamicTrial_0", "DefaultGroup"); 366 "AutocompleteDynamicTrial_0", "DefaultGroup");
365 trial->group(); 367 trial->group();
366 } 368 }
367 369
370 void SearchProviderTest::ClearAllResults() {
371 provider_->ClearAllResults();
372 }
373
368 // Actual Tests --------------------------------------------------------------- 374 // Actual Tests ---------------------------------------------------------------
369 375
370 // Make sure we query history for the default provider and a URLFetcher is 376 // Make sure we query history for the default provider and a URLFetcher is
371 // created for the default provider suggest results. 377 // created for the default provider suggest results.
372 TEST_F(SearchProviderTest, QueryDefaultProvider) { 378 TEST_F(SearchProviderTest, QueryDefaultProvider) {
373 string16 term = term1_.substr(0, term1_.length() - 1); 379 string16 term = term1_.substr(0, term1_.length() - 1);
374 QueryForInput(term, false, false); 380 QueryForInput(term, false, false);
375 381
376 // Make sure the default providers suggest service was queried. 382 // Make sure the default providers suggest service was queried.
377 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( 383 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID(
(...skipping 2483 matching lines...) Expand 10 before | Expand all | Expand 10 after
2861 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents)); 2867 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents));
2862 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched, 2868 EXPECT_EQ(cases[i].matches[j].allowed_to_be_prefetched,
2863 SearchProvider::ShouldPrefetch(matches[j])); 2869 SearchProvider::ShouldPrefetch(matches[j]));
2864 EXPECT_EQ(cases[i].matches[j].type, matches[j].type); 2870 EXPECT_EQ(cases[i].matches[j].type, matches[j].type);
2865 EXPECT_EQ(cases[i].matches[j].from_keyword, 2871 EXPECT_EQ(cases[i].matches[j].from_keyword,
2866 matches[j].keyword == ASCIIToUTF16("k")); 2872 matches[j].keyword == ASCIIToUTF16("k"));
2867 } 2873 }
2868 } 2874 }
2869 } 2875 }
2870 2876
2877 // A basic test that verifies that the XSSI guarded JSON response is parsed
2878 // correctly.
2879 TEST_F(SearchProviderTest, XSSIGuardedJSONParsing) {
2880 struct Match {
2881 std::string contents;
2882 AutocompleteMatchType::Type type;
2883 };
2884 const Match kEmptyMatch = { kNotApplicable,
2885 AutocompleteMatchType::NUM_TYPES};
2886
2887 struct {
2888 const std::string input_text;
2889 const std::string default_provider_response_json;
2890 const Match matches[4];
2891 } cases[] = {
2892 // No XSSI guard.
2893 { "a",
2894 "[\"a\",[\"b\", \"c\"],[],[],"
2895 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"],"
2896 "\"google:suggestrelevance\":[1, 2]}]",
2897 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED },
2898 { "c", AutocompleteMatchType::SEARCH_SUGGEST },
2899 { "b", AutocompleteMatchType::SEARCH_SUGGEST },
2900 kEmptyMatch,
2901 },
2902 },
2903 // Standard XSSI guard - )]}'\n.
2904 { "a",
2905 ")]}'\n[\"a\",[\"b\", \"c\"],[],[],"
2906 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"],"
2907 "\"google:suggestrelevance\":[1, 2]}]",
2908 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED },
2909 { "c", AutocompleteMatchType::SEARCH_SUGGEST },
2910 { "b", AutocompleteMatchType::SEARCH_SUGGEST },
2911 kEmptyMatch,
2912 },
2913 },
2914 // Modified XSSI guard - contains "[".
2915 { "a",
2916 ")]}'\n[)\"[\"a\",[\"b\", \"c\"],[],[],"
2917 "{\"google:suggesttype\":[\"QUERY\",\"QUERY\"],"
2918 "\"google:suggestrelevance\":[1, 2]}]",
2919 { { "a", AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED },
2920 { "c", AutocompleteMatchType::SEARCH_SUGGEST },
2921 { "b", AutocompleteMatchType::SEARCH_SUGGEST },
2922 kEmptyMatch,
2923 },
2924 },
2925 };
2926
2927 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
2928 ClearAllResults();
2929 QueryForInput(ASCIIToUTF16(cases[i].input_text), false, false);
2930
2931 // Set up a default fetcher with provided results.
2932 net::TestURLFetcher* fetcher =
2933 test_factory_.GetFetcherByID(
2934 SearchProvider::kDefaultProviderURLFetcherID);
2935 ASSERT_TRUE(fetcher);
2936 fetcher->set_response_code(200);
2937 fetcher->SetResponseString(cases[i].default_provider_response_json);
2938 fetcher->delegate()->OnURLFetchComplete(fetcher);
2939
2940 RunTillProviderDone();
2941
2942 const ACMatches& matches = provider_->matches();
2943 // The top match must inline and score as highly as calculated verbatim.
2944 ASSERT_FALSE(matches.empty());
2945 EXPECT_GE(matches[0].relevance, 1300);
2946
2947 SCOPED_TRACE("for case: " + base::IntToString(i));
2948 size_t j = 0;
2949 // Ensure that the returned matches equal the expectations.
2950 for (; j < matches.size(); ++j) {
2951 SCOPED_TRACE("and match: " + base::IntToString(j));
2952 EXPECT_EQ(cases[i].matches[j].contents, UTF16ToUTF8(matches[j].contents));
2953 EXPECT_EQ(cases[i].matches[j].type, matches[j].type);
2954 }
2955 for (; j < ARRAYSIZE_UNSAFE(cases[i].matches); ++j) {
2956 SCOPED_TRACE("and match: " + base::IntToString(j));
2957 EXPECT_EQ(cases[i].matches[j].contents, kNotApplicable);
2958 EXPECT_EQ(cases[i].matches[j].type, AutocompleteMatchType::NUM_TYPES);
2959 }
2960 }
2961 }
2962
2963
2871 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) { 2964 TEST_F(SearchProviderTest, ReflectsBookmarkBarState) {
2872 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false); 2965 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false);
2873 string16 term = term1_.substr(0, term1_.length() - 1); 2966 string16 term = term1_.substr(0, term1_.length() - 1);
2874 QueryForInput(term, true, false); 2967 QueryForInput(term, true, false);
2875 ASSERT_FALSE(provider_->matches().empty()); 2968 ASSERT_FALSE(provider_->matches().empty());
2876 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 2969 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
2877 provider_->matches()[0].type); 2970 provider_->matches()[0].type);
2878 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); 2971 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL);
2879 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); 2972 EXPECT_FALSE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned);
2880 2973
2881 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); 2974 profile_.GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
2882 term = term1_.substr(0, term1_.length() - 1); 2975 term = term1_.substr(0, term1_.length() - 1);
2883 QueryForInput(term, true, false); 2976 QueryForInput(term, true, false);
2884 ASSERT_FALSE(provider_->matches().empty()); 2977 ASSERT_FALSE(provider_->matches().empty());
2885 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 2978 EXPECT_EQ(AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
2886 provider_->matches()[0].type); 2979 provider_->matches()[0].type);
2887 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL); 2980 ASSERT_TRUE(provider_->matches()[0].search_terms_args != NULL);
2888 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned); 2981 EXPECT_TRUE(provider_->matches()[0].search_terms_args->bookmark_bar_pinned);
2889 } 2982 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/search_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698