Skip to content

Add FirAuthUrlPresenter #222

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

Merged
merged 16 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions AuthSamples/Samples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
52C975C71EE10B1304EBBEB2 /* Pods_SwiftSample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC8C39EF1F42A0C750FF5186 /* Pods_SwiftSample.framework */; };
569C3F4E18627674CABE02AE /* Pods_EarlGreyTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AEE2E563FADF8C3382956B4F /* Pods_EarlGreyTests.framework */; };
67AFFB52FF0FC4668D92F2E4 /* Pods_Sample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D5FE06BD9AA795DFBA9EFAAD /* Pods_Sample.framework */; };
7E04ACA41F565EA900788114 /* FIRAuthURLPresenterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E04ACA31F565EA900788114 /* FIRAuthURLPresenterTests.m */; };
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks.

7E9969EE1F4E277900627C2B /* FIRGetProjectConfigRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E9969EC1F4E277900627C2B /* FIRGetProjectConfigRequestTests.m */; };
7EDFD35B1F0EA29200B29DC5 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 7EDFD35D1F0EA29200B29DC5 /* Localizable.strings */; };
7EEEFEE61F4E4F75000FF966 /* FIRGetProjectConfigResponseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EEA8ADD1F4E4E840014A23B /* FIRGetProjectConfigResponseTests.m */; };
Expand Down Expand Up @@ -183,6 +184,7 @@
4FFAD3F37BC4D7CEF0CAD579 /* Pods_FirebaseAuthUnitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_FirebaseAuthUnitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
57150555A6B03949ECB58AD9 /* Pods-TestApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TestApp.debug.xcconfig"; path = "Pods/Target Support Files/Pods-TestApp/Pods-TestApp.debug.xcconfig"; sourceTree = "<group>"; };
6EC09307D636721EAAB89BB2 /* Pods_ApiTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ApiTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7E04ACA31F565EA900788114 /* FIRAuthURLPresenterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIRAuthURLPresenterTests.m; path = ../../Example/Auth/Tests/FIRAuthURLPresenterTests.m; sourceTree = "<group>"; };
7E0BC64A1F199D86008BE4E0 /* fr-FR */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "fr-FR"; path = "fr-FR.lproj/Localizable.strings"; sourceTree = "<group>"; };
7E0BC64E1F19A77C008BE4E0 /* ru-RU */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "ru-RU"; path = "ru-RU.lproj/Localizable.strings"; sourceTree = "<group>"; };
7E9969EC1F4E277900627C2B /* FIRGetProjectConfigRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FIRGetProjectConfigRequestTests.m; path = ../../Example/Auth/Tests/FIRGetProjectConfigRequestTests.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -557,6 +559,7 @@
DE5371B21EA7E89D000DA57F /* Tests-Info.plist */,
7E9969EC1F4E277900627C2B /* FIRGetProjectConfigRequestTests.m */,
7EEA8ADD1F4E4E840014A23B /* FIRGetProjectConfigResponseTests.m */,
7E04ACA31F565EA900788114 /* FIRAuthURLPresenterTests.m */,
);
path = UnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1332,6 +1335,7 @@
DECEA56E1EBBED1200273585 /* FIRAuthAppCredentialManagerTests.m in Sources */,
DE5371DE1EA7E89D000DA57F /* OCMStubRecorder+FIRAuthUnitTests.m in Sources */,
DE5371DC1EA7E89D000DA57F /* FIRVerifyPhoneNumberRequestTests.m in Sources */,
7E04ACA41F565EA900788114 /* FIRAuthURLPresenterTests.m in Sources */,
DE5371B51EA7E89D000DA57F /* FIRAuthAppCredentialTests.m in Sources */,
DE5371CE1EA7E89D000DA57F /* FIRSetAccountInfoRequestTests.m in Sources */,
DE5371D41EA7E89D000DA57F /* FIRVerifyAssertionRequestTests.m in Sources */,
Expand Down
183 changes: 183 additions & 0 deletions Example/Auth/Tests/FIRAuthURLPresenterTests.m
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
2 changes: 1 addition & 1 deletion Example/Auth/Tests/FIRGetProjectConfigRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (void)testGetProjectConfigRequest {
gAPIHost,
kGetProjectConfigEndPoint,
kTestAPIKey];
XCTAssertTrue([URLString isEqualToString:[request requestURL]]);
XCTAssertEqualObjects(URLString, [request requestURL].absoluteString);
}

@end
Loading