Add an exponential backoff to rechecking the app list doodle.
[chromium-blink-merge.git] / components / omnibox / suggestion_answer_unittest.cc
blob3252a9fa6094d60666ea297640b14e532e311452
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.
5 #include "components/omnibox/suggestion_answer.h"
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"
12 namespace {
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;
20 return SuggestionAnswer::ParseAnswer(dict);
23 } // namespace
25 TEST(SuggestionAnswerTest, DefaultAreEqual) {
26 SuggestionAnswer answer1;
27 SuggestionAnswer answer2;
28 EXPECT_TRUE(answer1.Equals(answer2));
31 TEST(SuggestionAnswerTest, CopiesAreEqual) {
32 SuggestionAnswer answer1;
33 EXPECT_TRUE(answer1.Equals(SuggestionAnswer(answer1)));
35 auto answer2 = make_scoped_ptr(new SuggestionAnswer);
36 answer2->set_type(832345);
37 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2)));
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)));
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);
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));
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));
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_ = base::UTF8ToUTF16("some text");
78 EXPECT_FALSE(answer1->Equals(*answer2));
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));
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));
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));
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));
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));
113 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
114 ASSERT_FALSE(ParseAnswer(""));
117 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
118 ASSERT_FALSE(ParseAnswer("} malformed json {"));
121 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
122 std::string json =
123 "{ \"l\": ["
124 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
125 "] }";
126 ASSERT_FALSE(ParseAnswer(json));
128 json =
129 "{ \"l\": ["
130 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
131 "] }";
132 ASSERT_FALSE(ParseAnswer(json));
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));
148 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
149 std::string json =
150 "{ \"l\": ["
151 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
152 "] }";
153 ASSERT_FALSE(ParseAnswer(json));
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));
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));
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));
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));
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));
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());
212 const SuggestionAnswer::ImageLine& first_line = answer->first_line();
213 EXPECT_EQ(2U, first_line.text_fields().size());
214 EXPECT_EQ(base::UTF8ToUTF16("text"), first_line.text_fields()[0].text());
215 EXPECT_EQ(8, first_line.text_fields()[0].type());
216 EXPECT_EQ(base::UTF8ToUTF16("moar text"), first_line.text_fields()[1].text());
217 EXPECT_EQ(0, first_line.text_fields()[1].type());
219 EXPECT_FALSE(first_line.additional_text());
220 EXPECT_FALSE(first_line.status_text());
222 EXPECT_TRUE(first_line.image_url().is_valid());
223 EXPECT_EQ(GURL("https://example.com/foo.jpg"), first_line.image_url());
225 const SuggestionAnswer::ImageLine& second_line = answer->second_line();
226 EXPECT_EQ(1U, second_line.text_fields().size());
227 EXPECT_EQ(
228 base::UTF8ToUTF16("other text"), second_line.text_fields()[0].text());
229 EXPECT_EQ(5, second_line.text_fields()[0].type());
231 EXPECT_TRUE(second_line.additional_text());
232 EXPECT_EQ(
233 base::UTF8ToUTF16("slatfatf"), second_line.additional_text()->text());
234 EXPECT_EQ(42, second_line.additional_text()->type());
236 EXPECT_TRUE(second_line.status_text());
237 EXPECT_EQ(
238 base::UTF8ToUTF16("oh hi, Mark"), second_line.status_text()->text());
239 EXPECT_EQ(729347, second_line.status_text()->type());
241 EXPECT_FALSE(second_line.image_url().is_valid());
244 TEST(SuggestionAnswerTest, AddImageURLsTo) {
245 SuggestionAnswer::URLs urls;
246 std::string json =
247 "{ \"l\": ["
248 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
249 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } }] }";
250 scoped_ptr<SuggestionAnswer> answer = ParseAnswer(json);
251 ASSERT_TRUE(answer);
252 answer->AddImageURLsTo(&urls);
253 ASSERT_EQ(0U, urls.size());
255 json =
256 "{ \"l\" : ["
257 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
258 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
259 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}";
260 answer = ParseAnswer(json);
261 ASSERT_TRUE(answer);
262 answer->AddImageURLsTo(&urls);
263 ASSERT_EQ(1U, urls.size());
264 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[0]);
266 json =
267 "{ \"l\" : ["
268 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }],"
269 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, "
270 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
271 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}";
272 answer = ParseAnswer(json);
273 ASSERT_TRUE(answer);
274 answer->AddImageURLsTo(&urls);
275 ASSERT_EQ(3U, urls.size());
276 EXPECT_EQ(GURL("https://gstatic.com/foo.png"), urls[1]);
277 EXPECT_EQ(GURL("https://gstatic.com/bar.jpg"), urls[2]);