| 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_response_helper.h" | 5 #include "components/payments/content/payment_response_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/browser/autofill_profile.h" | 14 #include "components/autofill/core/browser/autofill_profile.h" |
| 15 #include "components/autofill/core/browser/autofill_test_utils.h" | 15 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 16 #include "components/autofill/core/browser/credit_card.h" | 16 #include "components/autofill/core/browser/credit_card.h" |
| 17 #include "components/autofill/core/browser/test_personal_data_manager.h" | 17 #include "components/autofill/core/browser/test_personal_data_manager.h" |
| 18 #include "components/payments/content/payment_request_spec.h" | 18 #include "components/payments/content/payment_request_spec.h" |
| 19 #include "components/payments/core/address_normalizer.h" |
| 19 #include "components/payments/core/autofill_payment_instrument.h" | 20 #include "components/payments/core/autofill_payment_instrument.h" |
| 20 #include "components/payments/core/payment_request_delegate.h" | 21 #include "components/payments/core/payment_request_delegate.h" |
| 21 #include "components/payments/mojom/payment_request.mojom.h" | 22 #include "components/payments/mojom/payment_request.mojom.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 namespace payments { | 25 namespace payments { |
| 25 | 26 |
| 27 class FakeAddressNormalizer : public AddressNormalizer { |
| 28 public: |
| 29 FakeAddressNormalizer() {} |
| 30 |
| 31 void LoadRulesForRegion(const std::string& region_code) override {} |
| 32 |
| 33 bool AreRulesLoadedForRegion(const std::string& region_code) override { |
| 34 return true; |
| 35 } |
| 36 |
| 37 void StartAddressNormalization( |
| 38 const autofill::AutofillProfile& profile, |
| 39 const std::string& region_code, |
| 40 int timeout_seconds, |
| 41 AddressNormalizer::Delegate* requester) override { |
| 42 requester->OnAddressNormalized(profile); |
| 43 } |
| 44 |
| 45 void OnAddressValidationRulesLoaded(const std::string& region_code, |
| 46 bool success) override {} |
| 47 }; |
| 48 |
| 26 class FakePaymentRequestDelegate : public PaymentRequestDelegate { | 49 class FakePaymentRequestDelegate : public PaymentRequestDelegate { |
| 27 public: | 50 public: |
| 28 FakePaymentRequestDelegate( | 51 FakePaymentRequestDelegate( |
| 29 autofill::PersonalDataManager* personal_data_manager) | 52 autofill::PersonalDataManager* personal_data_manager) |
| 30 : personal_data_manager_(personal_data_manager), | 53 : personal_data_manager_(personal_data_manager), |
| 31 locale_("en-US"), | 54 locale_("en-US"), |
| 32 last_committed_url_("https://shop.com") {} | 55 last_committed_url_("https://shop.com") {} |
| 33 void ShowDialog(PaymentRequest* request) override {} | 56 void ShowDialog(PaymentRequest* request) override {} |
| 34 | 57 |
| 35 void CloseDialog() override {} | 58 void CloseDialog() override {} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 51 } | 74 } |
| 52 | 75 |
| 53 void DoFullCardRequest( | 76 void DoFullCardRequest( |
| 54 const autofill::CreditCard& credit_card, | 77 const autofill::CreditCard& credit_card, |
| 55 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> | 78 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate> |
| 56 result_delegate) override { | 79 result_delegate) override { |
| 57 result_delegate->OnFullCardRequestSucceeded(credit_card, | 80 result_delegate->OnFullCardRequestSucceeded(credit_card, |
| 58 base::ASCIIToUTF16("123")); | 81 base::ASCIIToUTF16("123")); |
| 59 } | 82 } |
| 60 | 83 |
| 61 std::unique_ptr<const ::i18n::addressinput::Source> GetAddressInputSource() | 84 std::unique_ptr<::i18n::addressinput::Source> GetAddressInputSource() |
| 62 override { | 85 override { |
| 63 return nullptr; | 86 return nullptr; |
| 64 } | 87 } |
| 65 | 88 |
| 66 std::unique_ptr<::i18n::addressinput::Storage> GetAddressInputStorage() | 89 std::unique_ptr<::i18n::addressinput::Storage> GetAddressInputStorage() |
| 67 override { | 90 override { |
| 68 return nullptr; | 91 return nullptr; |
| 69 } | 92 } |
| 70 | 93 |
| 94 AddressNormalizer* GetAddressNormalizer() override { |
| 95 return &address_normalizer_; |
| 96 } |
| 97 |
| 71 private: | 98 private: |
| 72 autofill::PersonalDataManager* personal_data_manager_; | 99 autofill::PersonalDataManager* personal_data_manager_; |
| 73 std::string locale_; | 100 std::string locale_; |
| 74 const GURL last_committed_url_; | 101 const GURL last_committed_url_; |
| 102 FakeAddressNormalizer address_normalizer_; |
| 75 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); | 103 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate); |
| 76 }; | 104 }; |
| 77 | 105 |
| 78 class PaymentResponseHelperTest : public testing::Test, | 106 class PaymentResponseHelperTest : public testing::Test, |
| 79 public PaymentResponseHelper::Delegate { | 107 public PaymentResponseHelper::Delegate { |
| 80 protected: | 108 protected: |
| 81 PaymentResponseHelperTest() | 109 PaymentResponseHelperTest() |
| 82 : payment_request_delegate_(&test_personal_data_manager_), | 110 : payment_request_delegate_(&test_personal_data_manager_), |
| 83 address_(autofill::test::GetFullProfile()), | 111 address_(autofill::test::GetFullProfile()), |
| 84 billing_addresses_({&address_}) { | 112 billing_addresses_({&address_}) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); | 161 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); |
| 134 entry->supported_methods.push_back("visa"); | 162 entry->supported_methods.push_back("visa"); |
| 135 method_data.push_back(std::move(entry)); | 163 method_data.push_back(std::move(entry)); |
| 136 return method_data; | 164 return method_data; |
| 137 } | 165 } |
| 138 | 166 |
| 139 PaymentRequestSpec* spec() { return spec_.get(); } | 167 PaymentRequestSpec* spec() { return spec_.get(); } |
| 140 const mojom::PaymentResponsePtr& response() { return payment_response_; } | 168 const mojom::PaymentResponsePtr& response() { return payment_response_; } |
| 141 autofill::AutofillProfile* test_address() { return &address_; } | 169 autofill::AutofillProfile* test_address() { return &address_; } |
| 142 PaymentInstrument* test_instrument() { return autofill_instrument_.get(); } | 170 PaymentInstrument* test_instrument() { return autofill_instrument_.get(); } |
| 171 PaymentRequestDelegate* test_payment_request_delegate() { |
| 172 return &payment_request_delegate_; |
| 173 } |
| 143 | 174 |
| 144 private: | 175 private: |
| 145 std::unique_ptr<PaymentRequestSpec> spec_; | 176 std::unique_ptr<PaymentRequestSpec> spec_; |
| 146 mojom::PaymentResponsePtr payment_response_; | 177 mojom::PaymentResponsePtr payment_response_; |
| 147 autofill::TestPersonalDataManager test_personal_data_manager_; | 178 autofill::TestPersonalDataManager test_personal_data_manager_; |
| 148 FakePaymentRequestDelegate payment_request_delegate_; | 179 FakePaymentRequestDelegate payment_request_delegate_; |
| 149 | 180 |
| 150 // Test data. | 181 // Test data. |
| 151 autofill::AutofillProfile address_; | 182 autofill::AutofillProfile address_; |
| 152 const std::vector<autofill::AutofillProfile*> billing_addresses_; | 183 const std::vector<autofill::AutofillProfile*> billing_addresses_; |
| 153 std::unique_ptr<AutofillPaymentInstrument> autofill_instrument_; | 184 std::unique_ptr<AutofillPaymentInstrument> autofill_instrument_; |
| 154 }; | 185 }; |
| 155 | 186 |
| 156 // Test generating a PaymentResponse. | 187 // Test generating a PaymentResponse. |
| 157 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_SupportedMethod) { | 188 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_SupportedMethod) { |
| 158 // Default options (no shipping, no contact info). | 189 // Default options (no shipping, no contact info). |
| 159 RecreateSpecWithOptions(mojom::PaymentOptions::New()); | 190 RecreateSpecWithOptions(mojom::PaymentOptions::New()); |
| 160 | 191 |
| 161 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | 192 // TODO(mathp): Currently synchronous, when async will need a RunLoop. |
| 162 // "visa" is specified directly in the supportedMethods so it is returned | 193 // "visa" is specified directly in the supportedMethods so it is returned |
| 163 // as the method name. | 194 // as the method name. |
| 164 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 195 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 165 test_address(), test_address(), this); | 196 test_payment_request_delegate(), test_address(), |
| 197 test_address(), this); |
| 166 EXPECT_EQ("visa", response()->method_name); | 198 EXPECT_EQ("visa", response()->method_name); |
| 167 EXPECT_EQ( | 199 EXPECT_EQ( |
| 168 "{\"billingAddress\":" | 200 "{\"billingAddress\":" |
| 169 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | 201 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," |
| 170 "\"city\":\"Elysium\"," | 202 "\"city\":\"Elysium\"," |
| 171 "\"country\":\"US\"," | 203 "\"country\":\"US\"," |
| 172 "\"organization\":\"Underworld\"," | 204 "\"organization\":\"Underworld\"," |
| 173 "\"phone\":\"16502111111\"," | 205 "\"phone\":\"16502111111\"," |
| 174 "\"postalCode\":\"91111\"," | 206 "\"postalCode\":\"91111\"," |
| 175 "\"recipient\":\"John H. Doe\"," | 207 "\"recipient\":\"John H. Doe\"," |
| (...skipping 15 matching lines...) Expand all Loading... |
| 191 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA); | 223 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA); |
| 192 std::vector<mojom::PaymentMethodDataPtr> method_data; | 224 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 193 method_data.push_back(std::move(entry)); | 225 method_data.push_back(std::move(entry)); |
| 194 RecreateSpecWithOptionsAndDetails(mojom::PaymentOptions::New(), | 226 RecreateSpecWithOptionsAndDetails(mojom::PaymentOptions::New(), |
| 195 mojom::PaymentDetails::New(), | 227 mojom::PaymentDetails::New(), |
| 196 std::move(method_data)); | 228 std::move(method_data)); |
| 197 | 229 |
| 198 // TODO(mathp): Currently synchronous, when async will need a RunLoop. | 230 // TODO(mathp): Currently synchronous, when async will need a RunLoop. |
| 199 // "basic-card" is specified so it is returned as the method name. | 231 // "basic-card" is specified so it is returned as the method name. |
| 200 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 232 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 201 test_address(), test_address(), this); | 233 test_payment_request_delegate(), test_address(), |
| 234 test_address(), this); |
| 202 EXPECT_EQ("basic-card", response()->method_name); | 235 EXPECT_EQ("basic-card", response()->method_name); |
| 203 EXPECT_EQ( | 236 EXPECT_EQ( |
| 204 "{\"billingAddress\":" | 237 "{\"billingAddress\":" |
| 205 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," | 238 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"]," |
| 206 "\"city\":\"Elysium\"," | 239 "\"city\":\"Elysium\"," |
| 207 "\"country\":\"US\"," | 240 "\"country\":\"US\"," |
| 208 "\"organization\":\"Underworld\"," | 241 "\"organization\":\"Underworld\"," |
| 209 "\"phone\":\"16502111111\"," | 242 "\"phone\":\"16502111111\"," |
| 210 "\"postalCode\":\"91111\"," | 243 "\"postalCode\":\"91111\"," |
| 211 "\"recipient\":\"John H. Doe\"," | 244 "\"recipient\":\"John H. Doe\"," |
| (...skipping 16 matching lines...) Expand all Loading... |
| 228 option->selected = true; | 261 option->selected = true; |
| 229 shipping_options.push_back(std::move(option)); | 262 shipping_options.push_back(std::move(option)); |
| 230 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New(); | 263 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New(); |
| 231 details->shipping_options = std::move(shipping_options); | 264 details->shipping_options = std::move(shipping_options); |
| 232 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 265 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 233 options->request_shipping = true; | 266 options->request_shipping = true; |
| 234 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details), | 267 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details), |
| 235 GetMethodDataForVisa()); | 268 GetMethodDataForVisa()); |
| 236 | 269 |
| 237 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 270 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 238 test_address(), test_address(), this); | 271 test_payment_request_delegate(), test_address(), |
| 272 test_address(), this); |
| 239 | 273 |
| 240 // Check that all the expected values were set. | 274 // Check that all the expected values were set. |
| 241 EXPECT_EQ("US", response()->shipping_address->country); | 275 EXPECT_EQ("US", response()->shipping_address->country); |
| 242 EXPECT_EQ("666 Erebus St.", response()->shipping_address->address_line[0]); | 276 EXPECT_EQ("666 Erebus St.", response()->shipping_address->address_line[0]); |
| 243 EXPECT_EQ("Apt 8", response()->shipping_address->address_line[1]); | 277 EXPECT_EQ("Apt 8", response()->shipping_address->address_line[1]); |
| 244 EXPECT_EQ("CA", response()->shipping_address->region); | 278 EXPECT_EQ("CA", response()->shipping_address->region); |
| 245 EXPECT_EQ("Elysium", response()->shipping_address->city); | 279 EXPECT_EQ("Elysium", response()->shipping_address->city); |
| 246 EXPECT_EQ("", response()->shipping_address->dependent_locality); | 280 EXPECT_EQ("", response()->shipping_address->dependent_locality); |
| 247 EXPECT_EQ("91111", response()->shipping_address->postal_code); | 281 EXPECT_EQ("91111", response()->shipping_address->postal_code); |
| 248 EXPECT_EQ("", response()->shipping_address->sorting_code); | 282 EXPECT_EQ("", response()->shipping_address->sorting_code); |
| 249 EXPECT_EQ("", response()->shipping_address->language_code); | 283 EXPECT_EQ("", response()->shipping_address->language_code); |
| 250 EXPECT_EQ("Underworld", response()->shipping_address->organization); | 284 EXPECT_EQ("Underworld", response()->shipping_address->organization); |
| 251 EXPECT_EQ("John H. Doe", response()->shipping_address->recipient); | 285 EXPECT_EQ("John H. Doe", response()->shipping_address->recipient); |
| 252 EXPECT_EQ("16502111111", response()->shipping_address->phone); | 286 EXPECT_EQ("16502111111", response()->shipping_address->phone); |
| 253 } | 287 } |
| 254 | 288 |
| 255 // Tests the the generated PaymentResponse has the correct values for the | 289 // Tests the the generated PaymentResponse has the correct values for the |
| 256 // contact details when all values are requested. | 290 // contact details when all values are requested. |
| 257 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_All) { | 291 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_All) { |
| 258 // Request all contact detail values. | 292 // Request all contact detail values. |
| 259 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 293 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 260 options->request_payer_name = true; | 294 options->request_payer_name = true; |
| 261 options->request_payer_phone = true; | 295 options->request_payer_phone = true; |
| 262 options->request_payer_email = true; | 296 options->request_payer_email = true; |
| 263 RecreateSpecWithOptions(std::move(options)); | 297 RecreateSpecWithOptions(std::move(options)); |
| 264 | 298 |
| 265 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 299 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 266 test_address(), test_address(), this); | 300 test_payment_request_delegate(), test_address(), |
| 301 test_address(), this); |
| 267 | 302 |
| 268 // Check that all the expected values were set. | 303 // Check that all the expected values were set. |
| 269 EXPECT_EQ("John H. Doe", response()->payer_name.value()); | 304 EXPECT_EQ("John H. Doe", response()->payer_name.value()); |
| 270 EXPECT_EQ("+16502111111", response()->payer_phone.value()); | 305 EXPECT_EQ("+16502111111", response()->payer_phone.value()); |
| 271 EXPECT_EQ("johndoe@hades.com", response()->payer_email.value()); | 306 EXPECT_EQ("johndoe@hades.com", response()->payer_email.value()); |
| 272 } | 307 } |
| 273 | 308 |
| 274 // Tests the the generated PaymentResponse has the correct values for the | 309 // Tests the the generated PaymentResponse has the correct values for the |
| 275 // contact details when all values are requested. | 310 // contact details when all values are requested. |
| 276 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_Some) { | 311 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_Some) { |
| 277 // Request one contact detail value. | 312 // Request one contact detail value. |
| 278 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 313 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 279 options->request_payer_name = true; | 314 options->request_payer_name = true; |
| 280 RecreateSpecWithOptions(std::move(options)); | 315 RecreateSpecWithOptions(std::move(options)); |
| 281 | 316 |
| 282 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 317 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 283 test_address(), test_address(), this); | 318 test_payment_request_delegate(), test_address(), |
| 319 test_address(), this); |
| 284 | 320 |
| 285 // Check that the name was set, but not the other values. | 321 // Check that the name was set, but not the other values. |
| 286 EXPECT_EQ("John H. Doe", response()->payer_name.value()); | 322 EXPECT_EQ("John H. Doe", response()->payer_name.value()); |
| 287 EXPECT_FALSE(response()->payer_phone.has_value()); | 323 EXPECT_FALSE(response()->payer_phone.has_value()); |
| 288 EXPECT_FALSE(response()->payer_email.has_value()); | 324 EXPECT_FALSE(response()->payer_email.has_value()); |
| 289 } | 325 } |
| 290 | 326 |
| 291 // Tests the the generated PaymentResponse has the correct values for the | 327 // Tests the the generated PaymentResponse has the correct values for the |
| 292 // contact details when all values are requested. | 328 // contact details when all values are requested. |
| 293 TEST_F(PaymentResponseHelperTest, | 329 TEST_F(PaymentResponseHelperTest, |
| 294 GeneratePaymentResponse_ContactPhoneIsFormatted) { | 330 GeneratePaymentResponse_ContactPhoneIsFormatted) { |
| 295 // Request one contact detail value. | 331 // Request one contact detail value. |
| 296 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); | 332 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 297 options->request_payer_phone = true; | 333 options->request_payer_phone = true; |
| 298 test_address()->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, | 334 test_address()->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER, |
| 299 base::UTF8ToUTF16("(515) 123-1234")); | 335 base::UTF8ToUTF16("(515) 123-1234")); |
| 300 RecreateSpecWithOptions(std::move(options)); | 336 RecreateSpecWithOptions(std::move(options)); |
| 301 | 337 |
| 302 PaymentResponseHelper helper("en-US", spec(), test_instrument(), | 338 PaymentResponseHelper helper("en-US", spec(), test_instrument(), |
| 303 test_address(), test_address(), this); | 339 test_payment_request_delegate(), test_address(), |
| 340 test_address(), this); |
| 304 | 341 |
| 305 // Check that the phone was formatted. | 342 // Check that the phone was formatted. |
| 306 EXPECT_EQ("+15151231234", response()->payer_phone.value()); | 343 EXPECT_EQ("+15151231234", response()->payer_phone.value()); |
| 307 } | 344 } |
| 308 | 345 |
| 309 } // namespace payments | 346 } // namespace payments |
| OLD | NEW |