|
8 | 8 | //
|
9 | 9 |
|
10 | 10 | class TestRunLoop : XCTestCase {
|
11 |
| - static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] { |
12 |
| - return [ |
13 |
| - ("test_constants", test_constants), |
14 |
| - ("test_runLoopInit", test_runLoopInit), |
15 |
| - ("test_commonModes", test_commonModes), |
16 |
| - // these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399 |
17 |
| -// ("test_runLoopRunMode", test_runLoopRunMode), |
18 |
| -// ("test_runLoopLimitDate", test_runLoopLimitDate), |
19 |
| - ] |
20 |
| - } |
21 |
| - |
22 | 11 | func test_constants() {
|
23 | 12 | XCTAssertEqual(RunLoop.Mode.common.rawValue, "kCFRunLoopCommonModes",
|
24 | 13 | "\(RunLoop.Mode.common.rawValue) is not equal to kCFRunLoopCommonModes")
|
@@ -92,4 +81,82 @@ class TestRunLoop : XCTestCase {
|
92 | 81 |
|
93 | 82 | waitForExpectations(timeout: 10)
|
94 | 83 | }
|
| 84 | + |
| 85 | + func test_addingRemovingPorts() { |
| 86 | + let runLoop = RunLoop.current |
| 87 | + var didDeallocate = false |
| 88 | + |
| 89 | + do { |
| 90 | + let port = TestPort { |
| 91 | + didDeallocate = true |
| 92 | + } |
| 93 | + let customMode = RunLoop.Mode(rawValue: "Custom") |
| 94 | + |
| 95 | + XCTAssertEqual(port.scheduledModes, []) |
| 96 | + |
| 97 | + runLoop.add(port, forMode: .default) |
| 98 | + XCTAssertEqual(port.scheduledModes, [.default]) |
| 99 | + |
| 100 | + runLoop.add(port, forMode: .default) |
| 101 | + XCTAssertEqual(port.scheduledModes, [.default]) |
| 102 | + |
| 103 | + runLoop.add(port, forMode: customMode) |
| 104 | + XCTAssertEqual(port.scheduledModes, [.default, customMode]) |
| 105 | + |
| 106 | + runLoop.remove(port, forMode: customMode) |
| 107 | + XCTAssertEqual(port.scheduledModes, [.default]) |
| 108 | + |
| 109 | + runLoop.add(port, forMode: customMode) |
| 110 | + XCTAssertEqual(port.scheduledModes, [.default, customMode]) |
| 111 | + |
| 112 | + port.invalidate() |
| 113 | + } |
| 114 | + |
| 115 | + XCTAssertTrue(didDeallocate) |
| 116 | + } |
| 117 | + |
| 118 | + static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] { |
| 119 | + return [ |
| 120 | + ("test_constants", test_constants), |
| 121 | + ("test_runLoopInit", test_runLoopInit), |
| 122 | + ("test_commonModes", test_commonModes), |
| 123 | + // these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399 |
| 124 | + // ("test_runLoopRunMode", test_runLoopRunMode), |
| 125 | + // ("test_runLoopLimitDate", test_runLoopLimitDate), |
| 126 | + ("test_addingRemovingPorts", test_addingRemovingPorts), |
| 127 | + ] |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +class TestPort: Port { |
| 132 | + let sentinel: () -> Void |
| 133 | + init(sentinel: @escaping () -> Void) { |
| 134 | + self.sentinel = sentinel |
| 135 | + super.init() |
| 136 | + } |
| 137 | + |
| 138 | + deinit { |
| 139 | + invalidate() |
| 140 | + sentinel() |
| 141 | + } |
| 142 | + |
| 143 | + private var _isValid = true |
| 144 | + open override var isValid: Bool { return _isValid } |
| 145 | + |
| 146 | + open override func invalidate() { |
| 147 | + guard isValid else { return } |
| 148 | + |
| 149 | + _isValid = false |
| 150 | + NotificationCenter.default.post(name: Port.didBecomeInvalidNotification, object: self) |
| 151 | + } |
| 152 | + |
| 153 | + var scheduledModes: [RunLoop.Mode] = [] |
| 154 | + |
| 155 | + open override func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) { |
| 156 | + scheduledModes.append(mode) |
| 157 | + } |
| 158 | + |
| 159 | + open override func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) { |
| 160 | + scheduledModes = scheduledModes.filter { $0 != mode } |
| 161 | + } |
95 | 162 | }
|
0 commit comments