OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/autofill/core/browser/legal_message_line.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/json/json_reader.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace autofill { | |
17 | |
18 class LegalMessageLineTest : public ::testing::Test { | |
19 public: | |
20 LegalMessageLineTest() {} | |
21 ~LegalMessageLineTest() override {} | |
22 | |
23 void SetLegalMessage(const std::string& message_json) { | |
24 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); | |
25 ASSERT_TRUE(value); | |
26 base::DictionaryValue* dictionary = nullptr; | |
27 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); | |
28 ASSERT_TRUE(dictionary); | |
29 LegalMessageLine::Parse(*dictionary, &actual_lines_); | |
30 } | |
31 | |
32 // Returns true if lines are the same. | |
gone
2016/01/08 00:08:09
nit: Private & static?
please use gerrit instead
2016/01/12 00:47:25
Done.
| |
33 bool CompareLegalMessageLines(const LegalMessageLine& a, | |
34 const LegalMessageLine& b) { | |
35 if (a.text() != b.text()) | |
36 return false; | |
37 if (a.links().size() != b.links().size()) | |
38 return false; | |
39 for (size_t i = 0; i < a.links().size(); ++i) { | |
40 if (a.links()[i].range != b.links()[i].range) | |
41 return false; | |
42 if (a.links()[i].url != b.links()[i].url) | |
43 return false; | |
44 } | |
45 return true; | |
46 } | |
47 | |
48 // Returns true if lines are the same. | |
49 bool CompareLegalMessages(const LegalMessageLine& single_line, | |
50 const LegalMessageLines& b) { | |
51 return b.size() == 1 && CompareLegalMessageLines(single_line, *b[0]); | |
52 } | |
53 | |
54 // Returns true if messages are the same. | |
55 bool CompareLegalMessages(const LegalMessageLines& a, | |
56 const LegalMessageLines& b) { | |
57 if (a.size() != b.size()) | |
58 return false; | |
59 for (size_t i = 0; i < a.size(); ++i) { | |
60 if (!CompareLegalMessageLines(*a[i], *b[i])) | |
61 return false; | |
62 } | |
63 return true; | |
64 } | |
65 | |
66 const LegalMessageLines& actual_lines() const { | |
67 return actual_lines_; | |
68 } | |
69 | |
70 private: | |
71 LegalMessageLines actual_lines_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(LegalMessageLineTest); | |
74 }; | |
75 | |
76 TEST_F(LegalMessageLineTest, NoParameters) { | |
77 SetLegalMessage( | |
78 "{" | |
79 " \"line\" : [ {" | |
80 " \"template\": \"This is the entire message.\"" | |
81 " } ]" | |
82 "}"); | |
83 | |
84 LegalMessageLine expected_line; | |
85 expected_line.text_ = base::ASCIIToUTF16("This is the entire message."); | |
86 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
87 } | |
88 | |
89 TEST_F(LegalMessageLineTest, SingleParameter) { | |
90 SetLegalMessage( | |
91 "{" | |
92 " \"line\" : [ {" | |
93 " \"template\": \"Panda {0}.\"," | |
94 " \"template_parameter\": [ {" | |
95 " \"display_text\": \"bears are fuzzy\"," | |
96 " \"url\": \"http://www.example.com\"" | |
97 " } ]" | |
98 " } ]" | |
99 "}"); | |
100 | |
101 LegalMessageLine expected_line; | |
102 expected_line.text_ = base::ASCIIToUTF16("Panda bears are fuzzy."); | |
103 expected_line.links_ = { | |
104 {{6, 21}, GURL("http://www.example.com")}, | |
105 }; | |
106 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
107 } | |
108 | |
109 TEST_F(LegalMessageLineTest, MissingUrl) { | |
110 SetLegalMessage( | |
111 "{" | |
112 " \"line\" : [ {" | |
113 " \"template\": \"Panda {0}.\"," | |
114 " \"template_parameter\": [ {" | |
115 " \"display_text\": \"bear\"" | |
116 " } ]" | |
117 " } ]" | |
118 "}"); | |
119 // Legal message is invalid so actual_lines() should return no lines. | |
120 EXPECT_TRUE( | |
121 CompareLegalMessages(LegalMessageLines(), actual_lines())); | |
122 } | |
123 | |
124 TEST_F(LegalMessageLineTest, MissingDisplayText) { | |
125 SetLegalMessage( | |
126 "{" | |
127 " \"line\" : [ {" | |
128 " \"template\": \"Panda {0}.\"," | |
129 " \"template_parameter\": [ {" | |
130 " \"url\": \"http://www.example.com\"" | |
131 " } ]" | |
132 " } ]" | |
133 "}"); | |
134 // Legal message is invalid so actual_lines() should return no lines. | |
135 EXPECT_TRUE( | |
136 CompareLegalMessages(LegalMessageLines(), actual_lines())); | |
137 } | |
138 | |
139 TEST_F(LegalMessageLineTest, EscapeCharacters) { | |
140 SetLegalMessage( | |
141 "{" | |
142 " \"line\" : [ {" | |
143 " \"template\": \"Panda '{'{0}'}' '{1}' don't $1.\"," | |
144 " \"template_parameter\": [ {" | |
145 " \"display_text\": \"bears\"," | |
146 " \"url\": \"http://www.example.com\"" | |
147 " } ]" | |
148 " } ]" | |
149 "}"); | |
150 | |
151 LegalMessageLine expected_line; | |
152 expected_line.text_ = base::ASCIIToUTF16("Panda {bears} {1} don't $1."); | |
153 expected_line.links_ = { | |
154 {{7, 12}, GURL("http://www.example.com")}, | |
155 }; | |
156 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
157 } | |
158 | |
159 TEST_F(LegalMessageLineTest, ConsecutiveDollarSigns) { | |
160 SetLegalMessage( | |
161 "{" | |
162 " \"line\" : [ {" | |
163 " \"template\": \"$$\"" | |
164 " } ]" | |
165 "}"); | |
166 | |
167 // Consecutive dollar signs do not expand correctly (see comment in | |
168 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
169 // If this is fixed and this test starts to fail, please update the | |
170 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
171 // header file comment. | |
172 LegalMessageLine expected_line; | |
173 expected_line.text_ = base::ASCIIToUTF16("$$$"); | |
174 | |
175 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
176 } | |
177 | |
178 TEST_F(LegalMessageLineTest, DollarAndParenthesis) { | |
179 // "${" does not expand correctly (see comment in | |
180 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
181 // If this is fixed and this test starts to fail, please update the | |
182 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
183 // header file comment. | |
184 SetLegalMessage( | |
185 "{" | |
186 " \"line\" : [ {" | |
187 " \"template\": \"${0}\"," | |
188 " \"template_parameter\": [ {" | |
189 " \"display_text\": \"bears\"," | |
190 " \"url\": \"http://www.example.com\"" | |
191 " } ]" | |
192 " } ]" | |
193 "}"); | |
194 // Legal message is invalid so actual_lines() should return no lines. | |
195 EXPECT_TRUE( | |
196 CompareLegalMessages(LegalMessageLines(), actual_lines())); | |
197 } | |
198 | |
199 TEST_F(LegalMessageLineTest, MultipleParameters) { | |
200 SetLegalMessage( | |
201 "{" | |
202 " \"line\" : [ {" | |
203 " \"template\": \"Panda {0} like {2} eat {1}.\"," | |
204 " \"template_parameter\": [ {" | |
205 " \"display_text\": \"bears\"," | |
206 " \"url\": \"http://www.example.com/0\"" | |
207 " }, {" | |
208 " \"display_text\": \"bamboo\"," | |
209 " \"url\": \"http://www.example.com/1\"" | |
210 " }, {" | |
211 " \"display_text\": \"to\"," | |
212 " \"url\": \"http://www.example.com/2\"" | |
213 " } ]" | |
214 " } ]" | |
215 "}"); | |
216 | |
217 LegalMessageLine expected_line; | |
218 expected_line.text_ = base::ASCIIToUTF16("Panda bears like to eat bamboo."); | |
219 expected_line.links_ = { | |
220 {{6, 11}, GURL("http://www.example.com/0")}, | |
221 {{24, 30}, GURL("http://www.example.com/1")}, | |
222 {{17, 19}, GURL("http://www.example.com/2")}, | |
223 }; | |
224 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
225 } | |
226 | |
227 TEST_F(LegalMessageLineTest, MultipleLineElements) { | |
228 SetLegalMessage( | |
229 "{" | |
230 " \"line\" : [ {" | |
231 " \"template\": \"Panda {0}\"," | |
232 " \"template_parameter\": [ {" | |
233 " \"display_text\": \"bears\"," | |
234 " \"url\": \"http://www.example.com/line_0_param_0\"" | |
235 " } ]" | |
236 " }, {" | |
237 " \"template\": \"like {1} eat {0}.\"," | |
238 " \"template_parameter\": [ {" | |
239 " \"display_text\": \"bamboo\"," | |
240 " \"url\": \"http://www.example.com/line_1_param_0\"" | |
241 " }, {" | |
242 " \"display_text\": \"to\"," | |
243 " \"url\": \"http://www.example.com/line_1_param_1\"" | |
244 " } ]" | |
245 " }, {" | |
246 " \"template\": \"The {0}.\"," | |
247 " \"template_parameter\": [ {" | |
248 " \"display_text\": \"end\"," | |
249 " \"url\": \"http://www.example.com/line_2_param_0\"" | |
250 " } ]" | |
251 " } ]" | |
252 "}"); | |
253 | |
254 LegalMessageLines expected(3); | |
255 | |
256 // Line 0. | |
257 expected[0].reset(new LegalMessageLine); | |
258 expected[0]->text_ = base::ASCIIToUTF16("Panda bears"); | |
259 expected[0]->links_ = { | |
260 {{6, 11}, GURL("http://www.example.com/line_0_param_0")}, | |
261 }; | |
262 | |
263 // Line 1. | |
264 expected[1].reset(new LegalMessageLine); | |
265 expected[1]->text_ = base::ASCIIToUTF16("like to eat bamboo."); | |
266 expected[1]->links_ = { | |
267 {{12, 18}, GURL("http://www.example.com/line_1_param_0")}, | |
268 {{5, 7}, GURL("http://www.example.com/line_1_param_1")}, | |
269 }; | |
270 | |
271 // Line 2. | |
272 expected[2].reset(new LegalMessageLine); | |
273 expected[2]->text_ = base::ASCIIToUTF16("The end."); | |
274 expected[2]->links_ = { | |
275 {{4, 7}, GURL("http://www.example.com/line_2_param_0")}, | |
276 }; | |
277 | |
278 EXPECT_TRUE(CompareLegalMessages(expected, actual_lines())); | |
279 } | |
280 | |
281 TEST_F(LegalMessageLineTest, EmbeddedNewlines) { | |
282 SetLegalMessage( | |
283 "{" | |
284 " \"line\" : [ {" | |
285 " \"template\": \"Panda {0}\nlike {2} eat {1}.\nThe {3}.\"," | |
286 " \"template_parameter\": [ {" | |
287 " \"display_text\": \"bears\"," | |
288 " \"url\": \"http://www.example.com/0\"" | |
289 " }, {" | |
290 " \"display_text\": \"bamboo\"," | |
291 " \"url\": \"http://www.example.com/1\"" | |
292 " }, {" | |
293 " \"display_text\": \"to\"," | |
294 " \"url\": \"http://www.example.com/2\"" | |
295 " }, {" | |
296 " \"display_text\": \"end\"," | |
297 " \"url\": \"http://www.example.com/3\"" | |
298 " } ]" | |
299 " } ]" | |
300 "}"); | |
301 | |
302 LegalMessageLine expected_line; | |
303 expected_line.text_ = | |
304 base::ASCIIToUTF16("Panda bears\nlike to eat bamboo.\nThe end."); | |
305 expected_line.links_ = { | |
306 {{6, 11}, GURL("http://www.example.com/0")}, | |
307 {{24, 30}, GURL("http://www.example.com/1")}, | |
308 {{17, 19}, GURL("http://www.example.com/2")}, | |
309 {{36, 39}, GURL("http://www.example.com/3")}, | |
310 }; | |
311 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
312 } | |
313 | |
314 TEST_F(LegalMessageLineTest, MaximumPlaceholders) { | |
315 SetLegalMessage( | |
316 "{" | |
317 " \"line\" : [ {" | |
318 " \"template\": \"a{0} b{1} c{2} d{3} e{4} f{5} g{6}\"," | |
319 " \"template_parameter\": [ {" | |
320 " \"display_text\": \"A\"," | |
321 " \"url\": \"http://www.example.com/0\"" | |
322 " }, {" | |
323 " \"display_text\": \"B\"," | |
324 " \"url\": \"http://www.example.com/1\"" | |
325 " }, {" | |
326 " \"display_text\": \"C\"," | |
327 " \"url\": \"http://www.example.com/2\"" | |
328 " }, {" | |
329 " \"display_text\": \"D\"," | |
330 " \"url\": \"http://www.example.com/3\"" | |
331 " }, {" | |
332 " \"display_text\": \"E\"," | |
333 " \"url\": \"http://www.example.com/4\"" | |
334 " }, {" | |
335 " \"display_text\": \"F\"," | |
336 " \"url\": \"http://www.example.com/5\"" | |
337 " }, {" | |
338 " \"display_text\": \"G\"," | |
339 " \"url\": \"http://www.example.com/6\"" | |
340 " } ]" | |
341 " } ]" | |
342 "}"); | |
343 | |
344 LegalMessageLine expected_line; | |
345 expected_line.text_ = base::ASCIIToUTF16("aA bB cC dD eE fF gG"); | |
346 expected_line.links_ = { | |
347 {{1, 2}, GURL("http://www.example.com/0")}, | |
348 {{4, 5}, GURL("http://www.example.com/1")}, | |
349 {{7, 8}, GURL("http://www.example.com/2")}, | |
350 {{10, 11}, GURL("http://www.example.com/3")}, | |
351 {{13, 14}, GURL("http://www.example.com/4")}, | |
352 {{16, 17}, GURL("http://www.example.com/5")}, | |
353 {{19, 20}, GURL("http://www.example.com/6")}, | |
354 }; | |
355 EXPECT_TRUE(CompareLegalMessages(expected_line, actual_lines())); | |
356 } | |
357 | |
358 } // namespace autofill | |
OLD | NEW |