OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "components/autofill/core/browser/autofill_field.h" | 8 #include "components/autofill/core/browser/autofill_field.h" |
9 #include "components/autofill/core/browser/autofill_scanner.h" | 9 #include "components/autofill/core/browser/autofill_scanner.h" |
10 #include "components/autofill/core/browser/credit_card_field.h" | 10 #include "components/autofill/core/browser/credit_card_field.h" |
11 #include "components/autofill/core/common/form_field_data.h" | 11 #include "components/autofill/core/common/form_field_data.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using base::ASCIIToUTF16; | 14 using base::ASCIIToUTF16; |
15 | 15 |
16 namespace autofill { | 16 namespace autofill { |
17 | 17 |
18 class CreditCardFieldTest : public testing::Test { | 18 class CreditCardFieldTest : public testing::Test { |
19 public: | 19 public: |
20 CreditCardFieldTest() {} | 20 CreditCardFieldTest() {} |
21 virtual ~CreditCardFieldTest() {} | 21 virtual ~CreditCardFieldTest() {} |
22 | 22 |
23 protected: | 23 protected: |
24 ScopedVector<const AutofillField> list_; | 24 ScopedVector<AutofillField> list_; |
25 scoped_ptr<const CreditCardField> field_; | 25 scoped_ptr<const CreditCardField> field_; |
26 ServerFieldTypeMap field_type_map_; | 26 ServerFieldTypeMap field_type_map_; |
27 | 27 |
28 // Parses the contents of |list_| as a form, and stores the result into | 28 // Parses the contents of |list_| as a form, and stores the result into |
29 // |field_|. | 29 // |field_|. |
30 void Parse() { | 30 void Parse() { |
31 AutofillScanner scanner(list_.get()); | 31 AutofillScanner scanner(list_.get()); |
32 field_.reset( | 32 field_.reset( |
33 static_cast<const CreditCardField*>(CreditCardField::Parse(&scanner))); | 33 static_cast<const CreditCardField*>(CreditCardField::Parse(&scanner))); |
34 } | 34 } |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number")]); | 373 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number")]); |
374 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("month")) != | 374 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("month")) != |
375 field_type_map_.end()); | 375 field_type_map_.end()); |
376 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month")]); | 376 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month")]); |
377 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("year")) != | 377 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("year")) != |
378 field_type_map_.end()); | 378 field_type_map_.end()); |
379 EXPECT_EQ(CREDIT_CARD_EXP_2_DIGIT_YEAR, | 379 EXPECT_EQ(CREDIT_CARD_EXP_2_DIGIT_YEAR, |
380 field_type_map_[ASCIIToUTF16("year")]); | 380 field_type_map_[ASCIIToUTF16("year")]); |
381 } | 381 } |
382 | 382 |
| 383 TEST_F(CreditCardFieldTest, ParseCreditCardNumberWithSplit) { |
| 384 FormFieldData field; |
| 385 field.form_control_type = "text"; |
| 386 |
| 387 field.label = ASCIIToUTF16("Card Number"); |
| 388 field.name = ASCIIToUTF16("card_number_q1"); |
| 389 field.max_length = 4; |
| 390 list_.push_back(new AutofillField(field, ASCIIToUTF16("number1"))); |
| 391 |
| 392 field.label = ASCIIToUTF16("Card Number"); |
| 393 field.name = ASCIIToUTF16("card_number_q2"); |
| 394 field.max_length = 4; |
| 395 list_.push_back(new AutofillField(field, ASCIIToUTF16("number2"))); |
| 396 |
| 397 field.label = ASCIIToUTF16("Card Number"); |
| 398 field.name = ASCIIToUTF16("card_number_q3"); |
| 399 field.max_length = 4; |
| 400 list_.push_back(new AutofillField(field, ASCIIToUTF16("number3"))); |
| 401 |
| 402 // For last credit card number input field it simply ignores the |max_length| |
| 403 // attribute. So even having a very big number, does not conside it an invalid |
| 404 // split for autofilling. |
| 405 field.label = ASCIIToUTF16("Card Number"); |
| 406 field.name = ASCIIToUTF16("card_number_q4"); |
| 407 field.max_length = 20; |
| 408 list_.push_back(new AutofillField(field, ASCIIToUTF16("number4"))); |
| 409 |
| 410 field.label = ASCIIToUTF16("Exp Month"); |
| 411 field.name = ASCIIToUTF16("ccmonth"); |
| 412 list_.push_back(new AutofillField(field, ASCIIToUTF16("month5"))); |
| 413 |
| 414 field.label = ASCIIToUTF16("Exp Year"); |
| 415 field.name = ASCIIToUTF16("ccyear"); |
| 416 list_.push_back(new AutofillField(field, ASCIIToUTF16("year6"))); |
| 417 |
| 418 Parse(); |
| 419 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get()); |
| 420 EXPECT_TRUE(ClassifyField()); |
| 421 |
| 422 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number1")) != |
| 423 field_type_map_.end()); |
| 424 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number1")]); |
| 425 EXPECT_EQ(0U, list_[0]->credit_card_number_offset()); |
| 426 |
| 427 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number2")) != |
| 428 field_type_map_.end()); |
| 429 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number2")]); |
| 430 EXPECT_EQ(4U, list_[1]->credit_card_number_offset()); |
| 431 |
| 432 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number3")) != |
| 433 field_type_map_.end()); |
| 434 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number3")]); |
| 435 EXPECT_EQ(8U, list_[2]->credit_card_number_offset()); |
| 436 |
| 437 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number4")) != |
| 438 field_type_map_.end()); |
| 439 EXPECT_EQ(CREDIT_CARD_NUMBER, field_type_map_[ASCIIToUTF16("number4")]); |
| 440 EXPECT_EQ(12U, list_[3]->credit_card_number_offset()); |
| 441 |
| 442 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("month5")) != |
| 443 field_type_map_.end()); |
| 444 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month5")]); |
| 445 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("year6")) != |
| 446 field_type_map_.end()); |
| 447 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 448 field_type_map_[ASCIIToUTF16("year6")]); |
| 449 } |
| 450 |
| 451 TEST_F(CreditCardFieldTest, ParseCreditCardNumberWithInvalidSplit) { |
| 452 FormFieldData field; |
| 453 field.form_control_type = "text"; |
| 454 |
| 455 field.label = ASCIIToUTF16("Name on Card"); |
| 456 field.name = ASCIIToUTF16("name_on_card"); |
| 457 list_.push_back(new AutofillField(field, ASCIIToUTF16("name1"))); |
| 458 |
| 459 field.label = ASCIIToUTF16("Card Number"); |
| 460 field.name = ASCIIToUTF16("card_number"); |
| 461 list_.push_back(new AutofillField(field, ASCIIToUTF16("number2"))); |
| 462 |
| 463 field.label = ASCIIToUTF16("Not Card Number"); |
| 464 field.name = ASCIIToUTF16("not_card_number"); |
| 465 list_.push_back(new AutofillField(field, ASCIIToUTF16("number3"))); |
| 466 |
| 467 field.label = ASCIIToUTF16("Exp Month"); |
| 468 field.name = ASCIIToUTF16("ccmonth"); |
| 469 list_.push_back(new AutofillField(field, ASCIIToUTF16("month4"))); |
| 470 |
| 471 field.label = ASCIIToUTF16("Exp Year"); |
| 472 field.name = ASCIIToUTF16("ccyear"); |
| 473 list_.push_back(new AutofillField(field, ASCIIToUTF16("year5"))); |
| 474 |
| 475 Parse(); |
| 476 ASSERT_NE(static_cast<CreditCardField*>(NULL), field_.get()); |
| 477 EXPECT_TRUE(ClassifyField()); |
| 478 |
| 479 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("name1")) != |
| 480 field_type_map_.end()); |
| 481 EXPECT_EQ(CREDIT_CARD_NAME, field_type_map_[ASCIIToUTF16("name1")]); |
| 482 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number2")) == |
| 483 field_type_map_.end()); |
| 484 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("number3")) == |
| 485 field_type_map_.end()); |
| 486 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("month4")) != |
| 487 field_type_map_.end()); |
| 488 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, field_type_map_[ASCIIToUTF16("month4")]); |
| 489 ASSERT_TRUE(field_type_map_.find(ASCIIToUTF16("year5")) != |
| 490 field_type_map_.end()); |
| 491 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 492 field_type_map_[ASCIIToUTF16("year5")]); |
| 493 } |
| 494 |
383 } // namespace autofill | 495 } // namespace autofill |
OLD | NEW |