| 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 "components/payments/content/payment_request_state.h" | 5 #include "components/payments/content/payment_request_state.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/autofill_test_utils.h" | 12 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 13 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
| 14 #include "components/autofill/core/browser/test_personal_data_manager.h" | 14 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 15 #include "components/payments/content/payment_request.mojom.h" | 15 #include "components/payments/content/payment_request.mojom.h" |
| 16 #include "components/payments/content/payment_request_delegate.h" |
| 16 #include "components/payments/content/payment_request_spec.h" | 17 #include "components/payments/content/payment_request_spec.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace payments { | 20 namespace payments { |
| 20 | 21 |
| 22 class FakePaymentRequestDelegate : public PaymentRequestDelegate { |
| 23 public: |
| 24 FakePaymentRequestDelegate( |
| 25 autofill::PersonalDataManager* personal_data_manager) |
| 26 : personal_data_manager_(personal_data_manager), locale_("en-US") {} |
| 27 void ShowDialog(PaymentRequest* request) override {} |
| 28 |
| 29 void CloseDialog() override {} |
| 30 |
| 31 void ShowErrorMessage() override {} |
| 32 |
| 33 autofill::PersonalDataManager* GetPersonalDataManager() override { |
| 34 return personal_data_manager_; |
| 35 } |
| 36 |
| 37 const std::string& GetApplicationLocale() const override { return locale_; } |
| 38 |
| 39 bool IsIncognito() const override { return false; } |
| 40 |
| 41 content::WebContents* GetWebContents() override { return nullptr; } |
| 42 |
| 43 void DoFullCardRequest( |
| 44 const autofill::CreditCard& credit_card, |
| 45 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> |
| 46 result_delegate) override { |
| 47 result_delegate->OnFullCardRequestSucceeded(credit_card, |
| 48 base::ASCIIToUTF16("123")); |
| 49 } |
| 50 |
| 51 private: |
| 52 autofill::PersonalDataManager* personal_data_manager_; |
| 53 std::string locale_; |
| 54 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); |
| 55 }; |
| 56 |
| 21 class PaymentRequestStateTest : public testing::Test, | 57 class PaymentRequestStateTest : public testing::Test, |
| 22 public PaymentRequestState::Observer, | 58 public PaymentRequestState::Observer, |
| 23 public PaymentRequestState::Delegate { | 59 public PaymentRequestState::Delegate { |
| 24 protected: | 60 protected: |
| 25 PaymentRequestStateTest() | 61 PaymentRequestStateTest() |
| 26 : num_on_selected_information_changed_called_(0), | 62 : num_on_selected_information_changed_called_(0), |
| 63 payment_request_delegate_( |
| 64 new FakePaymentRequestDelegate(&test_personal_data_manager_)), |
| 27 address_(autofill::test::GetFullProfile()), | 65 address_(autofill::test::GetFullProfile()), |
| 28 credit_card_visa_(autofill::test::GetCreditCard()), | 66 credit_card_visa_(autofill::test::GetCreditCard()), |
| 29 credit_card_amex_(autofill::test::GetCreditCard2()) { | 67 credit_card_amex_(autofill::test::GetCreditCard2()) { |
| 30 test_personal_data_manager_.AddTestingProfile(&address_); | 68 test_personal_data_manager_.AddTestingProfile(&address_); |
| 31 credit_card_visa_.set_billing_address_id(address_.guid()); | 69 credit_card_visa_.set_billing_address_id(address_.guid()); |
| 32 credit_card_visa_.set_use_count(5u); | 70 credit_card_visa_.set_use_count(5u); |
| 33 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); | 71 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); |
| 34 credit_card_amex_.set_billing_address_id(address_.guid()); | 72 credit_card_amex_.set_billing_address_id(address_.guid()); |
| 35 credit_card_amex_.set_use_count(1u); | 73 credit_card_amex_.set_use_count(1u); |
| 36 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); | 74 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 48 num_on_selected_information_changed_called_++; | 86 num_on_selected_information_changed_called_++; |
| 49 } | 87 } |
| 50 | 88 |
| 51 // PaymentRequestState::Delegate: | 89 // PaymentRequestState::Delegate: |
| 52 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { | 90 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { |
| 53 payment_response_ = std::move(response); | 91 payment_response_ = std::move(response); |
| 54 }; | 92 }; |
| 55 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} | 93 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} |
| 56 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} | 94 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} |
| 57 | 95 |
| 96 PaymentRequestDelegate* GetPaymentRequestDelegate() override { |
| 97 return payment_request_delegate_.get(); |
| 98 } |
| 99 |
| 58 void RecreateStateWithOptionsAndDetails( | 100 void RecreateStateWithOptionsAndDetails( |
| 59 mojom::PaymentOptionsPtr options, | 101 mojom::PaymentOptionsPtr options, |
| 60 mojom::PaymentDetailsPtr details, | 102 mojom::PaymentDetailsPtr details, |
| 61 std::vector<mojom::PaymentMethodDataPtr> method_data) { | 103 std::vector<mojom::PaymentMethodDataPtr> method_data) { |
| 62 // The spec will be based on the |options| and |details| passed in. | 104 // The spec will be based on the |options| and |details| passed in. |
| 63 spec_ = base::MakeUnique<PaymentRequestSpec>( | 105 spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 64 std::move(options), std::move(details), std::move(method_data), nullptr, | 106 std::move(options), std::move(details), std::move(method_data), nullptr, |
| 65 "en-US"); | 107 "en-US"); |
| 66 state_ = base::MakeUnique<PaymentRequestState>( | 108 state_ = base::MakeUnique<PaymentRequestState>( |
| 67 spec_.get(), this, "en-US", &test_personal_data_manager_); | 109 spec_.get(), this, "en-US", &test_personal_data_manager_); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 142 } |
| 101 | 143 |
| 102 autofill::AutofillProfile* test_address() { return &address_; } | 144 autofill::AutofillProfile* test_address() { return &address_; } |
| 103 | 145 |
| 104 private: | 146 private: |
| 105 std::unique_ptr<PaymentRequestState> state_; | 147 std::unique_ptr<PaymentRequestState> state_; |
| 106 std::unique_ptr<PaymentRequestSpec> spec_; | 148 std::unique_ptr<PaymentRequestSpec> spec_; |
| 107 int num_on_selected_information_changed_called_; | 149 int num_on_selected_information_changed_called_; |
| 108 mojom::PaymentResponsePtr payment_response_; | 150 mojom::PaymentResponsePtr payment_response_; |
| 109 autofill::TestPersonalDataManager test_personal_data_manager_; | 151 autofill::TestPersonalDataManager test_personal_data_manager_; |
| 152 std::unique_ptr<FakePaymentRequestDelegate> payment_request_delegate_; |
| 110 | 153 |
| 111 // Test data. | 154 // Test data. |
| 112 autofill::AutofillProfile address_; | 155 autofill::AutofillProfile address_; |
| 113 autofill::CreditCard credit_card_visa_; | 156 autofill::CreditCard credit_card_visa_; |
| 114 autofill::CreditCard credit_card_amex_; | 157 autofill::CreditCard credit_card_amex_; |
| 115 autofill::CreditCard credit_card_jcb_; | 158 autofill::CreditCard credit_card_jcb_; |
| 116 }; | 159 }; |
| 117 | 160 |
| 118 TEST_F(PaymentRequestStateTest, CanMakePayment) { | 161 TEST_F(PaymentRequestStateTest, CanMakePayment) { |
| 119 // Default options. | 162 // Default options. |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 EXPECT_TRUE(state()->is_ready_to_pay()); | 434 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 392 state()->GeneratePaymentResponse(); | 435 state()->GeneratePaymentResponse(); |
| 393 | 436 |
| 394 // Check that the name was set, but not the other values. | 437 // Check that the name was set, but not the other values. |
| 395 EXPECT_EQ("John H. Doe", response()->payer_name.value()); | 438 EXPECT_EQ("John H. Doe", response()->payer_name.value()); |
| 396 EXPECT_FALSE(response()->payer_phone.has_value()); | 439 EXPECT_FALSE(response()->payer_phone.has_value()); |
| 397 EXPECT_FALSE(response()->payer_email.has_value()); | 440 EXPECT_FALSE(response()->payer_email.has_value()); |
| 398 } | 441 } |
| 399 | 442 |
| 400 } // namespace payments | 443 } // namespace payments |
| OLD | NEW |