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

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

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch back to passing pointers to answers 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/omnibox/suggestion_answer.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 TEST(SuggestionAnswerTest, DefaultAreEqual) {
11 SuggestionAnswer answer1;
12 SuggestionAnswer answer2;
13 EXPECT_TRUE(answer1.Equals(answer2));
14 }
15
16 TEST(SuggestionAnswerTest, CopiesAreEqual) {
17 SuggestionAnswer answer1;
18 EXPECT_TRUE(answer1.Equals(SuggestionAnswer(answer1)));
19
20 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
21 answer2->set_type(832345);
22 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2)));
23
24 std::string json =
25 "{ \"l\": ["
26 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
27 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
28 "] }";
29 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
30 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2)));
31 }
32
33 TEST(SuggestionAnswerTest, DifferentValuesAreUnequal) {
34 std::string json =
35 "{ \"l\": ["
36 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
37 " { \"t\": \"moar text\", \"tt\": 0 }], "
38 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
39 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
40 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
41 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
42 "] }";
43 scoped_ptr<SuggestionAnswer> answer1(new SuggestionAnswer());
44 answer1->set_type(4);
45 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer1));
46
47 // Exactly the same just a different type for one of the text type values.
48 json =
49 "{ \"l\": ["
50 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
51 " { \"t\": \"moar text\", \"tt\": 1 }], "
52 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
53 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
54 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
55 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
56 "] }";
57 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
58 answer2->set_type(4);
59 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
60
61 EXPECT_FALSE(answer1->Equals(*answer2));
62 }
63
64 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
65 std::string json;
66 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
67 answer->set_type(1);
68 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
69 }
70
71 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
72 std::string json = "} malformed json {";
73 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
74 answer->set_type(921);
75 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
76 }
77
78 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
79 std::string json =
80 "{ \"l\": ["
81 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
82 "] }";
83 scoped_ptr<SuggestionAnswer> answer1(new SuggestionAnswer());
84 answer1->set_type(894);
85 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
86
87 json =
88 "{ \"l\": ["
89 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
90 "] }";
91 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
92 answer2->set_type(12);
93 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer2));
94 }
95
96 TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) {
97 std::string json =
98 "{ \"l\": ["
99 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
100 " { \"t\": \"moar text\", \"tt\": 0 }], "
101 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
102 " { \"il\": { \"t\": [], "
103 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
104 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
105 "] }";
106 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
107 answer->set_type(205);
108 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
109 }
110
111 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
112 std::string json =
113 "{ \"l\": ["
114 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
115 "] }";
116 scoped_ptr<SuggestionAnswer> answer1(new SuggestionAnswer());
117 answer1->set_type(8943);
118 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
119
120 json =
121 "{ \"l\": ["
122 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
123 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
124 "] }";
125 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
126 answer2->set_type(4028023);
127 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
128
129 json =
130 "{ \"l\": ["
131 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
132 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
133 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } "
134 "] }";
135 scoped_ptr<SuggestionAnswer> answer3(new SuggestionAnswer());
136 answer3->set_type(23);
137 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer3));
138 }
139
140 TEST(SuggestionAnswerTest, URLPresent) {
141 std::string json =
142 "{ \"l\": ["
143 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
144 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
145 " \"i\": { \"d\": \"\" } } } "
146 "] }";
147 scoped_ptr<SuggestionAnswer> answer1(new SuggestionAnswer());
148 answer1->set_type(1);
149 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
150
151 json =
152 "{ \"l\": ["
153 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
154 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
155 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } "
156 "] }";
157 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
158 answer2->set_type(8);
159 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
160
161 json =
162 "{ \"l\": ["
163 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
164 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
165 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } "
166 "] }";
167 scoped_ptr<SuggestionAnswer> answer3(new SuggestionAnswer());
168 answer3->set_type(4);
169 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
170 }
171
172 TEST(SuggestionAnswerTest, ValidPropertyValues) {
173 std::string json =
174 "{ \"l\": ["
175 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
176 " { \"t\": \"moar text\", \"tt\": 0 }], "
177 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
178 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
179 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
180 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
181 "] }";
182 scoped_ptr<SuggestionAnswer> answer(new SuggestionAnswer());
183 answer->set_type(420527);
184 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
185
186 const SuggestionAnswer::ImageLine& first_line = answer->first_line();
187 EXPECT_EQ(2U, first_line.text_fields().size());
188 EXPECT_EQ("text", first_line.text_fields()[0].text());
189 EXPECT_EQ(8, first_line.text_fields()[0].type());
190 EXPECT_EQ("moar text", first_line.text_fields()[1].text());
191 EXPECT_EQ(0, first_line.text_fields()[1].type());
192
193 EXPECT_FALSE(first_line.additional_text());
194 EXPECT_FALSE(first_line.status_text());
195
196 EXPECT_TRUE(first_line.image_url().is_valid());
197 EXPECT_EQ("https://example.com/foo.jpg", first_line.image_url().spec());
198
199 const SuggestionAnswer::ImageLine& second_line = answer->second_line();
200 EXPECT_EQ(1U, second_line.text_fields().size());
201 EXPECT_EQ("other text", second_line.text_fields()[0].text());
202 EXPECT_EQ(5, second_line.text_fields()[0].type());
203
204 EXPECT_TRUE(second_line.additional_text());
205 EXPECT_EQ("slatfatf", second_line.additional_text()->text());
206 EXPECT_EQ(42, second_line.additional_text()->type());
207
208 EXPECT_TRUE(second_line.status_text());
209 EXPECT_EQ("oh hi, Mark", second_line.status_text()->text());
210 EXPECT_EQ(729347, second_line.status_text()->type());
211
212 EXPECT_FALSE(second_line.image_url().is_valid());
213 }
214
215 TEST(SuggestionAnswerTest, GetImageURLsWithValidImageLines) {
216 std::vector<GURL> urls;
217 std::string json =
218 "{ \"l\" : ["
219 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
220 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
221 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}";
222
223 scoped_ptr<SuggestionAnswer> answer1(new SuggestionAnswer());
224 answer1->set_type(532);
225 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer1));
226 answer1->GetImageURLs(&urls);
227 ASSERT_EQ(1U, urls.size());
228 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec());
229
230 json =
231 "{ \"l\" : ["
232 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }],"
233 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, "
234 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
235 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}";
236
237 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
238 answer2->set_type(15);
239 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
240 answer2->GetImageURLs(&urls);
241 ASSERT_EQ(2U, urls.size());
242 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec());
243 EXPECT_EQ("https://gstatic.com/bar.jpg", urls[1].spec());
244 }
OLDNEW
« components/omnibox/search_suggestion_parser.cc ('K') | « components/omnibox/suggestion_answer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698