| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "ios/chrome/browser/payments/payment_request.h" | 5 #include "ios/chrome/browser/payments/payment_request.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "base/test/scoped_task_environment.h" | 8 #include "base/test/scoped_task_environment.h" |
| 9 #include "components/autofill/core/browser/autofill_test_utils.h" | 9 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 10 #include "components/autofill/core/browser/autofill_type.h" | 10 #include "components/autofill/core/browser/autofill_type.h" |
| 11 #include "components/autofill/core/browser/field_types.h" | 11 #include "components/autofill/core/browser/field_types.h" |
| 12 #include "components/autofill/core/browser/test_personal_data_manager.h" | 12 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 13 #include "components/payments/core/currency_formatter.h" | 13 #include "components/payments/core/currency_formatter.h" |
| 14 #include "components/payments/core/payment_method_data.h" | 14 #include "components/payments/core/payment_method_data.h" |
| 15 #include "ios/chrome/browser/application_context.h" | 15 #include "ios/chrome/browser/application_context.h" |
| 16 #include "ios/chrome/browser/payments/payment_request_test_util.h" | 16 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
| 17 #include "ios/chrome/browser/payments/test_payment_request.h" | 17 #include "ios/chrome/browser/payments/test_payment_request.h" |
| 18 #include "ios/web/public/payments/payment_request.h" | 18 #include "ios/web/public/payments/payment_request.h" |
| 19 #include "testing/gmock/include/gmock/gmock-matchers.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) | 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." | 24 #error "This file requires ARC support." |
| 23 #endif | 25 #endif |
| 24 | 26 |
| 27 namespace { |
| 28 |
| 29 class MockTestPersonalDataManager : public autofill::TestPersonalDataManager { |
| 30 public: |
| 31 MockTestPersonalDataManager() : TestPersonalDataManager() {} |
| 32 MOCK_METHOD1(RecordUseOf, void(const autofill::AutofillDataModel&)); |
| 33 }; |
| 34 |
| 35 MATCHER_P(GuidMatches, guid, "") { |
| 36 return arg.guid() == guid; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 25 class PaymentRequestTest : public testing::Test { | 41 class PaymentRequestTest : public testing::Test { |
| 26 protected: | 42 protected: |
| 27 // Returns PaymentDetails with one shipping option that's selected. | 43 // Returns PaymentDetails with one shipping option that's selected. |
| 28 web::PaymentDetails CreateDetailsWithShippingOption() { | 44 web::PaymentDetails CreateDetailsWithShippingOption() { |
| 29 web::PaymentDetails details; | 45 web::PaymentDetails details; |
| 30 std::vector<web::PaymentShippingOption> shipping_options; | 46 std::vector<web::PaymentShippingOption> shipping_options; |
| 31 web::PaymentShippingOption option1; | 47 web::PaymentShippingOption option1; |
| 32 option1.id = base::UTF8ToUTF16("option:1"); | 48 option1.id = base::UTF8ToUTF16("option:1"); |
| 33 option1.selected = true; | 49 option1.selected = true; |
| 34 shipping_options.push_back(std::move(option1)); | 50 shipping_options.push_back(std::move(option1)); |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 | 553 |
| 538 web::PaymentRequest web_payment_request = | 554 web::PaymentRequest web_payment_request = |
| 539 payment_request_test_util::CreateTestWebPaymentRequest(); | 555 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 540 | 556 |
| 541 // Even though credit_card2 has more use counts, credit_card is selected | 557 // Even though credit_card2 has more use counts, credit_card is selected |
| 542 // because it is complete. | 558 // because it is complete. |
| 543 TestPaymentRequest payment_request(web_payment_request, | 559 TestPaymentRequest payment_request(web_payment_request, |
| 544 &personal_data_manager); | 560 &personal_data_manager); |
| 545 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); | 561 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 546 } | 562 } |
| 563 |
| 564 // Test that the use counts of the data models are updated as expected when |
| 565 // different autofill profiles are used as the shipping address and the contact |
| 566 // info. |
| 567 TEST_F(PaymentRequestTest, RecordUseStats_RequestShippingAndContactInfo) { |
| 568 MockTestPersonalDataManager personal_data_manager; |
| 569 // Add a profile that is incomplete for a contact info, but is used more |
| 570 // frequently so is selected as the default shipping address. |
| 571 autofill::AutofillProfile address = autofill::test::GetFullProfile(); |
| 572 address.SetInfo(autofill::AutofillType(autofill::EMAIL_ADDRESS), |
| 573 base::string16(), "en-US"); |
| 574 address.set_use_count(10U); |
| 575 personal_data_manager.AddTestingProfile(&address); |
| 576 autofill::AutofillProfile contact_info = autofill::test::GetFullProfile2(); |
| 577 contact_info.set_use_count(5U); |
| 578 personal_data_manager.AddTestingProfile(&contact_info); |
| 579 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 580 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 581 credit_card.set_billing_address_id(address.guid()); |
| 582 |
| 583 web::PaymentRequest web_payment_request = |
| 584 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 585 |
| 586 TestPaymentRequest payment_request(web_payment_request, |
| 587 &personal_data_manager); |
| 588 EXPECT_EQ(address.guid(), |
| 589 payment_request.selected_shipping_profile()->guid()); |
| 590 EXPECT_EQ(contact_info.guid(), |
| 591 payment_request.selected_contact_profile()->guid()); |
| 592 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 593 |
| 594 EXPECT_CALL(personal_data_manager, RecordUseOf(GuidMatches(address.guid()))) |
| 595 .Times(1); |
| 596 EXPECT_CALL(personal_data_manager, |
| 597 RecordUseOf(GuidMatches(contact_info.guid()))) |
| 598 .Times(1); |
| 599 EXPECT_CALL(personal_data_manager, |
| 600 RecordUseOf(GuidMatches(credit_card.guid()))) |
| 601 .Times(1); |
| 602 |
| 603 payment_request.RecordUseStats(); |
| 604 } |
| 605 |
| 606 // Test that the use counts of the data models are updated as expected when the |
| 607 // same autofill profile is used as the shipping address and the contact info. |
| 608 TEST_F(PaymentRequestTest, RecordUseStats_SameShippingAndContactInfoProfile) { |
| 609 MockTestPersonalDataManager personal_data_manager; |
| 610 autofill::AutofillProfile address = autofill::test::GetFullProfile(); |
| 611 personal_data_manager.AddTestingProfile(&address); |
| 612 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 613 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 614 credit_card.set_billing_address_id(address.guid()); |
| 615 |
| 616 web::PaymentRequest web_payment_request = |
| 617 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 618 |
| 619 TestPaymentRequest payment_request(web_payment_request, |
| 620 &personal_data_manager); |
| 621 EXPECT_EQ(address.guid(), |
| 622 payment_request.selected_shipping_profile()->guid()); |
| 623 EXPECT_EQ(address.guid(), payment_request.selected_contact_profile()->guid()); |
| 624 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 625 |
| 626 // Even though |address| is used for contact info, shipping address, and |
| 627 // credit_card's billing address, the stats should be updated only once. |
| 628 EXPECT_CALL(personal_data_manager, RecordUseOf(GuidMatches(address.guid()))) |
| 629 .Times(1); |
| 630 EXPECT_CALL(personal_data_manager, |
| 631 RecordUseOf(GuidMatches(credit_card.guid()))) |
| 632 .Times(1); |
| 633 |
| 634 payment_request.RecordUseStats(); |
| 635 } |
| 636 |
| 637 // Test that the use counts of the data models are updated as expected when no |
| 638 // contact information is requested. |
| 639 TEST_F(PaymentRequestTest, RecordUseStats_RequestShippingOnly) { |
| 640 MockTestPersonalDataManager personal_data_manager; |
| 641 autofill::AutofillProfile address = autofill::test::GetFullProfile(); |
| 642 personal_data_manager.AddTestingProfile(&address); |
| 643 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 644 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 645 credit_card.set_billing_address_id(address.guid()); |
| 646 |
| 647 web::PaymentRequest web_payment_request = |
| 648 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 649 web_payment_request.options.request_payer_name = false; |
| 650 web_payment_request.options.request_payer_email = false; |
| 651 web_payment_request.options.request_payer_phone = false; |
| 652 |
| 653 TestPaymentRequest payment_request(web_payment_request, |
| 654 &personal_data_manager); |
| 655 EXPECT_EQ(address.guid(), |
| 656 payment_request.selected_shipping_profile()->guid()); |
| 657 EXPECT_EQ(nullptr, payment_request.selected_contact_profile()); |
| 658 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 659 |
| 660 EXPECT_CALL(personal_data_manager, RecordUseOf(GuidMatches(address.guid()))) |
| 661 .Times(1); |
| 662 EXPECT_CALL(personal_data_manager, |
| 663 RecordUseOf(GuidMatches(credit_card.guid()))) |
| 664 .Times(1); |
| 665 |
| 666 payment_request.RecordUseStats(); |
| 667 } |
| 668 |
| 669 // Test that the use counts of the data models are updated as expected when no |
| 670 // shipping information is requested. |
| 671 TEST_F(PaymentRequestTest, RecordUseStats_RequestContactInfoOnly) { |
| 672 MockTestPersonalDataManager personal_data_manager; |
| 673 autofill::AutofillProfile address = autofill::test::GetFullProfile(); |
| 674 personal_data_manager.AddTestingProfile(&address); |
| 675 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 676 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 677 credit_card.set_billing_address_id(address.guid()); |
| 678 |
| 679 web::PaymentRequest web_payment_request = |
| 680 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 681 web_payment_request.options.request_shipping = false; |
| 682 |
| 683 TestPaymentRequest payment_request(web_payment_request, |
| 684 &personal_data_manager); |
| 685 EXPECT_EQ(nullptr, payment_request.selected_shipping_profile()); |
| 686 EXPECT_EQ(address.guid(), payment_request.selected_contact_profile()->guid()); |
| 687 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 688 |
| 689 EXPECT_CALL(personal_data_manager, RecordUseOf(GuidMatches(address.guid()))) |
| 690 .Times(1); |
| 691 EXPECT_CALL(personal_data_manager, |
| 692 RecordUseOf(GuidMatches(credit_card.guid()))) |
| 693 .Times(1); |
| 694 |
| 695 payment_request.RecordUseStats(); |
| 696 } |
| 697 |
| 698 // Test that the use counts of the data models are updated as expected when no |
| 699 // shipping or contact information is requested. |
| 700 TEST_F(PaymentRequestTest, RecordUseStats_NoShippingOrContactInfoRequested) { |
| 701 MockTestPersonalDataManager personal_data_manager; |
| 702 autofill::AutofillProfile address = autofill::test::GetFullProfile(); |
| 703 personal_data_manager.AddTestingProfile(&address); |
| 704 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 705 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 706 credit_card.set_billing_address_id(address.guid()); |
| 707 |
| 708 web::PaymentRequest web_payment_request = |
| 709 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 710 web_payment_request.options.request_shipping = false; |
| 711 web_payment_request.options.request_payer_name = false; |
| 712 web_payment_request.options.request_payer_email = false; |
| 713 web_payment_request.options.request_payer_phone = false; |
| 714 |
| 715 TestPaymentRequest payment_request(web_payment_request, |
| 716 &personal_data_manager); |
| 717 EXPECT_EQ(nullptr, payment_request.selected_shipping_profile()); |
| 718 EXPECT_EQ(nullptr, payment_request.selected_contact_profile()); |
| 719 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 720 |
| 721 EXPECT_CALL(personal_data_manager, RecordUseOf(GuidMatches(address.guid()))) |
| 722 .Times(0); |
| 723 EXPECT_CALL(personal_data_manager, |
| 724 RecordUseOf(GuidMatches(credit_card.guid()))) |
| 725 .Times(1); |
| 726 |
| 727 payment_request.RecordUseStats(); |
| 728 } |
| OLD | NEW |