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