blob: 19426ae9fe975e4ce622a122fe73ef93a8f3ebe9 [file] [log] [blame]
jdonnelly7393cee2014-10-31 01:52:561// 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
blundell2102f7c2015-07-09 10:00:535#include "components/omnibox/browser/suggestion_answer.h"
jdonnelly7393cee2014-10-31 01:52:566
Kevin Bailey1e2a90e2017-10-27 21:02:057#include <algorithm>
Kevin Bailey2fe25ac2017-12-05 14:18:378#include <memory>
Kevin Bailey1e2a90e2017-10-27 21:02:059
jdonnelly7393cee2014-10-31 01:52:5610#include "base/json/json_reader.h"
11#include "base/strings/utf_string_conversions.h"
Mark Pearsonfb22ad212019-04-25 19:02:5512#include "base/test/metrics/histogram_tester.h"
jdonnelly7393cee2014-10-31 01:52:5613#include "base/values.h"
jdonnelly7393cee2014-10-31 01:52:5614#include "testing/gtest/include/gtest/gtest.h"
jdonnelly7393cee2014-10-31 01:52:5615
16namespace {
17
Kevin Bailey199f6e32018-08-29 23:12:0318bool ParseAnswer(const std::string& answer_json, SuggestionAnswer* answer) {
Anton Bikineev1156b5f2021-05-15 22:35:3619 absl::optional<base::Value> value = base::JSONReader::Read(answer_json);
Sylvain Defresnef2a65042019-03-13 16:07:2820 if (!value || !value->is_dict())
Kevin Bailey199f6e32018-08-29 23:12:0321 return false;
jdonnelly7393cee2014-10-31 01:52:5622
Orin Jaworski69f31702018-08-28 00:16:0323 // ParseAnswer previously did not change the default answer type of -1, so
24 // here we keep the same behavior by explicitly supplying default value.
Jan Wilken Dörrie2c470ea2021-03-22 22:26:2425 return SuggestionAnswer::ParseAnswer(*value, u"-1", answer);
jdonnelly7393cee2014-10-31 01:52:5626}
27
28} // namespace
29
30TEST(SuggestionAnswerTest, DefaultAreEqual) {
31 SuggestionAnswer answer1;
32 SuggestionAnswer answer2;
33 EXPECT_TRUE(answer1.Equals(answer2));
34}
35
36TEST(SuggestionAnswerTest, CopiesAreEqual) {
37 SuggestionAnswer answer1;
38 EXPECT_TRUE(answer1.Equals(SuggestionAnswer(answer1)));
39
Kevin Bailey2fe25ac2017-12-05 14:18:3740 auto answer2 = std::make_unique<SuggestionAnswer>();
jdonnelly7393cee2014-10-31 01:52:5641 answer2->set_type(832345);
42 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2)));
43
44 std::string json =
45 "{ \"l\": ["
46 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
47 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
48 "] }";
Kevin Bailey199f6e32018-08-29 23:12:0349 SuggestionAnswer answer3;
50 ASSERT_TRUE(ParseAnswer(json, &answer3));
51 EXPECT_TRUE(answer3.Equals(SuggestionAnswer(answer3)));
jdonnelly7393cee2014-10-31 01:52:5652}
53
54TEST(SuggestionAnswerTest, DifferentValuesAreUnequal) {
55 std::string json =
56 "{ \"l\": ["
57 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
58 " { \"t\": \"moar text\", \"tt\": 0 }], "
59 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
60 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
61 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
62 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
63 "] }";
Kevin Bailey199f6e32018-08-29 23:12:0364 SuggestionAnswer answer1;
65 ASSERT_TRUE(ParseAnswer(json, &answer1));
jdonnelly7393cee2014-10-31 01:52:5666
67 // Same but with a different answer type.
Kevin Bailey199f6e32018-08-29 23:12:0368 SuggestionAnswer answer2 = answer1;
69 EXPECT_TRUE(answer1.Equals(answer2));
70 answer2.set_type(44);
71 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:5672
73 // Same but with a different type for one of the text fields.
Kevin Bailey199f6e32018-08-29 23:12:0374 answer2 = answer1;
75 EXPECT_TRUE(answer1.Equals(answer2));
76 answer2.first_line_.text_fields_[1].type_ = 1;
77 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:5678
79 // Same but with different text for one of the text fields.
Kevin Bailey199f6e32018-08-29 23:12:0380 answer2 = answer1;
81 EXPECT_TRUE(answer1.Equals(answer2));
Jan Wilken Dörrie2c470ea2021-03-22 22:26:2482 answer2.first_line_.text_fields_[0].text_ = u"some text";
Kevin Bailey199f6e32018-08-29 23:12:0383 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:5684
85 // Same but with a new URL on the second line.
Kevin Bailey199f6e32018-08-29 23:12:0386 answer2 = answer1;
87 EXPECT_TRUE(answer1.Equals(answer2));
88 answer2.second_line_.image_url_ = GURL("http://foo.com/bar.png");
89 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:5690
91 // Same but with the additional text removed from the second line.
Kevin Bailey199f6e32018-08-29 23:12:0392 answer2 = answer1;
93 EXPECT_TRUE(answer1.Equals(answer2));
94 answer2.second_line_.additional_text_.reset();
95 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:5696
97 // Same but with the status text removed from the second line.
Kevin Bailey199f6e32018-08-29 23:12:0398 answer2 = answer1;
99 EXPECT_TRUE(answer1.Equals(answer2));
100 answer2.second_line_.status_text_.reset();
101 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:56102
103 // Same but with the status text removed from the second line of the first
104 // answer.
Kevin Bailey199f6e32018-08-29 23:12:03105 answer2 = answer1;
106 EXPECT_TRUE(answer1.Equals(answer2));
107 answer1.second_line_.status_text_.reset();
108 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:56109
110 // Same but with the additional text removed from the second line of the first
111 // answer.
Kevin Bailey199f6e32018-08-29 23:12:03112 answer2 = answer1;
113 EXPECT_TRUE(answer1.Equals(answer2));
114 answer1.second_line_.additional_text_.reset();
115 EXPECT_FALSE(answer1.Equals(answer2));
jdonnelly7393cee2014-10-31 01:52:56116}
117
118TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
Kevin Bailey199f6e32018-08-29 23:12:03119 SuggestionAnswer answer;
120 ASSERT_FALSE(ParseAnswer("", &answer));
jdonnelly7393cee2014-10-31 01:52:56121}
122
123TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
Kevin Bailey199f6e32018-08-29 23:12:03124 SuggestionAnswer answer;
125 ASSERT_FALSE(ParseAnswer("} malformed json {", &answer));
jdonnelly7393cee2014-10-31 01:52:56126}
127
128TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
Kevin Bailey199f6e32018-08-29 23:12:03129 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56130 std::string json =
131 "{ \"l\": ["
132 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
133 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03134 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56135
136 json =
137 "{ \"l\": ["
138 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
139 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03140 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56141}
142
143TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) {
Kevin Bailey199f6e32018-08-29 23:12:03144 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56145 std::string json =
146 "{ \"l\": ["
147 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
148 " { \"t\": \"moar text\", \"tt\": 0 }], "
149 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
150 " { \"il\": { \"t\": [], "
151 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
152 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
153 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03154 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56155}
156
157TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
Kevin Bailey199f6e32018-08-29 23:12:03158 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56159 std::string json =
160 "{ \"l\": ["
161 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
162 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03163 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56164
165 json =
166 "{ \"l\": ["
167 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
168 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
169 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03170 ASSERT_TRUE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56171
172 json =
173 "{ \"l\": ["
174 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
175 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
176 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } "
177 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03178 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56179}
180
181TEST(SuggestionAnswerTest, URLPresent) {
Kevin Bailey199f6e32018-08-29 23:12:03182 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56183 std::string json =
184 "{ \"l\": ["
185 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
186 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
187 " \"i\": { \"d\": \"\" } } } "
188 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03189 ASSERT_FALSE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56190
191 json =
192 "{ \"l\": ["
193 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
194 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
195 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } "
196 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03197 ASSERT_TRUE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56198
199 json =
200 "{ \"l\": ["
201 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
202 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
203 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } "
204 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03205 ASSERT_TRUE(ParseAnswer(json, &answer));
jdonnelly7393cee2014-10-31 01:52:56206}
207
208TEST(SuggestionAnswerTest, ValidPropertyValues) {
Kevin Bailey199f6e32018-08-29 23:12:03209 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56210 std::string json =
211 "{ \"l\": ["
krbe959e112016-06-30 14:15:56212 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
jdonnelly7393cee2014-10-31 01:52:56213 " { \"t\": \"moar text\", \"tt\": 0 }], "
214 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
krbe959e112016-06-30 14:15:56215 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5, \"ln\": 3 }], "
jdonnelly7393cee2014-10-31 01:52:56216 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
217 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
218 "] }";
Kevin Bailey199f6e32018-08-29 23:12:03219 ASSERT_TRUE(ParseAnswer(json, &answer));
220 answer.set_type(420527);
221 EXPECT_EQ(420527, answer.type());
jdonnelly7393cee2014-10-31 01:52:56222
Kevin Bailey199f6e32018-08-29 23:12:03223 const SuggestionAnswer::ImageLine& first_line = answer.first_line();
jdonnelly7393cee2014-10-31 01:52:56224 EXPECT_EQ(2U, first_line.text_fields().size());
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24225 EXPECT_EQ(u"text", first_line.text_fields()[0].text());
jdonnelly7393cee2014-10-31 01:52:56226 EXPECT_EQ(8, first_line.text_fields()[0].type());
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24227 EXPECT_EQ(u"moar text", first_line.text_fields()[1].text());
jdonnelly7393cee2014-10-31 01:52:56228 EXPECT_EQ(0, first_line.text_fields()[1].type());
krbba6250e2016-05-26 01:07:31229 EXPECT_FALSE(first_line.text_fields()[1].has_num_lines());
krbe959e112016-06-30 14:15:56230 EXPECT_EQ(1, first_line.num_text_lines());
jdonnelly7393cee2014-10-31 01:52:56231
232 EXPECT_FALSE(first_line.additional_text());
233 EXPECT_FALSE(first_line.status_text());
234
235 EXPECT_TRUE(first_line.image_url().is_valid());
236 EXPECT_EQ(GURL("https://example.com/foo.jpg"), first_line.image_url());
237
Kevin Bailey199f6e32018-08-29 23:12:03238 const SuggestionAnswer::ImageLine& second_line = answer.second_line();
jdonnelly7393cee2014-10-31 01:52:56239 EXPECT_EQ(1U, second_line.text_fields().size());
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24240 EXPECT_EQ(u"other text", second_line.text_fields()[0].text());
jdonnelly7393cee2014-10-31 01:52:56241 EXPECT_EQ(5, second_line.text_fields()[0].type());
krbe959e112016-06-30 14:15:56242 EXPECT_TRUE(second_line.text_fields()[0].has_num_lines());
243 EXPECT_EQ(3, second_line.text_fields()[0].num_lines());
244 EXPECT_EQ(3, second_line.num_text_lines());
jdonnelly7393cee2014-10-31 01:52:56245
246 EXPECT_TRUE(second_line.additional_text());
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24247 EXPECT_EQ(u"slatfatf", second_line.additional_text()->text());
jdonnelly7393cee2014-10-31 01:52:56248 EXPECT_EQ(42, second_line.additional_text()->type());
249
250 EXPECT_TRUE(second_line.status_text());
Jan Wilken Dörrie2c470ea2021-03-22 22:26:24251 EXPECT_EQ(u"oh hi, Mark", second_line.status_text()->text());
jdonnelly7393cee2014-10-31 01:52:56252 EXPECT_EQ(729347, second_line.status_text()->type());
253
254 EXPECT_FALSE(second_line.image_url().is_valid());
255}
256
257TEST(SuggestionAnswerTest, AddImageURLsTo) {
258 SuggestionAnswer::URLs urls;
Kevin Bailey199f6e32018-08-29 23:12:03259 SuggestionAnswer answer;
jdonnelly7393cee2014-10-31 01:52:56260 std::string json =
261 "{ \"l\": ["
262 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
263 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } }] }";
Kevin Bailey199f6e32018-08-29 23:12:03264 ASSERT_TRUE(ParseAnswer(json, &answer));
265 answer.AddImageURLsTo(&urls);
jdonnelly7393cee2014-10-31 01:52:56266 ASSERT_EQ(0U, urls.size());
267
Dave Schuylere10a24a2018-05-22 21:54:12268 {
Justin Donnelly1464fba2018-12-03 23:00:51269 // Test with the image URL supplied by the "i" (image) param.
Dave Schuylere10a24a2018-05-22 21:54:12270 json =
271 "{ \"i\": { \"d\": \"https://gstatic.com/foo.png\", \"t\": 3 },"
272 " \"l\" : ["
273 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
274 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }] } }"
275 " ]}";
Kevin Bailey199f6e32018-08-29 23:12:03276 ASSERT_TRUE(ParseAnswer(json, &answer));
277 answer.AddImageURLsTo(&urls);
Dave Schuylere10a24a2018-05-22 21:54:12278 ASSERT_EQ(1U, urls.size());
279 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]);
280 urls.clear();
281 }
282
Justin Donnelly1464fba2018-12-03 23:00:51283 // Test with the image URL supplied by the "il" (image line) param.
jdonnelly7393cee2014-10-31 01:52:56284 json =
285 "{ \"l\" : ["
286 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
287 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
288 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}";
Kevin Bailey199f6e32018-08-29 23:12:03289 ASSERT_TRUE(ParseAnswer(json, &answer));
290 answer.AddImageURLsTo(&urls);
jdonnelly7393cee2014-10-31 01:52:56291 ASSERT_EQ(1U, urls.size());
292 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]);
Dave Schuylere10a24a2018-05-22 21:54:12293 urls.clear();
294
Justin Donnelly1464fba2018-12-03 23:00:51295 // Test with image URLs supplied by both the "i" and "il" params. In this
296 // case, prefer the URL provided by the "i" param because the new answer code
297 // uses this.
Dave Schuylere10a24a2018-05-22 21:54:12298 json =
299 "{ \"i\": { \"d\": \"https://gstatic.com/foo.png\", \"t\": 3 },"
300 " \"l\" : ["
301 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
302 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
303 " \"i\": { \"d\": \"//gstatic.com/bar.png\", \"t\": 3 }}}"
304 " ]}";
Kevin Bailey199f6e32018-08-29 23:12:03305 ASSERT_TRUE(ParseAnswer(json, &answer));
306 answer.AddImageURLsTo(&urls);
Dave Schuylere10a24a2018-05-22 21:54:12307 ASSERT_EQ(1U, urls.size());
Justin Donnelly1464fba2018-12-03 23:00:51308 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]);
Dave Schuylere10a24a2018-05-22 21:54:12309 urls.clear();
jdonnelly7393cee2014-10-31 01:52:56310
Justin Donnelly1464fba2018-12-03 23:00:51311 // Test with the image URL supplied by both "il" params. In this case, prefer
312 // the URL in the second line as the first is currently not used.
jdonnelly7393cee2014-10-31 01:52:56313 json =
314 "{ \"l\" : ["
315 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }],"
316 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, "
317 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
318 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}";
Kevin Bailey199f6e32018-08-29 23:12:03319 ASSERT_TRUE(ParseAnswer(json, &answer));
320 answer.AddImageURLsTo(&urls);
Dave Schuylere10a24a2018-05-22 21:54:12321 ASSERT_EQ(1U, urls.size());
Dave Schuylere10a24a2018-05-22 21:54:12322 EXPECT_EQ(GURL("https://gstatic.com/bar.jpg"), urls[0]);
jdonnelly7393cee2014-10-31 01:52:56323}
Mark Pearsonfb22ad212019-04-25 19:02:55324
325TEST(SuggestionAnswerTest, LogAnswerUsed) {
326 {
327 base::HistogramTester histograms;
Anton Bikineev1156b5f2021-05-15 22:35:36328 absl::optional<SuggestionAnswer> answer;
Mark Pearsonfb22ad212019-04-25 19:02:55329 SuggestionAnswer::LogAnswerUsed(answer);
330 histograms.ExpectUniqueSample(SuggestionAnswer::kAnswerUsedUmaHistogramName,
331 0, 1);
332 }
333
334 {
335 base::HistogramTester histograms;
336 SuggestionAnswer answer;
337 answer.set_type(8);
338 SuggestionAnswer::LogAnswerUsed(answer);
339 histograms.ExpectUniqueSample(SuggestionAnswer::kAnswerUsedUmaHistogramName,
340 8, 1);
341 }
342
343 {
344 base::HistogramTester histograms;
345 SuggestionAnswer answer;
346 answer.set_type(5);
347 SuggestionAnswer::LogAnswerUsed(answer);
348 histograms.ExpectUniqueSample(SuggestionAnswer::kAnswerUsedUmaHistogramName,
349 5, 1);
350 }
351}