Skip to content

Commit a0e3e97

Browse files
committed
Tests for NSTimer
1 parent a619b96 commit a0e3e97

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
5BF7AEC01BCD51F9008F214A /* NSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4B1BCC5DCB00ED97BB /* NSUUID.swift */; };
201201
5BF7AEC11BCD51F9008F214A /* NSValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4C1BCC5DCB00ED97BB /* NSValue.swift */; };
202202
612952F91C1B235900BE0FD9 /* TestNSNull.swift in Sources */ = {isa = PBXBuildFile; fileRef = 612952F81C1B235900BE0FD9 /* TestNSNull.swift */; };
203+
61D6C9EF1C1DFE9500DEF583 /* TestNSTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */; };
203204
61E0117C1C1B554D000037DD /* TestNSRunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61E0117B1C1B554D000037DD /* TestNSRunLoop.swift */; };
204205
61E0117D1C1B5590000037DD /* NSRunLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B761BD15DFF00C49C64 /* NSRunLoop.swift */; };
205206
61E0117E1C1B55B9000037DD /* NSTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F481BCC5DCB00ED97BB /* NSTimer.swift */; };
@@ -547,6 +548,7 @@
547548
5BF7AEC21BCD568D008F214A /* ForSwiftFoundationOnly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ForSwiftFoundationOnly.h; sourceTree = "<group>"; };
548549
5EB6A15C1C188FC40037DCB8 /* TestNSJSONSerialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSJSONSerialization.swift; sourceTree = "<group>"; };
549550
612952F81C1B235900BE0FD9 /* TestNSNull.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNull.swift; sourceTree = "<group>"; };
551+
61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSTimer.swift; sourceTree = "<group>"; };
550552
61E0117B1C1B554D000037DD /* TestNSRunLoop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRunLoop.swift; sourceTree = "<group>"; };
551553
61F8AE7C1C180FC600FB62F0 /* TestNSNotificationCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNotificationCenter.swift; sourceTree = "<group>"; };
552554
6E203B8C1C1303BB003B2576 /* TestNSBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSBundle.swift; sourceTree = "<group>"; };
@@ -1077,6 +1079,7 @@
10771079
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
10781080
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */,
10791081
DCDBB8321C1768AC00313299 /* TestNSData.swift */,
1082+
61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */,
10801083
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */,
10811084
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
10821085
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */,
@@ -1812,6 +1815,7 @@
18121815
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,
18131816
ED58F76F1C134B3A00E6A5BE /* (null) in Sources */,
18141817
EA66F6581BF1619600136161 /* TestNSURL.swift in Sources */,
1818+
61D6C9EF1C1DFE9500DEF583 /* TestNSTimer.swift in Sources */,
18151819
61E0117C1C1B554D000037DD /* TestNSRunLoop.swift in Sources */,
18161820
EA66F6441BF1619600136161 /* main.swift in Sources */,
18171821
);

Foundation/NSRunLoop.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ extension NSRunLoop {
7272

7373
public func runMode(mode: String, beforeDate limitDate: NSDate) -> Bool {
7474
let runloopResult = CFRunLoopRunInMode(mode._cfObject, limitDate.timeIntervalSinceNow, false)
75+
#if os(Linux)
7576
return runloopResult == 2 || runloopResult == 3
77+
#else
78+
return runloopResult == .HandledSource || runloopResult == .TimedOut
79+
#endif
7680
}
7781

7882
}

