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