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

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

Issue 2836353002: [Payments] Normalize shipping address change on Desktop (Closed)
Patch Set: 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_spec.h" 15 #include "components/payments/content/payment_request_spec.h"
16 #include "components/payments/core/address_normalizer.h"
17 #include "components/payments/core/payment_request_delegate.h"
16 #include "components/payments/mojom/payment_request.mojom.h" 18 #include "components/payments/mojom/payment_request.mojom.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 namespace payments { 21 namespace payments {
20 22
23 namespace {
24
25 class FakeAddressNormalizer : public AddressNormalizer {
Mathieu 2017/04/26 13:09:49 nit: I prefer TestAddressNormalizer instead of Fak
sebsg 2017/04/26 19:28:54 Done.
26 public:
27 FakeAddressNormalizer() {}
28
29 void LoadRulesForRegion(const std::string& region_code) override {}
30
31 bool AreRulesLoadedForRegion(const std::string& region_code) override {
32 return true;
33 }
34
35 void StartAddressNormalization(
36 const autofill::AutofillProfile& profile,
37 const std::string& region_code,
38 int timeout_seconds,
39 AddressNormalizer::Delegate* requester) override {
40 if (immediate_normalization_) {
41 requester->OnAddressNormalized(profile_);
42 return;
43 }
44
45 // Setup the necessary variables for the delayed normalization.
46 profile_ = profile;
47 requester_ = requester;
48 }
49
50 void OnAddressValidationRulesLoaded(const std::string& region_code,
51 bool success) override {}
52
53 void DelayNormalization() { immediate_normalization_ = false; }
54
55 void CompleteAddressNormalization() {
56 requester_->OnAddressNormalized(profile_);
57 }
58
59 private:
60 autofill::AutofillProfile profile_;
61 AddressNormalizer::Delegate* requester_;
62
63 bool immediate_normalization_ = true;
64 };
65
66 class FakePaymentRequestDelegate : public PaymentRequestDelegate {
67 public:
68 FakePaymentRequestDelegate()
69 : locale_("en-US"), last_committed_url_("https://shop.com") {}
70 void ShowDialog(PaymentRequest* request) override {}
71
72 void CloseDialog() override {}
73
74 void ShowErrorMessage() override {}
75
76 autofill::PersonalDataManager* GetPersonalDataManager() override {
77 return nullptr;
78 }
79
80 const std::string& GetApplicationLocale() const override { return locale_; }
81
82 bool IsIncognito() const override { return false; }
83
84 bool IsSslCertificateValid() override { return true; }
85
86 const GURL& GetLastCommittedURL() const override {
87 return last_committed_url_;
88 }
89
90 void DoFullCardRequest(
91 const autofill::CreditCard& credit_card,
92 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate>
93 result_delegate) override {}
94
95 AddressNormalizer* GetAddressNormalizer() override {
96 return &address_normalizer_;
97 }
98
99 FakeAddressNormalizer* GetTestAddressNormalizer() {
100 return &address_normalizer_;
101 }
102
103 autofill::RegionDataLoader* GetRegionDataLoader() override { return nullptr; }
104
105 private:
106 std::string locale_;
107 const GURL last_committed_url_;
108 FakeAddressNormalizer address_normalizer_;
109
110 autofill::CreditCard full_card_request_card_;
111 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate>
112 full_card_result_delegate_;
113 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate);
114 };
115
116 } // namespace
117
21 class PaymentRequestStateTest : public testing::Test, 118 class PaymentRequestStateTest : public testing::Test,
22 public PaymentRequestState::Observer, 119 public PaymentRequestState::Observer,
23 public PaymentRequestState::Delegate { 120 public PaymentRequestState::Delegate {
24 protected: 121 protected:
25 PaymentRequestStateTest() 122 PaymentRequestStateTest()
26 : num_on_selected_information_changed_called_(0), 123 : num_on_selected_information_changed_called_(0),
27 address_(autofill::test::GetFullProfile()), 124 address_(autofill::test::GetFullProfile()),
28 credit_card_visa_(autofill::test::GetCreditCard()) { 125 credit_card_visa_(autofill::test::GetCreditCard()) {
29 test_personal_data_manager_.AddTestingProfile(&address_); 126 test_personal_data_manager_.AddTestingProfile(&address_);
30 credit_card_visa_.set_billing_address_id(address_.guid()); 127 credit_card_visa_.set_billing_address_id(address_.guid());
31 credit_card_visa_.set_use_count(5u); 128 credit_card_visa_.set_use_count(5u);
32 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_); 129 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_);
33 } 130 }
34 ~PaymentRequestStateTest() override {} 131 ~PaymentRequestStateTest() override {}
35 132
36 // PaymentRequestState::Observer: 133 // PaymentRequestState::Observer:
37 void OnSelectedInformationChanged() override { 134 void OnSelectedInformationChanged() override {
38 num_on_selected_information_changed_called_++; 135 num_on_selected_information_changed_called_++;
39 } 136 }
40 137
41 // PaymentRequestState::Delegate: 138 // PaymentRequestState::Delegate:
42 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override { 139 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override {
43 payment_response_ = std::move(response); 140 payment_response_ = std::move(response);
44 }; 141 };
45 void OnShippingOptionIdSelected(std::string shipping_option_id) override {} 142 void OnShippingOptionIdSelected(std::string shipping_option_id) override {}
46 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {} 143 void OnShippingAddressSelected(mojom::PaymentAddressPtr address) override {
144 selected_shipping_address_ = std::move(address);
145 }
47 146
48 void RecreateStateWithOptionsAndDetails( 147 void RecreateStateWithOptionsAndDetails(
49 mojom::PaymentOptionsPtr options, 148 mojom::PaymentOptionsPtr options,
50 mojom::PaymentDetailsPtr details, 149 mojom::PaymentDetailsPtr details,
51 std::vector<mojom::PaymentMethodDataPtr> method_data) { 150 std::vector<mojom::PaymentMethodDataPtr> method_data) {
52 // The spec will be based on the |options| and |details| passed in. 151 // The spec will be based on the |options| and |details| passed in.
53 spec_ = base::MakeUnique<PaymentRequestSpec>( 152 spec_ = base::MakeUnique<PaymentRequestSpec>(
54 std::move(options), std::move(details), std::move(method_data), nullptr, 153 std::move(options), std::move(details), std::move(method_data), nullptr,
55 "en-US"); 154 "en-US");
56 state_ = base::MakeUnique<PaymentRequestState>( 155 state_ = base::MakeUnique<PaymentRequestState>(
57 spec_.get(), this, "en-US", &test_personal_data_manager_, nullptr); 156 spec_.get(), this, "en-US", &test_personal_data_manager_,
157 &test_payment_request_delegate_);
58 state_->AddObserver(this); 158 state_->AddObserver(this);
59 } 159 }
60 160
61 // Convenience method to create a PaymentRequestState with default details 161 // Convenience method to create a PaymentRequestState with default details
62 // (one shipping option) and method data (only supports visa). 162 // (one shipping option) and method data (only supports visa).
63 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) { 163 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) {
64 // Create dummy PaymentDetails with a single shipping option. 164 // Create dummy PaymentDetails with a single shipping option.
65 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; 165 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
66 mojom::PaymentShippingOptionPtr option = 166 mojom::PaymentShippingOptionPtr option =
67 mojom::PaymentShippingOption::New(); 167 mojom::PaymentShippingOption::New();
(...skipping 10 matching lines...) Expand all
78 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() { 178 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() {
79 std::vector<mojom::PaymentMethodDataPtr> method_data; 179 std::vector<mojom::PaymentMethodDataPtr> method_data;
80 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New(); 180 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
81 entry->supported_methods.push_back("visa"); 181 entry->supported_methods.push_back("visa");
82 method_data.push_back(std::move(entry)); 182 method_data.push_back(std::move(entry));
83 return method_data; 183 return method_data;
84 } 184 }
85 185
86 PaymentRequestState* state() { return state_.get(); } 186 PaymentRequestState* state() { return state_.get(); }
87 const mojom::PaymentResponsePtr& response() { return payment_response_; } 187 const mojom::PaymentResponsePtr& response() { return payment_response_; }
188 const mojom::PaymentAddressPtr& selected_shipping_address() {
189 return selected_shipping_address_;
190 }
88 int num_on_selected_information_changed_called() { 191 int num_on_selected_information_changed_called() {
89 return num_on_selected_information_changed_called_; 192 return num_on_selected_information_changed_called_;
90 } 193 }
91 194
92 autofill::AutofillProfile* test_address() { return &address_; } 195 autofill::AutofillProfile* test_address() { return &address_; }
196 FakePaymentRequestDelegate* test_payment_request_delegate() {
197 return &test_payment_request_delegate_;
198 }
93 199
94 private: 200 private:
95 std::unique_ptr<PaymentRequestState> state_; 201 std::unique_ptr<PaymentRequestState> state_;
96 std::unique_ptr<PaymentRequestSpec> spec_; 202 std::unique_ptr<PaymentRequestSpec> spec_;
97 int num_on_selected_information_changed_called_; 203 int num_on_selected_information_changed_called_;
98 mojom::PaymentResponsePtr payment_response_; 204 mojom::PaymentResponsePtr payment_response_;
205 mojom::PaymentAddressPtr selected_shipping_address_;
99 autofill::TestPersonalDataManager test_personal_data_manager_; 206 autofill::TestPersonalDataManager test_personal_data_manager_;
207 FakePaymentRequestDelegate test_payment_request_delegate_;
100 208
101 // Test data. 209 // Test data.
102 autofill::AutofillProfile address_; 210 autofill::AutofillProfile address_;
103 autofill::CreditCard credit_card_visa_; 211 autofill::CreditCard credit_card_visa_;
104 }; 212 };
105 213
106 TEST_F(PaymentRequestStateTest, CanMakePayment) { 214 TEST_F(PaymentRequestStateTest, CanMakePayment) {
107 // Default options. 215 // Default options.
108 RecreateStateWithOptions(mojom::PaymentOptions::New()); 216 RecreateStateWithOptions(mojom::PaymentOptions::New());
109 217
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 348
241 EXPECT_FALSE(state()->is_ready_to_pay()); 349 EXPECT_FALSE(state()->is_ready_to_pay());
242 350
243 state()->SetSelectedContactProfile(test_address()); 351 state()->SetSelectedContactProfile(test_address());
244 EXPECT_EQ(2, num_on_selected_information_changed_called()); 352 EXPECT_EQ(2, num_on_selected_information_changed_called());
245 353
246 // Ready to pay! 354 // Ready to pay!
247 EXPECT_TRUE(state()->is_ready_to_pay()); 355 EXPECT_TRUE(state()->is_ready_to_pay());
248 } 356 }
249 357
358 TEST_F(PaymentRequestStateTest, SelectedShippingAddressMessage_Normalized) {
Mathieu 2017/04/26 13:09:49 can you test the is_ready_to_pay state as well
sebsg 2017/04/26 19:28:54 Done.
359 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
360 options->request_shipping = true;
361 RecreateStateWithOptions(std::move(options));
362
363 // Make the normalization not be instantaneous.
364 test_payment_request_delegate()
365 ->GetTestAddressNormalizer()
366 ->DelayNormalization();
367
368 // Select an address, nothing should happen until the normalization is
369 // completed.
370 state()->SetSelectedShippingProfile(test_address());
371 EXPECT_EQ(0, num_on_selected_information_changed_called());
372
373 // Complete the normalization.
374 test_payment_request_delegate()
375 ->GetTestAddressNormalizer()
376 ->CompleteAddressNormalization();
377 EXPECT_EQ(1, num_on_selected_information_changed_called());
378
379 // Check that all the expected values were set for the shipping address.
380 EXPECT_EQ("US", selected_shipping_address()->country);
381 EXPECT_EQ("666 Erebus St.", selected_shipping_address()->address_line[0]);
382 EXPECT_EQ("Apt 8", selected_shipping_address()->address_line[1]);
383 EXPECT_EQ("CA", selected_shipping_address()->region);
384 EXPECT_EQ("Elysium", selected_shipping_address()->city);
385 EXPECT_EQ("", selected_shipping_address()->dependent_locality);
386 EXPECT_EQ("91111", selected_shipping_address()->postal_code);
387 EXPECT_EQ("", selected_shipping_address()->sorting_code);
388 EXPECT_EQ("", selected_shipping_address()->language_code);
389 EXPECT_EQ("Underworld", selected_shipping_address()->organization);
390 EXPECT_EQ("John H. Doe", selected_shipping_address()->recipient);
391 EXPECT_EQ("16502111111", selected_shipping_address()->phone);
392 }
393
250 } // namespace payments 394 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698