Commit 6fa7b73a authored by mahmadi's avatar mahmadi Committed by Commit bot

[Payment Request] Sever card summary section in the credit card edit form

Displays a PaymentMethodItem in the credit card form in order to identify
the server card being edited.

http://imgur.com/a/CM38f

BUG=602666

Review-Url: https://codereview.chromium.org/2826593005
Cr-Commit-Position: refs/heads/master@{#465706}
parent 4b18d5f5
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#import "components/autofill/ios/browser/credit_card_util.h" #import "components/autofill/ios/browser/credit_card_util.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h" #include "ios/chrome/browser/application_context.h"
#import "ios/chrome/browser/payments/cells/payment_method_item.h"
#include "ios/chrome/browser/payments/payment_request.h" #include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/payments/payment_request_editor_field.h" #import "ios/chrome/browser/payments/payment_request_editor_field.h"
#import "ios/chrome/browser/payments/payment_request_util.h" #import "ios/chrome/browser/payments/payment_request_util.h"
...@@ -64,6 +65,22 @@ const CGFloat kCardTypeIconDimension = 25.0; ...@@ -64,6 +65,22 @@ const CGFloat kCardTypeIconDimension = 25.0;
return self; return self;
} }
- (CollectionViewItem*)serverCardSummaryItem {
if (!_creditCard || autofill::IsCreditCardLocal(*_creditCard))
return nil;
PaymentMethodItem* cardSummaryItem = [[PaymentMethodItem alloc] init];
cardSummaryItem.methodID =
base::SysUTF16ToNSString(_creditCard->TypeAndLastFourDigits());
cardSummaryItem.methodDetail = base::SysUTF16ToNSString(
_creditCard->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL));
const int cardTypeIconID =
autofill::data_util::GetPaymentRequestData(_creditCard->type())
.icon_resource_id;
cardSummaryItem.methodTypeIcon = NativeImage(cardTypeIconID);
return cardSummaryItem;
}
- (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID { - (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID {
DCHECK(profileGUID); DCHECK(profileGUID);
autofill::AutofillProfile* profile = autofill::AutofillProfile* profile =
......
...@@ -21,6 +21,10 @@ typedef NS_ENUM(NSInteger, CreditCardEditViewControllerState) { ...@@ -21,6 +21,10 @@ typedef NS_ENUM(NSInteger, CreditCardEditViewControllerState) {
@protocol CreditCardEditViewControllerDataSource< @protocol CreditCardEditViewControllerDataSource<
PaymentRequestEditViewControllerDataSource> PaymentRequestEditViewControllerDataSource>
// Returns an item that identifies the server card being edited. Returns nil for
// local cards or when a card is being created.
- (CollectionViewItem*)serverCardSummaryItem;
// Returns the billing address label from an autofill profile with the given // Returns the billing address label from an autofill profile with the given
// guid. Returns nil if the profile does not have an address. // guid. Returns nil if the profile does not have an address.
- (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID; - (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID;
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h" #import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h" #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item.h" #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collection_view_controller.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h" #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_model.h" #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
#import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
...@@ -31,12 +32,14 @@ NSString* const kCreditCardEditCollectionViewId = ...@@ -31,12 +32,14 @@ NSString* const kCreditCardEditCollectionViewId =
@"kCreditCardEditCollectionViewId"; @"kCreditCardEditCollectionViewId";
typedef NS_ENUM(NSInteger, SectionIdentifier) { typedef NS_ENUM(NSInteger, SectionIdentifier) {
SectionIdentifierBillingAddress = kSectionIdentifierEnumStart, SectionIdentifierCardSummary = kSectionIdentifierEnumStart,
SectionIdentifierBillingAddress,
SectionIdentifierSaveCard, SectionIdentifierSaveCard,
}; };
typedef NS_ENUM(NSInteger, ItemType) { typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeBillingAddress = kItemTypeEnumStart, ItemTypeCardSummary = kItemTypeEnumStart,
ItemTypeBillingAddress,
ItemTypeSaveCard, ItemTypeSaveCard,
}; };
...@@ -155,6 +158,21 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -155,6 +158,21 @@ typedef NS_ENUM(NSInteger, ItemType) {
} }
} }
- (void)loadHeaderItems {
[super loadHeaderItems];
CollectionViewModel* model = self.collectionViewModel;
// Server card summary section.
CollectionViewItem* serverCardSummaryItem =
[_dataSource serverCardSummaryItem];
if (serverCardSummaryItem) {
[model addSectionWithIdentifier:SectionIdentifierCardSummary];
serverCardSummaryItem.type = ItemTypeCardSummary;
[model addItem:serverCardSummaryItem
toSectionWithIdentifier:SectionIdentifierCardSummary];
}
}
- (void)loadFooterItems { - (void)loadFooterItems {
CollectionViewModel* model = self.collectionViewModel; CollectionViewModel* model = self.collectionViewModel;
...@@ -267,6 +285,7 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -267,6 +285,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
CollectionViewItem* item = CollectionViewItem* item =
[self.collectionViewModel itemAtIndexPath:indexPath]; [self.collectionViewModel itemAtIndexPath:indexPath];
switch (item.type) { switch (item.type) {
case ItemTypeCardSummary:
case ItemTypeSaveCard: case ItemTypeSaveCard:
break; break;
case ItemTypeBillingAddress: { case ItemTypeBillingAddress: {
...@@ -287,6 +306,7 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -287,6 +306,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
switch (item.type) { switch (item.type) {
case ItemTypeBillingAddress: case ItemTypeBillingAddress:
return MDCCellDefaultOneLineHeight; return MDCCellDefaultOneLineHeight;
case ItemTypeCardSummary:
case ItemTypeSaveCard: case ItemTypeSaveCard:
return [MDCCollectionViewCell return [MDCCollectionViewCell
cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
...@@ -301,6 +321,7 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -301,6 +321,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
switch (type) { switch (type) {
case ItemTypeCardSummary:
case ItemTypeSaveCard: case ItemTypeSaveCard:
return YES; return YES;
default: default:
......
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/test_personal_data_manager.h" #import "ios/chrome/browser/payments/cells/payment_method_item.h"
#import "ios/chrome/browser/payments/credit_card_edit_mediator.h"
#include "ios/chrome/browser/payments/payment_request.h"
#import "ios/chrome/browser/payments/payment_request_editor_field.h" #import "ios/chrome/browser/payments/payment_request_editor_field.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h" #include "ios/chrome/browser/payments/payment_request_test_util.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type.h" #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h"
...@@ -17,6 +15,7 @@ ...@@ -17,6 +15,7 @@
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item.h" #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.h" #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h" #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/test_utils.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h" #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h"
#include "ios/web/public/payments/payment_request.h" #include "ios/web/public/payments/payment_request.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -26,13 +25,17 @@ ...@@ -26,13 +25,17 @@
#endif #endif
@interface TestCreditCardEditViewControllerMediator @interface TestCreditCardEditViewControllerMediator
: CreditCardEditViewControllerMediator : NSObject<CreditCardEditViewControllerDataSource>
@end @end
@implementation TestCreditCardEditViewControllerMediator @implementation TestCreditCardEditViewControllerMediator
NSArray<EditorField*>* editorFields() { - (CollectionViewItem*)serverCardSummaryItem {
return [[PaymentMethodItem alloc] init];
}
- (NSArray<EditorField*>*)editorFields {
return @[ return @[
[[EditorField alloc] initWithAutofillUIType:AutofillUITypeCreditCardNumber [[EditorField alloc] initWithAutofillUIType:AutofillUITypeCreditCardNumber
label:@"Credit Card Number" label:@"Credit Card Number"
...@@ -54,24 +57,25 @@ NSArray<EditorField*>* editorFields() { ...@@ -54,24 +57,25 @@ NSArray<EditorField*>* editorFields() {
]; ];
} }
- (NSString*)billingAddressLabelForProfileWithGUID:(NSString*)profileGUID {
return nil;
}
- (UIImage*)cardTypeIconFromCardNumber:(NSString*)cardNumber {
return nil;
}
@end @end
class PaymentRequestCreditCardEditViewControllerTest class PaymentRequestCreditCardEditViewControllerTest
: public CollectionViewControllerTest { : public CollectionViewControllerTest {
protected: protected:
CollectionViewController* InstantiateController() override { CollectionViewController* InstantiateController() override {
payment_request_ = base::MakeUnique<PaymentRequest>( mediator_ = [[TestCreditCardEditViewControllerMediator alloc] init];
payment_request_test_util::CreateTestWebPaymentRequest(),
&personal_data_manager_);
CreditCardEditViewControllerMediator* mediator =
[[CreditCardEditViewControllerMediator alloc]
initWithPaymentRequest:payment_request_.get()
creditCard:nil];
CreditCardEditViewController* viewController = CreditCardEditViewController* viewController =
[[CreditCardEditViewController alloc] init]; [[CreditCardEditViewController alloc] init];
[viewController setDataSource:mediator]; [viewController setDataSource:mediator_];
return viewController; return viewController;
} }
...@@ -80,8 +84,7 @@ class PaymentRequestCreditCardEditViewControllerTest ...@@ -80,8 +84,7 @@ class PaymentRequestCreditCardEditViewControllerTest
controller()); controller());
} }
autofill::TestPersonalDataManager personal_data_manager_; TestCreditCardEditViewControllerMediator* mediator_ = nil;
std::unique_ptr<PaymentRequest> payment_request_;
}; };
// Tests that the correct number of items are displayed after loading the model. // Tests that the correct number of items are displayed after loading the model.
...@@ -94,14 +97,17 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) { ...@@ -94,14 +97,17 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) {
[GetCreditCardEditViewController() loadModel]; [GetCreditCardEditViewController() loadModel];
// There is one section for every textfield (there are four textfields in // There is one section for every textfield (there are four textfields in
// total), one for the footer, and one for the billing address ID item. // total), one for the server card summary section, one for the footer, and
ASSERT_EQ(6, NumberOfSections()); // one for the billing address ID item.
ASSERT_EQ(7, NumberOfSections());
// The first four sections have only one item of the type AutofillEditItem. // The server card summary section is the first section and has one item of
// the type PaymentMethodItem.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(0))); ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(0)));
id item = GetCollectionViewItem(0, 0); id item = GetCollectionViewItem(0, 0);
EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); EXPECT_TRUE([item isMemberOfClass:[PaymentMethodItem class]]);
// The next four sections have only one item of the type AutofillEditItem.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(1))); ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(1)));
item = GetCollectionViewItem(1, 0); item = GetCollectionViewItem(1, 0);
EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]);
...@@ -114,10 +120,14 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) { ...@@ -114,10 +120,14 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) {
item = GetCollectionViewItem(3, 0); item = GetCollectionViewItem(3, 0);
EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]); EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]);
// The billing address section contains one item which is of the type
// CollectionViewDetailItem.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(4))); ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(4)));
item = GetCollectionViewItem(4, 0); item = GetCollectionViewItem(4, 0);
EXPECT_TRUE([item isMemberOfClass:[AutofillEditItem class]]);
// The billing address section contains one item which is of the type
// CollectionViewDetailItem.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(5)));
item = GetCollectionViewItem(5, 0);
EXPECT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]); EXPECT_TRUE([item isMemberOfClass:[CollectionViewDetailItem class]]);
CollectionViewDetailItem* billing_address_item = item; CollectionViewDetailItem* billing_address_item = item;
EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator, EXPECT_EQ(MDCCollectionViewCellAccessoryDisclosureIndicator,
...@@ -125,8 +135,8 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) { ...@@ -125,8 +135,8 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, TestModel) {
// The footer section contains one item which is of the type // The footer section contains one item which is of the type
// CollectionViewFooterItem. // CollectionViewFooterItem.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(5))); ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(6)));
item = GetCollectionViewItem(5, 0); item = GetCollectionViewItem(6, 0);
EXPECT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]); EXPECT_TRUE([item isMemberOfClass:[CollectionViewFooterItem class]]);
} }
...@@ -143,12 +153,12 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest, ...@@ -143,12 +153,12 @@ TEST_F(PaymentRequestCreditCardEditViewControllerTest,
// There is an extra section containing a switch that allows the user to save // There is an extra section containing a switch that allows the user to save
// the credit card locally. // the credit card locally.
ASSERT_EQ(7, NumberOfSections()); ASSERT_EQ(8, NumberOfSections());
// The switch section is the last section before the footer and has one item // The switch section is the last section before the footer and has one item
// of the type CollectionViewSwitchItem. The switch is on by defualt. // of the type CollectionViewSwitchItem. The switch is on by defualt.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(5))); ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(6)));
id item = GetCollectionViewItem(5, 0); id item = GetCollectionViewItem(6, 0);
EXPECT_TRUE([item isMemberOfClass:[CollectionViewSwitchItem class]]); EXPECT_TRUE([item isMemberOfClass:[CollectionViewSwitchItem class]]);
CollectionViewSwitchItem* switch_item = item; CollectionViewSwitchItem* switch_item = item;
EXPECT_EQ(YES, [switch_item isOn]); EXPECT_EQ(YES, [switch_item isOn]);
......
...@@ -22,6 +22,10 @@ const NSInteger kItemTypeEnumStart = kItemTypeEnumZero + 100; ...@@ -22,6 +22,10 @@ const NSInteger kItemTypeEnumStart = kItemTypeEnumZero + 100;
// value on the field. Returns YES if all the fields are validated successfully. // value on the field. Returns YES if all the fields are validated successfully.
- (BOOL)validateForm; - (BOOL)validateForm;
// Called before the editor field items are added to the the collection view
// model. Subclasses override this method to add items before the editor fields.
- (void)loadHeaderItems;
// Called after the editor field items have been added to the the collection // Called after the editor field items have been added to the the collection
// view model. Subclasses override this method to add items after the editor // view model. Subclasses override this method to add items after the editor
// fields. // fields.
......
...@@ -136,6 +136,8 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -136,6 +136,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
[super loadModel]; [super loadModel];
CollectionViewModel* model = self.collectionViewModel; CollectionViewModel* model = self.collectionViewModel;
[self loadHeaderItems];
// Iterate over the fields and add the respective sections and items. // Iterate over the fields and add the respective sections and items.
int sectionIdentifier = static_cast<int>(SectionIdentifierFirstTextField); int sectionIdentifier = static_cast<int>(SectionIdentifierFirstTextField);
for (EditorField* field in _fields) { for (EditorField* field in _fields) {
...@@ -403,6 +405,9 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -403,6 +405,9 @@ typedef NS_ENUM(NSInteger, ItemType) {
return YES; return YES;
} }
- (void)loadHeaderItems {
}
- (void)loadFooterItems { - (void)loadFooterItems {
CollectionViewModel* model = self.collectionViewModel; CollectionViewModel* model = self.collectionViewModel;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment