FIRAuthURLPresenterTests.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 =
  70. ^(NSURL *_Nullable callbackURL, 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] animated:YES completion:nil])
  78. .andDo(^(NSInvocation *invocation) {
  79. XCTAssertTrue([NSThread isMainThread]);
  80. __unsafe_unretained id unretainedArgument;
  81. // Indices 0 and 1 indicate the hidden arguments self and _cmd.
  82. // `presentViewController` is at index 2.
  83. [invocation getArgument:&unretainedArgument atIndex:2];
  84. id presentViewController = unretainedArgument;
  85. if (@available(iOS 9.0, *)) { // SFSafariViewController is available
  86. SFSafariViewController *viewController = presentViewController;
  87. XCTAssertTrue([viewController isKindOfClass:[SFSafariViewController class]]);
  88. XCTAssertEqual(viewController.delegate, presenter);
  89. } else {
  90. UINavigationController *navigationController = presentViewController;
  91. XCTAssertTrue([navigationController isKindOfClass:[UINavigationController class]]);
  92. FIRAuthWebViewController *webViewController =
  93. navigationController.viewControllers.firstObject;
  94. XCTAssertTrue([webViewController isKindOfClass:[FIRAuthWebViewController class]]);
  95. }
  96. [UIPresentationExpectation fulfill];
  97. });
  98. // Present the content.
  99. [presenter presentURL:presenterURL
  100. UIDelegate:usesDefaultUIDelegate ? nil : mockUIDelegate
  101. callbackMatcher:callbackMatcher
  102. completion:completionBlock];
  103. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  104. OCMVerifyAll(mockUIDelegate);
  105. // Pretend dismissing view controller.
  106. OCMExpect([mockUIDelegate dismissViewControllerAnimated:YES completion:OCMOCK_ANY])
  107. .andDo(^(NSInvocation *invocation) {
  108. XCTAssertTrue([NSThread isMainThread]);
  109. __unsafe_unretained id unretainedArgument;
  110. // Indices 0 and 1 indicate the hidden arguments self and _cmd.
  111. // `completion` is at index 3.
  112. [invocation getArgument:&unretainedArgument atIndex:3];
  113. void (^completion)(void) = unretainedArgument;
  114. dispatch_async(dispatch_get_main_queue(), completion);
  115. });
  116. completionBlockExpectation = [self expectationWithDescription:@"completion callback"];
  117. callbackMatcherExpectation = [self expectationWithDescription:@"callbackMatcher callback"];
  118. // Close the presented content.
  119. XCTAssertTrue([presenter canHandleURL:presenterURL]);
  120. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  121. OCMVerifyAll(mockUIDelegate);
  122. }
  123. @end