Skip to content

[auth-swift] AuthDispatcherTests.swift #11339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions FirebaseAuth/Sources/Swift/Auth/AuthDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@

import Foundation

// TODO: Get rid of public's and @objc's once FirebaseAuth.m is in Swift.

/** @class AuthDispatcher
@brief A utility class used to facilitate scheduling tasks to be executed in the future.
*/
@objc(FIRAuthDispatcher) public class AuthDispatcher: NSObject {
@objc(sharedInstance) public static let shared = AuthDispatcher()
class AuthDispatcher {
static let shared = AuthDispatcher()

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

/** @fn dispatchAfterDelay:queue:callback:
Expand All @@ -36,7 +33,6 @@ import Foundation
@param queue The dispatch queue on which the task will be submitted.
@param task The task (block) to be scheduled for future execution.
*/
@objc public
func dispatch(afterDelay delay: TimeInterval, queue: DispatchQueue, task: @escaping () -> Void) {
if let dispatchAfterImplementation {
dispatchAfterImplementation(delay, queue, task)
Expand Down
71 changes: 71 additions & 0 deletions FirebaseAuth/Tests/Unit/AuthDispatcherTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import XCTest

@testable import FirebaseAuth

class AuthDispatcherTests: XCTestCase {
let kTestDelay = 0.1
let kMaxDifferenceBetweenTimeIntervals = 0.3

/** @fn testSharedInstance
@brief Tests @c sharedInstance returns the same object.
*/
func testSharedInstance() {
let instance1 = AuthDispatcher.shared
let instance2 = AuthDispatcher.shared
XCTAssert(instance1 === instance2)
}

/** @fn testDispatchAfterDelay
@brief Tests @c dispatchAfterDelay indeed dispatches the specified task after the provided
delay.
*/
func testDispatchAfterDelay() {
let dispatcher = AuthDispatcher.shared
let testWorkQueue = DispatchQueue(label: "test.work.queue")
let expectation = self.expectation(description: #function)
let dateBeforeDispatch = Date()
dispatcher.dispatchAfterImplementation = nil
dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) { [self] in
let timeSinceDispatch = fabs(dateBeforeDispatch.timeIntervalSinceNow - self.kTestDelay)
XCTAssertLessThan(timeSinceDispatch, kMaxDifferenceBetweenTimeIntervals)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

/** @fn testSetDispatchAfterImplementation
@brief Tests that @c dispatchAfterImplementation indeed configures a custom implementation for
@c dispatchAfterDelay.
*/
func testSetDispatchAfterImplementation() {
let dispatcher = AuthDispatcher.shared
let testWorkQueue = DispatchQueue(label: "test.work.queue")
let expectation = self.expectation(description: #function)
dispatcher.dispatchAfterImplementation = { delay, queue, task in
XCTAssertEqual(self.kTestDelay, delay)
XCTAssertEqual(testWorkQueue, queue)
expectation.fulfill()
}

dispatcher.dispatch(afterDelay: kTestDelay, queue: testWorkQueue) {
// Fail to ensure this code is never executed.
XCTFail("Should not execute this code")
}
waitForExpectations(timeout: 5)
}
}
104 changes: 0 additions & 104 deletions FirebaseAuth/Tests/UnitObjC/FIRAuthDispatcherTests.m

This file was deleted.