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

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

Issue 2836353002: [Payments] Normalize shipping address change on Desktop (Closed)
Patch Set: Fixed BUILD deps Created 3 years, 7 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_spec.h" 15 #include "components/payments/content/payment_request_spec.h"
16 #include "components/payments/core/test_payment_request_delegate.h"
16 #include "components/payments/mojom/payment_request.mojom.h" 17 #include "components/payments/mojom/payment_request.mojom.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace payments { 20 namespace payments {
20 21
21 class PaymentRequestStateTest : public testing::Test, 22 class PaymentRequestStateTest : public testing::Test,
22 public PaymentRequestState::Observer, 23 public PaymentRequestState::Observer,
23 public PaymentRequestState::Delegate { 24 public PaymentRequestState::Delegate {
24 protected: 25 protected:
25 PaymentRequestStateTest() 26 PaymentRequestStateTest()
26 : num_on_selected_information_changed_called_(0), 27 : num_on_selected_information_changed_called_(0),
28 test_payment_request_delegate_(nullptr),
Mathieu 2017/04/27 11:04:01 /*personal_data_manager=*/
sebsg 2017/04/27 14:43:23 Done.
27 address_(autofill::test::GetFullProfile()), 29 address_(autofill::test::GetFullProfile()),
28 credit_card_visa_(autofill::test::GetCreditCard()) { 30 credit_card_visa_(autofill::test::GetCreditCard()) {
29 test_personal_data_manager_.AddTestingProfile(&address_); 31 test_personal_data_manager_.AddTestingProfile(&address_);
30 credit_card_visa_.set_billing_address_id(address_.guid()); 32 credit_card_visa_.set_billing_address_id(address_.guid());
31 credit_card_visa_.set_use_count(5u); 33 credit_card_visa_.set_use_count(5u);
32 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); 34 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_);
33 } 35 }
34 ~PaymentRequestStateTest() override {} 36 ~PaymentRequestStateTest() override {}
35 37
36 // PaymentRequestState::Observer: 38 // PaymentRequestState::Observer:
37 void OnSelectedInformationChanged() override { 39 void OnSelectedInformationChanged() override {
38 num_on_selected_information_changed_called_++; 40 num_on_selected_information_changed_called_++;
39 } 41 }
40 42
41 // PaymentRequestState::Delegate: 43 // PaymentRequestState::Delegate:
42 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { 44 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override {
43 payment_response_ = std::move(response); 45 payment_response_ = std::move(response);
44 }; 46 };
45 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} 47 void OnShippingOptionIdSelected(std::string shipping_option_id) override {}
46 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} 48 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {
49 selected_shipping_address_ = std::move(address);
50 }
47 51
48 void RecreateStateWithOptionsAndDetails( 52 void RecreateStateWithOptionsAndDetails(
49 mojom::PaymentOptionsPtr options, 53 mojom::PaymentOptionsPtr options,
50 mojom::PaymentDetailsPtr details, 54 mojom::PaymentDetailsPtr details,
51 std::vector<mojom::PaymentMethodDataPtr> method_data) { 55 std::vector<mojom::PaymentMethodDataPtr> method_data) {
52 // The spec will be based on the |options| and |details| passed in. 56 // The spec will be based on the |options| and |details| passed in.
53 spec_ = base::MakeUnique<PaymentRequestSpec>( 57 spec_ = base::MakeUnique<PaymentRequestSpec>(
54 std::move(options), std::move(details), std::move(method_data), nullptr, 58 std::move(options), std::move(details), std::move(method_data), nullptr,
55 "en-US"); 59 "en-US");
56 state_ = base::MakeUnique<PaymentRequestState>( 60 state_ = base::MakeUnique<PaymentRequestState>(
57 spec_.get(), this, "en-US", &test_personal_data_manager_, nullptr); 61 spec_.get(), this, "en-US", &test_personal_data_manager_,
62 &test_payment_request_delegate_);
58 state_->AddObserver(this); 63 state_->AddObserver(this);
59 } 64 }
60 65
61 // Convenience method to create a PaymentRequestState with default details 66 // Convenience method to create a PaymentRequestState with default details
62 // (one shipping option) and method data (only supports visa). 67 // (one shipping option) and method data (only supports visa).
63 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) { 68 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) {
64 // Create dummy PaymentDetails with a single shipping option. 69 RecreateStateWithOptionsAndDetails(
70 std::move(options), CreateDefaultDetails(), GetMethodDataForVisa());
71 }
72
73 // Convenience method that returns a dummy PaymentDetails with a single
74 // shipping option.
75 mojom::PaymentDetailsPtr CreateDefaultDetails() {
65 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; 76 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
66 mojom::PaymentShippingOptionPtr option = 77 mojom::PaymentShippingOptionPtr option =
67 mojom::PaymentShippingOption::New(); 78 mojom::PaymentShippingOption::New();
68 option->id = "option:1"; 79 option->id = "option:1";
69 shipping_options.push_back(std::move(option)); 80 shipping_options.push_back(std::move(option));
70 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New(); 81 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
71 details->shipping_options = std::move(shipping_options); 82 details->shipping_options = std::move(shipping_options);
72 83 return details;
73 RecreateStateWithOptionsAndDetails(std::move(options), std::move(details),
74 GetMethodDataForVisa());
75 } 84 }
76 85
77 // Convenience method that returns MethodData that supports Visa. 86 // Convenience method that returns MethodData that supports Visa.
78 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() { 87 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() {
79 std::vector<mojom::PaymentMethodDataPtr> method_data; 88 std::vector<mojom::PaymentMethodDataPtr> method_data;
80 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); 89 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
81 entry->supported_methods.push_back("visa"); 90 entry->supported_methods.push_back("visa");
82 method_data.push_back(std::move(entry)); 91 method_data.push_back(std::move(entry));
83 return method_data; 92 return method_data;
84 } 93 }
85 94
86 PaymentRequestState* state() { return state_.get(); } 95 PaymentRequestState* state() { return state_.get(); }
96 PaymentRequestSpec* spec() { return spec_.get(); }
87 const mojom::PaymentResponsePtr& response() { return payment_response_; } 97 const mojom::PaymentResponsePtr& response() { return payment_response_; }
98 const mojom::PaymentAddressPtr& selected_shipping_address() {
99 return selected_shipping_address_;
100 }
88 int num_on_selected_information_changed_called() { 101 int num_on_selected_information_changed_called() {
89 return num_on_selected_information_changed_called_; 102 return num_on_selected_information_changed_called_;
90 } 103 }
91 104
92 autofill::AutofillProfile* test_address() { return &address_; } 105 autofill::AutofillProfile* test_address() { return &address_; }
106 TestPaymentRequestDelegate* test_payment_request_delegate() {
107 return &test_payment_request_delegate_;
108 }
93 109
94 private: 110 private:
95 std::unique_ptr<PaymentRequestState> state_; 111 std::unique_ptr<PaymentRequestState> state_;
96 std::unique_ptr<PaymentRequestSpec> spec_; 112 std::unique_ptr<PaymentRequestSpec> spec_;
97 int num_on_selected_information_changed_called_; 113 int num_on_selected_information_changed_called_;
98 mojom::PaymentResponsePtr payment_response_; 114 mojom::PaymentResponsePtr payment_response_;
115 mojom::PaymentAddressPtr selected_shipping_address_;
99 autofill::TestPersonalDataManager test_personal_data_manager_; 116 autofill::TestPersonalDataManager test_personal_data_manager_;
117 TestPaymentRequestDelegate test_payment_request_delegate_;
100 118
101 // Test data. 119 // Test data.
102 autofill::AutofillProfile address_; 120 autofill::AutofillProfile address_;
103 autofill::CreditCard credit_card_visa_; 121 autofill::CreditCard credit_card_visa_;
104 }; 122 };
105 123
106 TEST_F(PaymentRequestStateTest, CanMakePayment) { 124 TEST_F(PaymentRequestStateTest, CanMakePayment) {
107 // Default options. 125 // Default options.
108 RecreateStateWithOptions(mojom::PaymentOptions::New()); 126 RecreateStateWithOptions(mojom::PaymentOptions::New());
109 127
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 options->request_payer_email = true; 217 options->request_payer_email = true;
200 RecreateStateWithOptions(std::move(options)); 218 RecreateStateWithOptions(std::move(options));
201 219
202 // Because there are shipping options, no address is selected by default. 220 // Because there are shipping options, no address is selected by default.
203 // Therefore we are not ready to pay. 221 // Therefore we are not ready to pay.
204 EXPECT_FALSE(state()->is_ready_to_pay()); 222 EXPECT_FALSE(state()->is_ready_to_pay());
205 223
206 state()->SetSelectedShippingProfile(test_address()); 224 state()->SetSelectedShippingProfile(test_address());
207 EXPECT_EQ(1, num_on_selected_information_changed_called()); 225 EXPECT_EQ(1, num_on_selected_information_changed_called());
208 226
227 // Simulate that the merchant has validated the shipping address change.
228 spec()->UpdateWith(CreateDefaultDetails());
229 EXPECT_EQ(2, num_on_selected_information_changed_called());
209 EXPECT_TRUE(state()->is_ready_to_pay()); 230 EXPECT_TRUE(state()->is_ready_to_pay());
210 } 231 }
211 232
212 // Testing that only supported intruments are shown. In this test the merchant 233 // Testing that only supported intruments are shown. In this test the merchant
213 // only supports Visa. 234 // only supports Visa.
214 TEST_F(PaymentRequestStateTest, UnsupportedCardAreNotAvailable) { 235 TEST_F(PaymentRequestStateTest, UnsupportedCardAreNotAvailable) {
215 // Default options. 236 // Default options.
216 RecreateStateWithOptions(mojom::PaymentOptions::New()); 237 RecreateStateWithOptions(mojom::PaymentOptions::New());
217 238
218 // Ready to pay because the default instrument is selected and supported. 239 // Ready to pay because the default instrument is selected and supported.
(...skipping 21 matching lines...) Expand all
240 261
241 EXPECT_FALSE(state()->is_ready_to_pay()); 262 EXPECT_FALSE(state()->is_ready_to_pay());
242 263
243 state()->SetSelectedContactProfile(test_address()); 264 state()->SetSelectedContactProfile(test_address());
244 EXPECT_EQ(2, num_on_selected_information_changed_called()); 265 EXPECT_EQ(2, num_on_selected_information_changed_called());
245 266
246 // Ready to pay! 267 // Ready to pay!
247 EXPECT_TRUE(state()->is_ready_to_pay()); 268 EXPECT_TRUE(state()->is_ready_to_pay());
248 } 269 }
249 270
271 TEST_F(PaymentRequestStateTest, SelectedShippingAddressMessage_Normalized) {
272 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
273 options->request_shipping = true;
274 RecreateStateWithOptions(std::move(options));
275
276 // Make the normalization not be instantaneous.
277 test_payment_request_delegate()
278 ->GetTestAddressNormalizer()
279 ->DelayNormalization();
280
281 EXPECT_EQ(0, num_on_selected_information_changed_called());
282
283 // Select an address, nothing should happen until the normalization is
284 // completed and the merchant has validated the address.
285 state()->SetSelectedShippingProfile(test_address());
286 EXPECT_EQ(1, num_on_selected_information_changed_called());
287 EXPECT_FALSE(state()->is_ready_to_pay());
288
289 // Complete the normalization.
290 test_payment_request_delegate()
291 ->GetTestAddressNormalizer()
292 ->CompleteAddressNormalization();
293 EXPECT_EQ(1, num_on_selected_information_changed_called());
294 EXPECT_FALSE(state()->is_ready_to_pay());
295
296 // Simulate that the merchant has validated the shipping address change.
297 spec()->UpdateWith(CreateDefaultDetails());
298 EXPECT_EQ(2, num_on_selected_information_changed_called());
299 EXPECT_TRUE(state()->is_ready_to_pay());
300
301 // Check that all the expected values were set for the shipping address.
302 EXPECT_EQ("US", selected_shipping_address()->country);
303 EXPECT_EQ("666 Erebus St.", selected_shipping_address()->address_line[0]);
304 EXPECT_EQ("Apt 8", selected_shipping_address()->address_line[1]);
305 EXPECT_EQ("CA", selected_shipping_address()->region);
306 EXPECT_EQ("Elysium", selected_shipping_address()->city);
307 EXPECT_EQ("", selected_shipping_address()->dependent_locality);
308 EXPECT_EQ("91111", selected_shipping_address()->postal_code);
309 EXPECT_EQ("", selected_shipping_address()->sorting_code);
310 EXPECT_EQ("", selected_shipping_address()->language_code);
311 EXPECT_EQ("Underworld", selected_shipping_address()->organization);
312 EXPECT_EQ("John H. Doe", selected_shipping_address()->recipient);
313 EXPECT_EQ("16502111111", selected_shipping_address()->phone);
314 }
315
250 } // namespace payments 316 } // 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