OLD | NEW |
---|---|
(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_EQ(answer1, answer2); | |
14 } | |
15 | |
16 TEST(SuggestionAnswerTest, CopiesAreEqual) { | |
17 SuggestionAnswer answer1; | |
18 SuggestionAnswer answer1copy(answer1); | |
19 EXPECT_EQ(answer1, answer1copy); | |
20 | |
21 SuggestionAnswer answer2; | |
22 answer2.SetType(base::ASCIIToUTF16("832345")); | |
23 SuggestionAnswer answer2copy1(answer2); | |
24 EXPECT_EQ(answer2, answer2copy1); | |
Peter Kasting
2014/10/24 22:38:11
Nit: Instead of answer2 + answer2copy1 and answer2
Justin Donnelly
2014/10/27 21:41:16
Done.
| |
25 | |
26 std::string json = | |
27 "{ \"l\": [" | |
28 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
29 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
30 "] }"; | |
31 SuggestionAnswer::ParseAnswer(json, &answer2); | |
32 EXPECT_TRUE(answer2.is_valid()); | |
33 SuggestionAnswer answer2copy2(answer2); | |
34 EXPECT_EQ(answer2, answer2copy2); | |
35 } | |
36 | |
37 TEST(SuggestionAnswerTest, DifferentValusAreUnequal) { | |
Peter Kasting
2014/10/24 22:38:11
Nit: Values
Justin Donnelly
2014/10/27 21:41:16
Done.
| |
38 std::string json = | |
39 "{ \"l\": [" | |
40 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " | |
41 " { \"t\": \"moar text\", \"tt\": 0 }], " | |
42 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " | |
43 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
44 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " | |
45 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " | |
46 "] }"; | |
47 SuggestionAnswer answer1; | |
48 answer1.SetType(base::ASCIIToUTF16("4")); | |
49 SuggestionAnswer::ParseAnswer(json, &answer1); | |
50 EXPECT_TRUE(answer1.is_valid()); | |
51 | |
52 // Exactly the same just a different type for one of the text type values. | |
53 json = | |
54 "{ \"l\": [" | |
55 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " | |
56 " { \"t\": \"moar text\", \"tt\": 1 }], " | |
57 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " | |
58 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
59 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " | |
60 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " | |
61 "] }"; | |
62 SuggestionAnswer answer2; | |
63 answer2.SetType(base::ASCIIToUTF16("4")); | |
64 SuggestionAnswer::ParseAnswer(json, &answer2); | |
65 EXPECT_TRUE(answer2.is_valid()); | |
66 | |
67 EXPECT_NE(answer1, answer2); | |
68 } | |
69 | |
70 TEST(SuggestionAnswerTest, EmptyAnswerIsInvalid) { | |
71 SuggestionAnswer answer; | |
72 EXPECT_FALSE(answer.is_valid()); | |
73 } | |
74 | |
75 TEST(SuggestionAnswerTest, AnswerWithTypeOnlyIsInvalid) { | |
76 SuggestionAnswer answer; | |
77 answer.SetType(base::ASCIIToUTF16("2940")); | |
78 EXPECT_FALSE(answer.is_valid()); | |
79 } | |
80 | |
81 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) { | |
82 std::string json = ""; | |
Peter Kasting
2014/10/24 22:38:11
Nit: = "" not necessary
Justin Donnelly
2014/10/27 21:41:16
Done.
| |
83 SuggestionAnswer answer; | |
84 answer.SetType(base::ASCIIToUTF16("1")); | |
85 SuggestionAnswer::ParseAnswer(json, &answer); | |
86 EXPECT_FALSE(answer.is_valid()); | |
87 } | |
88 | |
89 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) { | |
90 std::string json = "} malformed json {"; | |
91 SuggestionAnswer answer; | |
92 answer.SetType(base::ASCIIToUTF16("921")); | |
93 SuggestionAnswer::ParseAnswer(json, &answer); | |
94 EXPECT_FALSE(answer.is_valid()); | |
95 } | |
96 | |
97 TEST(SuggestionAnswerTest, AnswerWithoutTypeIsInvalid) { | |
98 std::string json = | |
99 "{ \"l\": [" | |
100 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
101 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
102 "] }"; | |
103 SuggestionAnswer answer; | |
104 SuggestionAnswer::ParseAnswer(json, &answer); | |
105 EXPECT_FALSE(answer.is_valid()); | |
106 } | |
107 | |
108 TEST(SuggestionAnswerTest, TypeMustBeNonNegativeToBeValid) { | |
109 std::string json = | |
110 "{ \"l\": [" | |
111 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
112 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
113 "] }"; | |
114 SuggestionAnswer answer; | |
115 answer.SetType(base::ASCIIToUTF16("-238")); | |
116 SuggestionAnswer::ParseAnswer(json, &answer); | |
117 EXPECT_FALSE(answer.is_valid()); | |
118 | |
119 answer.SetType(base::ASCIIToUTF16("-1")); | |
120 EXPECT_FALSE(answer.is_valid()); | |
121 | |
122 answer.SetType(base::ASCIIToUTF16("-14982")); | |
123 EXPECT_FALSE(answer.is_valid()); | |
124 | |
125 answer.SetType(base::ASCIIToUTF16("0")); | |
126 EXPECT_TRUE(answer.is_valid()); | |
127 | |
128 answer.SetType(base::ASCIIToUTF16("1")); | |
129 EXPECT_TRUE(answer.is_valid()); | |
130 | |
131 answer.SetType(base::ASCIIToUTF16("3284327")); | |
132 EXPECT_TRUE(answer.is_valid()); | |
133 } | |
134 | |
135 TEST(SuggestionAnswerTest, ClearedAnswerIsInvalid) { | |
136 std::string json = | |
137 "{ \"l\": [" | |
138 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
139 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
140 "] }"; | |
141 SuggestionAnswer answer; | |
142 answer.SetType(base::ASCIIToUTF16("921")); | |
143 SuggestionAnswer::ParseAnswer(json, &answer); | |
144 EXPECT_TRUE(answer.is_valid()); | |
145 | |
146 answer.Clear(); | |
147 EXPECT_FALSE(answer.is_valid()); | |
148 } | |
149 | |
150 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) { | |
151 std::string json = | |
152 "{ \"l\": [" | |
153 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, " | |
154 "] }"; | |
155 SuggestionAnswer answer1; | |
156 answer1.SetType(base::ASCIIToUTF16("894")); | |
157 SuggestionAnswer::ParseAnswer(json, &answer1); | |
158 EXPECT_FALSE(answer1.is_valid()); | |
159 | |
160 json = | |
161 "{ \"l\": [" | |
162 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, " | |
163 "] }"; | |
164 SuggestionAnswer answer2; | |
165 answer2.SetType(base::ASCIIToUTF16("12")); | |
166 SuggestionAnswer::ParseAnswer(json, &answer2); | |
167 EXPECT_FALSE(answer2.is_valid()); | |
168 } | |
169 | |
170 TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) { | |
171 std::string json = | |
172 "{ \"l\": [" | |
173 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " | |
174 " { \"t\": \"moar text\", \"tt\": 0 }], " | |
175 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " | |
176 " { \"il\": { \"t\": [], " | |
177 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " | |
178 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " | |
179 "] }"; | |
180 SuggestionAnswer answer; | |
181 answer.SetType(base::ASCIIToUTF16("205")); | |
182 SuggestionAnswer::ParseAnswer(json, &answer); | |
183 EXPECT_FALSE(answer.is_valid()); | |
184 } | |
185 | |
186 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) { | |
187 std::string json = | |
188 "{ \"l\": [" | |
189 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
190 "] }"; | |
191 SuggestionAnswer answer1; | |
192 answer1.SetType(base::ASCIIToUTF16("8943")); | |
193 SuggestionAnswer::ParseAnswer(json, &answer1); | |
194 EXPECT_FALSE(answer1.is_valid()); | |
195 | |
196 json = | |
197 "{ \"l\": [" | |
198 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
199 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
200 "] }"; | |
201 SuggestionAnswer answer2; | |
202 answer2.SetType(base::ASCIIToUTF16("4028023")); | |
203 SuggestionAnswer::ParseAnswer(json, &answer2); | |
204 EXPECT_TRUE(answer2.is_valid()); | |
205 | |
206 json = | |
207 "{ \"l\": [" | |
208 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
209 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " | |
210 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } " | |
211 "] }"; | |
212 SuggestionAnswer answer3; | |
213 answer3.SetType(base::ASCIIToUTF16("23")); | |
214 SuggestionAnswer::ParseAnswer(json, &answer3); | |
215 EXPECT_FALSE(answer3.is_valid()); | |
216 } | |
217 | |
218 TEST(SuggestionAnswerTest, URLPresent) { | |
219 std::string json = | |
220 "{ \"l\": [" | |
221 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
222 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
223 " \"i\": { \"d\": \"\" } } } " | |
224 "] }"; | |
225 SuggestionAnswer answer1; | |
226 answer1.SetType(base::ASCIIToUTF16("1")); | |
227 SuggestionAnswer::ParseAnswer(json, &answer1); | |
228 EXPECT_FALSE(answer1.is_valid()); | |
229 | |
230 json = | |
231 "{ \"l\": [" | |
232 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
233 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
234 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } " | |
235 "] }"; | |
236 SuggestionAnswer answer2; | |
237 answer2.SetType(base::ASCIIToUTF16("8")); | |
238 SuggestionAnswer::ParseAnswer(json, &answer2); | |
239 EXPECT_TRUE(answer2.is_valid()); | |
240 | |
241 json = | |
242 "{ \"l\": [" | |
243 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " | |
244 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
245 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } " | |
246 "] }"; | |
247 SuggestionAnswer answer3; | |
248 answer3.SetType(base::ASCIIToUTF16("4")); | |
249 SuggestionAnswer::ParseAnswer(json, &answer3); | |
250 EXPECT_TRUE(answer3.is_valid()); | |
251 } | |
252 | |
253 TEST(SuggestionAnswerTest, ValidPropertyValues) { | |
254 std::string json = | |
255 "{ \"l\": [" | |
256 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " | |
257 " { \"t\": \"moar text\", \"tt\": 0 }], " | |
258 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " | |
259 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " | |
260 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " | |
261 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " | |
262 "] }"; | |
263 SuggestionAnswer answer; | |
264 answer.SetType(base::ASCIIToUTF16("420527")); | |
265 SuggestionAnswer::ParseAnswer(json, &answer); | |
266 EXPECT_TRUE(answer.is_valid()); | |
267 | |
268 const SuggestionAnswer::ImageLine& first_line = answer.first_line(); | |
269 EXPECT_EQ(2U, first_line.text_fields().size()); | |
270 EXPECT_EQ("text", first_line.text_fields()[0].text()); | |
271 EXPECT_EQ(8, first_line.text_fields()[0].type()); | |
272 EXPECT_EQ("moar text", first_line.text_fields()[1].text()); | |
273 EXPECT_EQ(0, first_line.text_fields()[1].type()); | |
274 | |
275 EXPECT_FALSE(first_line.has_additional_text()); | |
276 EXPECT_FALSE(first_line.has_status_text()); | |
277 | |
278 EXPECT_TRUE(first_line.has_image_url()); | |
279 EXPECT_EQ("https://example.com/foo.jpg", first_line.image_url().spec()); | |
280 | |
281 const SuggestionAnswer::ImageLine& second_line = answer.second_line(); | |
282 EXPECT_EQ(1U, second_line.text_fields().size()); | |
283 EXPECT_EQ("other text", second_line.text_fields()[0].text()); | |
284 EXPECT_EQ(5, second_line.text_fields()[0].type()); | |
285 | |
286 EXPECT_TRUE(second_line.has_additional_text()); | |
287 EXPECT_EQ("slatfatf", second_line.additional_text().text()); | |
288 EXPECT_EQ(42, second_line.additional_text().type()); | |
289 | |
290 EXPECT_TRUE(second_line.has_status_text()); | |
291 EXPECT_EQ("oh hi, Mark", second_line.status_text().text()); | |
292 EXPECT_EQ(729347, second_line.status_text().type()); | |
293 | |
294 EXPECT_FALSE(second_line.has_image_url()); | |
295 } | |
296 | |
297 TEST(SuggestionAnswerTest, GetImageURLsWithoutImagelines) { | |
298 std::vector<GURL> urls; | |
299 | |
300 // No "l" entry in the dictionary. | |
301 SuggestionAnswer answer; | |
302 SuggestionAnswer::ParseAnswer("", &answer); | |
303 answer.GetImageURLs(&urls); | |
304 EXPECT_TRUE(urls.empty()); | |
305 | |
306 // Empty "l" entry in the dictionary. | |
307 SuggestionAnswer::ParseAnswer("{ \"l\" : {} }", &answer); | |
308 answer.GetImageURLs(&urls); | |
309 EXPECT_TRUE(urls.empty()); | |
310 } | |
311 | |
312 TEST(SuggestionAnswerTest, GetImageURLsWithValidImageLines) { | |
313 std::vector<GURL> urls; | |
314 std::string json = | |
315 "{ \"l\" : [" | |
316 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } }," | |
317 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }]," | |
318 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}"; | |
319 | |
320 SuggestionAnswer answer1; | |
321 answer1.SetType(base::ASCIIToUTF16("532")); | |
322 SuggestionAnswer::ParseAnswer(json, &answer1); | |
323 answer1.GetImageURLs(&urls); | |
324 ASSERT_EQ(1U, urls.size()); | |
325 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec()); | |
326 | |
327 json = | |
328 "{ \"l\" : [" | |
329 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }]," | |
330 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, " | |
331 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }]," | |
332 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}"; | |
333 | |
334 SuggestionAnswer answer2; | |
335 answer2.SetType(base::ASCIIToUTF16("15")); | |
336 SuggestionAnswer::ParseAnswer(json, &answer2); | |
337 answer2.GetImageURLs(&urls); | |
338 ASSERT_EQ(2U, urls.size()); | |
339 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec()); | |
340 EXPECT_EQ("https://gstatic.com/bar.jpg", urls[1].spec()); | |
341 } | |
OLD | NEW |