| 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 "components/autofill/core/browser/autofill_test_utils.h" | 8 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 9 #include "components/autofill/core/browser/autofill_type.h" | 9 #include "components/autofill/core/browser/autofill_type.h" |
| 10 #include "components/autofill/core/browser/field_types.h" | 10 #include "components/autofill/core/browser/field_types.h" |
| 11 #include "components/autofill/core/browser/test_personal_data_manager.h" | 11 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 12 #include "components/payments/core/currency_formatter.h" | 12 #include "components/payments/core/currency_formatter.h" |
| 13 #include "components/payments/core/payment_method_data.h" | 13 #include "components/payments/core/payment_method_data.h" |
| 14 #include "ios/chrome/browser/application_context.h" | 14 #include "ios/chrome/browser/application_context.h" |
| 15 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
| 15 #include "ios/web/public/payments/payment_request.h" | 16 #include "ios/web/public/payments/payment_request.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." | 20 #error "This file requires ARC support." |
| 20 #endif | 21 #endif |
| 21 | 22 |
| 22 class PaymentRequestTest : public testing::Test { | 23 class PaymentRequestTest : public testing::Test { |
| 23 protected: | 24 protected: |
| 24 // Returns PaymentDetails with one shipping option that's selected. | 25 // Returns PaymentDetails with one shipping option that's selected. |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 // address1 has more use counts, and even though it has no phone number, it's | 436 // address1 has more use counts, and even though it has no phone number, it's |
| 436 // still selected as the contact profile because merchant doesn't require | 437 // still selected as the contact profile because merchant doesn't require |
| 437 // phone. address2 is selected as the shipping profile because it's the most | 438 // phone. address2 is selected as the shipping profile because it's the most |
| 438 // complete for shipping. | 439 // complete for shipping. |
| 439 PaymentRequest payment_request(web_payment_request, &personal_data_manager); | 440 PaymentRequest payment_request(web_payment_request, &personal_data_manager); |
| 440 EXPECT_EQ(address2.guid(), | 441 EXPECT_EQ(address2.guid(), |
| 441 payment_request.selected_shipping_profile()->guid()); | 442 payment_request.selected_shipping_profile()->guid()); |
| 442 EXPECT_EQ(address1.guid(), | 443 EXPECT_EQ(address1.guid(), |
| 443 payment_request.selected_contact_profile()->guid()); | 444 payment_request.selected_contact_profile()->guid()); |
| 444 } | 445 } |
| 446 |
| 447 // Test that loading payment methods when none are available works as expected. |
| 448 TEST_F(PaymentRequestTest, SelectedPaymentMethod_NoPaymentMethods) { |
| 449 autofill::TestPersonalDataManager personal_data_manager; |
| 450 web::PaymentRequest web_payment_request = |
| 451 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 452 |
| 453 // No payment methods are selected because none are available! |
| 454 PaymentRequest payment_request(web_payment_request, &personal_data_manager); |
| 455 EXPECT_EQ(nullptr, payment_request.selected_credit_card()); |
| 456 } |
| 457 |
| 458 // Test that loading expired credit cards works as expected. |
| 459 TEST_F(PaymentRequestTest, SelectedPaymentMethod_ExpiredCard) { |
| 460 autofill::TestPersonalDataManager personal_data_manager; |
| 461 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 462 personal_data_manager.AddTestingProfile(&billing_address); |
| 463 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 464 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 465 credit_card.SetExpirationYear(2016); // Expired. |
| 466 credit_card.set_billing_address_id(billing_address.guid()); |
| 467 |
| 468 web::PaymentRequest web_payment_request = |
| 469 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 470 |
| 471 // credit_card is selected because expired cards are valid for payment. |
| 472 PaymentRequest payment_request(web_payment_request, &personal_data_manager); |
| 473 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 474 } |
| 475 |
| 476 // Test that loading complete payment methods works as expected. |
| 477 TEST_F(PaymentRequestTest, SelectedPaymentMethod_Complete) { |
| 478 autofill::TestPersonalDataManager personal_data_manager; |
| 479 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 480 personal_data_manager.AddTestingProfile(&billing_address); |
| 481 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 482 credit_card.set_use_count(5U); |
| 483 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 484 credit_card.set_billing_address_id(billing_address.guid()); |
| 485 autofill::CreditCard credit_card2 = autofill::test::GetCreditCard2(); |
| 486 credit_card2.set_use_count(15U); |
| 487 personal_data_manager.AddTestingCreditCard(&credit_card2); |
| 488 credit_card2.set_billing_address_id(billing_address.guid()); |
| 489 |
| 490 web::PaymentRequest web_payment_request = |
| 491 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 492 |
| 493 // credit_card2 is selected because it has the most use count (Frecency |
| 494 // model). |
| 495 PaymentRequest payment_request(web_payment_request, &personal_data_manager); |
| 496 EXPECT_EQ(credit_card2.guid(), |
| 497 payment_request.selected_credit_card()->guid()); |
| 498 } |
| 499 |
| 500 // Test that loading incomplete payment methods works as expected. |
| 501 TEST_F(PaymentRequestTest, SelectedPaymentMethod_Incomplete) { |
| 502 autofill::TestPersonalDataManager personal_data_manager; |
| 503 autofill::AutofillProfile billing_address = autofill::test::GetFullProfile(); |
| 504 personal_data_manager.AddTestingProfile(&billing_address); |
| 505 autofill::CreditCard credit_card = autofill::test::GetCreditCard(); |
| 506 credit_card.set_use_count(5U); |
| 507 personal_data_manager.AddTestingCreditCard(&credit_card); |
| 508 credit_card.set_billing_address_id(billing_address.guid()); |
| 509 autofill::CreditCard credit_card2 = autofill::test::GetCreditCard2(); |
| 510 credit_card2.set_use_count(15U); |
| 511 personal_data_manager.AddTestingCreditCard(&credit_card2); |
| 512 |
| 513 web::PaymentRequest web_payment_request = |
| 514 payment_request_test_util::CreateTestWebPaymentRequest(); |
| 515 |
| 516 // Even though credit_card2 has more use counts, credit_card is selected |
| 517 // because it is complete. |
| 518 PaymentRequest payment_request(web_payment_request, &personal_data_manager); |
| 519 EXPECT_EQ(credit_card.guid(), payment_request.selected_credit_card()->guid()); |
| 520 } |
| OLD | NEW |