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

Side by Side Diff: components/payments/content/payment_response_helper_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
« no previous file with comments | « components/payments/content/payment_response_helper.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_response_helper.h"
6
7 #include <utility>
8
9 #include "base/memory/weak_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_profile.h"
12 #include "components/autofill/core/browser/autofill_test_utils.h"
13 #include "components/autofill/core/browser/credit_card.h"
14 #include "components/autofill/core/browser/test_personal_data_manager.h"
15 #include "components/payments/content/payment_request.mojom.h"
16 #include "components/payments/content/payment_request_spec.h"
17 #include "components/payments/core/autofill_payment_instrument.h"
18 #include "components/payments/core/payment_request_delegate.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace payments {
22
23 class FakePaymentRequestDelegate : public PaymentRequestDelegate {
24 public:
25 FakePaymentRequestDelegate(
26 autofill::PersonalDataManager* personal_data_manager)
27 : personal_data_manager_(personal_data_manager), locale_("en-US") {}
28 void ShowDialog(PaymentRequest* request) override {}
29
30 void CloseDialog() override {}
31
32 void ShowErrorMessage() override {}
33
34 autofill::PersonalDataManager* GetPersonalDataManager() override {
35 return personal_data_manager_;
36 }
37
38 const std::string& GetApplicationLocale() const override { return locale_; }
39
40 bool IsIncognito() const override { return false; }
41
42 void DoFullCardRequest(
43 const autofill::CreditCard& credit_card,
44 base::WeakPtr<autofill::payments::FullCardRequest::ResultDelegate>
45 result_delegate) override {
46 result_delegate->OnFullCardRequestSucceeded(credit_card,
47 base::ASCIIToUTF16("123"));
48 }
49
50 private:
51 autofill::PersonalDataManager* personal_data_manager_;
52 std::string locale_;
53 DISALLOW_COPY_AND_ASSIGN(FakePaymentRequestDelegate);
54 };
55
56 class PaymentResponseHelperTest : public testing::Test,
57 public PaymentResponseHelper::Delegate {
58 protected:
59 PaymentResponseHelperTest()
60 : payment_request_delegate_(&test_personal_data_manager_),
61 address_(autofill::test::GetFullProfile()),
62 billing_addresses_({&address_}) {
63 test_personal_data_manager_.AddTestingProfile(&address_);
64
65 // Setup the autofill payment instrument.
66 autofill::CreditCard visa_card = autofill::test::GetCreditCard();
67 visa_card.set_billing_address_id(address_.guid());
68 visa_card.set_use_count(5u);
69 autofill_instrument_ = base::MakeUnique<AutofillPaymentInstrument>(
70 "visa", visa_card, billing_addresses_, "en-US",
71 &payment_request_delegate_);
72 }
73 ~PaymentResponseHelperTest() override {}
74
75 // PaymentRequestState::Delegate:
76 void OnPaymentResponseReady(mojom::PaymentResponsePtr response) override {
77 payment_response_ = std::move(response);
78 };
79
80 // Convenience method to create a PaymentRequestSpec with specified |details|
81 // and |method_data|.
82 void RecreateSpecWithOptionsAndDetails(
83 mojom::PaymentOptionsPtr options,
84 mojom::PaymentDetailsPtr details,
85 std::vector<mojom::PaymentMethodDataPtr> method_data) {
86 // The spec will be based on the |options| and |details| passed in.
87 spec_ = base::MakeUnique<PaymentRequestSpec>(
88 std::move(options), std::move(details), std::move(method_data), nullptr,
89 "en-US");
90 }
91
92 // Convenience method to create a PaymentRequestSpec with default details
93 // (one shipping option) and method data (only supports visa).
94 void RecreateSpecWithOptions(mojom::PaymentOptionsPtr options) {
95 // Create dummy PaymentDetails with a single shipping option.
96 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
97 mojom::PaymentShippingOptionPtr option =
98 mojom::PaymentShippingOption::New();
99 option->id = "option:1";
100 shipping_options.push_back(std::move(option));
101 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
102 details->shipping_options = std::move(shipping_options);
103
104 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details),
105 GetMethodDataForVisa());
106 }
107
108 // Convenience method that returns MethodData that supports Visa.
109 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() {
110 std::vector<mojom::PaymentMethodDataPtr> method_data;
111 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
112 entry->supported_methods.push_back("visa");
113 method_data.push_back(std::move(entry));
114 return method_data;
115 }
116
117 PaymentRequestSpec* spec() { return spec_.get(); }
118 const mojom::PaymentResponsePtr& response() { return payment_response_; }
119 autofill::AutofillProfile* test_address() { return &address_; }
120 PaymentInstrument* test_instrument() { return autofill_instrument_.get(); }
121
122 private:
123 std::unique_ptr<PaymentRequestSpec> spec_;
124 mojom::PaymentResponsePtr payment_response_;
125 autofill::TestPersonalDataManager test_personal_data_manager_;
126 FakePaymentRequestDelegate payment_request_delegate_;
127
128 // Test data.
129 autofill::AutofillProfile address_;
130 const std::vector<autofill::AutofillProfile*> billing_addresses_;
131 std::unique_ptr<AutofillPaymentInstrument> autofill_instrument_;
132 };
133
134 // Test generating a PaymentResponse.
135 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_SupportedMethod) {
136 // Default options (no shipping, no contact info).
137 RecreateSpecWithOptions(mojom::PaymentOptions::New());
138
139 // TODO(mathp): Currently synchronous, when async will need a RunLoop.
140 // "visa" is specified directly in the supportedMethods so it is returned
141 // as the method name.
142 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
143 test_address(), test_address(), this);
144 EXPECT_EQ("visa", response()->method_name);
145 EXPECT_EQ(
146 "{\"billingAddress\":"
147 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
148 "\"city\":\"Elysium\","
149 "\"country\":\"US\","
150 "\"organization\":\"Underworld\","
151 "\"phone\":\"16502111111\","
152 "\"postalCode\":\"91111\","
153 "\"recipient\":\"John H. Doe\","
154 "\"region\":\"CA\"},"
155 "\"cardNumber\":\"4111111111111111\","
156 "\"cardSecurityCode\":\"123\","
157 "\"cardholderName\":\"Test User\","
158 "\"expiryMonth\":\"11\","
159 "\"expiryYear\":\"2022\"}",
160 response()->stringified_details);
161 }
162
163 // Test generating a PaymentResponse when the method is specified through
164 // "basic-card".
165 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_BasicCard) {
166 // The method data supports visa through basic-card.
167 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
168 entry->supported_methods.push_back("basic-card");
169 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA);
170 std::vector<mojom::PaymentMethodDataPtr> method_data;
171 method_data.push_back(std::move(entry));
172 RecreateSpecWithOptionsAndDetails(mojom::PaymentOptions::New(),
173 mojom::PaymentDetails::New(),
174 std::move(method_data));
175
176 // TODO(mathp): Currently synchronous, when async will need a RunLoop.
177 // "basic-card" is specified so it is returned as the method name.
178 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
179 test_address(), test_address(), this);
180 EXPECT_EQ("basic-card", response()->method_name);
181 EXPECT_EQ(
182 "{\"billingAddress\":"
183 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
184 "\"city\":\"Elysium\","
185 "\"country\":\"US\","
186 "\"organization\":\"Underworld\","
187 "\"phone\":\"16502111111\","
188 "\"postalCode\":\"91111\","
189 "\"recipient\":\"John H. Doe\","
190 "\"region\":\"CA\"},"
191 "\"cardNumber\":\"4111111111111111\","
192 "\"cardSecurityCode\":\"123\","
193 "\"cardholderName\":\"Test User\","
194 "\"expiryMonth\":\"11\","
195 "\"expiryYear\":\"2022\"}",
196 response()->stringified_details);
197 }
198
199 // Tests the the generated PaymentResponse has the correct values for the
200 // shipping address.
201 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ShippingAddress) {
202 // Setup so that a shipping address is requested.
203 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
204 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New();
205 option->id = "option:1";
206 option->selected = true;
207 shipping_options.push_back(std::move(option));
208 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
209 details->shipping_options = std::move(shipping_options);
210 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
211 options->request_shipping = true;
212 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details),
213 GetMethodDataForVisa());
214
215 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
216 test_address(), test_address(), this);
217
218 // Check that all the expected values were set.
219 EXPECT_EQ("US", response()->shipping_address->country);
220 EXPECT_EQ("666 Erebus St.", response()->shipping_address->address_line[0]);
221 EXPECT_EQ("Apt 8", response()->shipping_address->address_line[1]);
222 EXPECT_EQ("CA", response()->shipping_address->region);
223 EXPECT_EQ("Elysium", response()->shipping_address->city);
224 EXPECT_EQ("", response()->shipping_address->dependent_locality);
225 EXPECT_EQ("91111", response()->shipping_address->postal_code);
226 EXPECT_EQ("", response()->shipping_address->sorting_code);
227 EXPECT_EQ("", response()->shipping_address->language_code);
228 EXPECT_EQ("Underworld", response()->shipping_address->organization);
229 EXPECT_EQ("John H. Doe", response()->shipping_address->recipient);
230 EXPECT_EQ("16502111111", response()->shipping_address->phone);
231 }
232
233 // Tests the the generated PaymentResponse has the correct values for the
234 // contact details when all values are requested.
235 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_All) {
236 // Request all contact detail values.
237 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
238 options->request_payer_name = true;
239 options->request_payer_phone = true;
240 options->request_payer_email = true;
241 RecreateSpecWithOptions(std::move(options));
242
243 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
244 test_address(), test_address(), this);
245
246 // Check that all the expected values were set.
247 EXPECT_EQ("John H. Doe", response()->payer_name.value());
248 EXPECT_EQ("16502111111", response()->payer_phone.value());
249 EXPECT_EQ("johndoe@hades.com", response()->payer_email.value());
250 }
251
252 // Tests the the generated PaymentResponse has the correct values for the
253 // contact details when all values are requested.
254 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_Some) {
255 // Request one contact detail value.
256 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
257 options->request_payer_name = true;
258 RecreateSpecWithOptions(std::move(options));
259
260 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
261 test_address(), test_address(), this);
262
263 // Check that the name was set, but not the other values.
264 EXPECT_EQ("John H. Doe", response()->payer_name.value());
265 EXPECT_FALSE(response()->payer_phone.has_value());
266 EXPECT_FALSE(response()->payer_email.has_value());
267 }
268
269 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_response_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698