1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
final class NotificationCenterDummyObject : NSObject, Sendable { }
class TestNotificationCenter : XCTestCase {
func test_defaultCenter() {
let defaultCenter1 = NotificationCenter.default
let defaultCenter2 = NotificationCenter.default
XCTAssertEqual(defaultCenter1, defaultCenter2)
}
func removeObserver(_ observer: NSObjectProtocol, notificationCenter: NotificationCenter) {
guard let observer = observer as? NSObject else {
return
}
notificationCenter.removeObserver(observer)
}
func test_postNotification() {
let notificationCenter = NotificationCenter()
let notificationName = Notification.Name(rawValue: "test_postNotification_name")
nonisolated(unsafe) var flag = false
let dummyObject = NotificationCenterDummyObject()
let observer = notificationCenter.addObserver(forName: notificationName, object: dummyObject, queue: nil) { notification in
XCTAssertEqual(notificationName, notification.name)
XCTAssertTrue(dummyObject === notification.object as? NotificationCenterDummyObject)
flag = true
}
notificationCenter.post(name: notificationName, object: dummyObject)
XCTAssertTrue(flag)
removeObserver(observer, notificationCenter: notificationCenter)
}
func test_postNotificationForObject() {
let notificationCenter = NotificationCenter()
let notificationName = Notification.Name(rawValue: "test_postNotificationForObject_name")
nonisolated(unsafe) var flag = true
let dummyObject = NotificationCenterDummyObject()
let dummyObject2 = NotificationCenterDummyObject()
let observer = notificationCenter.addObserver(forName: notificationName, object: dummyObject, queue: nil) { notification in
flag = false
}
notificationCenter.post(name: notificationName, object: dummyObject2)
XCTAssertTrue(flag)
removeObserver(observer, notificationCenter: notificationCenter)
}
func test_postMultipleNotifications() {
let notificationCenter = NotificationCenter()
let notificationName = Notification.Name(rawValue: "test_postMultipleNotifications_name")
nonisolated(unsafe) var flag1 = false
let observer1 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag1 = true
}
nonisolated(unsafe) var flag2 = true
let observer2 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag2 = false
}
nonisolated(unsafe) var flag3 = false
let observer3 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag3 = true
}
removeObserver(observer2, notificationCenter: notificationCenter)
notificationCenter.post(name: notificationName, object: nil)
XCTAssertTrue(flag1)
XCTAssertTrue(flag2)
XCTAssertTrue(flag3)
removeObserver(observer1, notificationCenter: notificationCenter)
removeObserver(observer3, notificationCenter: notificationCenter)
}
func test_addObserverForNilName() {
let notificationCenter = NotificationCenter()
let notificationName = Notification.Name(rawValue: "test_addObserverForNilName_name")
let invalidNotificationName = Notification.Name(rawValue: "test_addObserverForNilName_name_invalid")
nonisolated(unsafe) var flag1 = false
let observer1 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag1 = true
}
nonisolated(unsafe) var flag2 = true
let observer2 = notificationCenter.addObserver(forName: invalidNotificationName, object: nil, queue: nil) { _ in
flag2 = false
}
nonisolated(unsafe) var flag3 = false
let observer3 = notificationCenter.addObserver(forName: nil, object: nil, queue: nil) { _ in
flag3 = true
}
notificationCenter.post(name: notificationName, object: nil)
XCTAssertTrue(flag1)
XCTAssertTrue(flag2)
XCTAssertTrue(flag3)
removeObserver(observer1, notificationCenter: notificationCenter)
removeObserver(observer2, notificationCenter: notificationCenter)
removeObserver(observer3, notificationCenter: notificationCenter)
}
func test_removeObserver() {
let notificationCenter = NotificationCenter()
let notificationName = Notification.Name(rawValue: "test_removeObserver_name")
nonisolated(unsafe) var flag = true
let observer = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag = false
}
removeObserver(observer, notificationCenter: notificationCenter)
notificationCenter.post(name: notificationName, object: nil)
XCTAssertTrue(flag)
}
func test_observeOnPostingQueue() {
let notificationCenter = NotificationCenter()
let name = Notification.Name(rawValue: "\(#function)_name")
let postingQueue = OperationQueue()
let expectation = self.expectation(description: "Observer was not notified.")
_ = notificationCenter.addObserver(forName: name, object: nil, queue: nil) { _ in
XCTAssertEqual(OperationQueue.current, postingQueue)
expectation.fulfill()
}
postingQueue.addOperation {
notificationCenter.post(name: name, object: nil)
}
self.waitForExpectations(timeout: 1)
}
func test_observeOnSpecificQueuePostFromMainQueue() {
let name = Notification.Name(rawValue: "\(#function)_name")
let notificationCenter = NotificationCenter()
let operationQueue = OperationQueue()
nonisolated(unsafe) var flag1 = false
nonisolated(unsafe) var flag2 = false
_ = notificationCenter.addObserver(forName: name, object: nil, queue: operationQueue) { _ in
XCTAssertEqual(OperationQueue.current, operationQueue)
flag1 = true
}
_ = notificationCenter.addObserver(forName: name, object: nil, queue: .main) { _ in
XCTAssertEqual(OperationQueue.current, .main)
flag2 = true
}
notificationCenter.post(name: name, object: nil)
// All observers should be notified synchronously regardless of the observer queue.
XCTAssertTrue(flag1)
XCTAssertTrue(flag2)
}
func test_observeOnSpecificQueuePostFromObservedQueue() {
let name = Notification.Name(rawValue: "\(#function)_name")
let notificationCenter = NotificationCenter()
let observingQueue = OperationQueue()
let expectation = self.expectation(description: "Notification posting operation was not executed.")
// Protected by the inherent ordering of notification center posts - which is the point of this test
nonisolated(unsafe) var flag1 = false
nonisolated(unsafe) var flag2 = false
_ = notificationCenter.addObserver(forName: name, object: nil, queue: observingQueue) { _ in
XCTAssertEqual(OperationQueue.current, observingQueue)
flag1 = true
}
_ = notificationCenter.addObserver(forName: name, object: nil, queue: .main) { _ in
XCTAssertEqual(OperationQueue.current, .main)
flag2 = true
}
observingQueue.addOperation {
notificationCenter.post(name: name, object: nil)
// All observers should be notified synchronously regardless of the observer queue.
XCTAssertTrue(flag1)
XCTAssertTrue(flag2)
expectation.fulfill()
}
self.waitForExpectations(timeout: 1)
}
func test_observeOnSpecificQueuePostFromUnrelatedQueue() {
let name = Notification.Name(rawValue: "\(#function)_name")
let notificationCenter = NotificationCenter()
let operationQueue = OperationQueue()
let postingQueue = OperationQueue()
let expectation = self.expectation(description: "Notification posting operation was not executed.")
// Protected by the inherent ordering of notification center posts - which is the point of this test
nonisolated(unsafe) var flag1 = false
nonisolated(unsafe) var flag2 = false
_ = notificationCenter.addObserver(forName: name, object: nil, queue: operationQueue) { _ in
XCTAssertEqual(OperationQueue.current, operationQueue)
flag1 = true
}
_ = notificationCenter.addObserver(forName: name, object: nil, queue: .main) { _ in
XCTAssertEqual(OperationQueue.current, .main)
flag2 = true
}
postingQueue.addOperation {
notificationCenter.post(name: name, object: nil)
// All observers should be notified synchronously regardless of the observer queue.
XCTAssertTrue(flag1)
XCTAssertTrue(flag2)
expectation.fulfill()
}
self.waitForExpectations(timeout: 1)
}
}
|