Foundation/NSSwiftRuntime.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal func __CFInitializeSwift() {
121121
// _CFRuntimeBridgeTypeToClass(CFAttributedStringGetTypeID(), unsafeBitCast(NSMutableAttributedString.self, UnsafePointer<Void>.self))
122122
// _CFRuntimeBridgeTypeToClass(CFReadStreamGetTypeID(), unsafeBitCast(NSInputStream.self, UnsafePointer<Void>.self))
123123
// _CFRuntimeBridgeTypeToClass(CFWriteStreamGetTypeID(), unsafeBitCast(NSOutputStream.self, UnsafePointer<Void>.self))
124-
// _CFRuntimeBridgeTypeToClass(CFRunLoopTimerGetTypeID(), unsafeBitCast(NSTimer.self, UnsafePointer<Void>.self))
124+
_CFRuntimeBridgeTypeToClass(CFRunLoopTimerGetTypeID(), unsafeBitCast(NSTimer.self, UnsafePointer<Void>.self))
125125

126126
__CFSwiftBridge.NSObject.isEqual = _CFSwiftIsEqual
127127
__CFSwiftBridge.NSObject.hash = _CFSwiftGetHash

TestFoundation/TestNSTimer.swift

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
20+
class TestNSTimer : XCTestCase {
21+
var allTests : [(String, () -> ())] {
22+
return [
23+
("test_timerInit", test_timerInit),
24+
("test_timerTickOnce", test_timerTickOnce),
25+
("test_timerRepeats", test_timerRepeats),
26+
("test_timerInvalidate", test_timerInvalidate),
27+
]
28+
}
29+
30+
func test_timerInit() {
31+
let timer = NSTimer(fireDate: NSDate(), interval: 0.3, repeats: false) { _ in }
32+
XCTAssertNotNil(timer)
33+
}
34+
35+
func test_timerTickOnce() {
36+
var flag = false
37+
38+
let dummyTimer = NSTimer.scheduledTimer(0.01, repeats: false) { timer in
39+
XCTAssertFalse(flag)
40+
41+
flag = true
42+
timer.invalidate()
43+
}
44+
45+
let runLoop = NSRunLoop.currentRunLoop()
46+
runLoop.addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
47+
runLoop.runUntilDate(NSDate(timeIntervalSinceNow: 0.05))
48+
49+
XCTAssertTrue(flag)
50+
}
51+
52+
func test_timerRepeats() {
53+
var flag = 0
54+
let interval = NSTimeInterval(0.1)
55+
let numberOfRepeats = 3
56+
var previousInterval = NSDate().timeIntervalSince1970
57+
58+
let dummyTimer = NSTimer.scheduledTimer(interval, repeats: true) { timer in
59+
XCTAssertEqual(timer.timeInterval, interval)
60+
61+
let currentInterval = NSDate().timeIntervalSince1970
62+
XCTAssertEqualWithAccuracy(currentInterval, previousInterval + interval, accuracy: 0.01)
63+
previousInterval = currentInterval
64+
65+
flag += 1
66+
if (flag == numberOfRepeats) {
67+
timer.invalidate()
68+
}
69+
}
70+
71+
let runLoop = NSRunLoop.currentRunLoop()
72+
runLoop.addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
73+
runLoop.runUntilDate(NSDate(timeIntervalSinceNow: interval * Double(numberOfRepeats + 1)))
74+
75+
XCTAssertEqual(flag, numberOfRepeats)
76+
}
77+
78+
func test_timerInvalidate() {
79+
var flag = false
80+
81+
let dummyTimer = NSTimer.scheduledTimer(0.01, repeats: true) { timer in
82+
XCTAssertTrue(timer.valid)
83+
XCTAssertFalse(flag) // timer should tick only once
84+
85+
flag = true
86+
87+
timer.invalidate()
88+
XCTAssertFalse(timer.valid)
89+
}
90+
91+
let runLoop = NSRunLoop.currentRunLoop()
92+
runLoop.addTimer(dummyTimer, forMode: NSDefaultRunLoopMode)
93+
runLoop.runUntilDate(NSDate(timeIntervalSinceNow: 0.05))
94+
95+
XCTAssertTrue(flag)
96+
}
97+
98+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ XCTMain([
4646
TestNSScanner(),
4747
TestNSSet(),
4848
TestNSString(),
49+
TestNSTimer(),
4950
TestNSTimeZone(),
5051
TestNSURL(),
5152
TestNSURLComponents(),

build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@
242242

243243
swift_sources = CompileSwiftSources([
244244
'Foundation/NSObject.swift',
245-
'Foundation/NSRunLoop.swift',
246245
'Foundation/NSAffineTransform.swift',
247246
'Foundation/NSArray.swift',
248247
'Foundation/NSAttributedString.swift',
@@ -304,6 +303,7 @@
304303
'Foundation/NSPropertyList.swift',
305304
'Foundation/NSRange.swift',
306305
'Foundation/NSRegularExpression.swift',
306+
'Foundation/NSRunLoop.swift',
307307
'Foundation/NSScanner.swift',
308308
'Foundation/NSSet.swift',
309309
'Foundation/NSSortDescriptor.swift',

0 commit comments

Comments
 (0)