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/json/json_reader.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 scoped_ptr<SuggestionAnswer> ParseAnswer(const std::string& answer_json) { |
| 15 scoped_ptr<base::Value> value(base::JSONReader::Read(answer_json)); |
| 16 base::DictionaryValue* dict; |
| 17 if (!value || !value->GetAsDictionary(&dict)) |
| 18 return nullptr; |
| 19 |
| 20 return SuggestionAnswer::ParseAnswer(dict); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(SuggestionAnswerTest, DefaultAreEqual) { |
| 26 SuggestionAnswer answer1; |
| 27 SuggestionAnswer answer2; |
| 28 EXPECT_TRUE(answer1.Equals(answer2)); |
| 29 } |
| 30 |
| 31 TEST(SuggestionAnswerTest, CopiesAreEqual) { |
| 32 SuggestionAnswer answer1; |
| 33 EXPECT_TRUE(answer1.Equals(SuggestionAnswer(answer1))); |
| 34 |
| 35 auto answer2 = make_scoped_ptr(new SuggestionAnswer); |
| 36 answer2->set_type(832345); |
| 37 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2))); |
| 38 |
| 39 std::string json = |
| 40 "{ \"l\": [" |
| 41 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 42 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " |
| 43 "] }"; |
| 44 answer2 = ParseAnswer(json); |
| 45 ASSERT_TRUE(answer2); |
| 46 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2))); |
| 47 } |
| 48 |
| 49 TEST(SuggestionAnswerTest, DifferentValuesAreUnequal) { |
| 50 std::string json = |
| 51 "{ \"l\": [" |
| 52 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " |
| 53 " { \"t\": \"moar text\", \"tt\": 0 }], " |
| 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 scoped_ptr<SuggestionAnswer> answer1 = ParseAnswer(json); |
| 60 ASSERT_TRUE(answer1); |
| 61 |
| 62 // Same but with a different answer type. |
| 63 scoped_ptr<SuggestionAnswer> answer2 = SuggestionAnswer::copy(answer1.get()); |
| 64 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 65 answer2->set_type(44); |
| 66 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 67 |
| 68 // Same but with a different type for one of the text fields. |
| 69 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 70 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 71 answer2->first_line_.text_fields_[1].type_ = 1; |
| 72 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 73 |
| 74 // Same but with different text for one of the text fields. |
| 75 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 76 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 77 answer2->first_line_.text_fields_[0].text_ = "some text"; |
| 78 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 79 |
| 80 // Same but with a new URL on the second line. |
| 81 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 82 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 83 answer2->second_line_.image_url_ = GURL("http://foo.com/bar.png"); |
| 84 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 85 |
| 86 // Same but with the additional text removed from the second line. |
| 87 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 88 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 89 answer2->second_line_.additional_text_.reset(); |
| 90 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 91 |
| 92 // Same but with the status text removed from the second line. |
| 93 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 94 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 95 answer2->second_line_.status_text_.reset(); |
| 96 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 97 |
| 98 // Same but with the status text removed from the second line of the first |
| 99 // answer. |
| 100 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 101 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 102 answer1->second_line_.status_text_.reset(); |
| 103 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 104 |
| 105 // Same but with the additional text removed from the second line of the first |
| 106 // answer. |
| 107 answer2 = SuggestionAnswer::copy(answer1.get()); |
| 108 EXPECT_TRUE(answer1->Equals(*answer2)); |
| 109 answer1->second_line_.additional_text_.reset(); |
| 110 EXPECT_FALSE(answer1->Equals(*answer2)); |
| 111 } |
| 112 |
| 113 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) { |
| 114 ASSERT_FALSE(ParseAnswer("")); |
| 115 } |
| 116 |
| 117 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) { |
| 118 ASSERT_FALSE(ParseAnswer("} malformed json {")); |
| 119 } |
| 120 |
| 121 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) { |
| 122 std::string json = |
| 123 "{ \"l\": [" |
| 124 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, " |
| 125 "] }"; |
| 126 ASSERT_FALSE(ParseAnswer(json)); |
| 127 |
| 128 json = |
| 129 "{ \"l\": [" |
| 130 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, " |
| 131 "] }"; |
| 132 ASSERT_FALSE(ParseAnswer(json)); |
| 133 } |
| 134 |
| 135 TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) { |
| 136 std::string json = |
| 137 "{ \"l\": [" |
| 138 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " |
| 139 " { \"t\": \"moar text\", \"tt\": 0 }], " |
| 140 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " |
| 141 " { \"il\": { \"t\": [], " |
| 142 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " |
| 143 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " |
| 144 "] }"; |
| 145 ASSERT_FALSE(ParseAnswer(json)); |
| 146 } |
| 147 |
| 148 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) { |
| 149 std::string json = |
| 150 "{ \"l\": [" |
| 151 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 152 "] }"; |
| 153 ASSERT_FALSE(ParseAnswer(json)); |
| 154 |
| 155 json = |
| 156 "{ \"l\": [" |
| 157 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 158 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " |
| 159 "] }"; |
| 160 ASSERT_TRUE(ParseAnswer(json)); |
| 161 |
| 162 json = |
| 163 "{ \"l\": [" |
| 164 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 165 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } " |
| 166 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } " |
| 167 "] }"; |
| 168 ASSERT_FALSE(ParseAnswer(json)); |
| 169 } |
| 170 |
| 171 TEST(SuggestionAnswerTest, URLPresent) { |
| 172 std::string json = |
| 173 "{ \"l\": [" |
| 174 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 175 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " |
| 176 " \"i\": { \"d\": \"\" } } } " |
| 177 "] }"; |
| 178 ASSERT_FALSE(ParseAnswer(json)); |
| 179 |
| 180 json = |
| 181 "{ \"l\": [" |
| 182 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 183 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " |
| 184 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } " |
| 185 "] }"; |
| 186 ASSERT_TRUE(ParseAnswer(json)); |
| 187 |
| 188 json = |
| 189 "{ \"l\": [" |
| 190 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 191 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " |
| 192 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } " |
| 193 "] }"; |
| 194 ASSERT_TRUE(ParseAnswer(json)); |
| 195 } |
| 196 |
| 197 TEST(SuggestionAnswerTest, ValidPropertyValues) { |
| 198 std::string json = |
| 199 "{ \"l\": [" |
| 200 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, " |
| 201 " { \"t\": \"moar text\", \"tt\": 0 }], " |
| 202 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, " |
| 203 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], " |
| 204 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, " |
| 205 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } " |
| 206 "] }"; |
| 207 scoped_ptr<SuggestionAnswer> answer = ParseAnswer(json); |
| 208 ASSERT_TRUE(answer); |
| 209 answer->set_type(420527); |
| 210 EXPECT_EQ(420527, answer->type()); |
| 211 |
| 212 const SuggestionAnswer::ImageLine& first_line = answer->first_line(); |
| 213 EXPECT_EQ(2U, first_line.text_fields().size()); |
| 214 EXPECT_EQ("text", first_line.text_fields()[0].text()); |
| 215 EXPECT_EQ(8, first_line.text_fields()[0].type()); |
| 216 EXPECT_EQ("moar text", first_line.text_fields()[1].text()); |
| 217 EXPECT_EQ(0, first_line.text_fields()[1].type()); |
| 218 |
| 219 EXPECT_FALSE(first_line.additional_text()); |
| 220 EXPECT_FALSE(first_line.status_text()); |
| 221 |
| 222 EXPECT_TRUE(first_line.image_url().is_valid()); |
| 223 EXPECT_EQ(GURL("https://example.com/foo.jpg"), first_line.image_url()); |
| 224 |
| 225 const SuggestionAnswer::ImageLine& second_line = answer->second_line(); |
| 226 EXPECT_EQ(1U, second_line.text_fields().size()); |
| 227 EXPECT_EQ("other text", second_line.text_fields()[0].text()); |
| 228 EXPECT_EQ(5, second_line.text_fields()[0].type()); |
| 229 |
| 230 EXPECT_TRUE(second_line.additional_text()); |
| 231 EXPECT_EQ("slatfatf", second_line.additional_text()->text()); |
| 232 EXPECT_EQ(42, second_line.additional_text()->type()); |
| 233 |
| 234 EXPECT_TRUE(second_line.status_text()); |
| 235 EXPECT_EQ("oh hi, Mark", second_line.status_text()->text()); |
| 236 EXPECT_EQ(729347, second_line.status_text()->type()); |
| 237 |
| 238 EXPECT_FALSE(second_line.image_url().is_valid()); |
| 239 } |
| 240 |
| 241 TEST(SuggestionAnswerTest, AddImageURLsTo) { |
| 242 SuggestionAnswer::URLs urls; |
| 243 std::string json = |
| 244 "{ \"l\": [" |
| 245 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, " |
| 246 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } }] }"; |
| 247 scoped_ptr<SuggestionAnswer> answer = ParseAnswer(json); |
| 248 ASSERT_TRUE(answer); |
| 249 answer->AddImageURLsTo(&urls); |
| 250 ASSERT_EQ(0U, urls.size()); |
| 251 |
| 252 json = |
| 253 "{ \"l\" : [" |
| 254 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } }," |
| 255 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }]," |
| 256 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}"; |
| 257 answer = ParseAnswer(json); |
| 258 ASSERT_TRUE(answer); |
| 259 answer->AddImageURLsTo(&urls); |
| 260 ASSERT_EQ(1U, urls.size()); |
| 261 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]); |
| 262 |
| 263 json = |
| 264 "{ \"l\" : [" |
| 265 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }]," |
| 266 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, " |
| 267 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }]," |
| 268 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}"; |
| 269 answer = ParseAnswer(json); |
| 270 ASSERT_TRUE(answer); |
| 271 answer->AddImageURLsTo(&urls); |
| 272 ASSERT_EQ(3U, urls.size()); |
| 273 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[1]); |
| 274 EXPECT_EQ(GURL("https://gstatic.com/bar.jpg"), urls[2]); |
| 275 } |
OLD | NEW |