Skip to content

Commit 685776d

Browse files
authored
[auth-swift] AuthDispatcherTests.swift (#11339)
1 parent 4ebf7bd commit 685776d

File tree

3 files changed

+73
-110
lines changed

3 files changed

+73
-110
lines changed

FirebaseAuth/Sources/Swift/Auth/AuthDispatcher.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@
1414

1515
import Foundation
1616

17-
// TODO: Get rid of public's and @objc's once FirebaseAuth.m is in Swift.
18-
1917
/** @class AuthDispatcher
2018
@brief A utility class used to facilitate scheduling tasks to be executed in the future.
2119
*/
22-
@objc(FIRAuthDispatcher) public class AuthDispatcher: NSObject {
23-
@objc(sharedInstance) public static let shared = AuthDispatcher()
20+
class AuthDispatcher {
21+
static let shared = AuthDispatcher()
2422

2523
/** @property dispatchAfterImplementation
2624
@brief Allows custom implementation of dispatchAfterDelay:queue:callback:.
2725
@remarks Set to nil to restore default implementation.
2826
*/
29-
@objc public
3027
var dispatchAfterImplementation: ((TimeInterval, DispatchQueue, @escaping () -> Void) -> Void)?
3128

3229
/** @fn dispatchAfterDelay:queue:callback:
@@ -36,7 +33,6 @@ import Foundation
3633
@param queue The dispatch queue on which the task will be submitted.
3734
@param task The task (block) to be scheduled for future execution.
3835
*/
39-
@objc public
4036
func dispatch(afterDelay delay: TimeInterval, queue: DispatchQueue, task: @escaping () -> Void) {
4137
if let dispatchAfterImplementation {
4238
dispatchAfterImplementation(delay, queue, task)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

FirebaseAuth/Tests/UnitObjC/FIRAuthDispatcherTests.m

-104
This file was deleted.

0 commit comments

Comments
 (0)