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