UnitDarwinCenterTest.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import XCTest
  6. @testable import NextcloudTalk
  7. final class UnitDarwinCenterTest: XCTestCase {
  8. override func setUpWithError() throws {
  9. // Reset any remaining handlers for each test
  10. for (notificationName, handlerDict) in DarwinNotificationCenter.shared.handlers {
  11. for (owner, _) in handlerDict {
  12. DarwinNotificationCenter.shared.removeHandler(notificationName: notificationName, owner: owner)
  13. }
  14. }
  15. XCTAssertTrue(DarwinNotificationCenter.shared.handlers.isEmpty)
  16. }
  17. func testDarwinCenterHandlerSingle() throws {
  18. let center = DarwinNotificationCenter.shared
  19. let expStarted = expectation(description: "\(#function)\(#line)")
  20. let expStopped = expectation(description: "\(#function)\(#line)")
  21. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self) {
  22. expStarted.fulfill()
  23. }
  24. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: self) {
  25. expStopped.fulfill()
  26. }
  27. // Check if the handlers are correctly registered
  28. XCTAssertEqual(center.handlers[DarwinNotificationCenter.broadcastStartedNotification]?.count, 1)
  29. XCTAssertEqual(center.handlers[DarwinNotificationCenter.broadcastStoppedNotification]?.count, 1)
  30. // Check if the handlers are correctly called after posting a notification
  31. center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
  32. center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
  33. wait(for: [expStarted, expStopped], timeout: TestConstants.timeoutShort)
  34. // Check if the handlers are correctly cleaned up
  35. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self)
  36. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: self)
  37. XCTAssertNil(center.handlers[DarwinNotificationCenter.broadcastStartedNotification])
  38. XCTAssertNil(center.handlers[DarwinNotificationCenter.broadcastStoppedNotification])
  39. }
  40. func testDarwinCenterHandlerMultiple() throws {
  41. let center = DarwinNotificationCenter.shared
  42. let owner1 = NSObject()
  43. // We need to wait twice for the expectation
  44. // 1. Before the handler is removed to ensure it is correctly called
  45. // 2. After a notification was posted a second time to ensure the first handler wasn't called multiple times
  46. let expSingleStarted = expectation(description: "\(#function)\(#line)")
  47. let expSingleStopped = expectation(description: "\(#function)\(#line)")
  48. let expSingleStartedEnd = expectation(description: "\(#function)\(#line)")
  49. let expSingleStoppedEnd = expectation(description: "\(#function)\(#line)")
  50. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner1) {
  51. expSingleStarted.fulfill()
  52. expSingleStartedEnd.fulfill()
  53. }
  54. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner1) {
  55. expSingleStopped.fulfill()
  56. expSingleStoppedEnd.fulfill()
  57. }
  58. let owner2 = NSObject()
  59. let expStartedSecond = expectation(description: "\(#function)\(#line)")
  60. let expStoppedSecond = expectation(description: "\(#function)\(#line)")
  61. expStartedSecond.expectedFulfillmentCount = 2
  62. expStoppedSecond.expectedFulfillmentCount = 2
  63. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner2) {
  64. expStartedSecond.fulfill()
  65. }
  66. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner2) {
  67. expStoppedSecond.fulfill()
  68. }
  69. // Call the handlers a first time
  70. center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
  71. center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
  72. wait(for: [expSingleStarted, expSingleStopped], timeout: TestConstants.timeoutShort)
  73. // Remove the handlers of owner1
  74. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: owner1)
  75. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStoppedNotification, owner: owner1)
  76. // Call the handlers a second time
  77. center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
  78. center.postNotification(DarwinNotificationCenter.broadcastStoppedNotification)
  79. // Also check the expectations from the first call to make sure, they were only called once and not again
  80. // We can't wait for an expectation twice, that's why we use a second expectation
  81. wait(for: [expStartedSecond, expStoppedSecond, expSingleStartedEnd, expSingleStoppedEnd], timeout: TestConstants.timeoutShort)
  82. }
  83. func testDarwinCenterUnbalancedRemove() throws {
  84. let center = DarwinNotificationCenter.shared
  85. let expStarted = expectation(description: "\(#function)\(#line)")
  86. center.addHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self) {
  87. expStarted.fulfill()
  88. }
  89. center.postNotification(DarwinNotificationCenter.broadcastStartedNotification)
  90. wait(for: [expStarted], timeout: TestConstants.timeoutShort)
  91. // Remove ourselves twice
  92. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self)
  93. center.removeHandler(notificationName: DarwinNotificationCenter.broadcastStartedNotification, owner: self)
  94. XCTAssertNil(center.handlers[DarwinNotificationCenter.broadcastStartedNotification])
  95. }
  96. }