Skip to content

Commit

Permalink
Set webView.delegate to nil upon dealloc. Add some comments. Make uni…
Browse files Browse the repository at this point in the history
…t tests pass.
  • Loading branch information
mcglincy committed Jul 8, 2012
1 parent ac55f61 commit 1450e0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
43 changes: 34 additions & 9 deletions UnitTests/EvernoteSessionTests.m
Expand Up @@ -17,18 +17,21 @@ @interface EvernoteSessionTests() {
} }


@property (nonatomic, retain) id mockSession; @property (nonatomic, retain) id mockSession;
@property (nonatomic, retain) id mockViewController;
@property (nonatomic, retain) NSURLConnection *dummyURLConnection; @property (nonatomic, retain) NSURLConnection *dummyURLConnection;


@end @end


@implementation EvernoteSessionTests @implementation EvernoteSessionTests


@synthesize mockSession = _mockSession; @synthesize mockSession = _mockSession;
@synthesize mockViewController = _mockViewController;
@synthesize dummyURLConnection = _dummyURLConnection; @synthesize dummyURLConnection = _dummyURLConnection;


- (void)dealloc - (void)dealloc
{ {
[_mockSession release]; [_mockSession release];
[_mockViewController release];
[_dummyURLConnection release]; [_dummyURLConnection release];
[super dealloc]; [super dealloc];
} }
Expand All @@ -50,6 +53,8 @@ - (void)setUp
// mock out our verification methods, since we don't care // mock out our verification methods, since we don't care
[[self.mockSession stub] verifyConsumerKeyAndSecret]; [[self.mockSession stub] verifyConsumerKeyAndSecret];


self.mockViewController = [OCMockObject mockForClass:[UIViewController class]];

