FIRAuthURLPresenterTests.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <Foundation/Foundation.h>
  19. #import <SafariServices/SafariServices.h>
  20. #import <XCTest/XCTest.h>
  21. #import "OCMock.h"
  22. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthUIDelegate.h"
  23. #import "FirebaseAuth/Sources/Utilities/FIRAuthURLPresenter.h"
  24. #import "FirebaseAuth/Sources/Utilities/FIRAuthWebViewController.h"
  25. /** @var kExpectationTimeout
  26. @brief The maximum time waiting for expectations to fulfill.
  27. */
  28. static NSTimeInterval kExpectationTimeout = 2;
  29. @interface FIRAuthDefaultUIDelegate : NSObject <FIRAuthUIDelegate>
  30. /** @fn defaultUIDelegate
  31. @brief Returns a default FIRAuthUIDelegate object.
  32. @return The default FIRAuthUIDelegate object.
  33. */
  34. + (id<FIRAuthUIDelegate>)defaultUIDelegate;
  35. @end
  36. @interface FIRAuthURLPresenterTests : XCTestCase
  37. @end
  38. @implementation FIRAuthURLPresenterTests
  39. /** @fn testFIRAuthURLPresenterNonNilUIDelegate
  40. @brief Tests @c FIRAuthURLPresenter class showing UI with a non-nil UIDelegate.
  41. */
  42. - (void)testFIRAuthURLPresenterNonNilUIDelegate {
  43. [self testFIRAuthURLPresenterUsingDefaultUIDelegate:NO];
  44. }
  45. /** @fn testFIRAuthURLPresenterNilUIDelegate
  46. @brief Tests @c FIRAuthURLPresenter class showing UI with a nil UIDelegate.
  47. */
  48. - (void)testFIRAuthURLPresenterNilUIDelegate {
  49. [self testFIRAuthURLPresenterUsingDefaultUIDelegate:YES];
  50. }
  51. /** @fn testFIRAuthURLPresenterUsingDefaultUIDelegate:
  52. @brief Tests @c FIRAuthURLPresenter class showing UIe.
  53. @param usesDefaultUIDelegate Whether or not to test the default UI delegate.
  54. */
  55. - (void)testFIRAuthURLPresenterUsingDefaultUIDelegate:(BOOL)usesDefaultUIDelegate {
  56. id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate));
  57. NSURL *presenterURL = [NSURL URLWithString:@"https://presenter.url"];
  58. FIRAuthURLPresenter *presenter = [[FIRAuthURLPresenter alloc] init];
  59. if (usesDefaultUIDelegate) {
  60. id mockDefaultUIDelegateClass = OCMClassMock([FIRAuthDefaultUIDelegate class]);
  61. OCMStub(ClassMethod([mockDefaultUIDelegateClass defaultUIDelegate])).andReturn(mockUIDelegate);
  62. }
  63. __block XCTestExpectation *callbackMatcherExpectation;
  64. FIRAuthURLCallbackMatcher callbackMatcher = ^BOOL(NSURL *_Nonnull callbackURL) {
  65. XCTAssertNotNil(callbackMatcherExpectation);
  66. XCTAssertEqualObjects(callbackURL, presenterURL);
  67. [callbackMatcherExpectation fulfill];
  68. return YES;
  69. };
  70. __block XCTestExpectation *completionBlockExpectation;
  71. FIRAuthURLPresentationCompletion completionBlock =
  72. ^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
  73. XCTAssertNotNil(completionBlockExpectation);
  74. XCTAssertEqualObjects(callbackURL, presenterURL);
  75. XCTAssertNil(error);
  76. [completionBlockExpectation fulfill];
  77. };
  78. XCTestExpectation *UIPresentationExpectation = [self expectationWithDescription:@"present UI"];
  79. OCMExpect([mockUIDelegate presentViewController:[OCMArg any] animated:YES completion:nil])
  80. .andDo(^(NSInvocation *invocation) {
  81. XCTAssertTrue([NSThread isMainThread]);
  82. __unsafe_unretained id unretainedArgument;
  83. // Indices 0 and 1 indicate the hidden arguments self and _cmd.
  84. // `presentViewController` is at index 2.
  85. [invocation getArgument:&unretainedArgument atIndex:2];
  86. id presentViewController = unretainedArgument;
  87. #if TARGET_OS_MACCATALYST
  88. // SFSafariViewController is not available
  89. UINavigationController *navigationController = presentViewController;
  90. XCTAssertTrue([navigationController isKindOfClass:[UINavigationController class]]);
  91. FIRAuthWebViewController *webViewController =
  92. navigationController.viewControllers.firstObject;
  93. XCTAssertTrue([webViewController isKindOfClass:[FIRAuthWebViewController class]]);
  94. #else
  95. SFSafariViewController *viewController = presentViewController;
  96. XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]);
  97. XCTAssertEqual(viewController.delegate, presenter);
  98. #endif
  99. [UIPresentationExpectation fulfill];
  100. });
  101. // Present the content.
  102. [presenter presentURL:presenterURL
  103. UIDelegate:usesDefaultUIDelegate ? nil : mockUIDelegate
  104. callbackMatcher:callbackMatcher
  105. completion:completionBlock];
  106. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  107. OCMVerifyAll(mockUIDelegate);
  108. // Pretend dismissing view controller.
  109. OCMExpect([mockUIDelegate dismissViewControllerAnimated:YES completion:OCMOCK_ANY])
  110. .andDo(^(NSInvocation *invocation) {
  111. XCTAssertTrue([NSThread isMainThread]);
  112. __unsafe_unretained id unretainedArgument;
  113. // Indices 0 and 1 indicate the hidden arguments self and _cmd.
  114. // `completion` is at index 3.
  115. [invocation getArgument:&unretainedArgument atIndex:3];
  116. void (^completion)(void) = unretainedArgument;
  117. dispatch_async(dispatch_get_main_queue(), completion);
  118. });
  119. completionBlockExpectation = [self expectationWithDescription:@"completion callback"];
  120. callbackMatcherExpectation = [self expectationWithDescription:@"callbackMatcher callback"];
  121. // Close the presented content.
  122. XCTAssertTrue([presenter canHandleURL:presenterURL]);
  123. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  124. OCMVerifyAll(mockUIDelegate);
  125. }
  126. @end
  127. #endif