FIRAuthURLPresenterTests.m 5.7 KB

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