| 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 "components/autofill/core/browser/autofill_profile.h" | 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 11 #include "components/autofill/core/browser/autofill_test_utils.h" | 12 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/test_personal_data_manager.h" | 14 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 14 #include "components/payments/content/payment_request.mojom.h" | 15 #include "components/payments/content/payment_request.mojom.h" |
| 15 #include "components/payments/content/payment_request_spec.h" | 16 #include "components/payments/content/payment_request_spec.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace payments { | 19 namespace payments { |
| 19 | 20 |
| 20 class PaymentRequestStateTest : public testing::Test, | 21 class PaymentRequestStateTest : public testing::Test, |
| 21 public PaymentRequestState::Observer, | 22 public PaymentRequestState::Observer, |
| 22 public PaymentRequestState::Delegate { | 23 public PaymentRequestState::Delegate { |
| 23 protected: | 24 protected: |
| 24 PaymentRequestStateTest() | 25 PaymentRequestStateTest() |
| 25 : num_on_selected_information_changed_called_(0), | 26 : num_on_selected_information_changed_called_(0), |
| 26 address_(autofill::test::GetFullProfile()), | 27 address_(autofill::test::GetFullProfile()), |
| 27 credit_card_visa_(autofill::test::GetCreditCard()), | 28 credit_card_visa_(autofill::test::GetCreditCard()), |
| 28 credit_card_amex_(autofill::test::GetCreditCard2()) { | 29 credit_card_amex_(autofill::test::GetCreditCard2()) { |
| 29 test_personal_data_manager_.AddTestingProfile(&address_); | 30 test_personal_data_manager_.AddTestingProfile(&address_); |
| 30 credit_card_visa_.set_billing_address_id(address_.guid()); | 31 credit_card_visa_.set_billing_address_id(address_.guid()); |
| 31 credit_card_visa_.set_use_count(5u); | 32 credit_card_visa_.set_use_count(5u); |
| 32 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); | 33 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); |
| 33 credit_card_amex_.set_billing_address_id(address_.guid()); | 34 credit_card_amex_.set_billing_address_id(address_.guid()); |
| 34 credit_card_amex_.set_use_count(1u); | 35 credit_card_amex_.set_use_count(1u); |
| 35 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); | 36 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); |
| 37 // Add an expired JCB card here. |
| 38 credit_card_jcb_ = autofill::test::GetCreditCard(); |
| 39 credit_card_jcb_.SetNumber(base::ASCIIToUTF16("3530111333300000")); |
| 40 credit_card_jcb_.set_billing_address_id(address_.guid()); |
| 41 credit_card_jcb_.set_use_count(1u); |
| 42 credit_card_jcb_.SetExpirationDateFromString(base::ASCIIToUTF16("01/17")); |
| 36 } | 43 } |
| 37 ~PaymentRequestStateTest() override {} | 44 ~PaymentRequestStateTest() override {} |
| 38 | 45 |
| 39 // PaymentRequestState::Observer: | 46 // PaymentRequestState::Observer: |
| 40 void OnSelectedInformationChanged() override { | 47 void OnSelectedInformationChanged() override { |
| 41 num_on_selected_information_changed_called_++; | 48 num_on_selected_information_changed_called_++; |
| 42 } | 49 } |
| 43 | 50 |
| 44 // PaymentRequestState::Delegate: | 51 // PaymentRequestState::Delegate: |
| 45 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { | 52 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 std::unique_ptr<PaymentRequestState> state_; | 103 std::unique_ptr<PaymentRequestState> state_; |
| 97 std::unique_ptr<PaymentRequestSpec> spec_; | 104 std::unique_ptr<PaymentRequestSpec> spec_; |
| 98 int num_on_selected_information_changed_called_; | 105 int num_on_selected_information_changed_called_; |
| 99 mojom::PaymentResponsePtr payment_response_; | 106 mojom::PaymentResponsePtr payment_response_; |
| 100 autofill::TestPersonalDataManager test_personal_data_manager_; | 107 autofill::TestPersonalDataManager test_personal_data_manager_; |
| 101 | 108 |
| 102 // Test data. | 109 // Test data. |
| 103 autofill::AutofillProfile address_; | 110 autofill::AutofillProfile address_; |
| 104 autofill::CreditCard credit_card_visa_; | 111 autofill::CreditCard credit_card_visa_; |
| 105 autofill::CreditCard credit_card_amex_; | 112 autofill::CreditCard credit_card_amex_; |
| 113 autofill::CreditCard credit_card_jcb_; |
| 106 }; | 114 }; |
| 107 | 115 |
| 116 TEST_F(PaymentRequestStateTest, CanMakePayment) { |
| 117 // Default options. |
| 118 RecreateStateWithOptions(mojom::PaymentOptions::New()); |
| 119 |
| 120 // CanMakePayment returns true because the method data requires Visa, and the |
| 121 // user has a Visa card on file. |
| 122 EXPECT_TRUE(state()->CanMakePayment()); |
| 123 } |
| 124 |
| 125 TEST_F(PaymentRequestStateTest, CanMakePayment_CannotMakePayment) { |
| 126 // The method data requires MasterCard. |
| 127 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 128 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 129 entry->supported_methods.push_back("mastercard"); |
| 130 method_data.push_back(std::move(entry)); |
| 131 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 132 mojom::PaymentDetails::New(), |
| 133 std::move(method_data)); |
| 134 |
| 135 // CanMakePayment returns false because the method data requires MasterCard, |
| 136 // and the user doesn't have such an instrument. |
| 137 EXPECT_FALSE(state()->CanMakePayment()); |
| 138 } |
| 139 |
| 140 TEST_F(PaymentRequestStateTest, CanMakePayment_OnlyBasicCard) { |
| 141 // The method data supports everything in basic-card. |
| 142 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 143 entry->supported_methods.push_back("basic-card"); |
| 144 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 145 method_data.push_back(std::move(entry)); |
| 146 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 147 mojom::PaymentDetails::New(), |
| 148 std::move(method_data)); |
| 149 |
| 150 // CanMakePayment returns true because the method data supports everything, |
| 151 // and the user has at least one instrument. |
| 152 EXPECT_TRUE(state()->CanMakePayment()); |
| 153 } |
| 154 |
| 155 TEST_F(PaymentRequestStateTest, CanMakePayment_BasicCard_SpecificAvailable) { |
| 156 // The method data supports visa through basic-card. |
| 157 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 158 entry->supported_methods.push_back("basic-card"); |
| 159 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA); |
| 160 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 161 method_data.push_back(std::move(entry)); |
| 162 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 163 mojom::PaymentDetails::New(), |
| 164 std::move(method_data)); |
| 165 |
| 166 // CanMakePayment returns true because the method data supports visa, and the |
| 167 // user has a Visa instrument. |
| 168 EXPECT_TRUE(state()->CanMakePayment()); |
| 169 } |
| 170 |
| 171 TEST_F(PaymentRequestStateTest, |
| 172 CanMakePayment_BasicCard_SpecificAvailableButInvalid) { |
| 173 // The method data supports jcb through basic-card. |
| 174 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 175 entry->supported_methods.push_back("basic-card"); |
| 176 entry->supported_networks.push_back(mojom::BasicCardNetwork::JCB); |
| 177 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 178 method_data.push_back(std::move(entry)); |
| 179 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 180 mojom::PaymentDetails::New(), |
| 181 std::move(method_data)); |
| 182 |
| 183 // CanMakePayment returns false because the method data supports jcb, and the |
| 184 // user has a JCB instrument, but it's invalid. |
| 185 EXPECT_FALSE(state()->CanMakePayment()); |
| 186 } |
| 187 |
| 188 TEST_F(PaymentRequestStateTest, CanMakePayment_BasicCard_SpecificUnavailable) { |
| 189 // The method data supports mastercard through basic-card. |
| 190 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 191 entry->supported_methods.push_back("basic-card"); |
| 192 entry->supported_networks.push_back(mojom::BasicCardNetwork::MASTERCARD); |
| 193 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 194 method_data.push_back(std::move(entry)); |
| 195 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 196 mojom::PaymentDetails::New(), |
| 197 std::move(method_data)); |
| 198 |
| 199 // CanMakePayment returns false because the method data supports mastercard, |
| 200 // and the user doesn't have such an instrument. |
| 201 EXPECT_FALSE(state()->CanMakePayment()); |
| 202 } |
| 203 |
| 108 // Test that the last shipping option is selected. | 204 // Test that the last shipping option is selected. |
| 109 TEST_F(PaymentRequestStateTest, ShippingOptionsSelection) { | 205 TEST_F(PaymentRequestStateTest, ShippingOptionsSelection) { |
| 110 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; | 206 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; |
| 111 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); | 207 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); |
| 112 option->id = "option:1"; | 208 option->id = "option:1"; |
| 113 option->selected = false; | 209 option->selected = false; |
| 114 shipping_options.push_back(std::move(option)); | 210 shipping_options.push_back(std::move(option)); |
| 115 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New(); | 211 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New(); |
| 116 option2->id = "option:2"; | 212 option2->id = "option:2"; |
| 117 option2->selected = true; | 213 option2->selected = true; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 "\"region\":\"CA\"}," | 295 "\"region\":\"CA\"}," |
| 200 "\"cardNumber\":\"4111111111111111\"," | 296 "\"cardNumber\":\"4111111111111111\"," |
| 201 "\"cardSecurityCode\":\"123\"," | 297 "\"cardSecurityCode\":\"123\"," |
| 202 "\"cardholderName\":\"Test User\"," | 298 "\"cardholderName\":\"Test User\"," |
| 203 "\"expiryMonth\":\"11\"," | 299 "\"expiryMonth\":\"11\"," |
| 204 "\"expiryYear\":\"2017\"}", | 300 "\"expiryYear\":\"2017\"}", |
| 205 response()->stringified_details); | 301 response()->stringified_details); |
| 206 } | 302 } |
| 207 | 303 |
| 208 } // namespace payments | 304 } // namespace payments |
| OLD | NEW |