Chromium Code Reviews| 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_spec.h" | 16 #include "components/payments/content/payment_request_spec.h" |
| 17 #include "components/payments/core/payment_request_delegate.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace payments { | 19 namespace payments { |
| 21 | 20 |
| 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 void DoFullCardRequest( | |
| 42 const autofill::CreditCard& credit_card, | |
| 43 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> | |
| 44 result_delegate) override { | |
| 45 result_delegate->OnFullCardRequestSucceeded(credit_card, | |
| 46 base::ASCIIToUTF16("123")); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 autofill::PersonalDataManager* personal_data_manager_; | |
| 51 std::string locale_; | |
| 52 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); | |
| 53 }; | |
| 54 | |
| 55 class PaymentRequestStateTest : public testing::Test, | 21 class PaymentRequestStateTest : public testing::Test, |
| 56 public PaymentRequestState::Observer, | 22 public PaymentRequestState::Observer, |
| 57 public PaymentRequestState::Delegate { | 23 public PaymentRequestState::Delegate { |
| 58 protected: | 24 protected: |
| 59 PaymentRequestStateTest() | 25 PaymentRequestStateTest() |
| 60 : num_on_selected_information_changed_called_(0), | 26 : num_on_selected_information_changed_called_(0), |
| 61 payment_request_delegate_( | |
| 62 new FakePaymentRequestDelegate(&test_personal_data_manager_)), | |
| 63 address_(autofill::test::GetFullProfile()), | 27 address_(autofill::test::GetFullProfile()), |
| 64 credit_card_visa_(autofill::test::GetCreditCard()), | 28 credit_card_visa_(autofill::test::GetCreditCard()), |
|
Mathieu
2017/04/11 14:35:24
See if we can clean up some of these members?
sebsg
2017/04/11 14:54:41
Done.
| |
| 65 credit_card_amex_(autofill::test::GetCreditCard2()) { | 29 credit_card_amex_(autofill::test::GetCreditCard2()) { |
| 66 test_personal_data_manager_.AddTestingProfile(&address_); | 30 test_personal_data_manager_.AddTestingProfile(&address_); |
| 67 credit_card_visa_.set_billing_address_id(address_.guid()); | 31 credit_card_visa_.set_billing_address_id(address_.guid()); |
| 68 credit_card_visa_.set_use_count(5u); | 32 credit_card_visa_.set_use_count(5u); |
| 69 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); | 33 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); |
| 70 credit_card_amex_.set_billing_address_id(address_.guid()); | 34 credit_card_amex_.set_billing_address_id(address_.guid()); |
| 71 credit_card_amex_.set_use_count(1u); | 35 credit_card_amex_.set_use_count(1u); |
| 72 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); | 36 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_); |
| 73 // Add an expired JCB card here. | 37 // Add an expired JCB card here. |
| 74 credit_card_jcb_ = autofill::test::GetCreditCard(); | 38 credit_card_jcb_ = autofill::test::GetCreditCard(); |
|
Mathieu
2017/04/11 14:35:24
I think this is never used?
sebsg
2017/04/11 14:54:41
Done.
| |
| 75 credit_card_jcb_.SetNumber(base::ASCIIToUTF16("3530111333300000")); | 39 credit_card_jcb_.SetNumber(base::ASCIIToUTF16("3530111333300000")); |
| 76 credit_card_jcb_.set_billing_address_id(address_.guid()); | 40 credit_card_jcb_.set_billing_address_id(address_.guid()); |
| 77 credit_card_jcb_.set_use_count(1u); | 41 credit_card_jcb_.set_use_count(1u); |
| 78 credit_card_jcb_.SetExpirationDateFromString(base::ASCIIToUTF16("01/17")); | 42 credit_card_jcb_.SetExpirationDateFromString(base::ASCIIToUTF16("01/17")); |
| 79 } | 43 } |
| 80 ~PaymentRequestStateTest() override {} | 44 ~PaymentRequestStateTest() override {} |
| 81 | 45 |
| 82 // PaymentRequestState::Observer: | 46 // PaymentRequestState::Observer: |
| 83 void OnSelectedInformationChanged() override { | 47 void OnSelectedInformationChanged() override { |
| 84 num_on_selected_information_changed_called_++; | 48 num_on_selected_information_changed_called_++; |
| 85 } | 49 } |
| 86 | 50 |
| 87 // PaymentRequestState::Delegate: | 51 // PaymentRequestState::Delegate: |
| 88 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { | 52 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { |
| 89 payment_response_ = std::move(response); | 53 payment_response_ = std::move(response); |
| 90 }; | 54 }; |
| 91 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} | 55 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} |
| 92 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} | 56 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} |
| 93 | 57 |
| 94 void RecreateStateWithOptionsAndDetails( | 58 void RecreateStateWithOptionsAndDetails( |
| 95 mojom::PaymentOptionsPtr options, | 59 mojom::PaymentOptionsPtr options, |
| 96 mojom::PaymentDetailsPtr details, | 60 mojom::PaymentDetailsPtr details, |
| 97 std::vector<mojom::PaymentMethodDataPtr> method_data) { | 61 std::vector<mojom::PaymentMethodDataPtr> method_data) { |
| 98 // The spec will be based on the |options| and |details| passed in. | 62 // The spec will be based on the |options| and |details| passed in. |
| 99 spec_ = base::MakeUnique<PaymentRequestSpec>( | 63 spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 100 std::move(options), std::move(details), std::move(method_data), nullptr, | 64 std::move(options), std::move(details), std::move(method_data), nullptr, |
| 101 "en-US"); | 65 "en-US"); |
| 102 state_ = base::MakeUnique<PaymentRequestState>( | 66 state_ = base::MakeUnique<PaymentRequestState>( |
| 103 spec_.get(), this, "en-US", &test_personal_data_manager_, | 67 spec_.get(), this, "en-US", &test_personal_data_manager_, nullptr); |
| 104 payment_request_delegate_.get()); | |
| 105 state_->AddObserver(this); | 68 state_->AddObserver(this); |
| 106 } | 69 } |
| 107 | 70 |
| 108 // Convenience method to create a PaymentRequestState with default details | 71 // Convenience method to create a PaymentRequestState with default details |
| 109 // (one shipping option) and method data (only supports visa). | 72 // (one shipping option) and method data (only supports visa). |
| 110 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) { | 73 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) { |
| 111 // Create dummy PaymentDetails with a single shipping option. | 74 // Create dummy PaymentDetails with a single shipping option. |
| 112 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; | 75 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; |
| 113 mojom::PaymentShippingOptionPtr option = | 76 mojom::PaymentShippingOptionPtr option = |
| 114 mojom::PaymentShippingOption::New(); | 77 mojom::PaymentShippingOption::New(); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 137 } | 100 } |
| 138 | 101 |
| 139 autofill::AutofillProfile* test_address() { return &address_; } | 102 autofill::AutofillProfile* test_address() { return &address_; } |
| 140 | 103 |
| 141 private: | 104 private: |
| 142 std::unique_ptr<PaymentRequestState> state_; | 105 std::unique_ptr<PaymentRequestState> state_; |
| 143 std::unique_ptr<PaymentRequestSpec> spec_; | 106 std::unique_ptr<PaymentRequestSpec> spec_; |
| 144 int num_on_selected_information_changed_called_; | 107 int num_on_selected_information_changed_called_; |
| 145 mojom::PaymentResponsePtr payment_response_; | 108 mojom::PaymentResponsePtr payment_response_; |
| 146 autofill::TestPersonalDataManager test_personal_data_manager_; | 109 autofill::TestPersonalDataManager test_personal_data_manager_; |
| 147 std::unique_ptr<FakePaymentRequestDelegate> payment_request_delegate_; | |
| 148 | 110 |
| 149 // Test data. | 111 // Test data. |
| 150 autofill::AutofillProfile address_; | 112 autofill::AutofillProfile address_; |
| 151 autofill::CreditCard credit_card_visa_; | 113 autofill::CreditCard credit_card_visa_; |
| 152 autofill::CreditCard credit_card_amex_; | 114 autofill::CreditCard credit_card_amex_; |
| 153 autofill::CreditCard credit_card_jcb_; | 115 autofill::CreditCard credit_card_jcb_; |
| 154 }; | 116 }; |
| 155 | 117 |
| 156 TEST_F(PaymentRequestStateTest, CanMakePayment) { | 118 TEST_F(PaymentRequestStateTest, CanMakePayment) { |
| 157 // Default options. | 119 // Default options. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 | 252 |
| 291 EXPECT_FALSE(state()->is_ready_to_pay()); | 253 EXPECT_FALSE(state()->is_ready_to_pay()); |
| 292 | 254 |
| 293 state()->SetSelectedContactProfile(test_address()); | 255 state()->SetSelectedContactProfile(test_address()); |
| 294 EXPECT_EQ(2, num_on_selected_information_changed_called()); | 256 EXPECT_EQ(2, num_on_selected_information_changed_called()); |
| 295 | 257 |
| 296 // Ready to pay! | 258 // Ready to pay! |
| 297 EXPECT_TRUE(state()->is_ready_to_pay()); | 259 EXPECT_TRUE(state()->is_ready_to_pay()); |
| 298 } | 260 } |
| 299 | 261 |
| 300 // Test generating a PaymentResponse. | |
| 301 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse_SupportedMethod) { | |
| 302 // Default options (no shipping, no contact info). | |
| 303 RecreateStateWithOptions(mojom::PaymentOptions::New()); | |
| 304 state()->SetSelectedInstrument(state()->available_instruments()[0].get()); | |
| 305 EXPECT_EQ(1, num_on_selected_information_changed_called()); | |
| 306 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 307 | |
| 308 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | |
| 309 // "visa" is specified directly in the supportedMethods so it is returned | |
| 310 // as the method name. | |
| 311 state()->GeneratePaymentResponse(); | |
| 312 EXPECT_EQ("visa", response()->method_name); | |
| 313 EXPECT_EQ( | |
| 314 "{\"billingAddress\":" | |
| 315 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | |
| 316 "\"city\":\"Elysium\"," | |
| 317 "\"country\":\"US\"," | |
| 318 "\"organization\":\"Underworld\"," | |
| 319 "\"phone\":\"16502111111\"," | |
| 320 "\"postalCode\":\"91111\"," | |
| 321 "\"recipient\":\"John H. Doe\"," | |
| 322 "\"region\":\"CA\"}," | |
| 323 "\"cardNumber\":\"4111111111111111\"," | |
| 324 "\"cardSecurityCode\":\"123\"," | |
| 325 "\"cardholderName\":\"Test User\"," | |
| 326 "\"expiryMonth\":\"11\"," | |
| 327 "\"expiryYear\":\"2017\"}", | |
| 328 response()->stringified_details); | |
| 329 } | |
| 330 | |
| 331 // Test generating a PaymentResponse when the method is specified through | |
| 332 // "basic-card". | |
| 333 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse_BasicCard) { | |
| 334 // The method data supports visa through basic-card. | |
| 335 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); | |
| 336 entry->supported_methods.push_back("basic-card"); | |
| 337 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA); | |
| 338 std::vector<mojom::PaymentMethodDataPtr> method_data; | |
| 339 method_data.push_back(std::move(entry)); | |
| 340 RecreateStateWithOptionsAndDetails(mojom::PaymentOptions::New(), | |
| 341 mojom::PaymentDetails::New(), | |
| 342 std::move(method_data)); | |
| 343 | |
| 344 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 345 | |
| 346 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | |
| 347 // "basic-card" is specified so it is returned as the method name. | |
| 348 state()->GeneratePaymentResponse(); | |
| 349 EXPECT_EQ("basic-card", response()->method_name); | |
| 350 EXPECT_EQ( | |
| 351 "{\"billingAddress\":" | |
| 352 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | |
| 353 "\"city\":\"Elysium\"," | |
| 354 "\"country\":\"US\"," | |
| 355 "\"organization\":\"Underworld\"," | |
| 356 "\"phone\":\"16502111111\"," | |
| 357 "\"postalCode\":\"91111\"," | |
| 358 "\"recipient\":\"John H. Doe\"," | |
| 359 "\"region\":\"CA\"}," | |
| 360 "\"cardNumber\":\"4111111111111111\"," | |
| 361 "\"cardSecurityCode\":\"123\"," | |
| 362 "\"cardholderName\":\"Test User\"," | |
| 363 "\"expiryMonth\":\"11\"," | |
| 364 "\"expiryYear\":\"2017\"}", | |
| 365 response()->stringified_details); | |
| 366 } | |
| 367 | |
| 368 // Tests the the generated PaymentResponse has the correct values for the | |
| 369 // shipping address. | |
| 370 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse_ShippingAddress) { | |
| 371 // Setup so that a shipping address is requested. | |
| 372 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; | |
| 373 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); | |
| 374 option->id = "option:1"; | |
| 375 option->selected = true; | |
| 376 shipping_options.push_back(std::move(option)); | |
| 377 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New(); | |
| 378 details->shipping_options = std::move(shipping_options); | |
| 379 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | |
| 380 options->request_shipping = true; | |
| 381 RecreateStateWithOptionsAndDetails(std::move(options), std::move(details), | |
| 382 GetMethodDataForVisa()); | |
| 383 | |
| 384 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 385 state()->GeneratePaymentResponse(); | |
| 386 | |
| 387 // Check that all the expected values were set. | |
| 388 EXPECT_EQ("US", response()->shipping_address->country); | |
| 389 EXPECT_EQ("666 Erebus St.", response()->shipping_address->address_line[0]); | |
| 390 EXPECT_EQ("Apt 8", response()->shipping_address->address_line[1]); | |
| 391 EXPECT_EQ("CA", response()->shipping_address->region); | |
| 392 EXPECT_EQ("Elysium", response()->shipping_address->city); | |
| 393 EXPECT_EQ("", response()->shipping_address->dependent_locality); | |
| 394 EXPECT_EQ("91111", response()->shipping_address->postal_code); | |
| 395 EXPECT_EQ("", response()->shipping_address->sorting_code); | |
| 396 EXPECT_EQ("", response()->shipping_address->language_code); | |
| 397 EXPECT_EQ("Underworld", response()->shipping_address->organization); | |
| 398 EXPECT_EQ("John H. Doe", response()->shipping_address->recipient); | |
| 399 EXPECT_EQ("16502111111", response()->shipping_address->phone); | |
| 400 } | |
| 401 | |
| 402 // Tests the the generated PaymentResponse has the correct values for the | |
| 403 // contact details when all values are requested. | |
| 404 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse_ContactDetails_All) { | |
| 405 // Request all contact detail values. | |
| 406 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | |
| 407 options->request_payer_name = true; | |
| 408 options->request_payer_phone = true; | |
| 409 options->request_payer_email = true; | |
| 410 RecreateStateWithOptions(std::move(options)); | |
| 411 | |
| 412 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 413 state()->GeneratePaymentResponse(); | |
| 414 | |
| 415 // Check that all the expected values were set. | |
| 416 EXPECT_EQ("John H. Doe", response()->payer_name.value()); | |
| 417 EXPECT_EQ("16502111111", response()->payer_phone.value()); | |
| 418 EXPECT_EQ("johndoe@hades.com", response()->payer_email.value()); | |
| 419 } | |
| 420 | |
| 421 // Tests the the generated PaymentResponse has the correct values for the | |
| 422 // contact details when all values are requested. | |
| 423 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse_ContactDetails_Some) { | |
| 424 // Request one contact detail value. | |
| 425 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | |
| 426 options->request_payer_name = true; | |
| 427 RecreateStateWithOptions(std::move(options)); | |
| 428 | |
| 429 EXPECT_TRUE(state()->is_ready_to_pay()); | |
| 430 state()->GeneratePaymentResponse(); | |
| 431 | |
| 432 // Check that the name was set, but not the other values. | |
| 433 EXPECT_EQ("John H. Doe", response()->payer_name.value()); | |
| 434 EXPECT_FALSE(response()->payer_phone.has_value()); | |
| 435 EXPECT_FALSE(response()->payer_email.has_value()); | |
| 436 } | |
| 437 | |
| 438 } // namespace payments | 262 } // namespace payments |
| OLD | NEW |