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

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

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

Powered by Google App Engine
This is Rietveld 408576698