AuthURLPresenterTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License")
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if os(iOS)
  15. import Foundation
  16. import XCTest
  17. @testable import FirebaseAuth
  18. import FirebaseCore
  19. import SafariServices
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class AuthURLPresenterTests: XCTestCase {
  22. /** @fn testFIRAuthURLPresenterNonNilUIDelegate
  23. @brief Tests @c FIRAuthURLPresenter class showing UI with a non-nil UIDelegate.
  24. */
  25. func testAuthURLPresenterNonNilUIDelegate() throws {
  26. try internalAuthURLPresenterTests(useDefaultUIDelegate: false)
  27. }
  28. /** @fn testFIRAuthURLPresenterNilUIDelegate
  29. @brief Tests @c FIRAuthURLPresenter class showing UI with a nil UIDelegate.
  30. */
  31. func testFIRAuthURLPresenterNilUIDelegate() throws {
  32. try internalAuthURLPresenterTests(useDefaultUIDelegate: true)
  33. }
  34. private func internalAuthURLPresenterTests(useDefaultUIDelegate: Bool) throws {
  35. let presenterExpectation = expectation(description: "presentation expectation")
  36. let presenter = AuthURLPresenter()
  37. presenter.fakeUIDelegate = FakeUIDelegate(presenter, presenterExpectation)
  38. let presenterURL = try XCTUnwrap(URL(string: "https://presenter.url"))
  39. let uiDelegate = useDefaultUIDelegate ? AuthDefaultUIDelegate(withViewController: nil) : nil
  40. let callbackMatcherExpectation = expectation(description: "callback matcher expectation")
  41. let callbackMatcher: (URL?) -> Bool = { callbackURL in
  42. XCTAssertEqual(callbackURL, presenterURL)
  43. callbackMatcherExpectation.fulfill()
  44. return true
  45. }
  46. let completionExpectation = expectation(description: "completion expectation")
  47. let completion: (URL?, Error?) -> Void = { callbackURL, error in
  48. XCTAssertEqual(callbackURL, presenterURL)
  49. XCTAssertNil(error)
  50. completionExpectation.fulfill()
  51. }
  52. // Present the content.
  53. presenter.present(presenterURL,
  54. uiDelegate: uiDelegate,
  55. callbackMatcher: callbackMatcher,
  56. completion: completion)
  57. // Close the presented content and trigger callbacks.
  58. XCTAssertTrue(presenter.canHandle(url: presenterURL))
  59. waitForExpectations(timeout: 5)
  60. class FakeUIDelegate: NSObject, AuthUIDelegate {
  61. func present(_ viewControllerToPresent: UIViewController,
  62. animated flag: Bool,
  63. completion: (() -> Void)? = nil) {
  64. #if targetEnvironment(macCatalyst)
  65. let navigationController = viewControllerToPresent as? UINavigationController
  66. let webViewController = navigationController?.viewControllers
  67. .first as? AuthWebViewController
  68. let delegate = webViewController?.delegate as? AuthURLPresenter
  69. #else
  70. let safariViewController = viewControllerToPresent as? SFSafariViewController
  71. let delegate = safariViewController?.delegate as? AuthURLPresenter
  72. #endif
  73. XCTAssertEqual(delegate, presenter)
  74. presenterExpectation.fulfill()
  75. }
  76. func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
  77. XCTFail("implement me")
  78. }
  79. init(_ presenter: AuthURLPresenter, _ expectation: XCTestExpectation) {
  80. self.presenter = presenter
  81. presenterExpectation = expectation
  82. }
  83. let presenter: AuthURLPresenter
  84. let presenterExpectation: XCTestExpectation
  85. }
  86. }
  87. }
  88. #endif