// mock out our connection-making method to return a dummy // mock out our connection-making method to return a dummy
self.dummyURLConnection = [[[NSURLConnection alloc] self.dummyURLConnection = [[[NSURLConnection alloc]
initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:foo"]] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"file:foo"]]
Expand Down Expand Up @@ -82,16 +87,30 @@ - (void)testAuthenticationVerifyMethods
[session authenticateWithViewController:nil completionHandler:^(NSError *error) {}]; [session authenticateWithViewController:nil completionHandler:^(NSError *error) {}];
} }


// Make sure a nil NSURLConnection causes a proper error calback. // Make sure a nil UIViewController causes a proper error callback.
- (void)testNilConnection - (void)testNilViewController
{ {
[[[self.mockSession stub] andReturn:nil] connectionWithRequest:[OCMArg any]];

[self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) { [self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) {
authenticationCompleted = YES; authenticationCompleted = YES;
authenticationError = error; authenticationError = error;
}]; }];


STAssertTrue(authenticationCompleted, nil);
STAssertNotNil(authenticationError, nil);
STAssertEqualObjects(authenticationError.domain, EvernoteSDKErrorDomain, nil);
STAssertEquals(authenticationError.code, EvernoteSDKErrorCode_NO_VIEWCONTROLLER, nil);
}

// Make sure a nil NSURLConnection causes a proper error callback.
- (void)testNilConnection
{
[[[self.mockSession stub] andReturn:nil] connectionWithRequest:[OCMArg any]];

[self.mockSession authenticateWithViewController:self.mockViewController completionHandler:^(NSError *error) {
authenticationCompleted = YES;
authenticationError = error;
}];

STAssertTrue(authenticationCompleted, nil); STAssertTrue(authenticationCompleted, nil);
STAssertNotNil(authenticationError, nil); STAssertNotNil(authenticationError, nil);
STAssertEqualObjects(authenticationError.domain, EvernoteSDKErrorDomain, nil); STAssertEqualObjects(authenticationError.domain, EvernoteSDKErrorDomain, nil);
Expand All @@ -103,7 +122,7 @@ - (void)testURLConnectionDidFailWithError
{ {
[[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]]; [[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]];


[self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) { [self.mockSession authenticateWithViewController:self.mockViewController completionHandler:^(NSError *error) {
authenticationCompleted = YES; authenticationCompleted = YES;
authenticationError = error; authenticationError = error;
}]; }];
Expand All @@ -119,7 +138,7 @@ - (void)testURLConnectionNon200
{ {
[[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]]; [[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]];


[self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) { [self.mockSession authenticateWithViewController:self.mockViewController completionHandler:^(NSError *error) {
authenticationCompleted = YES; authenticationCompleted = YES;
authenticationError = error; authenticationError = error;
}]; }];
Expand Down Expand Up @@ -149,7 +168,7 @@ - (void)testBadTempTokenResponse
{ {
[[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]]; [[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]];


[self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) { [self.mockSession authenticateWithViewController:self.mockViewController completionHandler:^(NSError *error) {
authenticationCompleted = YES; authenticationCompleted = YES;
authenticationError = error; authenticationError = error;
}]; }];
Expand Down Expand Up @@ -179,7 +198,7 @@ - (void)testSuccessfulAuthentication
{ {
[[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]]; [[[self.mockSession stub] andReturn:self.dummyURLConnection] connectionWithRequest:[OCMArg any]];


[self.mockSession authenticateWithViewController:nil completionHandler:^(NSError *error) { [self.mockSession authenticateWithViewController:self.mockViewController completionHandler:^(NSError *error) {
authenticationCompleted = YES; authenticationCompleted = YES;
authenticationError = error; authenticationError = error;
}]; }];
Expand All @@ -206,12 +225,17 @@ - (void)testSuccessfulAuthentication
STAssertFalse(authenticationCompleted, nil); STAssertFalse(authenticationCompleted, nil);
STAssertNil(authenticationError, nil); STAssertNil(authenticationError, nil);
[self.mockSession verify]; [self.mockSession verify];

// successful authentication will dismiss the modal popup
[[self.mockViewController expect] dismissModalViewControllerAnimated:YES];


NSString *urlString = @"en-dummyaccount-1234://response?action=oauthCallback&oauth_token=en_oauth_test.12BF88D95B9.687474703A2F2F6C6F63616C686F73742F7E736574682F4544414D576562546573742F696E6465782E7068703F616374696F6E3D63616C6C6261636B.AEDE24F1FAFD67D267E78D27D14F01D3&oauth_verifier=0D6A636CD623302F8D69DBB8DF76D86E"; NSString *urlString = @"en-dummyaccount-1234://response?action=oauthCallback&oauth_token=en_oauth_test.12BF88D95B9.687474703A2F2F6C6F63616C686F73742F7E736574682F4544414D576562546573742F696E6465782E7068703F616374696F6E3D63616C6C6261636B.AEDE24F1FAFD67D267E78D27D14F01D3&oauth_verifier=0D6A636CD623302F8D69DBB8DF76D86E";
[self.mockSession oauthViewController:nil receivedOAuthCallbackURL:[NSURL URLWithString:urlString]]; [self.mockSession oauthViewController:nil receivedOAuthCallbackURL:[NSURL URLWithString:urlString]];


// now we can poke the NSURLConnectionDelegate methods again, for the 4th step of OAuth. [self.mockViewController verify];


// now we can poke the NSURLConnectionDelegate methods again, for the 4th step of OAuth.

// connection:didReceiveResponse: // connection:didReceiveResponse:
[self.mockSession connection:self.dummyURLConnection didReceiveResponse:responseMock]; [self.mockSession connection:self.dummyURLConnection didReceiveResponse:responseMock];
STAssertFalse(authenticationCompleted, nil); STAssertFalse(authenticationCompleted, nil);
Expand All @@ -232,6 +256,7 @@ - (void)testSuccessfulAuthentication
authenticationToken:@"sometokenvalue"]; authenticationToken:@"sometokenvalue"];
[self.mockSession connectionDidFinishLoading:self.dummyURLConnection]; [self.mockSession connectionDidFinishLoading:self.dummyURLConnection];
[self.mockSession verify]; [self.mockSession verify];

// and make sure our callback happened, without error. // and make sure our callback happened, without error.
STAssertTrue(authenticationCompleted, nil); STAssertTrue(authenticationCompleted, nil);
STAssertNil(authenticationError, nil); STAssertNil(authenticationError, nil);
Expand Down
7 changes: 4 additions & 3 deletions evernote-sdk-ios/internal/ENOAuthViewController.m
Expand Up @@ -26,10 +26,11 @@ @implementation ENOAuthViewController
- (void)dealloc - (void)dealloc
{ {
self.delegate = nil; self.delegate = nil;
self.webView.delegate = nil;
[self.webView stopLoading]; [self.webView stopLoading];
[_webView release];
[_authorizationURL release]; [_authorizationURL release];
[_oauthCallbackPrefix release]; [_oauthCallbackPrefix release];
[_webView release];
[super dealloc]; [super dealloc];
} }


Expand All @@ -55,7 +56,7 @@ - (void)viewDidLoad
target:self action:@selector(cancel:)]; target:self action:@selector(cancel:)];
self.navigationItem.rightBarButtonItem = cancelItem; self.navigationItem.rightBarButtonItem = cancelItem;


// Using self.view.frame leaves a 20px black space above the webview... from status bar spacing? // TODO: Using self.view.frame leaves a 20px black space above the webview... from status bar spacing?
//self.webView = [[UIWebView alloc] initWithFrame:self.view.frame]; //self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Expand Down Expand Up @@ -89,7 +90,7 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
} }


if (error.code == NSURLErrorCancelled) { if (error.code == NSURLErrorCancelled) {
// ignore rapid repeated clicking // ignore rapid repeated clicking (error code -999)
return; return;
} }


Expand Down

0 comments on commit 1450e0c

Please sign in to comment.