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

Side by Side Diff: components/omnibox/base_search_provider_unittest.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to comments, fix a copy bug Created 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/omnibox/base_search_provider.h" 5 #include "components/omnibox/base_search_provider.h"
6 6
7 #include "base/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/autocomplete_match.h" 9 #include "components/omnibox/autocomplete_match.h"
10 #include "components/omnibox/autocomplete_match_type.h" 10 #include "components/omnibox/autocomplete_match_type.h"
11 #include "components/omnibox/autocomplete_provider_client.h" 11 #include "components/omnibox/autocomplete_provider_client.h"
12 #include "components/omnibox/autocomplete_scheme_classifier.h" 12 #include "components/omnibox/autocomplete_scheme_classifier.h"
13 #include "components/omnibox/search_suggestion_parser.h" 13 #include "components/omnibox/search_suggestion_parser.h"
14 #include "components/omnibox/suggestion_answer.h"
14 #include "components/search_engines/search_terms_data.h" 15 #include "components/search_engines/search_terms_data.h"
15 #include "components/search_engines/template_url_service.h" 16 #include "components/search_engines/template_url_service.h"
16 #include "components/search_engines/template_url_service_client.h" 17 #include "components/search_engines/template_url_service_client.h"
17 #include "testing/gmock/include/gmock/gmock.h" 18 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 using testing::NiceMock; 21 using testing::NiceMock;
21 using testing::Return; 22 using testing::Return;
22 using testing::_; 23 using testing::_;
23 24
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 118
118 TEST_F(BaseSearchProviderTest, PreserveAnswersWhenDeduplicating) { 119 TEST_F(BaseSearchProviderTest, PreserveAnswersWhenDeduplicating) {
119 TemplateURLData data; 120 TemplateURLData data;
120 data.SetURL("http://foo.com/url?bar={searchTerms}"); 121 data.SetURL("http://foo.com/url?bar={searchTerms}");
121 scoped_ptr<TemplateURL> template_url(new TemplateURL(data)); 122 scoped_ptr<TemplateURL> template_url(new TemplateURL(data));
122 123
123 TestBaseSearchProvider::MatchMap map; 124 TestBaseSearchProvider::MatchMap map;
124 base::string16 query = base::ASCIIToUTF16("weather los angeles"); 125 base::string16 query = base::ASCIIToUTF16("weather los angeles");
125 base::string16 answer_contents = base::ASCIIToUTF16("some answer content"); 126 base::string16 answer_contents = base::ASCIIToUTF16("some answer content");
126 base::string16 answer_type = base::ASCIIToUTF16("2334"); 127 base::string16 answer_type = base::ASCIIToUTF16("2334");
128 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
129 answer->set_type(2334);
127 130
128 EXPECT_CALL(*provider_, GetInput(_)) 131 EXPECT_CALL(*provider_, GetInput(_))
129 .WillRepeatedly(Return(AutocompleteInput())); 132 .WillRepeatedly(Return(AutocompleteInput()));
130 EXPECT_CALL(*provider_, GetTemplateURL(_)) 133 EXPECT_CALL(*provider_, GetTemplateURL(_))
131 .WillRepeatedly(Return(template_url.get())); 134 .WillRepeatedly(Return(template_url.get()));
132 135
133 SearchSuggestionParser::SuggestResult more_relevant( 136 SearchSuggestionParser::SuggestResult more_relevant(
134 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(), 137 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(),
135 base::string16(), base::string16(), base::string16(), std::string(), 138 base::string16(), base::string16(), base::string16(), nullptr,
136 std::string(), false, 1300, true, false, query); 139 std::string(), std::string(), false, 1300, true, false, query);
137 provider_->AddMatchToMap( 140 provider_->AddMatchToMap(
138 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN, 141 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
139 false, false, &map); 142 false, false, &map);
140 143
141 SearchSuggestionParser::SuggestResult less_relevant( 144 SearchSuggestionParser::SuggestResult less_relevant(
142 query, AutocompleteMatchType::SEARCH_SUGGEST, query, base::string16(), 145 query, AutocompleteMatchType::SEARCH_SUGGEST, query, base::string16(),
143 base::string16(), answer_contents, answer_type, std::string(), 146 base::string16(), answer_contents, answer_type, answer.Pass(),
groby-ooo-7-16 2014/10/30 20:28:29 If you use SuggestionAnswer::copy here, you save y
Justin Donnelly 2014/10/30 21:38:03 Done.
144 std::string(), false, 850, true, false, query); 147 std::string(), std::string(), false, 850, true, false, query);
145 provider_->AddMatchToMap( 148 provider_->AddMatchToMap(
146 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN, 149 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
147 false, false, &map); 150 false, false, &map);
148 151
149 ASSERT_EQ(1U, map.size()); 152 ASSERT_EQ(1U, map.size());
150 AutocompleteMatch match = map.begin()->second; 153 AutocompleteMatch match = map.begin()->second;
151 ASSERT_EQ(1U, match.duplicate_matches.size()); 154 ASSERT_EQ(1U, match.duplicate_matches.size());
152 AutocompleteMatch duplicate = match.duplicate_matches[0]; 155 AutocompleteMatch duplicate = match.duplicate_matches[0];
153 156
157 // Setup |answer| again since it was reset by the Pass() above.
158 answer.reset(new SuggestionAnswer());
159 answer->set_type(2334);
154 EXPECT_EQ(answer_contents, match.answer_contents); 160 EXPECT_EQ(answer_contents, match.answer_contents);
155 EXPECT_EQ(answer_type, match.answer_type); 161 EXPECT_EQ(answer_type, match.answer_type);
162 EXPECT_TRUE(answer->Equals(*match.answer));
156 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type); 163 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type);
157 EXPECT_EQ(1300, match.relevance); 164 EXPECT_EQ(1300, match.relevance);
158 165
159 EXPECT_EQ(answer_contents, duplicate.answer_contents); 166 EXPECT_EQ(answer_contents, duplicate.answer_contents);
160 EXPECT_EQ(answer_type, duplicate.answer_type); 167 EXPECT_EQ(answer_type, duplicate.answer_type);
168 EXPECT_TRUE(answer->Equals(*duplicate.answer));
161 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type); 169 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type);
162 EXPECT_EQ(850, duplicate.relevance); 170 EXPECT_EQ(850, duplicate.relevance);
163 171
164 // Ensure answers are not copied over existing answers. 172 // Ensure answers are not copied over existing answers.
165 map.clear(); 173 map.clear();
166 base::string16 answer_contents2 = base::ASCIIToUTF16("different answer"); 174 base::string16 answer_contents2 = base::ASCIIToUTF16("different answer");
175 base::string16 answer_type2 = base::ASCIIToUTF16("8242");
176 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
177 answer2->set_type(8242);
167 more_relevant = SearchSuggestionParser::SuggestResult( 178 more_relevant = SearchSuggestionParser::SuggestResult(
168 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(), 179 query, AutocompleteMatchType::SEARCH_HISTORY, query, base::string16(),
169 base::string16(), answer_contents2, answer_type, std::string(), 180 base::string16(), answer_contents2, answer_type2, answer2.Pass(),
groby-ooo-7-16 2014/10/30 20:28:29 See above.
Justin Donnelly 2014/10/30 21:38:03 Done.
170 std::string(), false, 1300, true, false, query); 181 std::string(), std::string(), false, 1300, true, false, query);
171 provider_->AddMatchToMap( 182 provider_->AddMatchToMap(
172 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN, 183 more_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
173 false, false, &map); 184 false, false, &map);
174 provider_->AddMatchToMap( 185 provider_->AddMatchToMap(
175 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN, 186 less_relevant, std::string(), TemplateURLRef::NO_SUGGESTION_CHOSEN,
176 false, false, &map); 187 false, false, &map);
177 ASSERT_EQ(1U, map.size()); 188 ASSERT_EQ(1U, map.size());
178 match = map.begin()->second; 189 match = map.begin()->second;
179 ASSERT_EQ(1U, match.duplicate_matches.size()); 190 ASSERT_EQ(1U, match.duplicate_matches.size());
180 duplicate = match.duplicate_matches[0]; 191 duplicate = match.duplicate_matches[0];
181 192
193 // Setup |answer2| again since it was reset by the Pass() above.
194 answer2.reset(new SuggestionAnswer());
195 answer2->set_type(8242);
182 EXPECT_EQ(answer_contents2, match.answer_contents); 196 EXPECT_EQ(answer_contents2, match.answer_contents);
183 EXPECT_EQ(answer_type, match.answer_type); 197 EXPECT_EQ(answer_type2, match.answer_type);
198 EXPECT_TRUE(answer2->Equals(*match.answer));
184 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type); 199 EXPECT_EQ(AutocompleteMatchType::SEARCH_HISTORY, match.type);
185 EXPECT_EQ(1300, match.relevance); 200 EXPECT_EQ(1300, match.relevance);
186 201
187 EXPECT_EQ(answer_contents, duplicate.answer_contents); 202 EXPECT_EQ(answer_contents, duplicate.answer_contents);
188 EXPECT_EQ(answer_type, duplicate.answer_type); 203 EXPECT_EQ(answer_type, duplicate.answer_type);
204 EXPECT_TRUE(answer->Equals(*duplicate.answer));
189 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type); 205 EXPECT_EQ(AutocompleteMatchType::SEARCH_SUGGEST, duplicate.type);
190 EXPECT_EQ(850, duplicate.relevance); 206 EXPECT_EQ(850, duplicate.relevance);
191
192 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698