| OLD | NEW |
| (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/credit_card_edit_view_controller.h" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "components/autofill/core/browser/field_types.h" | |
| 10 #include "ios/chrome/browser/payments/payment_request_test_util.h" | |
| 11 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h" | |
| 12 #import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h" | |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item
.h" | |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" | |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/test_utils.h" | |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" | |
| 17 #import "ios/chrome/browser/ui/payments/cells/accepted_payment_methods_item.h" | |
| 18 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h" | |
| 19 #import "ios/chrome/browser/ui/payments/cells/payments_selector_edit_item.h" | |
| 20 #import "ios/chrome/browser/ui/payments/payment_request_edit_consumer.h" | |
| 21 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h" | |
| 22 #include "ios/web/public/payments/payment_request.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 26 #error "This file requires ARC support." | |
| 27 #endif | |
| 28 | |
| 29 @interface TestCreditCardEditViewControllerMediator | |
| 30 : NSObject<CreditCardEditViewControllerDataSource> | |
| 31 | |
| 32 @property(nonatomic, weak) id<PaymentRequestEditConsumer> consumer; | |
| 33 | |
| 34 @end | |
| 35 | |
| 36 @implementation TestCreditCardEditViewControllerMediator | |
| 37 | |
| 38 @synthesize state = _state; | |
| 39 @synthesize consumer = _consumer; | |
| 40 | |
| 41 - (CollectionViewItem*)headerItem { | |
| 42 return [[PaymentMethodItem alloc] init]; | |
| 43 } | |
| 44 | |
| 45 - (BOOL)shouldHideBackgroundForHeaderItem { | |
| 46 return NO; | |
| 47 } | |
| 48 | |
| 49 - (void)setConsumer:(id<PaymentRequestEditConsumer>)consumer { | |
| 50 _consumer = consumer; | |
| 51 [self.consumer setEditorFields:@[ | |
| 52 [[EditorField alloc] initWithAutofillUIType:AutofillUITypeCreditCardNumber | |
| 53 fieldType:EditorFieldTypeTextField | |
| 54 label:@"Credit Card Number" | |
| 55 value:@"4111111111111111" /* Visa */ | |
| 56 required:YES], | |
| 57 [[EditorField alloc] | |
| 58 initWithAutofillUIType:AutofillUITypeCreditCardHolderFullName | |
| 59 fieldType:EditorFieldTypeTextField | |
| 60 label:@"Cardholder Name" | |
| 61 value:@"John Doe" | |
| 62 required:YES], | |
| 63 [[EditorField alloc] initWithAutofillUIType:AutofillUITypeCreditCardExpMonth | |
| 64 fieldType:EditorFieldTypeTextField | |
| 65 label:@"Expiration Month" | |
| 66 value:@"12" | |
| 67 required:YES], | |
| 68 [[EditorField alloc] initWithAutofillUIType:AutofillUITypeCreditCardExpYear | |
| 69 fieldType:EditorFieldTypeTextField | |
| 70 label:@"Expiration Year" | |
| 71 value:@"2090" | |
| 72 required:YES], | |
| 73 [[EditorField alloc] | |
| 74 initWithAutofillUIType:AutofillUITypeCreditCardBillingAddress | |
| 75 fieldType:EditorFieldTypeSelector | |
| 76 label:@"Billing Address" | |
| 77 value:@"12345" | |
| 78 required:YES], | |
| 79 ]]; | |
| 80 } | |
| 81 | |
| 82 - (UIImage*)cardTypeIconFromCardNumber:(NSString*)cardNumber { | |
| 83 return nil; | |
| 84 } | |
| 85 | |
| 86 @end | |
| 87 | |
| 88 class PaymentRequestCreditCardEditViewControllerTest | |
| 89 : public CollectionViewControllerTest { | |
| 90 protected: | |
| 91 CollectionViewController* InstantiateController() override { | |
| 92 CreditCardEditViewController* viewController = | |
| 93 [[CreditCardEditViewController alloc] init]; | |
| 94 mediator_ = [[TestCreditCardEditViewControllerMediator alloc] init]; | |
| 95 [mediator_ setConsumer:viewController]; | |
| 96 [viewController setDataSource:mediator_]; | |
| 97 return viewController; | |
| 98 } | |
| 99 | |
| 100 CreditCardEditViewController* GetCreditCardEditViewController() { | |
| 101 return base::mac::ObjCCastStrict<CreditCardEditViewController>( | |
| 102 controller()); | |
| 103 } | |
| 104 | |
| 105 TestCreditCardEditViewControllerMediator* mediator_ = nil; | |
| 106 }; | |
| 107 | |
| 108 // Tests that the correct number of items are displayed after loading the model. | |
| 109 TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) { | |
| 110 CreateController(); | |
| 111 CheckController(); | |
| 112 | |
| 113 [mediator_ setState:EditViewControllerStateEdit]; | |
| 114 [GetCreditCardEditViewController() loadModel]; | |
| 115 | |
| 116 // There is one section containing the credit card type icons for the accepted | |
| 117 // payment methods. In addition to that, there is one section for every field | |
| 118 // (there are five form fields in total), and finally one for the footer. | |
| 119 ASSERT_EQ(7, NumberOfSections()); | |
| 120 | |
| 121 // The server card summary section is the first section and has one item of | |
| 122 // the type PaymentMethodItem. | |
| 123 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(0))); | |
| 124 id item = GetCollectionViewItem(0, 0); | |
| 125 EXPECT_TRUE([item isMemberOfClass:[PaymentMethodItem class]]); | |
| 126 | |
| 127 // The next four sections have only one item of the type AutofillEditItem. | |
| 128 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(1))); | |
| 129 item = GetCollectionViewItem(1, 0); | |
| 130 EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); | |
| 131 | |
| 132 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(2))); | |
| 133 item = GetCollectionViewItem(2, 0); | |
| 134 EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); | |
| 135 | |
| 136 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(3))); | |
| 137 item = GetCollectionViewItem(3, 0); | |
| 138 EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); | |
| 139 | |
| 140 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(4))); | |
| 141 item = GetCollectionViewItem(4, 0); | |
| 142 EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); | |
| 143 | |
| 144 // The billing address section contains one item which is of the type | |
| 145 // PaymentsSelectorEditItem. | |
| 146 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(5))); | |
| 147 item = GetCollectionViewItem(5, 0); | |
| 148 EXPECT_TRUE([item isMemberOfClass:[PaymentsSelectorEditItem class]]); | |
| 149 PaymentsSelectorEditItem* billing_address_item = item; | |
| 150 EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator, | |
| 151 billing_address_item.accessoryType); | |
| 152 | |
| 153 // The footer section contains one item which is of the type | |
| 154 // CollectionViewFooterItem. | |
| 155 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(6))); | |
| 156 item = GetCollectionViewItem(6, 0); | |
| 157 EXPECT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]); | |
| 158 } | |
| 159 | |
| 160 // Tests that the correct number of items are displayed after loading the model, | |
| 161 // when creating a new credit card. | |
| 162 TEST_F(PaymentRequestCreditCardEditViewControllerTest, | |
| 163 TestModelCreateNewCreditCard) { | |
| 164 CreateController(); | |
| 165 CheckController(); | |
| 166 | |
| 167 [mediator_ setState:EditViewControllerStateCreate]; | |
| 168 [GetCreditCardEditViewController() loadModel]; | |
| 169 | |
| 170 // There is an extra section containing a switch that allows the user to save | |
| 171 // the credit card locally. | |
| 172 ASSERT_EQ(8, NumberOfSections()); | |
| 173 | |
| 174 // The switch section is the last section before the footer and has one item | |
| 175 // of the type CollectionViewSwitchItem. The switch is on by defualt. | |
| 176 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(6))); | |
| 177 id item = GetCollectionViewItem(6, 0); | |
| 178 EXPECT_TRUE([item isMemberOfClass:[CollectionViewSwitchItem class]]); | |
| 179 CollectionViewSwitchItem* switch_item = item; | |
| 180 EXPECT_EQ(YES, [switch_item isOn]); | |
| 181 } | |
| OLD | NEW |