| 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 "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
| 11 #include "components/autofill/core/browser/autofill_test_utils.h" | 11 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/test_personal_data_manager.h" | 13 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 14 #include "components/payments/content/payment_request.mojom.h" | 14 #include "components/payments/content/payment_request.mojom.h" |
| 15 #include "components/payments/content/payment_request_spec.h" | 15 #include "components/payments/content/payment_request_spec.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace payments { | 18 namespace payments { |
| 19 | 19 |
| 20 class PaymentRequestStateTest : public testing::Test, | 20 class PaymentRequestStateTest : public testing::Test, |
| 21 public PaymentRequestState::Observer, | 21 public PaymentRequestState::Observer, |
| 22 public PaymentRequestState::Delegate { | 22 public PaymentRequestState::Delegate { |
| 23 protected: | 23 protected: |
| 24 PaymentRequestStateTest() | 24 PaymentRequestStateTest() |
| 25 : num_on_selected_information_changed_called_(0), | 25 : num_on_selected_information_changed_called_(0), |
| 26 address_(autofill::test::GetFullProfile()), | 26 address_(autofill::test::GetFullProfile()), |
| 27 credit_card_(autofill::test::GetCreditCard()) { | 27 credit_card_visa_(autofill::test::GetCreditCard()), |
| 28 credit_card_amex_(autofill::test::GetCreditCard2()) { |
| 28 test_personal_data_manager_.AddTestingProfile(&address_); | 29 test_personal_data_manager_.AddTestingProfile(&address_); |
| 29 credit_card_.set_billing_address_id(address_.guid()); | 30 credit_card_visa_.set_billing_address_id(address_.guid()); |
| 30 test_personal_data_manager_.AddTestingCreditCard(&credit_card_); | 31 credit_card_visa_.set_use_count(5u); |
| 32 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); |
| 33 credit_card_amex_.set_billing_address_id(address_.guid()); |
| 34 credit_card_amex_.set_use_count(1u); |
| 35 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); |
| 31 } | 36 } |
| 32 ~PaymentRequestStateTest() override {} | 37 ~PaymentRequestStateTest() override {} |
| 33 | 38 |
| 34 // PaymentRequestState::Observer: | 39 // PaymentRequestState::Observer: |
| 35 void OnSelectedInformationChanged() override { | 40 void OnSelectedInformationChanged() override { |
| 36 num_on_selected_information_changed_called_++; | 41 num_on_selected_information_changed_called_++; |
| 37 } | 42 } |
| 38 | 43 |
| 39 // PaymentRequestState::Delegate: | 44 // PaymentRequestState::Delegate: |
| 40 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { | 45 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 return method_data; | 84 return method_data; |
| 80 } | 85 } |
| 81 | 86 |
| 82 PaymentRequestState* state() { return state_.get(); } | 87 PaymentRequestState* state() { return state_.get(); } |
| 83 const mojom::PaymentResponsePtr& response() { return payment_response_; } | 88 const mojom::PaymentResponsePtr& response() { return payment_response_; } |
| 84 int num_on_selected_information_changed_called() { | 89 int num_on_selected_information_changed_called() { |
| 85 return num_on_selected_information_changed_called_; | 90 return num_on_selected_information_changed_called_; |
| 86 } | 91 } |
| 87 | 92 |
| 88 autofill::AutofillProfile* test_address() { return &address_; } | 93 autofill::AutofillProfile* test_address() { return &address_; } |
| 89 autofill::CreditCard* test_credit_card() { return &credit_card_; } | |
| 90 | 94 |
| 91 private: | 95 private: |
| 92 std::unique_ptr<PaymentRequestState> state_; | 96 std::unique_ptr<PaymentRequestState> state_; |
| 93 std::unique_ptr<PaymentRequestSpec> spec_; | 97 std::unique_ptr<PaymentRequestSpec> spec_; |
| 94 int num_on_selected_information_changed_called_; | 98 int num_on_selected_information_changed_called_; |
| 95 mojom::PaymentResponsePtr payment_response_; | 99 mojom::PaymentResponsePtr payment_response_; |
| 96 autofill::TestPersonalDataManager test_personal_data_manager_; | 100 autofill::TestPersonalDataManager test_personal_data_manager_; |
| 97 | 101 |
| 98 // Test data. | 102 // Test data. |
| 99 autofill::AutofillProfile address_; | 103 autofill::AutofillProfile address_; |
| 100 autofill::CreditCard credit_card_; | 104 autofill::CreditCard credit_card_visa_; |
| 105 autofill::CreditCard credit_card_amex_; |
| 101 }; | 106 }; |
| 102 | 107 |
| 103 // Test that the last shipping option is selected. | 108 // Test that the last shipping option is selected. |
| 104 TEST_F(PaymentRequestStateTest, ShippingOptionsSelection) { | 109 TEST_F(PaymentRequestStateTest, ShippingOptionsSelection) { |
| 105 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; | 110 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; |
| 106 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); | 111 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); |
| 107 option->id = "option:1"; | 112 option->id = "option:1"; |
| 108 option->selected = false; | 113 option->selected = false; |
| 109 shipping_options.push_back(std::move(option)); | 114 shipping_options.push_back(std::move(option)); |
| 110 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New(); | 115 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 126 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 131 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 127 options->request_shipping = true; | 132 options->request_shipping = true; |
| 128 options->request_payer_name = true; | 133 options->request_payer_name = true; |
| 129 options->request_payer_phone = true; | 134 options->request_payer_phone = true; |
| 130 options->request_payer_email = true; | 135 options->request_payer_email = true; |
| 131 RecreateStateWithOptions(std::move(options)); | 136 RecreateStateWithOptions(std::move(options)); |
| 132 | 137 |
| 133 EXPECT_TRUE(state()->is_ready_to_pay()); | 138 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 134 } | 139 } |
| 135 | 140 |
| 136 // Testing that the card is supported when determining "is ready to pay". In | 141 // Testing that only supported intruments are shown. In this test the merchant |
| 137 // this test the merchant only supports Visa. | 142 // only supports Visa. |
| 138 TEST_F(PaymentRequestStateTest, ReadyToPay_SelectUnsupportedCard) { | 143 TEST_F(PaymentRequestStateTest, UnsupportedCardAreNotAvailable) { |
| 139 // Default options. | 144 // Default options. |
| 140 RecreateStateWithOptions(mojom::PaymentOptions::New()); | 145 RecreateStateWithOptions(mojom::PaymentOptions::New()); |
| 141 | 146 |
| 142 // Ready to pay because the default card is selected and supported. | 147 // Ready to pay because the default instrument is selected and supported. |
| 143 EXPECT_TRUE(state()->is_ready_to_pay()); | 148 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 144 | 149 |
| 145 autofill::CreditCard amex_card = autofill::test::GetCreditCard2(); // Amex. | 150 // There's only one instrument available, even though there's an Amex in |
| 146 state()->SetSelectedCreditCard(&amex_card); | 151 // PersonalDataManager. |
| 147 EXPECT_EQ(1, num_on_selected_information_changed_called()); | 152 EXPECT_EQ(1u, state()->available_instruments().size()); |
| 148 | |
| 149 // Not ready to pay because the card is not supported. | |
| 150 EXPECT_FALSE(state()->is_ready_to_pay()); | |
| 151 | |
| 152 // Go back to the Visa card. | |
| 153 state()->SetSelectedCreditCard(test_credit_card()); // Visa card. | |
| 154 EXPECT_EQ(2, num_on_selected_information_changed_called()); | |
| 155 | |
| 156 // Visa card is supported by the merchant. | |
| 157 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 158 } | 153 } |
| 159 | 154 |
| 160 // Test selecting a contact info profile will make the user ready to pay. | 155 // Test selecting a contact info profile will make the user ready to pay. |
| 161 TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) { | 156 TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) { |
| 162 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 157 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 163 options->request_payer_name = true; | 158 options->request_payer_name = true; |
| 164 options->request_payer_phone = true; | 159 options->request_payer_phone = true; |
| 165 options->request_payer_email = true; | 160 options->request_payer_email = true; |
| 166 RecreateStateWithOptions(std::move(options)); | 161 RecreateStateWithOptions(std::move(options)); |
| 167 | 162 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 178 EXPECT_EQ(2, num_on_selected_information_changed_called()); | 173 EXPECT_EQ(2, num_on_selected_information_changed_called()); |
| 179 | 174 |
| 180 // Ready to pay! | 175 // Ready to pay! |
| 181 EXPECT_TRUE(state()->is_ready_to_pay()); | 176 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 182 } | 177 } |
| 183 | 178 |
| 184 // Test generating a PaymentResponse. | 179 // Test generating a PaymentResponse. |
| 185 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse) { | 180 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse) { |
| 186 // Default options (no shipping, no contact info). | 181 // Default options (no shipping, no contact info). |
| 187 RecreateStateWithOptions(mojom::PaymentOptions::New()); | 182 RecreateStateWithOptions(mojom::PaymentOptions::New()); |
| 188 state()->SetSelectedCreditCard(test_credit_card()); | 183 state()->SetSelectedInstrument(state()->available_instruments()[0].get()); |
| 189 EXPECT_EQ(1, num_on_selected_information_changed_called()); | 184 EXPECT_EQ(1, num_on_selected_information_changed_called()); |
| 190 EXPECT_TRUE(state()->is_ready_to_pay()); | 185 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 191 | 186 |
| 192 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | 187 // TODO(mathp): Currently synchronous, when async will need a RunLoop. |
| 193 state()->GeneratePaymentResponse(); | 188 state()->GeneratePaymentResponse(); |
| 194 EXPECT_EQ("basic-card", response()->method_name); | 189 EXPECT_EQ("visa", response()->method_name); |
| 195 EXPECT_EQ( | 190 EXPECT_EQ( |
| 196 "{\"billingAddress\":" | 191 "{\"billingAddress\":" |
| 197 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | 192 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," |
| 198 "\"city\":\"Elysium\"," | 193 "\"city\":\"Elysium\"," |
| 199 "\"country\":\"US\"," | 194 "\"country\":\"US\"," |
| 200 "\"organization\":\"Underworld\"," | 195 "\"organization\":\"Underworld\"," |
| 201 "\"phone\":\"16502111111\"," | 196 "\"phone\":\"16502111111\"," |
| 202 "\"postalCode\":\"91111\"," | 197 "\"postalCode\":\"91111\"," |
| 203 "\"recipient\":\"John H. Doe\"," | 198 "\"recipient\":\"John H. Doe\"," |
| 204 "\"region\":\"CA\"}," | 199 "\"region\":\"CA\"}," |
| 205 "\"cardNumber\":\"4111111111111111\"," | 200 "\"cardNumber\":\"4111111111111111\"," |
| 206 "\"cardSecurityCode\":\"123\"," | 201 "\"cardSecurityCode\":\"123\"," |
| 207 "\"cardholderName\":\"Test User\"," | 202 "\"cardholderName\":\"Test User\"," |
| 208 "\"expiryMonth\":\"11\"," | 203 "\"expiryMonth\":\"11\"," |
| 209 "\"expiryYear\":\"2017\"}", | 204 "\"expiryYear\":\"2017\"}", |
| 210 response()->stringified_details); | 205 response()->stringified_details); |
| 211 } | 206 } |
| 212 | 207 |
| 213 } // namespace payments | 208 } // namespace payments |
| OLD | NEW |