jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 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 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #include "components/omnibox/browser/suggestion_answer.h" |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 6 | |
Kevin Bailey | 1e2a90e | 2017-10-27 21:02:05 | [diff] [blame] | 7 | #include <algorithm> |
Kevin Bailey | 2fe25ac | 2017-12-05 14:18:37 | [diff] [blame] | 8 | #include <memory> |
Kevin Bailey | 1e2a90e | 2017-10-27 21:02:05 | [diff] [blame] | 9 | |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 10 | #include "base/json/json_reader.h" |
| 11 | #include "base/strings/utf_string_conversions.h" |
Mark Pearson | fb22ad21 | 2019-04-25 19:02:55 | [diff] [blame] | 12 | #include "base/test/metrics/histogram_tester.h" |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 13 | #include "base/values.h" |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 18 | bool ParseAnswer(const std::string& answer_json, SuggestionAnswer* answer) { |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 19 | absl::optional<base::Value> value = base::JSONReader::Read(answer_json); |
Sylvain Defresne | f2a6504 | 2019-03-13 16:07:28 | [diff] [blame] | 20 | if (!value || !value->is_dict()) |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 21 | return false; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 22 | |
Orin Jaworski | 69f3170 | 2018-08-28 00:16:03 | [diff] [blame] | 23 | // 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örrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 25 | return SuggestionAnswer::ParseAnswer(*value, u"-1", answer); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | TEST(SuggestionAnswerTest, DefaultAreEqual) { |
| 31 | SuggestionAnswer answer1; |
| 32 | SuggestionAnswer answer2; |
| 33 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 34 | } |
| 35 | |
| 36 | TEST(SuggestionAnswerTest, CopiesAreEqual) { |
| 37 | SuggestionAnswer answer1; |
| 38 | EXPECT_TRUE(answer1.Equals(SuggestionAnswer(answer1))); |
| 39 | |
Kevin Bailey | 2fe25ac | 2017-12-05 14:18:37 | [diff] [blame] | 40 | auto answer2 = std::make_unique<SuggestionAnswer>(); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 41 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 49 | SuggestionAnswer answer3; |
| 50 | ASSERT_TRUE(ParseAnswer(json, &answer3)); |
| 51 | EXPECT_TRUE(answer3.Equals(SuggestionAnswer(answer3))); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 64 | SuggestionAnswer answer1; |
| 65 | ASSERT_TRUE(ParseAnswer(json, &answer1)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 66 | |
| 67 | // Same but with a different answer type. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 68 | SuggestionAnswer answer2 = answer1; |
| 69 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 70 | answer2.set_type(44); |
| 71 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 72 | |
| 73 | // Same but with a different type for one of the text fields. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 74 | answer2 = answer1; |
| 75 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 76 | answer2.first_line_.text_fields_[1].type_ = 1; |
| 77 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 78 | |
| 79 | // Same but with different text for one of the text fields. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 80 | answer2 = answer1; |
| 81 | EXPECT_TRUE(answer1.Equals(answer2)); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 82 | answer2.first_line_.text_fields_[0].text_ = u"some text"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 83 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 84 | |
| 85 | // Same but with a new URL on the second line. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 86 | 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)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 90 | |
| 91 | // Same but with the additional text removed from the second line. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 92 | answer2 = answer1; |
| 93 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 94 | answer2.second_line_.additional_text_.reset(); |
| 95 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 96 | |
| 97 | // Same but with the status text removed from the second line. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 98 | answer2 = answer1; |
| 99 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 100 | answer2.second_line_.status_text_.reset(); |
| 101 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 102 | |
| 103 | // Same but with the status text removed from the second line of the first |
| 104 | // answer. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 105 | answer2 = answer1; |
| 106 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 107 | answer1.second_line_.status_text_.reset(); |
| 108 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 109 | |
| 110 | // Same but with the additional text removed from the second line of the first |
| 111 | // answer. |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 112 | answer2 = answer1; |
| 113 | EXPECT_TRUE(answer1.Equals(answer2)); |
| 114 | answer1.second_line_.additional_text_.reset(); |
| 115 | EXPECT_FALSE(answer1.Equals(answer2)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 119 | SuggestionAnswer answer; |
| 120 | ASSERT_FALSE(ParseAnswer("", &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 124 | SuggestionAnswer answer; |
| 125 | ASSERT_FALSE(ParseAnswer("} malformed json {", &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 129 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 130 | std::string json = |
| 131 | "{ \"l\": [" |
| 132 | " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, " |
| 133 | "] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 134 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 135 | |
| 136 | json = |
| 137 | "{ \"l\": [" |
| 138 | " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, " |
| 139 | "] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 140 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 144 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 145 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 154 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 158 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 159 | std::string json = |
| 160 | "{ \"l\": [" |
| 161 | " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 162 | "] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 163 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 164 | |
| 165 | json = |
| 166 | "{ \"l\": [" |
| 167 | " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 168 | " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " |
| 169 | "] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 170 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 171 | |
| 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 178 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST(SuggestionAnswerTest, URLPresent) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 182 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 183 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 189 | ASSERT_FALSE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 190 | |
| 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 197 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 198 | |
| 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 205 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | TEST(SuggestionAnswerTest, ValidPropertyValues) { |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 209 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 210 | std::string json = |
| 211 | "{ \"l\": [" |
krb | e959e11 | 2016-06-30 14:15:56 | [diff] [blame] | 212 | " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 213 | " { \"t\": \"moar text\", \"tt\": 0 }], " |
| 214 | " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " |
krb | e959e11 | 2016-06-30 14:15:56 | [diff] [blame] | 215 | " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5, \"ln\": 3 }], " |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 216 | " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " |
| 217 | " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " |
| 218 | "] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 219 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 220 | answer.set_type(420527); |
| 221 | EXPECT_EQ(420527, answer.type()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 222 | |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 223 | const SuggestionAnswer::ImageLine& first_line = answer.first_line(); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 224 | EXPECT_EQ(2U, first_line.text_fields().size()); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 225 | EXPECT_EQ(u"text", first_line.text_fields()[0].text()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 226 | EXPECT_EQ(8, first_line.text_fields()[0].type()); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 227 | EXPECT_EQ(u"moar text", first_line.text_fields()[1].text()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 228 | EXPECT_EQ(0, first_line.text_fields()[1].type()); |
krb | ba6250e | 2016-05-26 01:07:31 | [diff] [blame] | 229 | EXPECT_FALSE(first_line.text_fields()[1].has_num_lines()); |
krb | e959e11 | 2016-06-30 14:15:56 | [diff] [blame] | 230 | EXPECT_EQ(1, first_line.num_text_lines()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 231 | |
| 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 238 | const SuggestionAnswer::ImageLine& second_line = answer.second_line(); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 239 | EXPECT_EQ(1U, second_line.text_fields().size()); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 240 | EXPECT_EQ(u"other text", second_line.text_fields()[0].text()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 241 | EXPECT_EQ(5, second_line.text_fields()[0].type()); |
krb | e959e11 | 2016-06-30 14:15:56 | [diff] [blame] | 242 | 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()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 245 | |
| 246 | EXPECT_TRUE(second_line.additional_text()); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 247 | EXPECT_EQ(u"slatfatf", second_line.additional_text()->text()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 248 | EXPECT_EQ(42, second_line.additional_text()->type()); |
| 249 | |
| 250 | EXPECT_TRUE(second_line.status_text()); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 251 | EXPECT_EQ(u"oh hi, Mark", second_line.status_text()->text()); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 252 | EXPECT_EQ(729347, second_line.status_text()->type()); |
| 253 | |
| 254 | EXPECT_FALSE(second_line.image_url().is_valid()); |
| 255 | } |
| 256 | |
| 257 | TEST(SuggestionAnswerTest, AddImageURLsTo) { |
| 258 | SuggestionAnswer::URLs urls; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 259 | SuggestionAnswer answer; |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 260 | std::string json = |
| 261 | "{ \"l\": [" |
| 262 | " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 263 | " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } }] }"; |
Kevin Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 264 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 265 | answer.AddImageURLsTo(&urls); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 266 | ASSERT_EQ(0U, urls.size()); |
| 267 | |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 268 | { |
Justin Donnelly | 1464fba | 2018-12-03 23:00:51 | [diff] [blame] | 269 | // Test with the image URL supplied by the "i" (image) param. |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 270 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 276 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 277 | answer.AddImageURLsTo(&urls); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 278 | ASSERT_EQ(1U, urls.size()); |
| 279 | EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]); |
| 280 | urls.clear(); |
| 281 | } |
| 282 | |
Justin Donnelly | 1464fba | 2018-12-03 23:00:51 | [diff] [blame] | 283 | // Test with the image URL supplied by the "il" (image line) param. |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 284 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 289 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 290 | answer.AddImageURLsTo(&urls); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 291 | ASSERT_EQ(1U, urls.size()); |
| 292 | EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 293 | urls.clear(); |
| 294 | |
Justin Donnelly | 1464fba | 2018-12-03 23:00:51 | [diff] [blame] | 295 | // 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 Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 298 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 305 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 306 | answer.AddImageURLsTo(&urls); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 307 | ASSERT_EQ(1U, urls.size()); |
Justin Donnelly | 1464fba | 2018-12-03 23:00:51 | [diff] [blame] | 308 | EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 309 | urls.clear(); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 310 | |
Justin Donnelly | 1464fba | 2018-12-03 23:00:51 | [diff] [blame] | 311 | // 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. |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 313 | 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 Bailey | 199f6e3 | 2018-08-29 23:12:03 | [diff] [blame] | 319 | ASSERT_TRUE(ParseAnswer(json, &answer)); |
| 320 | answer.AddImageURLsTo(&urls); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 321 | ASSERT_EQ(1U, urls.size()); |
Dave Schuyler | e10a24a | 2018-05-22 21:54:12 | [diff] [blame] | 322 | EXPECT_EQ(GURL("https://gstatic.com/bar.jpg"), urls[0]); |
jdonnelly | 7393cee | 2014-10-31 01:52:56 | [diff] [blame] | 323 | } |
Mark Pearson | fb22ad21 | 2019-04-25 19:02:55 | [diff] [blame] | 324 | |
| 325 | TEST(SuggestionAnswerTest, LogAnswerUsed) { |
| 326 | { |
| 327 | base::HistogramTester histograms; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 328 | absl::optional<SuggestionAnswer> answer; |
Mark Pearson | fb22ad21 | 2019-04-25 19:02:55 | [diff] [blame] | 329 | 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 | } |