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

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: Tweaked API and finished unit tests 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 "testing/gtest/include/gtest/gtest.h"
8
9 TEST(SuggestionAnswerTest, DefaultAreEqual) {
10 SuggestionAnswer answer1;
11 SuggestionAnswer answer2;
12 EXPECT_EQ(answer1, answer2);
13 }
14
15 TEST(SuggestionAnswerTest, CopiesAreEqual) {
16 SuggestionAnswer answer1;
17 SuggestionAnswer answer1copy(answer1);
18 EXPECT_EQ(answer1, answer1copy);
19
20 SuggestionAnswer answer2;
21 answer2.SetType("832345");
22 SuggestionAnswer answer2copy1(answer2);
23 EXPECT_EQ(answer2, answer2copy1);
24
25 std::string json =
26 "{ \"l\": ["
27 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
28 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
29 "] }";
30 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
31 EXPECT_TRUE(answer2.is_valid());
32 SuggestionAnswer answer2copy2(answer2);
33 EXPECT_EQ(answer2, answer2copy2);
34
35 json =
36 "{ \"l\": [ "
37 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
38 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
39 "] }";
40 SuggestionAnswer answer3;
41 answer3.SetType("84");
42 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
43 EXPECT_TRUE(answer3.is_valid());
44 SuggestionAnswer answer3copy(answer3);
45 EXPECT_EQ(answer3, answer3copy);
46 }
47
48 TEST(SuggestionAnswerTest, EmptyAnswerIsInvalid) {
49 SuggestionAnswer answer;
50 EXPECT_FALSE(answer.is_valid());
51 }
52
53 TEST(SuggestionAnswerTest, AnswerWithTypeOnlyIsInvalid) {
54 SuggestionAnswer answer;
55 answer.SetType("2940");
56 EXPECT_FALSE(answer.is_valid());
57 }
58
59 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
60 std::string json = "";
61 SuggestionAnswer answer;
62 answer.SetType("1");
63 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
64 EXPECT_FALSE(answer.is_valid());
65 }
66
67 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
68 std::string json = "} malformed json {";
69 SuggestionAnswer answer;
70 answer.SetType("921");
71 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
72 EXPECT_FALSE(answer.is_valid());
73 }
74
75 TEST(SuggestionAnswerTest, AnswerWithoutTypeIsInvalid) {
76 std::string json =
77 "{ \"l\": [ "
78 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
79 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
80 "] }";
81 SuggestionAnswer answer;
82 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
83 EXPECT_FALSE(answer.is_valid());
84 }
85
86 TEST(SuggestionAnswerTest, TypeMustBeNonNegativeToBeValid) {
87 std::string json =
88 "{ \"l\": [ "
89 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
90 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
91 "] }";
92 SuggestionAnswer answer;
93 answer.SetType("-238");
groby-ooo-7-16 2014/10/23 00:46:20 Do we need all the tests? Do they actually cover d
Justin Donnelly 2014/10/23 17:59:37 You could definitely argue this is overkill but I
94 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
95 EXPECT_FALSE(answer.is_valid());
96
97 answer.SetType("-1");
98 EXPECT_FALSE(answer.is_valid());
99
100 answer.SetType("-14982");
101 EXPECT_FALSE(answer.is_valid());
102
103 answer.SetType("0");
104 EXPECT_TRUE(answer.is_valid());
105
106 answer.SetType("1");
107 EXPECT_TRUE(answer.is_valid());
108
109 answer.SetType("3284327");
110 EXPECT_TRUE(answer.is_valid());
111 }
112
113 TEST(SuggestionAnswerTest, ClearedAnswerIsInvalid) {
114 std::string json =
115 "{ \"l\": [ "
116 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
117 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
118 "] }";
119 SuggestionAnswer answer;
120 answer.SetType("921");
121 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
122 EXPECT_TRUE(answer.is_valid());
123
124 answer.Clear();
125 EXPECT_FALSE(answer.is_valid());
126 }
127
128 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
129 std::string json =
130 "{ \"l\": [ "
131 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
132 "] }";
133 SuggestionAnswer answer1;
134 answer1.SetType("894");
135 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
136 EXPECT_FALSE(answer1.is_valid());
137
138 json =
139 "{ \"l\": [ "
140 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
141 "] }";
142 SuggestionAnswer answer2;
143 answer2.SetType("12");
144 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer2));
145 EXPECT_FALSE(answer2.is_valid());
146 }
147
148 TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) {
149 std::string json =
150 "{ \"l\": [ "
151 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
152 " { \"t\": \"moar text\", \"tt\": 0 }], "
153 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
154 " { \"il\": { \"t\": [], "
155 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
156 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
157 "] }";
158 SuggestionAnswer answer;
159 answer.SetType("205");
160 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
161 EXPECT_FALSE(answer.is_valid());
162 }
163
164 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
165 std::string json =
166 "{ \"l\": [ "
167 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
168 "] }";
169 SuggestionAnswer answer1;
170 answer1.SetType("8943");
171 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
172 EXPECT_FALSE(answer1.is_valid());
173
174 json =
175 "{ \"l\": [ "
176 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
177 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
178 "] }";
179 SuggestionAnswer answer2;
180 answer2.SetType("4028023");
181 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
182 EXPECT_TRUE(answer2.is_valid());
183
184 json =
185 "{ \"l\": [ "
186 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
187 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
188 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } "
189 "] }";
190 SuggestionAnswer answer3;
191 answer3.SetType("23");
192 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer3));
193 EXPECT_FALSE(answer3.is_valid());
194 }
195
196 TEST(SuggestionAnswerTest, URLPresent) {
197 std::string json =
198 "{ \"l\": [ "
199 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
200 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
201 " \"i\": { \"d\": \"\" } } } "
202 "] }";
203 SuggestionAnswer answer1;
204 answer1.SetType("1");
205 EXPECT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
206 EXPECT_FALSE(answer1.is_valid());
207
208 json =
209 "{ \"l\": [ "
210 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
211 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
212 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } "
213 "] }";
214 SuggestionAnswer answer2;
215 answer2.SetType("8");
216 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
217 EXPECT_TRUE(answer2.is_valid());
218
219 json =
220 "{ \"l\": [ "
221 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
222 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
223 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } "
224 "] }";
225 SuggestionAnswer answer3;
226 answer3.SetType("4");
227 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
228 EXPECT_TRUE(answer3.is_valid());
229 }
230
231 TEST(SuggestionAnswerTest, ValidPropertyValues) {
232 std::string json =
233 "{ \"l\": [ "
234 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
235 " { \"t\": \"moar text\", \"tt\": 0 }], "
236 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
237 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
238 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
239 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
240 "] }";
241 SuggestionAnswer answer;
242 answer.SetType("420527");
243 EXPECT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
244 EXPECT_TRUE(answer.is_valid());
245
246 const SuggestionAnswer::ImageLine& first_line = answer.first_line();
247 EXPECT_EQ(2U, first_line.text_fields().size());
248 EXPECT_EQ("text", first_line.text_fields()[0].text());
249 EXPECT_EQ(8, first_line.text_fields()[0].type());
250 EXPECT_EQ("moar text", first_line.text_fields()[1].text());
251 EXPECT_EQ(0, first_line.text_fields()[1].type());
252
253 EXPECT_FALSE(first_line.has_additional_text());
254 EXPECT_FALSE(first_line.has_status_text());
255
256 EXPECT_TRUE(first_line.has_image_url());
257 EXPECT_EQ("https://example.com/foo.jpg", first_line.image_url().spec());
258
259 const SuggestionAnswer::ImageLine& second_line = answer.second_line();
260 EXPECT_EQ(1U, second_line.text_fields().size());
261 EXPECT_EQ("other text", second_line.text_fields()[0].text());
262 EXPECT_EQ(5, second_line.text_fields()[0].type());
263
264 EXPECT_TRUE(second_line.has_additional_text());
265 EXPECT_EQ("slatfatf", second_line.additional_text().text());
266 EXPECT_EQ(42, second_line.additional_text().type());
267
268 EXPECT_TRUE(second_line.has_status_text());
269 EXPECT_EQ("oh hi, Mark", second_line.status_text().text());
270 EXPECT_EQ(729347, second_line.status_text().type());
271
272 EXPECT_FALSE(second_line.has_image_url());
273 }
OLDNEW
« components/omnibox/suggestion_answer.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