|
| 1 | +/* |
| 2 | + * Copyright 2017 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import <Foundation/Foundation.h> |
| 18 | +#import <SafariServices/SafariServices.h> |
| 19 | +#import <XCTest/XCTest.h> |
| 20 | + |
| 21 | +#import "FIRAuthURLPresenter.h" |
| 22 | +#import "FIRAuthUIDelegate.h" |
| 23 | +#import <OCMock/OCMock.h> |
| 24 | + |
| 25 | +/** @var kExpectationTimeout |
| 26 | + @brief The maximum time waiting for expectations to fulfill. |
| 27 | + */ |
| 28 | +static NSTimeInterval kExpectationTimeout = 1; |
| 29 | + |
| 30 | +@interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate> |
| 31 | +/** @fn defaultUIDelegate |
| 32 | + @brief Returns a default FIRAuthUIDelegate object. |
| 33 | + @return The default FIRAuthUIDelegate object. |
| 34 | + */ |
| 35 | ++ (id<FIRAuthUIDelegate>)defaultUIDelegate; |
| 36 | +@end |
| 37 | + |
| 38 | +@interface FIRAuthURLPresenterTests : XCTestCase |
| 39 | + |
| 40 | +@end |
| 41 | + |
| 42 | +@implementation FIRAuthURLPresenterTests |
| 43 | + |
| 44 | +/** @fn testFIRAuthURLPresenterNonNilUIDelegate |
| 45 | + @brief Tests @c FIRAuthURLPresenter class showing UI with a non-nil UIDelegate. |
| 46 | + */ |
| 47 | +- (void)testFIRAuthURLPresenterNonNilUIDelegate { |
| 48 | + id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate)); |
| 49 | + NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"]; |
| 50 | + FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init]; |
| 51 | + |
| 52 | + if ([SFSafariViewController class]) { |
| 53 | + XCTestExpectation *callbackMatcherExpectation = |
| 54 | + [self expectationWithDescription:@"callbackMatcher callback"]; |
| 55 | + FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) { |
| 56 | + XCTAssertEqualObjects(callbackURL, presenterURL); |
| 57 | + [callbackMatcherExpectation fulfill]; |
| 58 | + return YES; |
| 59 | + }; |
| 60 | + |
| 61 | + XCTestExpectation *completionBlockExpectation = |
| 62 | + [self expectationWithDescription:@"completion callback"]; |
| 63 | + FIRAuthURLPresentationCompletion completionBlock = ^(NSURL *_Nullable callbackURL, |
| 64 | + NSError *_Nullable error) { |
| 65 | + XCTAssertEqualObjects(callbackURL, presenterURL); |
| 66 | + XCTAssertNil(error); |
| 67 | + [completionBlockExpectation fulfill]; |
| 68 | + }; |
| 69 | + |
| 70 | + id presenterArg = [OCMArg isKindOfClass:[SFSafariViewController class]]; |
| 71 | + OCMExpect([mockUIDelegate presentViewController:presenterArg |
| 72 | + animated:YES |
| 73 | + completion:nil]).andDo(^(NSInvocation *invocation) { |
| 74 | + __unsafe_unretained id unretainedArgument; |
| 75 | + // Indices 0 and 1 indicate the hidden arguments self and _cmd. |
| 76 | + // `presentViewController` is at index 2. |
| 77 | + [invocation getArgument:&unretainedArgument atIndex:2]; |
| 78 | + |
| 79 | + SFSafariViewController *viewController = unretainedArgument; |
| 80 | + XCTAssertEqual(viewController.delegate, presenter); |
| 81 | + XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]); |
| 82 | + }); |
| 83 | + [presenter presentURL:presenterURL |
| 84 | + UIDelegate:mockUIDelegate |
| 85 | + callbackMatcher:callbackMatcher |
| 86 | + completion:completionBlock]; |
| 87 | + OCMVerifyAll(mockUIDelegate); |
| 88 | + OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY |
| 89 | + completion:OCMOCK_ANY]). |
| 90 | + andDo(^(NSInvocation *invocation) { |
| 91 | + __unsafe_unretained id unretainedArgument; |
| 92 | + // Indices 0 and 1 indicate the hidden arguments self and _cmd. |
| 93 | + // `completion` is at index 3. |
| 94 | + [invocation getArgument:&unretainedArgument atIndex:3]; |
| 95 | + void (^finishBlock)() = unretainedArgument; |
| 96 | + finishBlock(); |
| 97 | + }); |
| 98 | + [presenter canHandleURL:presenterURL]; |
| 99 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 100 | + OCMVerifyAll(mockUIDelegate); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** @fn testFIRAuthURLPresenterNilUIDelegate |
| 105 | + @brief Tests @c FIRAuthURLPresenter class showing UI with a nil UIDelegate. |
| 106 | + */ |
| 107 | +- (void)testFIRAuthURLPresenterNilUIDelegate { |
| 108 | + NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"]; |
| 109 | + FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init]; |
| 110 | + |
| 111 | + if ([SFSafariViewController class]) { |
| 112 | + XCTestExpectation *callbackMatcherExpectation = |
| 113 | + [self expectationWithDescription:@"callbackMatcher callback"]; |
| 114 | + FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) { |
| 115 | + XCTAssertEqualObjects(callbackURL, presenterURL); |
| 116 | + [callbackMatcherExpectation fulfill]; |
| 117 | + return YES; |
| 118 | + }; |
| 119 | + XCTestExpectation *completionBlockExpectation = |
| 120 | + [self expectationWithDescription:@"completion callback"]; |
| 121 | + FIRAuthURLPresentationCompletion completionBlock = ^(NSURL *_Nullable callbackURL, |
| 122 | + NSError *_Nullable error) { |
| 123 | + XCTAssertEqualObjects(callbackURL, presenterURL); |
| 124 | + XCTAssertNil(error); |
| 125 | + [completionBlockExpectation fulfill]; |
| 126 | + }; |
| 127 | + |
| 128 | + id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate)); |
| 129 | + |
| 130 | + // Swizzle default UIDelegate |
| 131 | + Method method = class_getClassMethod([FIRAuthDefaultUIDelegate class], |
| 132 | + @selector(defaultUIDelegate)); |
| 133 | + __block IMP originalImplementation; |
| 134 | + IMP newImplmentation = imp_implementationWithBlock(^id(id object) { |
| 135 | + return mockUIDelegate; |
| 136 | + }); |
| 137 | + originalImplementation = method_setImplementation(method, newImplmentation); |
| 138 | + if ([SFSafariViewController class]) { |
| 139 | + id presenterArg = [OCMArg isKindOfClass:[SFSafariViewController class]]; |
| 140 | + OCMExpect([mockUIDelegate presentViewController:presenterArg |
| 141 | + animated:[OCMArg any] |
| 142 | + completion:[OCMArg any]]). |
| 143 | + andDo(^(NSInvocation *invocation) { |
| 144 | + __unsafe_unretained id unretainedArgument; |
| 145 | + // Indices 0 and 1 indicate the hidden arguments self and _cmd. |
| 146 | + // `presentViewController` is at index 2. |
| 147 | + [invocation getArgument:&unretainedArgument atIndex:2]; |
| 148 | + SFSafariViewController *viewController = unretainedArgument; |
| 149 | + XCTAssertEqual(viewController.delegate, presenter); |
| 150 | + XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]); |
| 151 | + }); |
| 152 | + } |
| 153 | + [presenter presentURL:presenterURL |
| 154 | + UIDelegate:nil |
| 155 | + callbackMatcher:callbackMatcher |
| 156 | + completion:completionBlock]; |
| 157 | + OCMVerifyAll(mockUIDelegate); |
| 158 | + |
| 159 | + OCMExpect([mockUIDelegate dismissViewControllerAnimated:OCMOCK_ANY |
| 160 | + completion:OCMOCK_ANY]). |
| 161 | + andDo(^(NSInvocation *invocation) { |
| 162 | + __unsafe_unretained id unretainedArgument; |
| 163 | + // Indices 0 and 1 indicate the hidden arguments self and _cmd. |
| 164 | + // `completion` is at index 3. |
| 165 | + [invocation getArgument:&unretainedArgument atIndex:3]; |
| 166 | + void (^finishBlock)() = unretainedArgument; |
| 167 | + finishBlock(); |
| 168 | + }); |
| 169 | + [presenter canHandleURL:presenterURL]; |
| 170 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 171 | + OCMVerifyAll(mockUIDelegate); |
| 172 | + // Unswizzle. |
| 173 | + imp_removeBlock(method_setImplementation(method, originalImplementation)); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +#pragma mark - Method Swizzling |
| 178 | + |
| 179 | ++ (id)mockDefaultUIDelegate { |
| 180 | + return OCMProtocolMock(@protocol(FIRAuthUIDelegate)); |
| 181 | +} |
| 182 | + |
| 183 | +@end |
0 commit comments