-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes crash in STPAddCardViewController with some prefilled billing addresses #1004
Changes from 1 commit
134a414
fe5b1aa
70e2b83
9e21431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,10 +115,6 @@ - (void)createAndSetupViews { | |
STPPaymentCardTextFieldCell *paymentCell = [[STPPaymentCardTextFieldCell alloc] init]; | ||
paymentCell.paymentField.delegate = self; | ||
self.paymentCell = paymentCell; | ||
|
||
if (self.prefilledInformation.billingAddress != nil) { | ||
self.addressViewModel.address = self.prefilledInformation.billingAddress; | ||
} | ||
|
||
self.activityIndicator = [[STPPaymentActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)]; | ||
|
||
|
@@ -128,6 +124,10 @@ - (void)createAndSetupViews { | |
self.tableView.dataSource = self; | ||
self.tableView.delegate = self; | ||
|
||
if (self.prefilledInformation.billingAddress != nil) { | ||
self.addressViewModel.address = self.prefilledInformation.billingAddress; | ||
} | ||
|
||
STPSectionHeaderView *addressHeaderView = [STPSectionHeaderView new]; | ||
addressHeaderView.theme = self.theme; | ||
addressHeaderView.title = STPLocalizedString(@"Billing Address", @"Title for billing address entry section"); | ||
|
@@ -398,14 +398,20 @@ - (void)paymentCardTextFieldDidEndEditingCVC:(__unused STPPaymentCardTextField * | |
#pragma mark - STPAddressViewModelDelegate | ||
|
||
- (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel addedCellAtIndex:(NSUInteger)index { | ||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; | ||
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | ||
[self updateInputAccessoryVisiblity]; | ||
NSInteger rowsInSection = [self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]; | ||
if (rowsInSection != NSNotFound && rowsInSection < [self tableView:self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]) { | ||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; | ||
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | ||
[self updateInputAccessoryVisiblity]; | ||
} | ||
} | ||
|
||
- (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel removedCellAtIndex:(NSUInteger)index { | ||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; | ||
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | ||
NSInteger rowsInSection = [self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]; | ||
if (rowsInSection != NSNotFound && index < (NSUInteger)rowsInSection) { | ||
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; | ||
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | ||
} | ||
[self updateInputAccessoryVisiblity]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it important that the call to I don't think so, but wanted to see if there was something I'm missing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, I'll move it out of the if in remove for consistency :) |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSLocale+STPSwizzling.h | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSLocale (STPSwizzling) | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale; | ||
+ (void)stp_resetCurrentLocale; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// NSLocale+STPSwizzling.m | ||
// StripeiOS Tests | ||
// | ||
// Created by Cameron Sabol on 7/17/18. | ||
// Copyright © 2018 Stripe, Inc. All rights reserved. | ||
// | ||
|
||
#import "NSLocale+STPSwizzling.h" | ||
|
||
#import <objc/runtime.h> | ||
|
||
@interface NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement; | ||
|
||
@end | ||
|
||
@implementation NSObject (STPSwizzling) | ||
|
||
+ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you consider using the This is a really simple usage, and since it's test code I don't think it needs to get fancy, mostly just curious. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't even know that existed! I'll keep in mind for next time |
||
{ | ||
method_exchangeImplementations(class_getClassMethod(self, original), class_getClassMethod(self, replacement)); | ||
} | ||
|
||
@end | ||
|
||
@implementation NSLocale (STPSwizzling) | ||
|
||
static NSLocale *_stpLocaleOverride = nil; | ||
|
||
+ (void)stp_setCurrentLocale:(NSLocale *)locale | ||
{ | ||
if (_stpLocaleOverride == nil & locale != nil) { | ||
[self stp_swizzleClassMethod:@selector(currentLocale) withReplacement:@selector(stp_currentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(autoupdatingCurrentLocale) withReplacement:@selector(stp_autoUpdatingCurrentLocale)]; | ||
[self stp_swizzleClassMethod:@selector(systemLocale) withReplacement:@selector(stp_systemLocale)]; | ||
} | ||
_stpLocaleOverride = locale; | ||
} | ||
|
||
+ (void)stp_resetCurrentLocale | ||
{ | ||
[self stp_setCurrentLocale:nil]; | ||
} | ||
|
||
+ (instancetype)stp_currentLocale { | ||
return _stpLocaleOverride ?: [self stp_currentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_autoUpdatingCurrentLocale { | ||
return _stpLocaleOverride ?: [self stp_autoUpdatingCurrentLocale]; | ||
} | ||
|
||
+ (instancetype)stp_systemLocale { | ||
return _stpLocaleOverride ?: [self stp_systemLocale]; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#import <OCMock/OCMock.h> | ||
#import <Stripe/Stripe.h> | ||
#import "NSError+Stripe.h" | ||
#import "NSLocale+STPSwizzling.h" | ||
#import "STPCard.h" | ||
#import "STPFixtures.h" | ||
#import "STPPaymentCardTextFieldCell.h" | ||
|
@@ -41,6 +42,53 @@ - (STPAddCardViewController *)buildAddCardViewController { | |
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
|
||
- (void)testPrefilledBillingAddress_removeAddress { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these need to be inside the |
||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredBillingAddressFields = STPBillingAddressFieldsZip; | ||
STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme]]; | ||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"Harare"; | ||
address.postalCode = @"10002"; | ||
address.country = @"ZW"; | ||
|
||
STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; | ||
prefilledInfo.billingAddress = address; | ||
sut.prefilledInformation = prefilledInfo; | ||
|
||
[sut loadView]; | ||
[sut viewDidLoad]; | ||
} | ||
|
||
- (void)testPrefilledBillingAddress_addAddress { | ||
[NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; | ||
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; | ||
config.requiredBillingAddressFields = STPBillingAddressFieldsZip; | ||
STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config | ||
theme:[STPTheme defaultTheme]]; | ||
STPAddress *address = [STPAddress new]; | ||
address.name = @"John Smith Doe"; | ||
address.phone = @"8885551212"; | ||
address.email = @"[email protected]"; | ||
address.line1 = @"55 John St"; | ||
address.city = @"New York"; | ||
address.state = @"NY"; | ||
address.postalCode = @"10002"; | ||
address.country = @"US"; | ||
|
||
STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; | ||
prefilledInfo.billingAddress = address; | ||
sut.prefilledInformation = prefilledInfo; | ||
|
||
[sut loadView]; | ||
[sut viewDidLoad]; | ||
[NSLocale stp_resetCurrentLocale]; | ||
} | ||
|
||
- (void)testNextWithCreateTokenError { | ||
STPAddCardViewController *sut = [self buildAddCardViewController]; | ||
STPCardParams *expectedCardParams = [STPFixtures cardParams]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to (cleanly) push this down into the
STPAddressViewModel
? It looks like it's already trying to trackshowingPostalCodeCell
, and has logic for whether it's being added or removed based on updates to the address.Or maybe we should move the
_addressViewModel.delegate = self
line to after we set the pre-filled info, because we don't care about updates to the ViewModel until after we've finished creating the views? That might allow us to avoid this complexity completely?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually prefer the logic to live in the view controller because this situation is a result of layout passes/table view caching/etc. that I think the view controller is closer too than the view model (correct me if I'm wrong in my view model understanding :) ).
I thought about this. I believe that change would also fix the crash, but this solution is more robust