talk-ios/NextcloudTalkTests/Unit/UnitDarwinCenterTest.swift

126 строки
5.8 KiB
Swift
Исходник Обычный вид История

//
// Copyright (c) 2024 Marcel Müller <marcel-mueller@gmx.de>
//
// Author Marcel Müller <marcel-mueller@gmx.de>
//
// GNU GPL version 3 or any later version
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import XCTest
@testable import NextcloudTalk
final class UnitDarwinCenterTest: XCTestCase {
override func setUpWithError() throws {
// Reset any remaining handlers for each test
for (notificationName, handlerDict) in DarwinNotificationCenter.shared.handlers {
for (owner, _) in handlerDict {
DarwinNotificationCenter.shared.removeHandler(notificationName: notificationName, owner: owner)
}
}
XCTAssertTrue(DarwinNotificationCenter.shared.handlers.isEmpty)
}
func testDarwinCenterHandlerSingle() throws {
let center = DarwinNotificationCenter.shared
let expStarted = expectation(description: "\(#function)\(#line)")
let expStopped = expectation(description: "\(#function)\(#line)")
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self) {
expStarted.fulfill()
}
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: self) {
expStopped.fulfill()
}
// Check if the handlers are correctly registered
XCTAssertEqual(center.handlers[DarwinNotificationCenter.broadcastStartedNotification]?.count, 1)
XCTAssertEqual(center.handlers[DarwinNotificationCenter.broadcastStoppedNotification]?.count, 1)
// Check if the handlers are correctly called after posting a notification
center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
wait(for: [expStarted, expStopped], timeout: TestConstants.timeoutShort)
// Check if the handlers are correctly cleaned up
center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self)
center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: self)
XCTAssertNil(center.handlers[DarwinNotificationCenter.broadcastStartedNotification])
XCTAssertNil(center.handlers[DarwinNotificationCenter.broadcastStoppedNotification])
}
func testDarwinCenterHandlerMultiple() throws {
let center = DarwinNotificationCenter.shared
let owner1 = NSObject()
// We need to wait twice for the expectation
// 1. Before the handler is removed to ensure it is correctly called
// 2. After a notification was posted a second time to ensure the first handler wasn't called multiple times
var expSingleStarted = expectation(description: "\(#function)\(#line)")
var expSingleStopped = expectation(description: "\(#function)\(#line)")
var expSingleStartedEnd = expectation(description: "\(#function)\(#line)")
var expSingleStoppedEnd = expectation(description: "\(#function)\(#line)")
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner1) {
expSingleStarted.fulfill()
expSingleStartedEnd.fulfill()
}
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner1) {
expSingleStopped.fulfill()
expSingleStoppedEnd.fulfill()
}
let owner2 = NSObject()
var expStartedSecond = expectation(description: "\(#function)\(#line)")
var expStoppedSecond = expectation(description: "\(#function)\(#line)")
expStartedSecond.expectedFulfillmentCount = 2
expStoppedSecond.expectedFulfillmentCount = 2
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner2) {
expStartedSecond.fulfill()
}
center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner2) {
expStoppedSecond.fulfill()
}
// Call the handlers a first time
center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
wait(for: [expSingleStarted, expSingleStopped], timeout: TestConstants.timeoutShort)
// Remove the handlers of owner1
center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner1)
center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner1)
// Call the handlers a second time
center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
// Also check the expectations from the first call to make sure, they were only called once and not again
// We can't wait for an expectation twice, that's why we use a second expectation
wait(for: [expStartedSecond, expStoppedSecond, expSingleStartedEnd, expSingleStoppedEnd], timeout: TestConstants.timeoutShort)
}
}