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

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

Issue 2808633002: [Payments] Move PaymentResponse logic to PaymentResponseHelper. (Closed)
Patch Set: Moved tests 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
(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_(
61 new FakePaymentRequestDelegate(&test_personal_data_manager_)),
62 address_(autofill::test::GetFullProfile()),
63 billing_addresses_({&address_}),
64 credit_card_visa_(autofill::test::GetCreditCard()) {
65 test_personal_data_manager_.AddTestingProfile(&address_);
66 credit_card_visa_.set_billing_address_id(address_.guid());
67 credit_card_visa_.set_use_count(5u);
68 test_personal_data_manager_.AddTestingCreditCard(&credit_card_visa_);
69 autofill_instrument_ = base::MakeUnique<AutofillPaymentInstrument>(
70 "visa", credit_card_visa_, billing_addresses_, "en-US",
Mathieu 2017/04/11 14:35:24 since the instrument takes a copy, I'm not sure cr
sebsg 2017/04/11 14:54:41 Done.
71 payment_request_delegate_.get());
72 }
73 ~PaymentResponseHelperTest() override {}
74
75 // PaymentRequestState::Delegate:
76 void OnPaymentResponseReady(mojom::PaymentResponsePtr response) override {
77 payment_response_ = std::move(response);
78 };
79
80 void RecreateSpecWithOptionsAndDetails(
81 mojom::PaymentOptionsPtr options,
82 mojom::PaymentDetailsPtr details,
83 std::vector<mojom::PaymentMethodDataPtr> method_data) {
84 // The spec will be based on the |options| and |details| passed in.
85 spec_ = base::MakeUnique<PaymentRequestSpec>(
86 std::move(options), std::move(details), std::move(method_data), nullptr,
87 "en-US");
88 }
89
90 // Convenience method to create a PaymentRequestState with default details
Mathieu 2017/04/11 14:35:24 Spec?
sebsg 2017/04/11 14:54:41 Done.
91 // (one shipping option) and method data (only supports visa).
92 void RecreateSpecWithOptions(mojom::PaymentOptionsPtr options) {
93 // Create dummy PaymentDetails with a single shipping option.
94 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
95 mojom::PaymentShippingOptionPtr option =
96 mojom::PaymentShippingOption::New();
97 option->id = "option:1";
98 shipping_options.push_back(std::move(option));
99 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
100 details->shipping_options = std::move(shipping_options);
101
102 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details),
103 GetMethodDataForVisa());
104 }
105
106 // Convenience method that returns MethodData that supports Visa.
107 std::vector<mojom::PaymentMethodDataPtr> GetMethodDataForVisa() {
108 std::vector<mojom::PaymentMethodDataPtr> method_data;
109 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
110 entry->supported_methods.push_back("visa");
111 method_data.push_back(std::move(entry));
112 return method_data;
113 }
114
115 PaymentRequestSpec* spec() { return spec_.get(); }
116 const mojom::PaymentResponsePtr& response() { return payment_response_; }
117 autofill::AutofillProfile* test_address() { return &address_; }
118 PaymentInstrument* test_instrument() { return autofill_instrument_.get(); }
119
120 private:
121 std::unique_ptr<PaymentRequestSpec> spec_;
122 mojom::PaymentResponsePtr payment_response_;
123 autofill::TestPersonalDataManager test_personal_data_manager_;
124 std::unique_ptr<FakePaymentRequestDelegate> payment_request_delegate_;
Mathieu 2017/04/11 14:35:24 this can be a regular member
sebsg 2017/04/11 14:54:41 Done.
125
126 // Test data.
127 autofill::AutofillProfile address_;
128 const std::vector<autofill::AutofillProfile*> billing_addresses_;
129 autofill::CreditCard credit_card_visa_;
130 std::unique_ptr<AutofillPaymentInstrument> autofill_instrument_;
131 };
132
133 // Test generating a PaymentResponse.
134 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_SupportedMethod) {
135 // Default options (no shipping, no contact info).
136 RecreateSpecWithOptions(mojom::PaymentOptions::New());
137
138 // TODO(mathp): Currently synchronous, when async will need a RunLoop.
139 // "visa" is specified directly in the supportedMethods so it is returned
140 // as the method name.
141 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
142 test_address(), test_address(), this);
143 EXPECT_EQ("visa", response()->method_name);
144 EXPECT_EQ(
145 "{\"billingAddress\":"
146 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
147 "\"city\":\"Elysium\","
148 "\"country\":\"US\","
149 "\"organization\":\"Underworld\","
150 "\"phone\":\"16502111111\","
151 "\"postalCode\":\"91111\","
152 "\"recipient\":\"John H. Doe\","
153 "\"region\":\"CA\"},"
154 "\"cardNumber\":\"4111111111111111\","
155 "\"cardSecurityCode\":\"123\","
156 "\"cardholderName\":\"Test User\","
157 "\"expiryMonth\":\"11\","
158 "\"expiryYear\":\"2017\"}",
159 response()->stringified_details);
160 }
161
162 // Test generating a PaymentResponse when the method is specified through
163 // "basic-card".
164 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_BasicCard) {
165 // The method data supports visa through basic-card.
166 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
167 entry->supported_methods.push_back("basic-card");
168 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA);
169 std::vector<mojom::PaymentMethodDataPtr> method_data;
170 method_data.push_back(std::move(entry));
171 RecreateSpecWithOptionsAndDetails(mojom::PaymentOptions::New(),
172 mojom::PaymentDetails::New(),
173 std::move(method_data));
174
175 // TODO(mathp): Currently synchronous, when async will need a RunLoop.
176 // "basic-card" is specified so it is returned as the method name.
177 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
178 test_address(), test_address(), this);
179 EXPECT_EQ("basic-card", response()->method_name);
180 EXPECT_EQ(
181 "{\"billingAddress\":"
182 "{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
183 "\"city\":\"Elysium\","
184 "\"country\":\"US\","
185 "\"organization\":\"Underworld\","
186 "\"phone\":\"16502111111\","
187 "\"postalCode\":\"91111\","
188 "\"recipient\":\"John H. Doe\","
189 "\"region\":\"CA\"},"
190 "\"cardNumber\":\"4111111111111111\","
191 "\"cardSecurityCode\":\"123\","
192 "\"cardholderName\":\"Test User\","
193 "\"expiryMonth\":\"11\","
194 "\"expiryYear\":\"2017\"}",
195 response()->stringified_details);
196 }
197
198 // Tests the the generated PaymentResponse has the correct values for the
199 // shipping address.
200 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ShippingAddress) {
201 // Setup so that a shipping address is requested.
202 std::vector<mojom::PaymentShippingOptionPtr> shipping_options;
203 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New();
204 option->id = "option:1";
205 option->selected = true;
206 shipping_options.push_back(std::move(option));
207 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New();
208 details->shipping_options = std::move(shipping_options);
209 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
210 options->request_shipping = true;
211 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details),
212 GetMethodDataForVisa());
213
214 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
215 test_address(), test_address(), this);
216
217 // Check that all the expected values were set.
218 EXPECT_EQ("US", response()->shipping_address->country);
219 EXPECT_EQ("666 Erebus St.", response()->shipping_address->address_line[0]);
220 EXPECT_EQ("Apt 8", response()->shipping_address->address_line[1]);
221 EXPECT_EQ("CA", response()->shipping_address->region);
222 EXPECT_EQ("Elysium", response()->shipping_address->city);
223 EXPECT_EQ("", response()->shipping_address->dependent_locality);
224 EXPECT_EQ("91111", response()->shipping_address->postal_code);
225 EXPECT_EQ("", response()->shipping_address->sorting_code);
226 EXPECT_EQ("", response()->shipping_address->language_code);
227 EXPECT_EQ("Underworld", response()->shipping_address->organization);
228 EXPECT_EQ("John H. Doe", response()->shipping_address->recipient);
229 EXPECT_EQ("16502111111", response()->shipping_address->phone);
230 }
231
232 // Tests the the generated PaymentResponse has the correct values for the
233 // contact details when all values are requested.
234 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_All) {
235 // Request all contact detail values.
236 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
237 options->request_payer_name = true;
238 options->request_payer_phone = true;
239 options->request_payer_email = true;
240 RecreateSpecWithOptions(std::move(options));
241
242 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
243 test_address(), test_address(), this);
244
245 // Check that all the expected values were set.
246 EXPECT_EQ("John H. Doe", response()->payer_name.value());
247 EXPECT_EQ("16502111111", response()->payer_phone.value());
248 EXPECT_EQ("johndoe@hades.com", response()->payer_email.value());
249 }
250
251 // Tests the the generated PaymentResponse has the correct values for the
252 // contact details when all values are requested.
253 TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_ContactDetails_Some) {
254 // Request one contact detail value.
255 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New();
256 options->request_payer_name = true;
257 RecreateSpecWithOptions(std::move(options));
258
259 PaymentResponseHelper helper("en-US", spec(), test_instrument(),
260 test_address(), test_address(), this);
261
262 // Check that the name was set, but not the other values.
263 EXPECT_EQ("John H. Doe", response()->payer_name.value());
264 EXPECT_FALSE(response()->payer_phone.has_value());
265 EXPECT_FALSE(response()->payer_email.has_value());
266 }
267
268 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698