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