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 "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace autofill { | |
13 | |
14 class SaveCardBubbleControllerImplTest | |
15 : public ChromeRenderViewHostTestHarness { | |
16 public: | |
17 SaveCardBubbleControllerImplTest() {} | |
18 | |
19 void SetUp() override { | |
20 ChromeRenderViewHostTestHarness::SetUp(); | |
21 SaveCardBubbleControllerImpl::CreateForWebContents(web_contents()); | |
22 } | |
23 | |
24 void SetLegalMessageExpectSuccess(const std::string& message_json) { | |
25 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); | |
26 const base::ListValue* lines; | |
27 ASSERT_TRUE(value->GetAsList(&lines)); | |
28 EXPECT_TRUE(controller()->SetLegalMessage(*lines)); | |
29 } | |
30 | |
31 void SetLegalMessageExpectFailure(const std::string& message_json) { | |
32 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); | |
33 const base::ListValue* lines; | |
34 ASSERT_TRUE(value->GetAsList(&lines)); | |
35 EXPECT_FALSE(controller()->SetLegalMessage(*lines)); | |
36 } | |
37 | |
38 // Returns true if lines are the same. | |
39 bool CompareLegalMessageLines( | |
40 const SaveCardBubbleController::LegalMessageLine& a, | |
41 const SaveCardBubbleController::LegalMessageLine& b) { | |
42 if (a.text != b.text) | |
43 return false; | |
44 if (a.links.size() != b.links.size()) | |
45 return false; | |
46 for (size_t i = 0; i < a.links.size(); ++i) { | |
47 if (a.links[i].range != b.links[i].range) | |
48 return false; | |
49 if (a.links[i].url != b.links[i].url) | |
50 return false; | |
51 } | |
52 return true; | |
53 } | |
54 | |
55 // Returns true if messages are the same. | |
56 bool CompareLegalMessage( | |
bondd
2015/11/13 22:30:44
AFAIK I can't use operator== on LegalMessageLine w
Evan Stade
2015/11/14 00:22:50
sgtm
| |
57 const std::vector<SaveCardBubbleController::LegalMessageLine>& a, | |
58 const std::vector<SaveCardBubbleController::LegalMessageLine>& b) { | |
59 if (a.size() != b.size()) | |
60 return false; | |
61 for (size_t i = 0; i < a.size(); ++i) { | |
62 if (!CompareLegalMessageLines(a[i], b[i])) | |
63 return false; | |
64 } | |
65 return true; | |
66 } | |
67 | |
68 protected: | |
69 SaveCardBubbleControllerImpl* controller() { | |
70 return SaveCardBubbleControllerImpl::FromWebContents(web_contents()); | |
71 } | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(SaveCardBubbleControllerImplTest); | |
75 }; | |
76 | |
77 TEST_F(SaveCardBubbleControllerImplTest, NoParameters) { | |
78 SetLegalMessageExpectSuccess( | |
79 "[ {" | |
80 " \"template\": \"This is the entire message.\"" | |
81 "} ]"); | |
82 | |
83 SaveCardBubbleController::LegalMessageLine expected_line; | |
84 expected_line.text = base::ASCIIToUTF16("This is the entire message."); | |
85 std::vector<SaveCardBubbleController::LegalMessageLine> expected = | |
86 std::vector<SaveCardBubbleController::LegalMessageLine>(); | |
87 expected.push_back(expected_line); | |
88 | |
89 EXPECT_TRUE( | |
90 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
91 } | |
92 | |
93 TEST_F(SaveCardBubbleControllerImplTest, SingleParameter) { | |
94 SetLegalMessageExpectSuccess( | |
95 "[ {" | |
96 " \"template\": \"Panda {0}.\"," | |
97 " \"template_parameter\": [ {" | |
98 " \"display_text\": \"bears are fuzzy\"," | |
99 " \"url\": \"http://www.example.com\"" | |
100 " } ]" | |
101 "} ]"); | |
102 | |
103 SaveCardBubbleController::LegalMessageLine expected_line; | |
104 expected_line.text = base::ASCIIToUTF16("Panda bears are fuzzy."); | |
105 | |
106 SaveCardBubbleController::LegalMessageLine::Link link; | |
107 link.range = gfx::Range(6, 21); | |
108 link.url = GURL("http://www.example.com"); | |
109 expected_line.links.push_back(link); | |
110 | |
111 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
112 expected_line}; | |
113 EXPECT_TRUE( | |
114 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
115 } | |
116 | |
117 TEST_F(SaveCardBubbleControllerImplTest, MissingUrl) { | |
118 SetLegalMessageExpectFailure( | |
119 "[ {" | |
120 " \"template\": \"Panda {0}.\"," | |
121 " \"template_parameter\": [ {" | |
122 " \"display_text\": \"bear\"" | |
123 " } ]" | |
124 "} ]"); | |
125 EXPECT_TRUE(CompareLegalMessage( | |
126 std::vector<SaveCardBubbleController::LegalMessageLine>(), | |
127 controller()->GetLegalMessageLines())); | |
128 } | |
129 | |
130 TEST_F(SaveCardBubbleControllerImplTest, MissingDisplayText) { | |
131 SetLegalMessageExpectFailure( | |
132 "[ {" | |
133 " \"template\": \"Panda {0}.\"," | |
134 " \"template_parameter\": [ {" | |
135 " \"url\": \"http://www.example.com\"" | |
136 " } ]" | |
137 "} ]"); | |
138 EXPECT_TRUE(CompareLegalMessage( | |
139 std::vector<SaveCardBubbleController::LegalMessageLine>(), | |
140 controller()->GetLegalMessageLines())); | |
141 } | |
142 | |
143 TEST_F(SaveCardBubbleControllerImplTest, EscapeCharacters) { | |
144 SetLegalMessageExpectSuccess( | |
145 "[ {" | |
146 " \"template\": \"Panda '{'{0}'}' '{1}' don't $1.\"," | |
147 " \"template_parameter\": [ {" | |
148 " \"display_text\": \"bears\"," | |
149 " \"url\": \"http://www.example.com\"" | |
150 " } ]" | |
151 "} ]"); | |
152 | |
153 SaveCardBubbleController::LegalMessageLine expected_line; | |
154 expected_line.text = base::ASCIIToUTF16("Panda {bears} {1} don't $1."); | |
155 | |
156 SaveCardBubbleController::LegalMessageLine::Link link; | |
157 link.range = gfx::Range(7, 12); | |
158 link.url = GURL("http://www.example.com"); | |
159 expected_line.links.push_back(link); | |
160 | |
161 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
162 expected_line}; | |
163 EXPECT_TRUE( | |
164 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
165 } | |
166 | |
167 TEST_F(SaveCardBubbleControllerImplTest, ConsecutiveDollarSigns) { | |
168 SetLegalMessageExpectSuccess( | |
169 "[ {" | |
170 " \"template\": \"$$\"" | |
171 "} ]"); | |
172 | |
173 // Consecutive dollar signs do not expand correctly (see comment in | |
174 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
175 // If this is fixed and this test starts to fail, please update the | |
176 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
177 // header file comment. | |
178 SaveCardBubbleController::LegalMessageLine expected_line; | |
179 expected_line.text = base::ASCIIToUTF16("$$$"); | |
180 | |
181 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
182 expected_line}; | |
183 EXPECT_TRUE( | |
184 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
185 } | |
186 | |
187 TEST_F(SaveCardBubbleControllerImplTest, DollarAndParenthesis) { | |
188 // "${" does not expand correctly (see comment in | |
189 // ReplaceTemplatePlaceholders() in save_card_bubble_controller_impl.cc). | |
190 // If this is fixed and this test starts to fail, please update the | |
191 // "Caveats" section of the SaveCardBubbleControllerImpl::SetLegalMessage() | |
192 // header file comment. | |
193 SetLegalMessageExpectFailure( | |
194 "[ {" | |
195 " \"template\": \"${0}\"," | |
196 " \"template_parameter\": [ {" | |
197 " \"display_text\": \"bears\"," | |
198 " \"url\": \"http://www.example.com\"" | |
199 " } ]" | |
200 "} ]"); | |
201 } | |
202 | |
203 TEST_F(SaveCardBubbleControllerImplTest, MultipleParameters) { | |
204 SetLegalMessageExpectSuccess( | |
205 "[ {" | |
206 " \"template\": \"Panda {0} like {2} eat {1}.\"," | |
207 " \"template_parameter\": [ {" | |
208 " \"display_text\": \"bears\"," | |
209 " \"url\": \"http://www.example.com/0\"" | |
210 " }, {" | |
211 " \"display_text\": \"bamboo\"," | |
212 " \"url\": \"http://www.example.com/1\"" | |
213 " }, {" | |
214 " \"display_text\": \"to\"," | |
215 " \"url\": \"http://www.example.com/2\"" | |
216 " } ]" | |
217 "} ]"); | |
218 | |
219 SaveCardBubbleController::LegalMessageLine expected_line; | |
220 expected_line.text = base::ASCIIToUTF16("Panda bears like to eat bamboo."); | |
221 | |
222 SaveCardBubbleController::LegalMessageLine::Link link; | |
223 link.range = gfx::Range(6, 11); | |
224 link.url = GURL("http://www.example.com/0"); | |
225 expected_line.links.push_back(link); | |
226 | |
227 link.range = gfx::Range(24, 30); | |
228 link.url = GURL("http://www.example.com/1"); | |
229 expected_line.links.push_back(link); | |
230 | |
231 link.range = gfx::Range(17, 19); | |
232 link.url = GURL("http://www.example.com/2"); | |
233 expected_line.links.push_back(link); | |
234 | |
235 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
236 expected_line}; | |
237 EXPECT_TRUE( | |
238 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
239 } | |
240 | |
241 TEST_F(SaveCardBubbleControllerImplTest, MultipleLineElements) { | |
242 SetLegalMessageExpectSuccess( | |
243 "[ {" | |
244 " \"template\": \"Panda {0}\"," | |
245 " \"template_parameter\": [ {" | |
246 " \"display_text\": \"bears\"," | |
247 " \"url\": \"http://www.example.com/line_0_param_0\"" | |
248 " } ]" | |
249 "}, {" | |
250 " \"template\": \"like {1} eat {0}.\"," | |
251 " \"template_parameter\": [ {" | |
252 " \"display_text\": \"bamboo\"," | |
253 " \"url\": \"http://www.example.com/line_1_param_0\"" | |
254 " }, {" | |
255 " \"display_text\": \"to\"," | |
256 " \"url\": \"http://www.example.com/line_1_param_1\"" | |
257 " } ]" | |
258 "}, {" | |
259 " \"template\": \"The {0}.\"," | |
260 " \"template_parameter\": [ {" | |
261 " \"display_text\": \"end\"," | |
262 " \"url\": \"http://www.example.com/line_2_param_0\"" | |
263 " } ]" | |
264 "} ]"); | |
265 | |
266 // Line 0. | |
267 SaveCardBubbleController::LegalMessageLine expected_line_0; | |
268 expected_line_0.text = base::ASCIIToUTF16("Panda bears"); | |
269 | |
270 SaveCardBubbleController::LegalMessageLine::Link link; | |
271 link.range = gfx::Range(6, 11); | |
272 link.url = GURL("http://www.example.com/line_0_param_0"); | |
273 expected_line_0.links.push_back(link); | |
274 | |
275 // Line 1. | |
276 SaveCardBubbleController::LegalMessageLine expected_line_1; | |
277 expected_line_1.text = base::ASCIIToUTF16("like to eat bamboo."); | |
278 | |
279 link.range = gfx::Range(12, 18); | |
280 link.url = GURL("http://www.example.com/line_1_param_0"); | |
281 expected_line_1.links.push_back(link); | |
282 | |
283 link.range = gfx::Range(5, 7); | |
284 link.url = GURL("http://www.example.com/line_1_param_1"); | |
285 expected_line_1.links.push_back(link); | |
286 | |
287 // Line 2. | |
288 SaveCardBubbleController::LegalMessageLine expected_line_2; | |
289 expected_line_2.text = base::ASCIIToUTF16("The end."); | |
290 | |
291 link.range = gfx::Range(4, 7); | |
292 link.url = GURL("http://www.example.com/line_2_param_0"); | |
293 expected_line_2.links.push_back(link); | |
294 | |
295 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
296 expected_line_0, expected_line_1, expected_line_2}; | |
297 EXPECT_TRUE( | |
298 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
299 } | |
300 | |
301 TEST_F(SaveCardBubbleControllerImplTest, EmbeddedNewlines) { | |
302 SetLegalMessageExpectSuccess( | |
303 "[ {" | |
304 " \"template\": \"Panda {0}\nlike {2} eat {1}.\nThe {3}.\"," | |
305 " \"template_parameter\": [ {" | |
306 " \"display_text\": \"bears\"," | |
307 " \"url\": \"http://www.example.com/0\"" | |
308 " }, {" | |
309 " \"display_text\": \"bamboo\"," | |
310 " \"url\": \"http://www.example.com/1\"" | |
311 " }, {" | |
312 " \"display_text\": \"to\"," | |
313 " \"url\": \"http://www.example.com/2\"" | |
314 " }, {" | |
315 " \"display_text\": \"end\"," | |
316 " \"url\": \"http://www.example.com/3\"" | |
317 " } ]" | |
318 "} ]"); | |
319 | |
320 SaveCardBubbleController::LegalMessageLine expected_line; | |
321 expected_line.text = | |
322 base::ASCIIToUTF16("Panda bears\nlike to eat bamboo.\nThe end."); | |
323 | |
324 SaveCardBubbleController::LegalMessageLine::Link link; | |
325 link.range = gfx::Range(6, 11); | |
326 link.url = GURL("http://www.example.com/0"); | |
327 expected_line.links.push_back(link); | |
328 | |
329 link.range = gfx::Range(24, 30); | |
330 link.url = GURL("http://www.example.com/1"); | |
331 expected_line.links.push_back(link); | |
332 | |
333 link.range = gfx::Range(17, 19); | |
334 link.url = GURL("http://www.example.com/2"); | |
335 expected_line.links.push_back(link); | |
336 | |
337 link.range = gfx::Range(36, 39); | |
338 link.url = GURL("http://www.example.com/3"); | |
339 expected_line.links.push_back(link); | |
340 | |
341 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
342 expected_line}; | |
343 EXPECT_TRUE( | |
344 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
345 } | |
346 | |
347 TEST_F(SaveCardBubbleControllerImplTest, MaximumPlaceholders) { | |
348 SetLegalMessageExpectSuccess( | |
349 "[ {" | |
350 " \"template\": \"a{0} b{1} c{2} d{3} e{4} f{5} g{6}\"," | |
351 " \"template_parameter\": [ {" | |
352 " \"display_text\": \"A\"," | |
353 " \"url\": \"http://www.example.com/0\"" | |
354 " }, {" | |
355 " \"display_text\": \"B\"," | |
356 " \"url\": \"http://www.example.com/1\"" | |
357 " }, {" | |
358 " \"display_text\": \"C\"," | |
359 " \"url\": \"http://www.example.com/2\"" | |
360 " }, {" | |
361 " \"display_text\": \"D\"," | |
362 " \"url\": \"http://www.example.com/3\"" | |
363 " }, {" | |
364 " \"display_text\": \"E\"," | |
365 " \"url\": \"http://www.example.com/4\"" | |
366 " }, {" | |
367 " \"display_text\": \"F\"," | |
368 " \"url\": \"http://www.example.com/5\"" | |
369 " }, {" | |
370 " \"display_text\": \"G\"," | |
371 " \"url\": \"http://www.example.com/6\"" | |
372 " } ]" | |
373 "} ]"); | |
374 | |
375 SaveCardBubbleController::LegalMessageLine expected_line; | |
376 expected_line.text = base::ASCIIToUTF16("aA bB cC dD eE fF gG"); | |
377 | |
378 SaveCardBubbleController::LegalMessageLine::Link link; | |
379 link.range = gfx::Range(1, 2); | |
380 link.url = GURL("http://www.example.com/0"); | |
Evan Stade
2015/11/14 00:22:50
you can greatly improve the legibility of this tes
bondd
2015/11/17 00:12:04
Thanks. I've made this test case and most of the o
| |
381 expected_line.links.push_back(link); | |
382 | |
383 link.range = gfx::Range(4, 5); | |
384 link.url = GURL("http://www.example.com/1"); | |
385 expected_line.links.push_back(link); | |
386 | |
387 link.range = gfx::Range(7, 8); | |
388 link.url = GURL("http://www.example.com/2"); | |
389 expected_line.links.push_back(link); | |
390 | |
391 link.range = gfx::Range(10, 11); | |
392 link.url = GURL("http://www.example.com/3"); | |
393 expected_line.links.push_back(link); | |
394 | |
395 link.range = gfx::Range(13, 14); | |
396 link.url = GURL("http://www.example.com/4"); | |
397 expected_line.links.push_back(link); | |
398 | |
399 link.range = gfx::Range(16, 17); | |
400 link.url = GURL("http://www.example.com/5"); | |
401 expected_line.links.push_back(link); | |
402 | |
403 link.range = gfx::Range(19, 20); | |
404 link.url = GURL("http://www.example.com/6"); | |
405 expected_line.links.push_back(link); | |
406 | |
407 std::vector<SaveCardBubbleController::LegalMessageLine> expected = { | |
408 expected_line}; | |
409 EXPECT_TRUE( | |
410 CompareLegalMessage(expected, controller()->GetLegalMessageLines())); | |
411 } | |
412 | |
413 } // namespace autofill | |
OLD | NEW |