-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add FirAuthUrlPresenter #222
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
378ab15
Adds FIRAuthURLPResenter and FIRAuthUIDelegate
protocol86 a466672
Introduces default UIDelegate
protocol86 b43d0cd
Update FirebaseCommunity to 0.1.1
paulb777 0f70bf0
removes line break
protocol86 e00eaa8
Encode the topic name when subscribing (#220)
rsattar 2816840
Merge pull request #223 from firebase/release-4.1.1
paulb777 1401575
Changes based on comments
protocol86 0ce4297
Adds missing unit tests to the Example project and fixes one test cas…
XiangtianDai baa139b
Addresses comments
protocol86 852500a
Adds FIRAuthURLPResenter and FIRAuthUIDelegate
protocol86 f950b57
Introduces default UIDelegate
protocol86 c111134
removes line break
protocol86 445512f
Changes based on comments
protocol86 d53e6eb
Addresses comments
protocol86 f701726
Addresses Comments
protocol86 2785125
Merge branch 'Add_FIRAuthURLPresenter' of https://github.com/firebase…
protocol86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright 2017 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <SafariServices/SafariServices.h> | ||
#import <XCTest/XCTest.h> | ||
|
||
#import "FIRAuthURLPresenter.h" | ||
#import "FIRAuthUIDelegate.h" | ||
#import <OCMock/OCMock.h> | ||
|
||
/** @var kExpectationTimeout | ||
@brief The maximum time waiting for expectations to fulfill. | ||
*/ | ||
static NSTimeInterval kExpectationTimeout = 1; | ||
|
||
@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate> | ||
/** @fn defaultUIDelegate | ||
@brief Returns a default FIRAuthUIDelegate object. | ||
@return The default FIRAuthUIDelegate object. | ||
*/ | ||
+ (id<FIRAuthUIDelegate>)defaultUIDelegate; | ||
@end | ||
|
||
@interface FIRAuthURLPresenterTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation FIRAuthURLPresenterTests | ||
|
||
/** @fn testFIRAuthURLPresenterNonNilUIDelegate | ||
@brief Tests @c FIRAuthURLPresenter class showing UI with a non-nil UIDelegate. | ||
*/ | ||
- (void)testFIRAuthURLPresenterNonNilUIDelegate { | ||
id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate)); | ||
NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"]; | ||
FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init]; | ||
|
||
if ([SFSafariViewController class]) { | ||
XCTestExpectation *callbackMatcherExpectation = | ||
[self expectationWithDescription:@"callbackMatcher callback"]; | ||
FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) { | ||
XCTAssertEqualObjects(callbackURL, presenterURL); | ||
[callbackMatcherExpectation fulfill]; | ||
return YES; | ||
}; | ||
|
||
XCTestExpectation *completionBlockExpectation = | ||
[self expectationWithDescription:@"completion callback"]; | ||
FIRAuthURLPresentationCompletion completionBlock = ^(NSURL *_Nullable callbackURL, | ||
NSError *_Nullable error) { | ||
XCTAssertEqualObjects(callbackURL, presenterURL); | ||
XCTAssertNil(error); | ||
[completionBlockExpectation fulfill]; | ||
}; | ||
|
||
id presenterArg = [OCMArg isKindOfClass:[SFSafariViewController class]]; | ||
OCMExpect([mockUIDelegate presentViewController:presenterArg | ||
animated:YES | ||
completion:nil]).andDo(^(NSInvocation *invocation) { | ||
__unsafe_unretained id unretainedArgument; | ||
// Indices 0 and 1 indicate the hidden arguments self and _cmd. | ||
// `presentViewController` is at index 2. | ||
[invocation getArgument:&unretainedArgument atIndex:2]; | ||
|
||
SFSafariViewController *viewController = unretainedArgument; | ||
XCTAssertEqual(viewController.delegate, presenter); | ||
XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]); | ||
}); | ||
[presenter presentURL:presenterURL | ||
UIDelegate:mockUIDelegate | ||
callbackMatcher:callbackMatcher | ||
completion:completionBlock]; | ||
OCMVerifyAll(mockUIDelegate); | ||
OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY | ||
completion:OCMOCK_ANY]). | ||
andDo(^(NSInvocation *invocation) { | ||
__unsafe_unretained id unretainedArgument; | ||
// Indices 0 and 1 indicate the hidden arguments self and _cmd. | ||
// `completion` is at index 3. | ||
[invocation getArgument:&unretainedArgument atIndex:3]; | ||
void (^finishBlock)() = unretainedArgument; | ||
finishBlock(); | ||
}); | ||
[presenter canHandleURL:presenterURL]; | ||
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; | ||
OCMVerifyAll(mockUIDelegate); | ||
} | ||
} | ||
|
||
/** @fn testFIRAuthURLPresenterNilUIDelegate | ||
@brief Tests @c FIRAuthURLPresenter class showing UI with a nil UIDelegate. | ||
*/ | ||
- (void)testFIRAuthURLPresenterNilUIDelegate { | ||
NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"]; | ||
FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init]; | ||
|
||
if ([SFSafariViewController class]) { | ||
XCTestExpectation *callbackMatcherExpectation = | ||
[self expectationWithDescription:@"callbackMatcher callback"]; | ||
FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) { | ||
XCTAssertEqualObjects(callbackURL, presenterURL); | ||
[callbackMatcherExpectation fulfill]; | ||
return YES; | ||
}; | ||
XCTestExpectation *completionBlockExpectation = | ||
[self expectationWithDescription:@"completion callback"]; | ||
FIRAuthURLPresentationCompletion completionBlock = ^(NSURL *_Nullable callbackURL, | ||
NSError *_Nullable error) { | ||
XCTAssertEqualObjects(callbackURL, presenterURL); | ||
XCTAssertNil(error); | ||
[completionBlockExpectation fulfill]; | ||
}; | ||
|
||
id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate)); | ||
|
||
// Swizzle default UIDelegate | ||
Method method = class_getClassMethod([FIRAuthDefaultUIDelegate class], | ||
@selector(defaultUIDelegate)); | ||
__block IMP originalImplementation; | ||
IMP newImplmentation = imp_implementationWithBlock(^id(id object) { | ||
return mockUIDelegate; | ||
}); | ||
originalImplementation = method_setImplementation(method, newImplmentation); | ||
if ([SFSafariViewController class]) { | ||
id presenterArg = [OCMArg isKindOfClass:[SFSafariViewController class]]; | ||
OCMExpect([mockUIDelegate presentViewController:presenterArg | ||
animated:[OCMArg any] | ||
completion:[OCMArg any]]). | ||
andDo(^(NSInvocation *invocation) { | ||
__unsafe_unretained id unretainedArgument; | ||
// Indices 0 and 1 indicate the hidden arguments self and _cmd. | ||
// `presentViewController` is at index 2. | ||
[invocation getArgument:&unretainedArgument atIndex:2]; | ||
SFSafariViewController *viewController = unretainedArgument; | ||
XCTAssertEqual(viewController.delegate, presenter); | ||
XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]); | ||
}); | ||
} | ||
[presenter presentURL:presenterURL | ||
UIDelegate:nil | ||
callbackMatcher:callbackMatcher | ||
completion:completionBlock]; | ||
OCMVerifyAll(mockUIDelegate); | ||
|
||
OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY | ||
completion:OCMOCK_ANY]). | ||
andDo(^(NSInvocation *invocation) { | ||
__unsafe_unretained id unretainedArgument; | ||
// Indices 0 and 1 indicate the hidden arguments self and _cmd. | ||
// `completion` is at index 3. | ||
[invocation getArgument:&unretainedArgument atIndex:3]; | ||
void (^finishBlock)() = unretainedArgument; | ||
finishBlock(); | ||
}); | ||
[presenter canHandleURL:presenterURL]; | ||
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; | ||
OCMVerifyAll(mockUIDelegate); | ||
// Unswizzle. | ||
imp_removeBlock(method_setImplementation(method, originalImplementation)); | ||
} | ||
} | ||
|
||
#pragma mark - Method Swizzling | ||
|
||
+ (id)mockDefaultUIDelegate { | ||
return OCMProtocolMock(@protocol(FIRAuthUIDelegate)); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
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. Please add this file to the macOS exclusion in Auth's podspec so it is not compiled for macOS. 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. Done |
||
* Copyright 2017 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "FIRAuthUIDelegate.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate> | ||
/** @fn defaultUIDelegate | ||
@brief Unavailable. Please use initWithViewController: | ||
*/ | ||
- (instancetype)init NS_UNAVAILABLE; | ||
|
||
/** @fn initWithViewController: | ||
@brief Initializes the instance with a view controller. | ||
@param viewController The view controller as the presenting view controller in @c GOIUIDelegate. | ||
@return The initialized instance. | ||
*/ | ||
- (instancetype)initWithViewController:(UIViewController *)viewController NS_DESIGNATED_INITIALIZER; | ||
@end | ||
|
||
@implementation FIRAuthDefaultUIDelegate { | ||
/** @var _viewController | ||
@brief The presenting view controller. | ||
*/ | ||
UIViewController *_viewController; | ||
} | ||
|
||
- (instancetype)initWithViewController:(UIViewController *)viewController { | ||
self = [super init]; | ||
if (self) { | ||
_viewController = viewController; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)presentViewController:(UIViewController *)viewControllerToPresent | ||
animated:(BOOL)flag | ||
completion:(nullable void (^)(void))completion { | ||
[_viewController presentViewController:viewControllerToPresent | ||
animated:flag | ||
completion:completion]; | ||
} | ||
|
||
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion { | ||
[_viewController dismissViewControllerAnimated:flag completion:completion]; | ||
} | ||
|
||
+ (id<FIRAuthUIDelegate>)defaultUIDelegate { | ||
UIViewController *topViewController = | ||
[UIApplication sharedApplication].keyWindow.rootViewController; | ||
while (true){ | ||
if (topViewController.presentedViewController) { | ||
topViewController = topViewController.presentedViewController; | ||
} else if ([topViewController isKindOfClass:[UINavigationController class]]) { | ||
UINavigationController *nav = (UINavigationController *)topViewController; | ||
topViewController = nav.topViewController; | ||
} else if ([topViewController isKindOfClass:[UITabBarController class]]) { | ||
UITabBarController *tab = (UITabBarController *)topViewController; | ||
topViewController = tab.selectedViewController; | ||
} else { | ||
break; | ||
} | ||
} | ||
return [[FIRAuthDefaultUIDelegate alloc] initWithViewController:topViewController]; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please also update Example/Firebase.xcodeproj/project.pbxproj. And please be care not to break macOS build/test, because the new class only works on iOS.
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.
done, thanks.