-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSNotificationCenter.swift
153 lines (124 loc) · 5.79 KB
/
TestNSNotificationCenter.swift
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
// 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
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestNSNotificationCenter : XCTestCase {
static var allTests: [(String, (TestNSNotificationCenter) -> () throws -> Void)] {
return [
("test_defaultCenter", test_defaultCenter),
("test_postNotification", test_postNotification),
("test_postNotificationForObject", test_postNotificationForObject),
("test_postMultipleNotifications", test_postMultipleNotifications),
("test_addObserverForNilName", test_addObserverForNilName),
("test_removeObserver", test_removeObserver),
]
}
func test_defaultCenter() {
let defaultCenter1 = NotificationCenter.default
XCTAssertNotNil(defaultCenter1)
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")
var flag = false
let dummyObject = NSObject()
let observer = notificationCenter.addObserver(forName: notificationName, object: dummyObject, queue: nil) { notification in
XCTAssertEqual(notificationName, notification.name)
XCTAssertTrue(dummyObject === notification.object as? NSObject)
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")
var flag = true
let dummyObject = NSObject()
let dummyObject2 = NSObject()
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")
var flag1 = false
let observer1 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag1 = true
}
var flag2 = true
let observer2 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag2 = false
}
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")
var flag1 = false
let observer1 = notificationCenter.addObserver(forName: notificationName, object: nil, queue: nil) { _ in
flag1 = true
}
var flag2 = true
let observer2 = notificationCenter.addObserver(forName: invalidNotificationName, object: nil, queue: nil) { _ in
flag2 = false
}
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")
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)
}
}