Skip to content

Commit

Permalink
Fixed PFAlertView cancel button handling logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlutsenko committed Aug 20, 2015
1 parent f5d1279 commit 89420cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions Parse/Internal/PFAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ + (void)showAlertWithTitle:(NSString *)title

void (^alertActionHandler)(UIAlertAction *) = [^(UIAlertAction *action){
// This block intentionally retains alertController, and releases it afterwards.
NSUInteger index = [alertController.actions indexOfObject:action];
completion(index - 1);
if (action.style == UIAlertActionStyleCancel) {
completion(NSNotFound);
} else {
NSUInteger index = [alertController.actions indexOfObject:action];
completion(index - 1);
}
alertController = nil;
} copy];

Expand Down Expand Up @@ -85,7 +89,11 @@ + (void)showAlertWithTitle:(NSString *)title

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (self.completion) {
self.completion(buttonIndex - alertView.firstOtherButtonIndex);
if (buttonIndex == alertView.cancelButtonIndex) {
self.completion(NSNotFound);
} else {
self.completion(buttonIndex - 1);
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions Tests/Unit/AlertViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ - (void)testShowAlertWithAlertViewController {
id mockedApplication = PFStrictClassMock([UIApplication class]);
UIWindow *mockedWindow = PFStrictClassMock([UIWindow class]);
UIViewController *mockedViewController = PFStrictClassMock([UIViewController class]);
OCMStub(mockedViewController.presentedViewController).andReturn(nil);

// Using .andReturn() here will result in a retain cycle, which will cause our mocked shared application to
// persist across tests.
Expand Down Expand Up @@ -102,7 +103,7 @@ - (void)testShowAlertWithAlertViewController {
cancelButtonTitle:@"Cancel"
otherButtonTitles:@[ @"Yes", @"No" ]
completion:^(NSUInteger selectedOtherButtonIndex) {
XCTAssertEqual(selectedOtherButtonIndex, -1);
XCTAssertEqual(selectedOtherButtonIndex, NSNotFound);

[expectation fulfill];
}];
Expand Down Expand Up @@ -147,15 +148,15 @@ - (void)testShowWithoutAlertViewController {
[delegate alertView:self clickedButtonAtIndex:0];
});

OCMStub([mockedAlertView firstOtherButtonIndex]).andReturn(1);
OCMStub([mockedAlertView cancelButtonIndex]).andReturn(0);

XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[PFAlertView showAlertWithTitle:@"Title"
message:@"Message"
cancelButtonTitle:@"Cancel"
otherButtonTitles:@[ @"Yes", @"No" ]
completion:^(NSUInteger selectedOtherButtonIndex) {
XCTAssertEqual(selectedOtherButtonIndex, -1);
XCTAssertEqual(selectedOtherButtonIndex, NSNotFound);

[expectation fulfill];
}];
Expand Down

0 comments on commit 89420cf

Please sign in to comment.