Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: ios/chrome/browser/payments/payment_request_unittest.mm

Issue 2972643003: [Payment Request] Records the use of data models used in Payment Request. (Closed)
Patch Set: Initial Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
please use gerrit instead 2017/07/05 12:51:07 Newline after.
Moe 2017/07/05 14:43:20 Done.
28 class MockTestPersonalDataManager : public autofill::TestPersonalDataManager {
29 public:
30 MockTestPersonalDataManager() : TestPersonalDataManager() {}
31 MOCK_METHOD1(RecordUseOf, void(const autofill::AutofillDataModel&));
32 };
33
34 MATCHER_P(DataModelMatches, guid, "") {
please use gerrit instead 2017/07/05 12:51:07 s/Data/Guid/
Moe 2017/07/05 14:43:20 Done.
35 return arg.guid() == guid;
36 }
37 } // namespace
please use gerrit instead 2017/07/05 12:51:07 Newline before.
Moe 2017/07/05 14:43:20 Done.
38
25 class PaymentRequestTest : public testing::Test { 39 class PaymentRequestTest : public testing::Test {
26 protected: 40 protected:
27 // Returns PaymentDetails with one shipping option that's selected. 41 // Returns PaymentDetails with one shipping option that's selected.
28 web::PaymentDetails CreateDetailsWithShippingOption() { 42 web::PaymentDetails CreateDetailsWithShippingOption() {
29 web::PaymentDetails details; 43 web::PaymentDetails details;
30 std::vector<web::PaymentShippingOption> shipping_options; 44 std::vector<web::PaymentShippingOption> shipping_options;
31 web::PaymentShippingOption option1; 45 web::PaymentShippingOption option1;
32 option1.id = base::UTF8ToUTF16("option:1"); 46 option1.id = base::UTF8ToUTF16("option:1");
33 option1.selected = true; 47 option1.selected = true;
34 shipping_options.push_back(std::move(option1)); 48 shipping_options.push_back(std::move(option1));
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 551
538 web::PaymentRequest web_payment_request = 552 web::PaymentRequest web_payment_request =
539 payment_request_test_util::CreateTestWebPaymentRequest(); 553 payment_request_test_util::CreateTestWebPaymentRequest();
540 554
541 // Even though credit_card2 has more use counts, credit_card is selected 555 // Even though credit_card2 has more use counts, credit_card is selected
542 // because it is complete. 556 // because it is complete.
543 TestPaymentRequest payment_request(web_payment_request, 557 TestPaymentRequest payment_request(web_payment_request,
544 &personal_data_manager); 558 &personal_data_manager);
545 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); 559 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid());
546 } 560 }
561
562 // Test that the use counts of the data models are updated as expected when the
563 // same autofill profile is used as the shipping address and the contact info.
564 TEST_F(PaymentRequestTest, RecordUseStats_RequestShippingAndContactInfo) {
565 MockTestPersonalDataManager personal_data_manager;
566 autofill::AutofillProfile address = autofill::test::GetFullProfile();
567 personal_data_manager.AddTestingProfile(&address);
568 autofill::CreditCard credit_card = autofill::test::GetCreditCard();
569 personal_data_manager.AddTestingCreditCard(&credit_card);
570 credit_card.set_billing_address_id(address.guid());
571
572 web::PaymentRequest web_payment_request =
573 payment_request_test_util::CreateTestWebPaymentRequest();
574
575 TestPaymentRequest payment_request(web_payment_request,
576 &personal_data_manager);
577 EXPECT_EQ(address.guid(),
578 payment_request.selected_shipping_profile()->guid());
579 EXPECT_EQ(address.guid(), payment_request.selected_contact_profile()->guid());
580 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid());
581
582 // Even though |address| is used for contact info, shipping address, and
583 // credit_card's billing address, the stats should be updated only once.
584 EXPECT_CALL(personal_data_manager,
585 RecordUseOf(DataModelMatches(address.guid())))
586 .Times(1);
587 EXPECT_CALL(personal_data_manager,
588 RecordUseOf(DataModelMatches(credit_card.guid())))
589 .Times(1);
590
591 payment_request.RecordUseStats();
592 }
593
594 // Test that the use counts of the data models are updated as expected when no
595 // contact information is requested.
596 TEST_F(PaymentRequestTest, RecordUseStats_RequestShippingOnly) {
597 MockTestPersonalDataManager personal_data_manager;
598 autofill::AutofillProfile address = autofill::test::GetFullProfile();
599 personal_data_manager.AddTestingProfile(&address);
600 autofill::CreditCard credit_card = autofill::test::GetCreditCard();
601 personal_data_manager.AddTestingCreditCard(&credit_card);
602 credit_card.set_billing_address_id(address.guid());
603
604 web::PaymentRequest web_payment_request =
605 payment_request_test_util::CreateTestWebPaymentRequest();
606 web_payment_request.options.request_payer_name = false;
607 web_payment_request.options.request_payer_email = false;
608 web_payment_request.options.request_payer_phone = false;
609
610 TestPaymentRequest payment_request(web_payment_request,
611 &personal_data_manager);
612 EXPECT_EQ(address.guid(),
613 payment_request.selected_shipping_profile()->guid());
614 EXPECT_EQ(nullptr, payment_request.selected_contact_profile());
615 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid());
616
617 EXPECT_CALL(personal_data_manager,
618 RecordUseOf(DataModelMatches(address.guid())))
619 .Times(1);
620 EXPECT_CALL(personal_data_manager,
621 RecordUseOf(DataModelMatches(credit_card.guid())))
622 .Times(1);
623
624 payment_request.RecordUseStats();
625 }
626
627 // Test that the use counts of the data models are updated as expected when no
628 // shipping information is requested.
629 TEST_F(PaymentRequestTest, RecordUseStats_RequestContactInfoOnly) {
630 MockTestPersonalDataManager personal_data_manager;
631 autofill::AutofillProfile address = autofill::test::GetFullProfile();
632 personal_data_manager.AddTestingProfile(&address);
633 autofill::CreditCard credit_card = autofill::test::GetCreditCard();
634 personal_data_manager.AddTestingCreditCard(&credit_card);
635 credit_card.set_billing_address_id(address.guid());
636
637 web::PaymentRequest web_payment_request =
638 payment_request_test_util::CreateTestWebPaymentRequest();
639 web_payment_request.options.request_shipping = false;
640
641 TestPaymentRequest payment_request(web_payment_request,
642 &personal_data_manager);
643 EXPECT_EQ(nullptr, payment_request.selected_shipping_profile());
644 EXPECT_EQ(address.guid(), payment_request.selected_contact_profile()->guid());
645 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid());
646
647 EXPECT_CALL(personal_data_manager,
648 RecordUseOf(DataModelMatches(address.guid())))
649 .Times(1);
650 EXPECT_CALL(personal_data_manager,
651 RecordUseOf(DataModelMatches(credit_card.guid())))
652 .Times(1);
653
654 payment_request.RecordUseStats();
655 }
656
657 // Test that the use counts of the data models are updated as expected when no
658 // shipping or contact information is requested.
659 TEST_F(PaymentRequestTest, RecordUseStats_NoShippingOrContactInfoRequested) {
660 MockTestPersonalDataManager personal_data_manager;
661 autofill::AutofillProfile address = autofill::test::GetFullProfile();
662 personal_data_manager.AddTestingProfile(&address);
663 autofill::CreditCard credit_card = autofill::test::GetCreditCard();
664 personal_data_manager.AddTestingCreditCard(&credit_card);
665 credit_card.set_billing_address_id(address.guid());
666
667 web::PaymentRequest web_payment_request =
668 payment_request_test_util::CreateTestWebPaymentRequest();
669 web_payment_request.options.request_shipping = false;
670 web_payment_request.options.request_payer_name = false;
671 web_payment_request.options.request_payer_email = false;
672 web_payment_request.options.request_payer_phone = false;
673
674 TestPaymentRequest payment_request(web_payment_request,
675 &personal_data_manager);
676 EXPECT_EQ(nullptr, payment_request.selected_shipping_profile());
677 EXPECT_EQ(nullptr, payment_request.selected_contact_profile());
678 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid());
679
680 EXPECT_CALL(personal_data_manager,
681 RecordUseOf(DataModelMatches(address.guid())))
682 .Times(0);
683 EXPECT_CALL(personal_data_manager,
684 RecordUseOf(DataModelMatches(credit_card.guid())))
685 .Times(1);
686
687 payment_request.RecordUseStats();
688 }
please use gerrit instead 2017/07/05 12:51:07 Please add a test case for different profiles bein
Moe 2017/07/05 14:43:20 Done.
OLDNEW
« no previous file with comments | « ios/chrome/browser/payments/payment_request.mm ('k') | ios/chrome/browser/ui/payments/payment_request_manager.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698