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

Side by Side Diff: ios/chrome/browser/ui/payments/payment_request_mediator_unittest.mm

Issue 2924663003: [Payment Request] Moves PaymentRequestViewController to :payments_ui (Closed)
Patch Set: Addressed comments Created 3 years, 6 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 #import "ios/chrome/browser/ui/payments/payment_request_mediator.h"
6
7 #import <Foundation/Foundation.h>
8
9 #include "base/mac/foundation_util.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/credit_card.h"
15 #include "components/autofill/core/browser/test_personal_data_manager.h"
16 #include "components/payments/core/strings_util.h"
17 #include "components/signin/core/browser/signin_manager.h"
18 #include "components/strings/grit/components_strings.h"
19 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
20 #include "ios/chrome/browser/payments/payment_request_test_util.h"
21 #include "ios/chrome/browser/payments/payment_request_util.h"
22 #include "ios/chrome/browser/payments/test_payment_request.h"
23 #include "ios/chrome/browser/signin/fake_signin_manager_builder.h"
24 #include "ios/chrome/browser/signin/signin_manager_factory.h"
25 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h"
26 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h"
27 #import "ios/chrome/browser/ui/payments/cells/autofill_profile_item.h"
28 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h"
29 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h"
30 #import "ios/chrome/browser/ui/payments/cells/price_item.h"
31 #include "ios/web/public/payments/payment_request.h"
32 #include "ios/web/public/test/test_web_thread_bundle.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 #include "testing/platform_test.h"
35 #include "ui/base/l10n/l10n_util.h"
36
37 #if !defined(__has_feature) || !__has_feature(objc_arc)
38 #error "This file requires ARC support."
39 #endif
40
41 namespace {
42 using ::payments::GetShippingOptionSectionString;
43 using ::payment_request_util::GetEmailLabelFromAutofillProfile;
44 using ::payment_request_util::GetNameLabelFromAutofillProfile;
45 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile;
46 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile;
47 } // namespace
48
49 class PaymentRequestMediatorTest : public PlatformTest {
50 protected:
51 PaymentRequestMediatorTest()
52 : autofill_profile_(autofill::test::GetFullProfile()),
53 credit_card_(autofill::test::GetCreditCard()) {
54 // Add testing profile and credit card to autofill::TestPersonalDataManager.
55 personal_data_manager_.AddTestingProfile(&autofill_profile_);
56 personal_data_manager_.AddTestingCreditCard(&credit_card_);
57
58 payment_request_ = base::MakeUnique<TestPaymentRequest>(
59 payment_request_test_util::CreateTestWebPaymentRequest(),
60 &personal_data_manager_);
61
62 TestChromeBrowserState::Builder test_cbs_builder;
63 test_cbs_builder.AddTestingFactory(ios::SigninManagerFactory::GetInstance(),
64 &ios::BuildFakeSigninManager);
65 chrome_browser_state_ = test_cbs_builder.Build();
66 mediator_ = [[PaymentRequestMediator alloc]
67 initWithBrowserState:chrome_browser_state_.get()
68 paymentRequest:payment_request_.get()];
69 }
70
71 PaymentRequestMediator* GetPaymentRequestMediator() { return mediator_; }
72
73 web::TestWebThreadBundle thread_bundle_;
74
75 autofill::AutofillProfile autofill_profile_;
76 autofill::CreditCard credit_card_;
77 autofill::TestPersonalDataManager personal_data_manager_;
78 std::unique_ptr<TestPaymentRequest> payment_request_;
79 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
80 PaymentRequestMediator* mediator_;
81 };
82
83 // Tests whether payment can be completed when expected.
84 TEST_F(PaymentRequestMediatorTest, TestCanPay) {
85 // Payment cannot be completed if there is no selected credit card.
86 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
87 autofill::CreditCard* selected_credit_card =
88 payment_request_->selected_credit_card();
89 payment_request_->set_selected_credit_card(nullptr);
90 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
91
92 // Restore the selected credit card.
93 payment_request_->set_selected_credit_card(selected_credit_card);
94 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
95
96 // Payment cannot be completed if there is no selected shipping profile,
97 // unless no shipping information is requested.
98 autofill::AutofillProfile* selected_shipping_profile =
99 payment_request_->selected_shipping_profile();
100 payment_request_->set_selected_shipping_profile(nullptr);
101 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
102 payment_request_->web_payment_request().options.request_shipping = false;
103 EXPECT_FALSE([GetPaymentRequestMediator() requestShipping]);
104 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
105
106 // Restore the selected shipping profile and request for shipping information.
107 payment_request_->set_selected_shipping_profile(selected_shipping_profile);
108 payment_request_->web_payment_request().options.request_shipping = true;
109 EXPECT_TRUE([GetPaymentRequestMediator() requestShipping]);
110 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
111
112 // Payment cannot be completed if there is no selected shipping option,
113 // unless no shipping information is requested.
114 web::PaymentShippingOption* selected_shipping_option =
115 payment_request_->selected_shipping_option();
116 payment_request_->set_selected_shipping_option(nullptr);
117 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
118 payment_request_->web_payment_request().options.request_shipping = false;
119 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
120
121 // Restore the selected shipping option and request for shipping information.
122 payment_request_->set_selected_shipping_option(selected_shipping_option);
123 payment_request_->web_payment_request().options.request_shipping = true;
124 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
125
126 // Payment cannot be completed if there is no selected contact profile, unless
127 // no contact information is requested.
128 payment_request_->set_selected_contact_profile(nullptr);
129 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
130 payment_request_->web_payment_request().options.request_payer_name = false;
131 EXPECT_TRUE([GetPaymentRequestMediator() requestContactInfo]);
132 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
133 payment_request_->web_payment_request().options.request_payer_phone = false;
134 EXPECT_TRUE([GetPaymentRequestMediator() requestContactInfo]);
135 EXPECT_FALSE([GetPaymentRequestMediator() canPay]);
136 payment_request_->web_payment_request().options.request_payer_email = false;
137 EXPECT_FALSE([GetPaymentRequestMediator() requestContactInfo]);
138 EXPECT_TRUE([GetPaymentRequestMediator() canPay]);
139 }
140
141 // Tests that the Payment Summary item is created as expected.
142 TEST_F(PaymentRequestMediatorTest, TestPaymentSummaryItem) {
143 EXPECT_TRUE([GetPaymentRequestMediator() hasPaymentItems]);
144
145 // Payment Summary item should be of type PriceItem.
146 id item = [GetPaymentRequestMediator() paymentSummaryItem];
147 ASSERT_TRUE([item isMemberOfClass:[PriceItem class]]);
148 PriceItem* payment_summary_item = base::mac::ObjCCastStrict<PriceItem>(item);
149 EXPECT_TRUE([payment_summary_item.item isEqualToString:@"Total"]);
150 EXPECT_TRUE([payment_summary_item.price isEqualToString:@"USD $1.00"]);
151 EXPECT_EQ(nil, payment_summary_item.notification);
152 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
153 payment_summary_item.accessoryType);
154
155 // A label should indicate if the total value was changed.
156 GetPaymentRequestMediator().totalValueChanged = YES;
157 item = [GetPaymentRequestMediator() paymentSummaryItem];
158 payment_summary_item = base::mac::ObjCCastStrict<PriceItem>(item);
159 EXPECT_TRUE([payment_summary_item.notification
160 isEqualToString:l10n_util::GetNSString(IDS_PAYMENTS_UPDATED_LABEL)]);
161
162 // The next time the data source is queried for the Payment Summary item, the
163 // label should disappear.
164 item = [GetPaymentRequestMediator() paymentSummaryItem];
165 payment_summary_item = base::mac::ObjCCastStrict<PriceItem>(item);
166 EXPECT_EQ(nil, payment_summary_item.notification);
167
168 // Remove the display items.
169 web::PaymentRequest web_payment_request =
170 payment_request_->web_payment_request();
171 web_payment_request.details.display_items.clear();
172 payment_request_->UpdatePaymentDetails(web_payment_request.details);
173 EXPECT_FALSE([GetPaymentRequestMediator() hasPaymentItems]);
174
175 // No accessory view indicates there are no display items.
176 item = [GetPaymentRequestMediator() paymentSummaryItem];
177 payment_summary_item = base::mac::ObjCCastStrict<PriceItem>(item);
178 EXPECT_EQ(MDCCollectionViewCellAccessoryNone,
179 payment_summary_item.accessoryType);
180 }
181
182 // Tests that the Shipping section header item is created as expected.
183 TEST_F(PaymentRequestMediatorTest, TestShippingHeaderItem) {
184 // Shipping section header item should be of type PaymentsTextItem.
185 id item = [GetPaymentRequestMediator() shippingSectionHeaderItem];
186 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
187 PaymentsTextItem* shipping_section_header_item =
188 base::mac::ObjCCastStrict<PaymentsTextItem>(item);
189 EXPECT_TRUE([shipping_section_header_item.text
190 isEqualToString:l10n_util::GetNSString(
191 IDS_PAYMENTS_SHIPPING_SUMMARY_LABEL)]);
192 EXPECT_EQ(nil, shipping_section_header_item.detailText);
193 }
194
195 // Tests that the Shipping Address item is created as expected.
196 TEST_F(PaymentRequestMediatorTest, TestShippingAddressItem) {
197 // Shipping Address item should be of type AutofillProfileItem.
198 id item = [GetPaymentRequestMediator() shippingAddressItem];
199 ASSERT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]);
200 AutofillProfileItem* shipping_address_item =
201 base::mac::ObjCCastStrict<AutofillProfileItem>(item);
202 EXPECT_TRUE([shipping_address_item.name
203 isEqualToString:GetNameLabelFromAutofillProfile(
204 *payment_request_->selected_shipping_profile())]);
205 EXPECT_TRUE([shipping_address_item.address
206 isEqualToString:GetShippingAddressLabelFromAutofillProfile(
207 *payment_request_->selected_shipping_profile())]);
208 EXPECT_TRUE([shipping_address_item.phoneNumber
209 isEqualToString:GetPhoneNumberLabelFromAutofillProfile(
210 *payment_request_->selected_shipping_profile())]);
211 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
212 shipping_address_item.accessoryType);
213
214 // Reset the selected shipping profile.
215 payment_request_->set_selected_shipping_profile(nullptr);
216
217 // When there is no selected shipping address, the Shipping Address item
218 // should be of type CollectionViewDetailItem.
219 item = [GetPaymentRequestMediator() shippingAddressItem];
220 ASSERT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]);
221 CollectionViewDetailItem* add_shipping_address_item =
222 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
223 EXPECT_TRUE([add_shipping_address_item.text
224 isEqualToString:l10n_util::GetNSString(
225 IDS_PAYMENTS_SHIPPING_ADDRESS_LABEL)]);
226 EXPECT_EQ(nil, add_shipping_address_item.detailText);
227 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
228 add_shipping_address_item.accessoryType);
229
230 // Remove the shipping profiles.
231 payment_request_->ClearShippingProfiles();
232
233 // No accessory view indicates there are no shipping profiles to choose from.
234 item = [GetPaymentRequestMediator() shippingAddressItem];
235 add_shipping_address_item =
236 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
237 EXPECT_TRUE([add_shipping_address_item.detailText
238 isEqualToString:[l10n_util::GetNSString(IDS_ADD)
239 uppercaseStringWithLocale:[NSLocale currentLocale]]]);
240 EXPECT_EQ(MDCCollectionViewCellAccessoryNone,
241 add_shipping_address_item.accessoryType);
242 }
243
244 // Tests that the Shipping Option item is created as expected.
245 TEST_F(PaymentRequestMediatorTest, TestShippingOptionItem) {
246 // Shipping Option item should be of type PaymentsTextItem.
247 id item = [GetPaymentRequestMediator() shippingOptionItem];
248 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
249 PaymentsTextItem* shipping_option_item =
250 base::mac::ObjCCastStrict<PaymentsTextItem>(item);
251 EXPECT_TRUE([shipping_option_item.text isEqualToString:@"1-Day"]);
252 EXPECT_TRUE([shipping_option_item.detailText isEqualToString:@"$0.99"]);
253 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
254 shipping_option_item.accessoryType);
255
256 // Reset the selected shipping option.
257 payment_request_->set_selected_shipping_option(nullptr);
258
259 // When there is no selected shipping option, the Shipping Option item should
260 // be of type CollectionViewDetailItem.
261 item = [GetPaymentRequestMediator() shippingOptionItem];
262 ASSERT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]);
263 CollectionViewDetailItem* add_shipping_option_item =
264 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
265 EXPECT_TRUE([add_shipping_option_item.text
266 isEqualToString:l10n_util::GetNSString(
267 IDS_PAYMENTS_SHIPPING_OPTION_LABEL)]);
268 EXPECT_EQ(nil, add_shipping_option_item.detailText);
269 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
270 add_shipping_option_item.accessoryType);
271 }
272
273 // Tests that the Payment Method section header item is created as expected.
274 TEST_F(PaymentRequestMediatorTest, TestPaymentMethodHeaderItem) {
275 // Payment Method section header item should be of type PaymentsTextItem.
276 id item = [GetPaymentRequestMediator() paymentMethodSectionHeaderItem];
277 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
278 PaymentsTextItem* payment_method_section_header_item =
279 base::mac::ObjCCastStrict<PaymentsTextItem>(item);
280 EXPECT_TRUE([payment_method_section_header_item.text
281 isEqualToString:l10n_util::GetNSString(
282 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME)]);
283 EXPECT_EQ(nil, payment_method_section_header_item.detailText);
284 }
285
286 // Tests that the Payment Method item is created as expected.
287 TEST_F(PaymentRequestMediatorTest, TestPaymentMethodItem) {
288 // Payment Method item should be of type PaymentsTextItem.
289 id item = [GetPaymentRequestMediator() paymentMethodItem];
290 ASSERT_TRUE([item isMemberOfClass:[PaymentMethodItem class]]);
291 PaymentMethodItem* payment_method_item =
292 base::mac::ObjCCastStrict<PaymentMethodItem>(item);
293 EXPECT_TRUE([payment_method_item.methodID hasPrefix:@"Visa"]);
294 EXPECT_TRUE([payment_method_item.methodID hasSuffix:@"1111"]);
295 EXPECT_TRUE([payment_method_item.methodDetail isEqualToString:@"Test User"]);
296 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
297 payment_method_item.accessoryType);
298
299 // Reset the selected credit card.
300 payment_request_->set_selected_credit_card(nullptr);
301
302 // When there is no selected credit card, the Payment Method item should be of
303 // type CollectionViewDetailItem.
304 item = [GetPaymentRequestMediator() paymentMethodItem];
305 ASSERT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]);
306 CollectionViewDetailItem* add_payment_method_item =
307 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
308 EXPECT_TRUE([add_payment_method_item.text
309 isEqualToString:l10n_util::GetNSString(
310 IDS_PAYMENT_REQUEST_PAYMENT_METHOD_SECTION_NAME)]);
311 EXPECT_EQ(nil, add_payment_method_item.detailText);
312 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
313 add_payment_method_item.accessoryType);
314
315 // Remove the credit cards.
316 payment_request_->ClearCreditCards();
317
318 // No accessory view indicates there are no payment methods to choose from.
319 item = [GetPaymentRequestMediator() paymentMethodItem];
320 add_payment_method_item =
321 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
322 EXPECT_TRUE([add_payment_method_item.detailText
323 isEqualToString:[l10n_util::GetNSString(IDS_ADD)
324 uppercaseStringWithLocale:[NSLocale currentLocale]]]);
325 EXPECT_EQ(MDCCollectionViewCellAccessoryNone,
326 add_payment_method_item.accessoryType);
327 }
328
329 // Tests that the Contact Info section header item is created as expected.
330 TEST_F(PaymentRequestMediatorTest, TestContactInfoHeaderItem) {
331 // Contact Info section header item should be of type PaymentsTextItem.
332 id item = [GetPaymentRequestMediator() contactInfoSectionHeaderItem];
333 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
334 PaymentsTextItem* contact_info_section_header_item =
335 base::mac::ObjCCastStrict<PaymentsTextItem>(item);
336 EXPECT_TRUE([contact_info_section_header_item.text
337 isEqualToString:l10n_util::GetNSString(
338 IDS_PAYMENTS_CONTACT_DETAILS_LABEL)]);
339 EXPECT_EQ(nil, contact_info_section_header_item.detailText);
340 }
341
342 // Tests that the Contact Info item is created as expected.
343 TEST_F(PaymentRequestMediatorTest, TestContactInfoItem) {
344 // Contact Info item should be of type AutofillProfileItem.
345 id item = [GetPaymentRequestMediator() contactInfoItem];
346 ASSERT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]);
347 AutofillProfileItem* contact_info_item =
348 base::mac::ObjCCastStrict<AutofillProfileItem>(item);
349 EXPECT_TRUE([contact_info_item.name
350 isEqualToString:GetNameLabelFromAutofillProfile(
351 *payment_request_->selected_contact_profile())]);
352 EXPECT_TRUE([contact_info_item.phoneNumber
353 isEqualToString:GetPhoneNumberLabelFromAutofillProfile(
354 *payment_request_->selected_contact_profile())]);
355 EXPECT_TRUE([contact_info_item.email
356 isEqualToString:GetEmailLabelFromAutofillProfile(
357 *payment_request_->selected_contact_profile())]);
358 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
359 contact_info_item.accessoryType);
360
361 // Reset the selected contact profile.
362 payment_request_->set_selected_contact_profile(nullptr);
363
364 // When there is no selected contact profile, the Payment Method item should
365 // be of type CollectionViewDetailItem.
366 item = [GetPaymentRequestMediator() contactInfoItem];
367 ASSERT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]);
368 CollectionViewDetailItem* add_contact_info_item =
369 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
370 EXPECT_TRUE([add_contact_info_item.text
371 isEqualToString:l10n_util::GetNSString(
372 IDS_PAYMENTS_CONTACT_DETAILS_LABEL)]);
373 EXPECT_EQ(nil, add_contact_info_item.detailText);
374 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
375 add_contact_info_item.accessoryType);
376
377 // Remove the contact profiles.
378 payment_request_->ClearContactProfiles();
379
380 // No accessory view indicates there are no contact profiles to choose from.
381 item = [GetPaymentRequestMediator() contactInfoItem];
382 add_contact_info_item =
383 base::mac::ObjCCastStrict<CollectionViewDetailItem>(item);
384 EXPECT_TRUE([add_contact_info_item.detailText
385 isEqualToString:[l10n_util::GetNSString(IDS_ADD)
386 uppercaseStringWithLocale:[NSLocale currentLocale]]]);
387 EXPECT_EQ(MDCCollectionViewCellAccessoryNone,
388 add_contact_info_item.accessoryType);
389 }
390
391 // Tests that the Footer item is created as expected.
392 TEST_F(PaymentRequestMediatorTest, TestFooterItem) {
393 // Make sure the user is signed out.
394 SigninManager* signin_manager = ios::SigninManagerFactory::GetForBrowserState(
395 chrome_browser_state_.get());
396 if (signin_manager->IsAuthenticated()) {
397 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST,
398 signin_metrics::SignoutDelete::IGNORE_METRIC);
399 }
400
401 // Footer item should be of type CollectionViewFooterItem.
402 id item = [GetPaymentRequestMediator() footerItem];
403 ASSERT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]);
404 CollectionViewFooterItem* footer_item =
405 base::mac::ObjCCastStrict<CollectionViewFooterItem>(item);
406 EXPECT_TRUE([footer_item.text
407 isEqualToString:l10n_util::GetNSString(
408 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT)]);
409
410 // Fake a signed in user.
411 signin_manager->SetAuthenticatedAccountInfo("12345", "username@example.com");
412
413 item = [GetPaymentRequestMediator() footerItem];
414 footer_item = base::mac::ObjCCastStrict<CollectionViewFooterItem>(item);
415 EXPECT_TRUE([footer_item.text
416 isEqualToString:l10n_util::GetNSStringF(
417 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN,
418 base::ASCIIToUTF16("username@example.com"))]);
419 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698