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

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

Issue 2742813004: [Payments] Refactor into PaymentRequestState and Spec (Closed)
Patch Set: don't stop rebasin' Created 3 years, 9 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
« no previous file with comments | « components/payments/content/payment_request_state.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/payments/content/payment_request_state.h"
6
7 #include <utility>
8
9 #include "base/memory/weak_ptr.h"
10 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/autofill_test_utils.h"
12 #include "components/autofill/core/browser/credit_card.h"
13 #include "components/autofill/core/browser/test_personal_data_manager.h"
14 #include "components/payments/content/payment_request.mojom.h"
15 #include "components/payments/content/payment_request_spec.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace payments {
19
20 class PaymentRequestStateTest : public testing::Test,
21 public PaymentRequestState::Observer,
22 public PaymentRequestState::Delegate {
23 protected:
24 PaymentRequestStateTest()
25 : num_on_selected_information_changed_called_(0),
26 locale_("en-US"),
27 address_(autofill::test::GetFullProfile()),
28 credit_card_(autofill::test::GetCreditCard()) {
29 test_personal_data_manager_.AddTestingProfile(&address_);
30 credit_card_.set_billing_address_id(address_.guid());
31 test_personal_data_manager_.AddTestingCreditCard(&credit_card_);
32 }
33 ~PaymentRequestStateTest() override {}
34
35 // PaymentRequestState::Observer:
36 void OnSelectedInformationChanged() override {
37 num_on_selected_information_changed_called_++;
38 }
39
40 // PaymentRequestState::Delegate:
41 const std::string& GetApplicationLocale() override { return locale_; };
42 autofill::PersonalDataManager* GetPersonalDataManager() override {
43 return &test_personal_data_manager_;
44 }
45 void OnPaymentResponseAvailable(mojom::PaymentResponsePtr response) override {
46 payment_response_ = std::move(response);
47 };
48
49 void RecreateStateWithOptionsAndDetails(
50 mojom::PaymentOptionsPtr options,
51 mojom::PaymentDetailsPtr details,
52 std::vector<mojom::PaymentMethodDataPtr> method_data) {
53 // The spec will be based on the |options| and |details| passed in.
54 spec_ = base::MakeUnique<PaymentRequestSpec>(
55 std::move(options), std::move(details), std::move(method_data),
56 nullptr);
57 state_ = base::MakeUnique<PaymentRequestState>(spec_.get(), this);
58 state_->AddObserver(this);
59 }
60
61 // Convenience method to create a PaymentRequestState with default details
62 // (one shipping option) and method data (only supports visa).
63 void RecreateStateWithOptions(mojom::PaymentOptionsPtr options) {
64 // Create dummy PaymentDetails with a single shipping option.
65 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
66 mojom::PaymentShippingOptionPtr option =
67 mojom::PaymentShippingOption::New();
68 option->id = "option:1";
69 shipping_options.push_back(std::move(option));
70 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
71 details->shipping_options = std::move(shipping_options);
72
73 RecreateStateWithOptionsAndDetails(std::move(options), std::move(details),
74 GetMethodDataForVisa());
75 }
76
77 // Convenience method that returns MethodData that supports Visa.
78 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() {
79 std::vector<mojom::PaymentMethodDataPtr> method_data;
80 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
81 entry->supported_methods.push_back("visa");
82 method_data.push_back(std::move(entry));
83 return method_data;
84 }
85
86 PaymentRequestState* state() { return state_.get(); }
87 const mojom::PaymentResponsePtr& response() { return payment_response_; }
88 int num_on_selected_information_changed_called() {
89 return num_on_selected_information_changed_called_;
90 }
91
92 autofill::AutofillProfile* test_address() { return &address_; }
93 autofill::CreditCard* test_credit_card() { return &credit_card_; }
94
95 private:
96 std::unique_ptr<PaymentRequestState> state_;
97 std::unique_ptr<PaymentRequestSpec> spec_;
98 int num_on_selected_information_changed_called_;
99 std::string locale_;
100 mojom::PaymentResponsePtr payment_response_;
101 autofill::TestPersonalDataManager test_personal_data_manager_;
102
103 // Test data.
104 autofill::AutofillProfile address_;
105 autofill::CreditCard credit_card_;
106 };
107
108 // Test that the last shipping option is selected.
109 TEST_F(PaymentRequestStateTest, ShippingOptionsSelection) {
110 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
111 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New();
112 option->id = "option:1";
113 option->selected = false;
114 shipping_options.push_back(std::move(option));
115 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New();
116 option2->id = "option:2";
117 option2->selected = true;
118 shipping_options.push_back(std::move(option2));
119 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
120 details->shipping_options = std::move(shipping_options);
121
122 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
123 options->request_shipping = true;
124 RecreateStateWithOptionsAndDetails(std::move(options), std::move(details),
125 GetMethodDataForVisa());
126
127 EXPECT_EQ("option:2", state()->selected_shipping_option()->id);
128 }
129
130 TEST_F(PaymentRequestStateTest, ReadyToPay_DefaultSelections) {
131 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
132 options->request_shipping = true;
133 options->request_payer_name = true;
134 options->request_payer_phone = true;
135 options->request_payer_email = true;
136 RecreateStateWithOptions(std::move(options));
137
138 EXPECT_TRUE(state()->is_ready_to_pay());
139 }
140
141 // Testing that the card is supported when determining "is ready to pay". In
142 // this test the merchant only supports Visa.
143 TEST_F(PaymentRequestStateTest, ReadyToPay_SelectUnsupportedCard) {
144 // Default options.
145 RecreateStateWithOptions(mojom::PaymentOptions::New());
146
147 // Ready to pay because the default card is selected and supported.
148 EXPECT_TRUE(state()->is_ready_to_pay());
149
150 autofill::CreditCard amex_card = autofill::test::GetCreditCard2(); // Amex.
151 state()->SetSelectedCreditCard(&amex_card);
152 EXPECT_EQ(1, num_on_selected_information_changed_called());
153
154 // Not ready to pay because the card is not supported.
155 EXPECT_FALSE(state()->is_ready_to_pay());
156
157 // Go back to the Visa card.
158 state()->SetSelectedCreditCard(test_credit_card()); // Visa card.
159 EXPECT_EQ(2, num_on_selected_information_changed_called());
160
161 // Visa card is supported by the merchant.
162 EXPECT_TRUE(state()->is_ready_to_pay());
163 }
164
165 // Test selecting a contact info profile will make the user ready to pay.
166 TEST_F(PaymentRequestStateTest, ReadyToPay_ContactInfo) {
167 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
168 options->request_payer_name = true;
169 options->request_payer_phone = true;
170 options->request_payer_email = true;
171 RecreateStateWithOptions(std::move(options));
172
173 // Ready to pay because of default selections.
174 EXPECT_TRUE(state()->is_ready_to_pay());
175
176 // Unselecting contact profile.
177 state()->SetSelectedContactProfile(nullptr);
178 EXPECT_EQ(1, num_on_selected_information_changed_called());
179
180 EXPECT_FALSE(state()->is_ready_to_pay());
181
182 state()->SetSelectedContactProfile(test_address());
183 EXPECT_EQ(2, num_on_selected_information_changed_called());
184
185 // Ready to pay!
186 EXPECT_TRUE(state()->is_ready_to_pay());
187 }
188
189 // Test generating a PaymentResponse.
190 TEST_F(PaymentRequestStateTest, GeneratePaymentResponse) {
191 // Default options (no shipping, no contact info).
192 RecreateStateWithOptions(mojom::PaymentOptions::New());
193 state()->SetSelectedCreditCard(test_credit_card());
194 EXPECT_EQ(1, num_on_selected_information_changed_called());
195 EXPECT_TRUE(state()->is_ready_to_pay());
196
197 // TODO(mathp): Currently synchronous, when async will need a RunLoop.
198 state()->GeneratePaymentResponse();
199 EXPECT_EQ("basic-card", response()->method_name);
200 EXPECT_EQ(
201 "{\"billingAddress\":"
202 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
203 "\"city\":\"Elysium\","
204 "\"country\":\"US\","
205 "\"organization\":\"Underworld\","
206 "\"phone\":\"16502111111\","
207 "\"postalCode\":\"91111\","
208 "\"recipient\":\"John H. Doe\","
209 "\"region\":\"CA\"},"
210 "\"cardNumber\":\"4111111111111111\","
211 "\"cardSecurityCode\":\"123\","
212 "\"cardholderName\":\"Test User\","
213 "\"expiryMonth\":\"11\","
214 "\"expiryYear\":\"2017\"}",
215 response()->stringified_details);
216 }
217
218 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_request_state.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698