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