1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/autofill/autofill_field.h"
9 #include "chrome/browser/autofill/credit_card_field.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/glue/form_field.h"
12
13 namespace {
14
15 class CreditCardFieldTest : public testing::Test {
16 public:
CreditCardFieldTest()17 CreditCardFieldTest() {}
18
19 protected:
20 ScopedVector<AutofillField> list_;
21 scoped_ptr<CreditCardField> field_;
22 FieldTypeMap field_type_map_;
23 std::vector<AutofillField*>::const_iterator iter_;
24
25 private:
26 DISALLOW_COPY_AND_ASSIGN(CreditCardFieldTest);
27 };
28
TEST_F(CreditCardFieldTest,Empty)29 TEST_F(CreditCardFieldTest, Empty) {
30 list_.push_back(NULL);
31 iter_ = list_.begin();
32 field_.reset(CreditCardField::Parse(&iter_, false));
33 ASSERT_EQ(static_cast<CreditCardField*>(NULL), field_.get());
34 }
35
TEST_F(CreditCardFieldTest,NonParse)36 TEST_F(CreditCardFieldTest, NonParse) {
37 list_.push_back(new AutofillField);
38 list_.push_back(NULL);
39 iter_ = list_.begin();
40 field_.reset(CreditCardField::Parse(&iter_, false));
41 ASSERT_EQ(static_cast<CreditCardField*>(NULL), field_.get());
42 }
43
TEST_F(CreditCardFieldTest,ParseCreditCardNoNumber)44 TEST_F(CreditCardFieldTest, ParseCreditCardNoNumber) {
45 list_.push_back(
46 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Month"),
47 ASCIIToUTF16("ccmonth"),
48 string16(),
49 ASCIIToUTF16("text"),
50 0,
51 false),
52 ASCIIToUTF16("month1")));
53 list_.push_back(
54 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Year"),
55 ASCIIToUTF16("ccyear"),
56 string16(),
57 ASCIIToUTF16("text"),
58 0,
59 false),
60 ASCIIToUTF16("year1")));
61 list_.push_back(NULL);
62 iter_ = list_.begin();
63 field_.reset(CreditCardField::Parse(&iter_, false));
64 ASSERT_EQ(static_cast<CreditCardField*>(NULL), field_.get());
65 }
66
TEST_F(CreditCardFieldTest,ParseCreditCardNoDate)67 TEST_F(CreditCardFieldTest, ParseCreditCardNoDate) {
68 list_.push_back(
69 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
70 ASCIIToUTF16("card_number"),
71 string16(),
72 ASCIIToUTF16("text"),
73 0,
74 false),
75 ASCIIToUTF16("number1")));
76 list_.push_back(NULL);
77 iter_ = list_.begin();
78 field_.reset(CreditCardField::Parse(&iter_, false));
79 ASSERT_EQ(static_cast<CreditCardField*>(NULL), field_.get());
80 }
81
TEST_F(CreditCardFieldTest,ParseMiniumCreditCard)82 TEST_F(CreditCardFieldTest, ParseMiniumCreditCard) {
83 list_.push_back(
84 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
85 ASCIIToUTF16("card_number"),
86 string16(),
87 ASCIIToUTF16("text"),
88 0,
89 false),
90 ASCIIToUTF16("number1")));
91 list_.push_back(
92 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Month"),
93 ASCIIToUTF16("ccmonth"),
94 string16(),
95 ASCIIToUTF16("text"),
96 0,
97 false),
98 ASCIIToUTF16("month1")));
99 list_.push_back(
100 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Year"),
101 ASCIIToUTF16("ccyear"),
102 string16(),
103 ASCIIToUTF16("text"),
104 0,
105 false),
106 ASCIIToUTF16("year1")));
107 list_.push_back(NULL);
108 iter_ = list_.begin();
109 field_.reset(CreditCardField::Parse(&iter_, false));
110 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
111 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
112 ASSERT_TRUE(
113 field_type_map_.find(ASCIIToUTF16("number1")) != field_type_map_.end());
114 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number1")]);
115 ASSERT_TRUE(
116 field_type_map_.find(ASCIIToUTF16("month1")) != field_type_map_.end());
117 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month1")]);
118 ASSERT_TRUE(
119 field_type_map_.find(ASCIIToUTF16("year1")) != field_type_map_.end());
120 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
121 field_type_map_[ASCIIToUTF16("year1")]);
122 }
123
TEST_F(CreditCardFieldTest,ParseMiniumCreditCardEcml)124 TEST_F(CreditCardFieldTest, ParseMiniumCreditCardEcml) {
125 list_.push_back(
126 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
127 ASCIIToUTF16(kEcmlCardNumber),
128 string16(),
129 ASCIIToUTF16("text"),
130 0,
131 false),
132 ASCIIToUTF16("number1")));
133 list_.push_back(
134 new AutofillField(
135 webkit_glue::FormField(ASCIIToUTF16("Exp Month"),
136 ASCIIToUTF16(kEcmlCardExpireMonth),
137 string16(),
138 ASCIIToUTF16("text"),
139 0,
140 false),
141 ASCIIToUTF16("month1")));
142 list_.push_back(
143 new AutofillField(
144 webkit_glue::FormField(ASCIIToUTF16("Exp Year"),
145 ASCIIToUTF16(kEcmlCardExpireYear),
146 string16(),
147 ASCIIToUTF16("text"),
148 0,
149 false),
150 ASCIIToUTF16("year1")));
151 list_.push_back(NULL);
152 iter_ = list_.begin();
153 field_.reset(CreditCardField::Parse(&iter_, false));
154 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
155 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
156 ASSERT_TRUE(
157 field_type_map_.find(ASCIIToUTF16("number1")) != field_type_map_.end());
158 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number1")]);
159 ASSERT_TRUE(
160 field_type_map_.find(ASCIIToUTF16("month1")) != field_type_map_.end());
161 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month1")]);
162 ASSERT_TRUE(
163 field_type_map_.find(ASCIIToUTF16("year1")) != field_type_map_.end());
164 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
165 field_type_map_[ASCIIToUTF16("year1")]);
166 }
167
TEST_F(CreditCardFieldTest,ParseFullCreditCard)168 TEST_F(CreditCardFieldTest, ParseFullCreditCard) {
169 list_.push_back(
170 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name on Card"),
171 ASCIIToUTF16("name on card"),
172 string16(),
173 ASCIIToUTF16("text"),
174 0,
175 false),
176 ASCIIToUTF16("name1")));
177 list_.push_back(
178 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
179 ASCIIToUTF16("card_number"),
180 string16(),
181 ASCIIToUTF16("text"),
182 0,
183 false),
184 ASCIIToUTF16("number1")));
185 list_.push_back(
186 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Month"),
187 ASCIIToUTF16("ccmonth"),
188 string16(),
189 ASCIIToUTF16("text"),
190 0,
191 false),
192 ASCIIToUTF16("month1")));
193 list_.push_back(
194 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Exp Year"),
195 ASCIIToUTF16("ccyear"),
196 string16(),
197 ASCIIToUTF16("text"),
198 0,
199 false),
200 ASCIIToUTF16("year1")));
201 list_.push_back(
202 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Verification"),
203 ASCIIToUTF16("verification"),
204 string16(),
205 ASCIIToUTF16("text"),
206 0,
207 false),
208 ASCIIToUTF16("cvc1")));
209 list_.push_back(NULL);
210 iter_ = list_.begin();
211 field_.reset(CreditCardField::Parse(&iter_, false));
212 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
213 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
214 ASSERT_TRUE(
215 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
216 EXPECT_EQ(CREDIT_CARD_NAME, field_type_map_[ASCIIToUTF16("name1")]);
217 ASSERT_TRUE(
218 field_type_map_.find(ASCIIToUTF16("number1")) != field_type_map_.end());
219 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number1")]);
220 ASSERT_TRUE(
221 field_type_map_.find(ASCIIToUTF16("month1")) != field_type_map_.end());
222 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month1")]);
223 ASSERT_TRUE(
224 field_type_map_.find(ASCIIToUTF16("year1")) != field_type_map_.end());
225 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
226 field_type_map_[ASCIIToUTF16("year1")]);
227 // We don't store CVV.
228 EXPECT_TRUE(
229 field_type_map_.find(ASCIIToUTF16("cvc1")) == field_type_map_.end());
230 }
231
TEST_F(CreditCardFieldTest,ParseFullCreditCardEcml)232 TEST_F(CreditCardFieldTest, ParseFullCreditCardEcml) {
233 list_.push_back(
234 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name on Card"),
235 ASCIIToUTF16(kEcmlCardHolder),
236 string16(),
237 ASCIIToUTF16("text"),
238 0,
239 false),
240 ASCIIToUTF16("name1")));
241 list_.push_back(
242 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
243 ASCIIToUTF16(kEcmlCardNumber),
244 string16(),
245 ASCIIToUTF16("text"),
246 0,
247 false),
248 ASCIIToUTF16("number1")));
249 list_.push_back(
250 new AutofillField(
251 webkit_glue::FormField(ASCIIToUTF16("Exp Month"),
252 ASCIIToUTF16(kEcmlCardExpireMonth),
253 string16(),
254 ASCIIToUTF16("text"),
255 0,
256 false),
257 ASCIIToUTF16("month1")));
258 list_.push_back(
259 new AutofillField(
260 webkit_glue::FormField(ASCIIToUTF16("Exp Year"),
261 ASCIIToUTF16(kEcmlCardExpireYear),
262 string16(),
263 ASCIIToUTF16("text"),
264 0,
265 false),
266 ASCIIToUTF16("year1")));
267 list_.push_back(
268 new AutofillField(
269 webkit_glue::FormField(ASCIIToUTF16("Verification"),
270 ASCIIToUTF16(kEcmlCardVerification),
271 string16(),
272 ASCIIToUTF16("text"),
273 0,
274 false),
275 ASCIIToUTF16("cvc1")));
276 list_.push_back(NULL);
277 iter_ = list_.begin();
278 field_.reset(CreditCardField::Parse(&iter_, false));
279 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
280 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
281 ASSERT_TRUE(
282 field_type_map_.find(ASCIIToUTF16("name1")) != field_type_map_.end());
283 EXPECT_EQ(CREDIT_CARD_NAME, field_type_map_[ASCIIToUTF16("name1")]);
284 ASSERT_TRUE(
285 field_type_map_.find(ASCIIToUTF16("number1")) != field_type_map_.end());
286 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number1")]);
287 ASSERT_TRUE(
288 field_type_map_.find(ASCIIToUTF16("month1")) != field_type_map_.end());
289 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month1")]);
290 ASSERT_TRUE(
291 field_type_map_.find(ASCIIToUTF16("year1")) != field_type_map_.end());
292 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
293 field_type_map_[ASCIIToUTF16("year1")]);
294 // We don't store the CVV.
295 EXPECT_TRUE(
296 field_type_map_.find(ASCIIToUTF16("cvc1")) == field_type_map_.end());
297 }
298
TEST_F(CreditCardFieldTest,ParseExpMonthYear)299 TEST_F(CreditCardFieldTest, ParseExpMonthYear) {
300 list_.push_back(
301 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name on Card"),
302 ASCIIToUTF16("Name"),
303 string16(),
304 ASCIIToUTF16("text"),
305 0,
306 false),
307 ASCIIToUTF16("name")));
308 list_.push_back(
309 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
310 ASCIIToUTF16("Card"),
311 string16(),
312 ASCIIToUTF16("text"),
313 0,
314 false),
315 ASCIIToUTF16("number")));
316 list_.push_back(
317 new AutofillField(
318 webkit_glue::FormField(ASCIIToUTF16("ExpDate Month / Year"),
319 ASCIIToUTF16("ExpDate"),
320 string16(),
321 ASCIIToUTF16("text"),
322 0,
323 false),
324 ASCIIToUTF16("month")));
325 list_.push_back(
326 new AutofillField(
327 webkit_glue::FormField(ASCIIToUTF16("ExpDate Month / Year"),
328 ASCIIToUTF16("ExpDate"),
329 string16(),
330 ASCIIToUTF16("text"),
331 0,
332 false),
333 ASCIIToUTF16("year")));
334 list_.push_back(NULL);
335 iter_ = list_.begin();
336 field_.reset(CreditCardField::Parse(&iter_, false));
337 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
338 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
339 ASSERT_TRUE(
340 field_type_map_.find(ASCIIToUTF16("name")) != field_type_map_.end());
341 EXPECT_EQ(CREDIT_CARD_NAME, field_type_map_[ASCIIToUTF16("name")]);
342 ASSERT_TRUE(
343 field_type_map_.find(ASCIIToUTF16("number")) != field_type_map_.end());
344 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number")]);
345 ASSERT_TRUE(
346 field_type_map_.find(ASCIIToUTF16("month")) != field_type_map_.end());
347 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month")]);
348 ASSERT_TRUE(
349 field_type_map_.find(ASCIIToUTF16("year")) != field_type_map_.end());
350 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
351 field_type_map_[ASCIIToUTF16("year")]);
352 }
353
TEST_F(CreditCardFieldTest,ParseExpMonthYear2)354 TEST_F(CreditCardFieldTest, ParseExpMonthYear2) {
355 list_.push_back(
356 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Name on Card"),
357 ASCIIToUTF16("Name"),
358 string16(),
359 ASCIIToUTF16("text"),
360 0,
361 false),
362 ASCIIToUTF16("name")));
363 list_.push_back(
364 new AutofillField(webkit_glue::FormField(ASCIIToUTF16("Card Number"),
365 ASCIIToUTF16("Card"),
366 string16(),
367 ASCIIToUTF16("text"),
368 0,
369 false),
370 ASCIIToUTF16("number")));
371 list_.push_back(
372 new AutofillField(
373 webkit_glue::FormField(ASCIIToUTF16("Expiration date Month / Year"),
374 ASCIIToUTF16("ExpDate"),
375 string16(),
376 ASCIIToUTF16("text"),
377 0,
378 false),
379 ASCIIToUTF16("month")));
380 list_.push_back(
381 new AutofillField(
382 webkit_glue::FormField(ASCIIToUTF16("Expiration date Month / Year"),
383 ASCIIToUTF16("ExpDate"),
384 string16(),
385 ASCIIToUTF16("text"),
386 0,
387 false),
388 ASCIIToUTF16("year")));
389 list_.push_back(NULL);
390 iter_ = list_.begin();
391 field_.reset(CreditCardField::Parse(&iter_, false));
392 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get());
393 ASSERT_TRUE(field_->GetFieldInfo(&field_type_map_));
394 ASSERT_TRUE(
395 field_type_map_.find(ASCIIToUTF16("name")) != field_type_map_.end());
396 EXPECT_EQ(CREDIT_CARD_NAME, field_type_map_[ASCIIToUTF16("name")]);
397 ASSERT_TRUE(
398 field_type_map_.find(ASCIIToUTF16("number")) != field_type_map_.end());
399 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number")]);
400 ASSERT_TRUE(
401 field_type_map_.find(ASCIIToUTF16("month")) != field_type_map_.end());
402 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month")]);
403 ASSERT_TRUE(
404 field_type_map_.find(ASCIIToUTF16("year")) != field_type_map_.end());
405 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
406 field_type_map_[ASCIIToUTF16("year")]);
407 }
408
409 } // namespace
410
411