Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: components/omnibox/suggestion_answer_unittest.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed some unit test issues Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 scoped_ptr<SuggestionAnswer> answer2(new SuggestionAnswer());
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 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
30 EXPECT_TRUE(answer2->Equals(SuggestionAnswer(*answer2)));
31 }
32
33 TEST(SuggestionAnswerTest, DifferentValuesAreUnequal) {
34 std::string json =
35 "{ \"l\": ["
36 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
37 " { \"t\": \"moar text\", \"tt\": 0 }], "
38 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
39 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
40 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
41 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
42 "] }";
43 scoped_ptr<SuggestionAnswer> answer1;
44 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer1));
45
46 // Exactly the same just a different type for one of the text type values.
47 json =
48 "{ \"l\": ["
49 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
50 " { \"t\": \"moar text\", \"tt\": 1 }], "
51 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
52 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
53 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
54 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
55 "] }";
56 scoped_ptr<SuggestionAnswer> answer2;
57 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
58
59 EXPECT_FALSE(answer1->Equals(*answer2));
60
61 scoped_ptr<SuggestionAnswer> answer3 = SuggestionAnswer::Copy(answer1.get());
62 EXPECT_TRUE(answer1->Equals(*answer3));
63 answer3->set_type(44);
64 EXPECT_FALSE(answer1->Equals(*answer3));
65 }
66
67 TEST(SuggestionAnswerTest, EmptyJsonIsInvalid) {
68 std::string json;
69 scoped_ptr<SuggestionAnswer> answer;
70 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
71 }
72
73 TEST(SuggestionAnswerTest, MalformedJsonIsInvalid) {
74 std::string json = "} malformed json {";
75 scoped_ptr<SuggestionAnswer> answer;
76 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
77 }
78
79 TEST(SuggestionAnswerTest, TextFieldsRequireBothTextAndType) {
80 std::string json =
81 "{ \"l\": ["
82 " { \"il\": { \"t\": [{ \"t\": \"text\" }] } }, "
83 "] }";
84 scoped_ptr<SuggestionAnswer> answer1;
85 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
86
87 json =
88 "{ \"l\": ["
89 " { \"il\": { \"t\": [{ \"tt\": 8 }] } }, "
90 "] }";
91 scoped_ptr<SuggestionAnswer> answer2;
92 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer2));
93 }
94
95 TEST(SuggestionAnswerTest, ImageLinesMustContainAtLeastOneTextField) {
96 std::string json =
97 "{ \"l\": ["
98 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
99 " { \"t\": \"moar text\", \"tt\": 0 }], "
100 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
101 " { \"il\": { \"t\": [], "
102 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
103 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
104 "] }";
105 scoped_ptr<SuggestionAnswer> answer;
106 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer));
107 }
108
109 TEST(SuggestionAnswerTest, ExactlyTwoLinesRequired) {
110 std::string json =
111 "{ \"l\": ["
112 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
113 "] }";
114 scoped_ptr<SuggestionAnswer> answer1;
115 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
116
117 json =
118 "{ \"l\": ["
119 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
120 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
121 "] }";
122 scoped_ptr<SuggestionAnswer> answer2;
123 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
124
125 json =
126 "{ \"l\": ["
127 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
128 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }] } } "
129 " { \"il\": { \"t\": [{ \"t\": \"yet more text\", \"tt\": 13 }] } } "
130 "] }";
131 scoped_ptr<SuggestionAnswer> answer3;
132 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer3));
133 }
134
135 TEST(SuggestionAnswerTest, URLPresent) {
136 std::string json =
137 "{ \"l\": ["
138 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
139 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
140 " \"i\": { \"d\": \"\" } } } "
141 "] }";
142 scoped_ptr<SuggestionAnswer> answer1;
143 ASSERT_FALSE(SuggestionAnswer::ParseAnswer(json, &answer1));
144
145 json =
146 "{ \"l\": ["
147 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
148 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
149 " \"i\": { \"d\": \"https://example.com/foo.jpg\" } } } "
150 "] }";
151 scoped_ptr<SuggestionAnswer> answer2;
152 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
153
154 json =
155 "{ \"l\": ["
156 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }] } }, "
157 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
158 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } } "
159 "] }";
160 scoped_ptr<SuggestionAnswer> answer3;
161 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer3));
162 }
163
164 TEST(SuggestionAnswerTest, ValidPropertyValues) {
165 std::string json =
166 "{ \"l\": ["
167 " { \"il\": { \"t\": [{ \"t\": \"text\", \"tt\": 8 }, "
168 " { \"t\": \"moar text\", \"tt\": 0 }], "
169 " \"i\": { \"d\": \"//example.com/foo.jpg\" } } }, "
170 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 5 }], "
171 " \"at\": { \"t\": \"slatfatf\", \"tt\": 42 }, "
172 " \"st\": { \"t\": \"oh hi, Mark\", \"tt\": 729347 } } } "
173 "] }";
174 scoped_ptr<SuggestionAnswer> answer;
175 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer));
176 answer->set_type(420527);
177 EXPECT_EQ(420527, answer->type());
178
179 const SuggestionAnswer::ImageLine& first_line = answer->first_line();
180 EXPECT_EQ(2U, first_line.text_fields().size());
181 EXPECT_EQ("text", first_line.text_fields()[0].text());
182 EXPECT_EQ(8, first_line.text_fields()[0].type());
183 EXPECT_EQ("moar text", first_line.text_fields()[1].text());
184 EXPECT_EQ(0, first_line.text_fields()[1].type());
185
186 EXPECT_FALSE(first_line.additional_text());
187 EXPECT_FALSE(first_line.status_text());
188
189 EXPECT_TRUE(first_line.image_url().is_valid());
190 EXPECT_EQ("https://example.com/foo.jpg", first_line.image_url().spec());
Peter Kasting 2014/10/30 03:43:30 Nit: Technically you should probably either use po
groby-ooo-7-16 2014/10/30 16:21:14 Since it's based on json defined in the test, we s
Justin Donnelly 2014/10/30 19:23:14 Done.
191
192 const SuggestionAnswer::ImageLine& second_line = answer->second_line();
193 EXPECT_EQ(1U, second_line.text_fields().size());
194 EXPECT_EQ("other text", second_line.text_fields()[0].text());
195 EXPECT_EQ(5, second_line.text_fields()[0].type());
196
197 EXPECT_TRUE(second_line.additional_text());
198 EXPECT_EQ("slatfatf", second_line.additional_text()->text());
199 EXPECT_EQ(42, second_line.additional_text()->type());
200
201 EXPECT_TRUE(second_line.status_text());
202 EXPECT_EQ("oh hi, Mark", second_line.status_text()->text());
203 EXPECT_EQ(729347, second_line.status_text()->type());
204
205 EXPECT_FALSE(second_line.image_url().is_valid());
206 }
207
208 TEST(SuggestionAnswerTest, GetImageURLsWithValidImageLines) {
209 std::vector<GURL> urls;
Peter Kasting 2014/10/30 03:43:30 Nit: Use typedef
Justin Donnelly 2014/10/30 19:23:14 Done.
210 std::string json =
211 "{ \"l\" : ["
212 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }] } },"
213 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
214 " \"i\": { \"d\": \"//gstatic.com/foo.png\", \"t\": 3 }}}]}";
215
216 scoped_ptr<SuggestionAnswer> answer1;
217 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer1));
218 answer1->GetImageURLs(&urls);
219 ASSERT_EQ(1U, urls.size());
220 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec());
221
222 json =
223 "{ \"l\" : ["
224 " { \"il\": { \"t\": [{ \"t\": \"some text\", \"tt\": 5 }],"
225 " \"i\": { \"d\": \"//gstatic.com/foo.png\" } } }, "
226 " { \"il\": { \"t\": [{ \"t\": \"other text\", \"tt\": 8 }],"
227 " \"i\": { \"d\": \"//gstatic.com/bar.jpg\", \"t\": 3 }}}]}";
228
229 scoped_ptr<SuggestionAnswer> answer2;
230 ASSERT_TRUE(SuggestionAnswer::ParseAnswer(json, &answer2));
231 answer2->GetImageURLs(&urls);
232 ASSERT_EQ(2U, urls.size());
233 EXPECT_EQ("https://gstatic.com/foo.png", urls[0].spec());
234 EXPECT_EQ("https://gstatic.com/bar.jpg", urls[1].spec());
235 }
OLDNEW
« components/omnibox/suggestion_answer.cc ('K') | « components/omnibox/suggestion_answer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698