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