| 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 24 matching lines...) Expand all Loading... |
| 135 | 140 |
| 136 // Testing that the card is supported when determining "is ready to pay". In | 141 // Testing that the card is supported when determining "is ready to pay". In |
| 137 // this test the merchant only supports Visa. | 142 // this test the merchant only supports Visa. |
| 138 TEST_F(PaymentRequestStateTest, ReadyToPay_SelectUnsupportedCard) { | 143 TEST_F(PaymentRequestStateTest, ReadyToPay_SelectUnsupportedCard) { |
| 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 card 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 state()->SetSelectedInstrument(state()->available_instruments()[1]); // Amex. |
| 146 state()->SetSelectedCreditCard(&amex_card); | |
| 147 EXPECT_EQ(1, num_on_selected_information_changed_called()); | 151 EXPECT_EQ(1, num_on_selected_information_changed_called()); |
| 148 | 152 |
| 149 // Not ready to pay because the card is not supported. | 153 // Not ready to pay because the card is not supported. |
| 150 EXPECT_FALSE(state()->is_ready_to_pay()); | 154 EXPECT_FALSE(state()->is_ready_to_pay()); |
| 151 | 155 |
| 152 // Go back to the Visa card. | 156 // Go back to the Visa card. |
| 153 state()->SetSelectedCreditCard(test_credit_card()); // Visa card. | 157 state()->SetSelectedInstrument(state()->available_instruments()[0]); |
| 154 EXPECT_EQ(2, num_on_selected_information_changed_called()); | 158 EXPECT_EQ(2, num_on_selected_information_changed_called()); |
| 155 | 159 |
| 156 // Visa card is supported by the merchant. | 160 // Visa card is supported by the merchant. |
| 157 EXPECT_TRUE(state()->is_ready_to_pay()); | 161 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 158 } | 162 } |
| 159 | 163 |
| 160 // Test selecting a contact info profile will make the user ready to pay. | 164 // Test selecting a contact info profile will make the user ready to pay. |
| 161 TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) { | 165 TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) { |
| 162 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 166 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 163 options->request_payer_name = true; | 167 options->request_payer_name = true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 178 EXPECT_EQ(2, num_on_selected_information_changed_called()); | 182 EXPECT_EQ(2, num_on_selected_information_changed_called()); |
| 179 | 183 |
| 180 // Ready to pay! | 184 // Ready to pay! |
| 181 EXPECT_TRUE(state()->is_ready_to_pay()); | 185 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 182 } | 186 } |
| 183 | 187 |
| 184 // Test generating a PaymentResponse. | 188 // Test generating a PaymentResponse. |
| 185 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse) { | 189 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse) { |
| 186 // Default options (no shipping, no contact info). | 190 // Default options (no shipping, no contact info). |
| 187 RecreateStateWithOptions(mojom::PaymentOptions::New()); | 191 RecreateStateWithOptions(mojom::PaymentOptions::New()); |
| 188 state()->SetSelectedCreditCard(test_credit_card()); | 192 state()->SetSelectedInstrument(state()->available_instruments()[0]); |
| 189 EXPECT_EQ(1, num_on_selected_information_changed_called()); | 193 EXPECT_EQ(1, num_on_selected_information_changed_called()); |
| 190 EXPECT_TRUE(state()->is_ready_to_pay()); | 194 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 191 | 195 |
| 192 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | 196 // TODO(mathp): Currently synchronous, when async will need a RunLoop. |
| 193 state()->GeneratePaymentResponse(); | 197 state()->GeneratePaymentResponse(); |
| 194 EXPECT_EQ("basic-card", response()->method_name); | 198 EXPECT_EQ("visa", response()->method_name); |
| 195 EXPECT_EQ( | 199 EXPECT_EQ( |
| 196 "{\"billingAddress\":" | 200 "{\"billingAddress\":" |
| 197 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | 201 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," |
| 198 "\"city\":\"Elysium\"," | 202 "\"city\":\"Elysium\"," |
| 199 "\"country\":\"US\"," | 203 "\"country\":\"US\"," |
| 200 "\"organization\":\"Underworld\"," | 204 "\"organization\":\"Underworld\"," |
| 201 "\"phone\":\"16502111111\"," | 205 "\"phone\":\"16502111111\"," |
| 202 "\"postalCode\":\"91111\"," | 206 "\"postalCode\":\"91111\"," |
| 203 "\"recipient\":\"John H. Doe\"," | 207 "\"recipient\":\"John H. Doe\"," |
| 204 "\"region\":\"CA\"}," | 208 "\"region\":\"CA\"}," |
| 205 "\"cardNumber\":\"4111111111111111\"," | 209 "\"cardNumber\":\"4111111111111111\"," |
| 206 "\"cardSecurityCode\":\"123\"," | 210 "\"cardSecurityCode\":\"123\"," |
| 207 "\"cardholderName\":\"Test User\"," | 211 "\"cardholderName\":\"Test User\"," |
| 208 "\"expiryMonth\":\"11\"," | 212 "\"expiryMonth\":\"11\"," |
| 209 "\"expiryYear\":\"2017\"}", | 213 "\"expiryYear\":\"2017\"}", |
| 210 response()->stringified_details); | 214 response()->stringified_details); |
| 211 } | 215 } |
| 212 | 216 |
| 213 } // namespace payments | 217 } // namespace payments |
| OLD | NEW |