| 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 #import "ios/chrome/browser/ui/payments/payment_request_mediator.h" | 5 #import "ios/chrome/browser/ui/payments/payment_request_mediator.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/test/scoped_task_environment.h" | 12 #include "base/test/scoped_task_environment.h" |
| 13 #include "components/autofill/core/browser/autofill_profile.h" | 13 #include "components/autofill/core/browser/autofill_profile.h" |
| 14 #include "components/autofill/core/browser/autofill_test_utils.h" | 14 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 15 #include "components/autofill/core/browser/credit_card.h" | 15 #include "components/autofill/core/browser/credit_card.h" |
| 16 #include "components/autofill/core/browser/test_personal_data_manager.h" | 16 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 17 #include "components/payments/core/payment_prefs.h" |
| 18 #include "components/payments/core/payments_test_util.h" |
| 17 #include "components/payments/core/strings_util.h" | 19 #include "components/payments/core/strings_util.h" |
| 20 #include "components/prefs/pref_service.h" |
| 18 #include "components/signin/core/browser/signin_manager.h" | 21 #include "components/signin/core/browser/signin_manager.h" |
| 19 #include "components/strings/grit/components_strings.h" | 22 #include "components/strings/grit/components_strings.h" |
| 20 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 23 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 21 #include "ios/chrome/browser/payments/payment_request_test_util.h" | 24 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
| 22 #include "ios/chrome/browser/payments/payment_request_util.h" | 25 #include "ios/chrome/browser/payments/payment_request_util.h" |
| 23 #include "ios/chrome/browser/payments/test_payment_request.h" | 26 #include "ios/chrome/browser/payments/test_payment_request.h" |
| 24 #include "ios/chrome/browser/signin/fake_signin_manager_builder.h" | 27 #include "ios/chrome/browser/signin/fake_signin_manager_builder.h" |
| 25 #include "ios/chrome/browser/signin/signin_manager_factory.h" | 28 #include "ios/chrome/browser/signin/signin_manager_factory.h" |
| 26 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" | 29 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
| 27 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" | 30 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 43 using ::payment_request_util::GetEmailLabelFromAutofillProfile; | 46 using ::payment_request_util::GetEmailLabelFromAutofillProfile; |
| 44 using ::payment_request_util::GetNameLabelFromAutofillProfile; | 47 using ::payment_request_util::GetNameLabelFromAutofillProfile; |
| 45 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; | 48 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; |
| 46 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; | 49 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; |
| 47 } // namespace | 50 } // namespace |
| 48 | 51 |
| 49 class PaymentRequestMediatorTest : public PlatformTest { | 52 class PaymentRequestMediatorTest : public PlatformTest { |
| 50 protected: | 53 protected: |
| 51 PaymentRequestMediatorTest() | 54 PaymentRequestMediatorTest() |
| 52 : autofill_profile_(autofill::test::GetFullProfile()), | 55 : autofill_profile_(autofill::test::GetFullProfile()), |
| 53 credit_card_(autofill::test::GetCreditCard()) { | 56 credit_card_(autofill::test::GetCreditCard()), |
| 57 pref_service_(payments::test::PrefServiceForTesting()) { |
| 54 // Add testing profile and credit card to autofill::TestPersonalDataManager. | 58 // Add testing profile and credit card to autofill::TestPersonalDataManager. |
| 55 personal_data_manager_.AddTestingProfile(&autofill_profile_); | 59 personal_data_manager_.AddTestingProfile(&autofill_profile_); |
| 56 credit_card_.set_billing_address_id(autofill_profile_.guid()); | 60 credit_card_.set_billing_address_id(autofill_profile_.guid()); |
| 57 personal_data_manager_.AddTestingCreditCard(&credit_card_); | 61 personal_data_manager_.AddTestingCreditCard(&credit_card_); |
| 58 | 62 |
| 59 payment_request_ = base::MakeUnique<TestPaymentRequest>( | |
| 60 payment_request_test_util::CreateTestWebPaymentRequest(), | |
| 61 &personal_data_manager_); | |
| 62 | |
| 63 TestChromeBrowserState::Builder test_cbs_builder; | 63 TestChromeBrowserState::Builder test_cbs_builder; |
| 64 test_cbs_builder.AddTestingFactory(ios::SigninManagerFactory::GetInstance(), | 64 test_cbs_builder.AddTestingFactory(ios::SigninManagerFactory::GetInstance(), |
| 65 &ios::BuildFakeSigninManager); | 65 &ios::BuildFakeSigninManager); |
| 66 chrome_browser_state_ = test_cbs_builder.Build(); | 66 chrome_browser_state_ = test_cbs_builder.Build(); |
| 67 |
| 68 payment_request_ = base::MakeUnique<TestPaymentRequest>( |
| 69 payment_request_test_util::CreateTestWebPaymentRequest(), |
| 70 chrome_browser_state_.get(), &personal_data_manager_); |
| 71 payment_request_->SetPrefService(pref_service_.get()); |
| 72 |
| 67 mediator_ = [[PaymentRequestMediator alloc] | 73 mediator_ = [[PaymentRequestMediator alloc] |
| 68 initWithBrowserState:chrome_browser_state_.get() | 74 initWithPaymentRequest:payment_request_.get()]; |
| 69 paymentRequest:payment_request_.get()]; | |
| 70 } | 75 } |
| 71 | 76 |
| 72 PaymentRequestMediator* GetPaymentRequestMediator() { return mediator_; } | 77 PaymentRequestMediator* GetPaymentRequestMediator() { return mediator_; } |
| 73 | 78 |
| 74 base::test::ScopedTaskEnvironment scoped_task_evironment_; | 79 base::test::ScopedTaskEnvironment scoped_task_evironment_; |
| 75 | 80 |
| 76 autofill::AutofillProfile autofill_profile_; | 81 autofill::AutofillProfile autofill_profile_; |
| 77 autofill::CreditCard credit_card_; | 82 autofill::CreditCard credit_card_; |
| 83 std::unique_ptr<PrefService> pref_service_; |
| 78 autofill::TestPersonalDataManager personal_data_manager_; | 84 autofill::TestPersonalDataManager personal_data_manager_; |
| 79 std::unique_ptr<TestPaymentRequest> payment_request_; | 85 std::unique_ptr<TestPaymentRequest> payment_request_; |
| 80 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; | 86 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 81 PaymentRequestMediator* mediator_; | 87 PaymentRequestMediator* mediator_; |
| 82 }; | 88 }; |
| 83 | 89 |
| 84 // Tests whether payment can be completed when expected. | 90 // Tests whether payment can be completed when expected. |
| 85 TEST_F(PaymentRequestMediatorTest, TestCanPay) { | 91 TEST_F(PaymentRequestMediatorTest, TestCanPay) { |
| 86 EXPECT_TRUE(payment_request_->selected_credit_card()); | 92 EXPECT_TRUE(payment_request_->selected_credit_card()); |
| 87 EXPECT_TRUE(payment_request_->selected_shipping_profile()); | 93 EXPECT_TRUE(payment_request_->selected_shipping_profile()); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item); | 422 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item); |
| 417 EXPECT_TRUE([add_contact_info_item.detailText | 423 EXPECT_TRUE([add_contact_info_item.detailText |
| 418 isEqualToString:[l10n_util::GetNSString(IDS_ADD) | 424 isEqualToString:[l10n_util::GetNSString(IDS_ADD) |
| 419 uppercaseStringWithLocale:[NSLocale currentLocale]]]); | 425 uppercaseStringWithLocale:[NSLocale currentLocale]]]); |
| 420 EXPECT_EQ(MDCCollectionViewCellAccessoryNone, | 426 EXPECT_EQ(MDCCollectionViewCellAccessoryNone, |
| 421 add_contact_info_item.accessoryType); | 427 add_contact_info_item.accessoryType); |
| 422 } | 428 } |
| 423 | 429 |
| 424 // Tests that the Footer item is created as expected. | 430 // Tests that the Footer item is created as expected. |
| 425 TEST_F(PaymentRequestMediatorTest, TestFooterItem) { | 431 TEST_F(PaymentRequestMediatorTest, TestFooterItem) { |
| 432 // Make sure the first transaction has not completed yet. |
| 433 pref_service_->SetBoolean(payments::kPaymentsFirstTransactionCompleted, |
| 434 false); |
| 435 |
| 426 // Make sure the user is signed out. | 436 // Make sure the user is signed out. |
| 427 SigninManager* signin_manager = ios::SigninManagerFactory::GetForBrowserState( | 437 SigninManager* signin_manager = ios::SigninManagerFactory::GetForBrowserState( |
| 428 chrome_browser_state_.get()); | 438 chrome_browser_state_.get()); |
| 429 if (signin_manager->IsAuthenticated()) { | 439 if (signin_manager->IsAuthenticated()) { |
| 430 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, | 440 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, |
| 431 signin_metrics::SignoutDelete::IGNORE_METRIC); | 441 signin_metrics::SignoutDelete::IGNORE_METRIC); |
| 432 } | 442 } |
| 433 | 443 |
| 434 // Footer item should be of type CollectionViewFooterItem. | 444 // Footer item should be of type CollectionViewFooterItem. |
| 435 id item = [GetPaymentRequestMediator() footerItem]; | 445 id item = [GetPaymentRequestMediator() footerItem]; |
| 436 ASSERT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]); | 446 ASSERT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]); |
| 437 CollectionViewFooterItem* footer_item = | 447 CollectionViewFooterItem* footer_item = |
| 438 base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); | 448 base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); |
| 439 EXPECT_TRUE([footer_item.text | 449 EXPECT_TRUE([footer_item.text |
| 440 isEqualToString:l10n_util::GetNSString( | 450 isEqualToString:l10n_util::GetNSString( |
| 441 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT)]); | 451 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT)]); |
| 442 | 452 |
| 443 // Fake a signed in user. | 453 // Fake a signed in user. |
| 444 signin_manager->SetAuthenticatedAccountInfo("12345", "username@example.com"); | 454 signin_manager->SetAuthenticatedAccountInfo("12345", "username@example.com"); |
| 445 | 455 |
| 446 item = [GetPaymentRequestMediator() footerItem]; | 456 item = [GetPaymentRequestMediator() footerItem]; |
| 447 footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); | 457 footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); |
| 448 EXPECT_TRUE([footer_item.text | 458 EXPECT_TRUE([footer_item.text |
| 449 isEqualToString:l10n_util::GetNSStringF( | 459 isEqualToString:l10n_util::GetNSStringF( |
| 450 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN, | 460 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN, |
| 451 base::ASCIIToUTF16("username@example.com"))]); | 461 base::ASCIIToUTF16("username@example.com"))]); |
| 462 |
| 463 // Record that the first transaction completed. |
| 464 pref_service_->SetBoolean(payments::kPaymentsFirstTransactionCompleted, true); |
| 465 |
| 466 item = [GetPaymentRequestMediator() footerItem]; |
| 467 footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); |
| 468 EXPECT_TRUE([footer_item.text |
| 469 isEqualToString:l10n_util::GetNSString( |
| 470 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS)]); |
| 471 |
| 472 // Sign the user out. |
| 473 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, |
| 474 signin_metrics::SignoutDelete::IGNORE_METRIC); |
| 475 |
| 476 // The signed in state has no effect on the footer text if the first |
| 477 // transaction has completed. |
| 478 footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item); |
| 479 EXPECT_TRUE([footer_item.text |
| 480 isEqualToString:l10n_util::GetNSString( |
| 481 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS)]); |
| 452 } | 482 } |
| OLD | NEW |