AuthDispatcherTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class AuthDispatcherTests: XCTestCase {
  19. let kTestDelay = 0.1
  20. let kMaxDifferenceBetweenTimeIntervals = 0.4
  21. /** @fn testSharedInstance
  22. @brief Tests @c sharedInstance returns the same object.
  23. */
  24. func testSharedInstance() {
  25. let instance1 = AuthDispatcher.shared
  26. let instance2 = AuthDispatcher.shared
  27. XCTAssert(instance1 === instance2)
  28. }
  29. /** @fn testDispatchAfterDelay
  30. @brief Tests @c dispatchAfterDelay indeed dispatches the specified task after the provided
  31. delay.
  32. */
  33. func testDispatchAfterDelay() {
  34. let dispatcher = AuthDispatcher.shared
  35. let testWorkQueue = DispatchQueue(label: "test.work.queue")
  36. let expectation = self.expectation(description: #function)
  37. let dateBeforeDispatch = Date()
  38. dispatcher.dispatchAfterImplementation = nil
  39. dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) { [self] in
  40. let timeSinceDispatch = fabs(dateBeforeDispatch.timeIntervalSinceNow - self.kTestDelay)
  41. XCTAssertLessThan(timeSinceDispatch, kMaxDifferenceBetweenTimeIntervals)
  42. expectation.fulfill()
  43. }
  44. waitForExpectations(timeout: 5)
  45. }
  46. /** @fn testSetDispatchAfterImplementation
  47. @brief Tests that @c dispatchAfterImplementation indeed configures a custom implementation for
  48. @c dispatchAfterDelay.
  49. */
  50. func testSetDispatchAfterImplementation() {
  51. let dispatcher = AuthDispatcher.shared
  52. let testWorkQueue = DispatchQueue(label: "test.work.queue")
  53. let expectation = self.expectation(description: #function)
  54. dispatcher.dispatchAfterImplementation = { delay, queue, task in
  55. XCTAssertEqual(self.kTestDelay, delay)
  56. XCTAssertEqual(testWorkQueue, queue)
  57. expectation.fulfill()
  58. }
  59. dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) {
  60. // Fail to ensure this code is never executed.
  61. XCTFail("Should not execute this code")
  62. }
  63. waitForExpectations(timeout: 5)
  64. }
  65. }