Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: components/payments/content/payment_request_state_unittest.cc

Issue 2808633002: [Payments] Move PaymentResponse logic to PaymentResponseHelper. (Closed)
Patch Set: Nit Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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()) {
65 credit_card_amex_(autofill::test::GetCreditCard2()) {
66 test_personal_data_manager_.AddTestingProfile(&address_); 29 test_personal_data_manager_.AddTestingProfile(&address_);
67 credit_card_visa_.set_billing_address_id(address_.guid()); 30 credit_card_visa_.set_billing_address_id(address_.guid());
68 credit_card_visa_.set_use_count(5u); 31 credit_card_visa_.set_use_count(5u);
69 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); 32 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_);
70 credit_card_amex_.set_billing_address_id(address_.guid());
71 credit_card_amex_.set_use_count(1u);
72 test_personal_data_manager_.AddTestingCreditCard(&credit_card_amex_);
73 // Add an expired JCB card here.
74 credit_card_jcb_ = autofill::test::GetCreditCard();
75 credit_card_jcb_.SetNumber(base::ASCIIToUTF16("3530111333300000"));
76 credit_card_jcb_.set_billing_address_id(address_.guid());
77 credit_card_jcb_.set_use_count(1u);
78 credit_card_jcb_.SetExpirationDateFromString(base::ASCIIToUTF16("01/17"));
79 } 33 }
80 ~PaymentRequestStateTest() override {} 34 ~PaymentRequestStateTest() override {}
81 35
82 // PaymentRequestState::Observer: 36 // PaymentRequestState::Observer:
83 void OnSelectedInformationChanged() override { 37 void OnSelectedInformationChanged() override {
84 num_on_selected_information_changed_called_++; 38 num_on_selected_information_changed_called_++;
85 } 39 }
86 40
87 // PaymentRequestState::Delegate: 41 // PaymentRequestState::Delegate:
88 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { 42 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override {
89 payment_response_ = std::move(response); 43 payment_response_ = std::move(response);
90 }; 44 };
91 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} 45 void OnShippingOptionIdSelected(std::string shipping_option_id) override {}
92 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} 46 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {}
93 47
94 void RecreateStateWithOptionsAndDetails( 48 void RecreateStateWithOptionsAndDetails(
95 mojom::PaymentOptionsPtr options, 49 mojom::PaymentOptionsPtr options,
96 mojom::PaymentDetailsPtr details, 50 mojom::PaymentDetailsPtr details,
97 std::vector<mojom::PaymentMethodDataPtr> method_data) { 51 std::vector<mojom::PaymentMethodDataPtr> method_data) {
98 // The spec will be based on the |options| and |details| passed in. 52 // The spec will be based on the |options| and |details| passed in.
99 spec_ = base::MakeUnique<PaymentRequestSpec>( 53 spec_ = base::MakeUnique<PaymentRequestSpec>(
100 std::move(options), std::move(details), std::move(method_data), nullptr, 54 std::move(options), std::move(details), std::move(method_data), nullptr,
101 "en-US"); 55 "en-US");
102 state_ = base::MakeUnique<PaymentRequestState>( 56 state_ = base::MakeUnique<PaymentRequestState>(
103 spec_.get(), this, "en-US", &test_personal_data_manager_, 57 spec_.get(), this, "en-US", &test_personal_data_manager_, nullptr);
104 payment_request_delegate_.get());
105 state_->AddObserver(this); 58 state_->AddObserver(this);
106 } 59 }
107 60
108 // Convenience method to create a PaymentRequestState with default details 61 // Convenience method to create a PaymentRequestState with default details
109 // (one shipping option) and method data (only supports visa). 62 // (one shipping option) and method data (only supports visa).
110 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) { 63 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) {
111 // Create dummy PaymentDetails with a single shipping option. 64 // Create dummy PaymentDetails with a single shipping option.
112 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; 65 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
113 mojom::PaymentShippingOptionPtr option = 66 mojom::PaymentShippingOptionPtr option =
114 mojom::PaymentShippingOption::New(); 67 mojom::PaymentShippingOption::New();
(...skipping 22 matching lines...) Expand all
137 } 90 }
138 91
139 autofill::AutofillProfile* test_address() { return &address_; } 92 autofill::AutofillProfile* test_address() { return &address_; }
140 93
141 private: 94 private:
142 std::unique_ptr<PaymentRequestState> state_; 95 std::unique_ptr<PaymentRequestState> state_;
143 std::unique_ptr<PaymentRequestSpec> spec_; 96 std::unique_ptr<PaymentRequestSpec> spec_;
144 int num_on_selected_information_changed_called_; 97 int num_on_selected_information_changed_called_;
145 mojom::PaymentResponsePtr payment_response_; 98 mojom::PaymentResponsePtr payment_response_;
146 autofill::TestPersonalDataManager test_personal_data_manager_; 99 autofill::TestPersonalDataManager test_personal_data_manager_;
147 std::unique_ptr<FakePaymentRequestDelegate> payment_request_delegate_;
148 100
149 // Test data. 101 // Test data.
150 autofill::AutofillProfile address_; 102 autofill::AutofillProfile address_;
151 autofill::CreditCard credit_card_visa_; 103 autofill::CreditCard credit_card_visa_;
152 autofill::CreditCard credit_card_amex_;
153 autofill::CreditCard credit_card_jcb_;
154 }; 104 };
155 105
156 TEST_F(PaymentRequestStateTest, CanMakePayment) { 106 TEST_F(PaymentRequestStateTest, CanMakePayment) {
157 // Default options. 107 // Default options.
158 RecreateStateWithOptions(mojom::PaymentOptions::New()); 108 RecreateStateWithOptions(mojom::PaymentOptions::New());
159 109
160 // CanMakePayment returns true because the method data requires Visa, and the 110 // CanMakePayment returns true because the method data requires Visa, and the
161 // user has a Visa card on file. 111 // user has a Visa card on file.
162 EXPECT_TRUE(state()->CanMakePayment()); 112 EXPECT_TRUE(state()->CanMakePayment());
163 } 113 }
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 240
291 EXPECT_FALSE(state()->is_ready_to_pay()); 241 EXPECT_FALSE(state()->is_ready_to_pay());
292 242
293 state()->SetSelectedContactProfile(test_address()); 243 state()->SetSelectedContactProfile(test_address());
294 EXPECT_EQ(2, num_on_selected_information_changed_called()); 244 EXPECT_EQ(2, num_on_selected_information_changed_called());
295 245
296 // Ready to pay! 246 // Ready to pay!
297 EXPECT_TRUE(state()->is_ready_to_pay()); 247 EXPECT_TRUE(state()->is_ready_to_pay());
298 } 248 }
299 249
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\":\"2022\"}",
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\":\"2022\"}",
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 250 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_request_state.cc ('k') | components/payments/content/payment_response_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698