AuthDispatcherTests.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 testDispatchAfterDelay
  22. @brief Tests @c dispatchAfterDelay indeed dispatches the specified task after the provided
  23. delay.
  24. */
  25. func testDispatchAfterDelay() {
  26. let dispatcher = AuthDispatcher()
  27. let testWorkQueue = DispatchQueue(label: "test.work.queue")
  28. let expectation = self.expectation(description: #function)
  29. let dateBeforeDispatch = Date()
  30. dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) { [self] in
  31. let timeSinceDispatch = fabs(dateBeforeDispatch.timeIntervalSinceNow - self.kTestDelay)
  32. XCTAssertLessThan(timeSinceDispatch, kMaxDifferenceBetweenTimeIntervals)
  33. expectation.fulfill()
  34. }
  35. waitForExpectations(timeout: 5)
  36. }
  37. /** @fn testSetDispatchAfterImplementation
  38. @brief Tests that @c dispatchAfterImplementation indeed configures a custom implementation for
  39. @c dispatchAfterDelay.
  40. */
  41. func testSetDispatchAfterImplementation() {
  42. let testWorkQueue = DispatchQueue(label: "test.work.queue")
  43. let expectation = self.expectation(description: #function)
  44. let dispatcher = AuthDispatcher { delay, queue, task in
  45. XCTAssertEqual(self.kTestDelay, delay)
  46. XCTAssertEqual(testWorkQueue, queue)
  47. expectation.fulfill()
  48. }
  49. dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) {
  50. // Fail to ensure this code is never executed.
  51. XCTFail("Should not execute this code")
  52. }
  53. waitForExpectations(timeout: 5)
  54. }
  55. }