Skip to content
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

Merged
merged 4 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Stripe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@
04F94DD31D22A23F004FC826 /* NSBundle+Stripe_AppName.h in Headers */ = {isa = PBXBuildFile; fileRef = 049A3F971CC76A2400F57DE7 /* NSBundle+Stripe_AppName.h */; };
04F94DD41D22A242004FC826 /* NSBundle+Stripe_AppName.m in Sources */ = {isa = PBXBuildFile; fileRef = 049A3F981CC76A2400F57DE7 /* NSBundle+Stripe_AppName.m */; };
04FCFA191BD59A8C00297732 /* STPCategoryLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FCFA171BD59A8C00297732 /* STPCategoryLoader.h */; };
3617A51420FE5BBB001A9E6A /* NSLocale+STPSwizzling.h in Headers */ = {isa = PBXBuildFile; fileRef = 3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */; };
3617A51520FE5BBB001A9E6A /* NSLocale+STPSwizzling.m in Sources */ = {isa = PBXBuildFile; fileRef = 3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */; };
8B013C891F1E784A00DD831B /* STPPaymentConfigurationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */; };
8B429AD81EF9D4B400F95F34 /* STPBankAccountParams+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 8B429AD71EF9D4A300F95F34 /* STPBankAccountParams+Private.h */; };
8B429AD91EF9D4B500F95F34 /* STPBankAccountParams+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 8B429AD71EF9D4A300F95F34 /* STPBankAccountParams+Private.h */; };
Expand Down Expand Up @@ -1036,6 +1038,8 @@
04F94D6F1D21CB20004FC826 /* StripeiOSResources.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = StripeiOSResources.xcconfig; sourceTree = "<group>"; };
04FCFA171BD59A8C00297732 /* STPCategoryLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STPCategoryLoader.h; sourceTree = "<group>"; };
11C74B9B164043050071C2CA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSLocale+STPSwizzling.h"; sourceTree = "<group>"; };
3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSLocale+STPSwizzling.m"; sourceTree = "<group>"; };
4A0D74F918F6106100966D7B /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
7E0B1132203572FB00271AD3 /* fi */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = fi; path = Localizations/fi.lproj/Localizable.strings; sourceTree = "<group>"; };
8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPPaymentConfigurationTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1500,12 +1504,14 @@
04CDB5281A5F3A9300B854EE /* StripeTests */ = {
isa = PBXGroup;
children = (
C18867D61E8B069E00A77634 /* Snapshot */,
C18867D71E8B07F600A77634 /* Functional */,
C18867D81E8B093300A77634 /* Unit */,
C1D23FB71D37FE0F002FD83C /* JSON */,
F1B8534D1FDF544B0065A49E /* FBSnapshotTestCase+STPViewControllerLoading.h */,
F1B8534E1FDF544B0065A49E /* FBSnapshotTestCase+STPViewControllerLoading.m */,
C18867D71E8B07F600A77634 /* Functional */,
C1D23FB71D37FE0F002FD83C /* JSON */,
3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */,
3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */,
C18867D61E8B069E00A77634 /* Snapshot */,
C1CFCB781ED5F85A00BE45DF /* stp_test_upload_image.jpeg */,
C18867D91E8B0C4100A77634 /* STPFixtures.h */,
C18867DA1E8B0C4100A77634 /* STPFixtures.m */,
F1D96F981DC7DCDE00477E64 /* STPLocalizationUtils+STPTestAdditions.h */,
Expand All @@ -1514,7 +1520,7 @@
C1CFCB6A1ED5E0F400BE45DF /* STPMocks.m */,
C1D23FAF1D37FC90002FD83C /* STPTestUtils.h */,
C1D23FB01D37FC90002FD83C /* STPTestUtils.m */,
C1CFCB781ED5F85A00BE45DF /* stp_test_upload_image.jpeg */,
C18867D81E8B093300A77634 /* Unit */,
);
name = StripeTests;
path = Tests/Tests;
Expand Down Expand Up @@ -2130,6 +2136,7 @@
F148ABFA1D5E88C40014FD92 /* STPTestUtils.h in Headers */,
C1CFCB6D1ED5E0F800BE45DF /* STPMocks.h in Headers */,
C18867DB1E8B0C4100A77634 /* STPFixtures.h in Headers */,
3617A51420FE5BBB001A9E6A /* NSLocale+STPSwizzling.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2833,6 +2840,7 @@
F148ABFB1D5E88C70014FD92 /* STPTestUtils.m in Sources */,
8BB97F081F26645B0095122A /* NSDictionary+StripeTest.m in Sources */,
045A62AB1B8E7259000165CE /* STPPaymentCardTextFieldTest.m in Sources */,
3617A51520FE5BBB001A9E6A /* NSLocale+STPSwizzling.m in Sources */,
C1054F911FE197AE0033C87E /* STPPaymentContextSnapshotTests.m in Sources */,
C127110A1DBA7E490087840D /* STPAddressViewModelTest.m in Sources */,
C17D24EE1E37DBAC005CB188 /* STPSourceTest.m in Sources */,
Expand Down
24 changes: 15 additions & 9 deletions Stripe/STPAddCardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)];

Expand All @@ -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");
Expand Down Expand Up @@ -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]) {
Copy link
Contributor

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 track showingPostalCodeCell, 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?

Copy link
Contributor Author

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?

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 :) ).

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?

I thought about this. I believe that change would also fix the crash, but this solution is more robust

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important that the call to updateInputAccessoryVisibility is inside the if block for add, but outside it for remove?

I don't think so, but wanted to see if there was something I'm missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 :)

}

Expand Down
16 changes: 16 additions & 0 deletions Tests/Tests/NSLocale+STPSwizzling.h
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
59 changes: 59 additions & 0 deletions Tests/Tests/NSLocale+STPSwizzling.m
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider using the STPAspects code instead?

This is a really simple usage, and since it's test code I don't think it needs to get fancy, mostly just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
48 changes: 48 additions & 0 deletions Tests/Tests/STPAddCardViewControllerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -41,6 +42,53 @@ - (STPAddCardViewController *)buildAddCardViewController {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

- (void)testPrefilledBillingAddress_removeAddress {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these need to be inside the #pragma clang diagnostic ignored "-Warc-performSelector-leaks" block? It might be slightly better to move them outside.

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];
Expand Down