forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestRunLoop.swift
93 lines (76 loc) · 3.81 KB
/
TestRunLoop.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
// 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 TestRunLoop : XCTestCase {
static var allTests : [(String, (TestRunLoop) -> () throws -> Void)] {
return [
("test_constants", test_constants),
("test_runLoopInit", test_runLoopInit),
// these tests do not work the same as Darwin https://bugs.swift.org/browse/SR-399
// ("test_runLoopRunMode", test_runLoopRunMode),
// ("test_runLoopLimitDate", test_runLoopLimitDate),
]
}
func test_constants() {
XCTAssertEqual(RunLoopMode.commonModes.rawValue, "kCFRunLoopCommonModes",
"\(RunLoopMode.commonModes.rawValue) is not equal to kCFRunLoopCommonModes")
XCTAssertEqual(RunLoopMode.defaultRunLoopMode.rawValue, "kCFRunLoopDefaultMode",
"\(RunLoopMode.defaultRunLoopMode.rawValue) is not equal to kCFRunLoopDefaultMode")
}
func test_runLoopInit() {
let mainRunLoop = RunLoop.main
XCTAssertNotNil(mainRunLoop)
let currentRunLoop = RunLoop.current
XCTAssertNotNil(currentRunLoop)
let secondAccessOfMainLoop = RunLoop.main
XCTAssertEqual(mainRunLoop, secondAccessOfMainLoop, "fetching the main loop a second time should be equal")
XCTAssertTrue(mainRunLoop === secondAccessOfMainLoop, "fetching the main loop a second time should be identical")
let secondAccessOfCurrentLoop = RunLoop.current
XCTAssertEqual(currentRunLoop, secondAccessOfCurrentLoop, "fetching the current loop a second time should be equal")
XCTAssertTrue(currentRunLoop === secondAccessOfCurrentLoop, "fetching the current loop a second time should be identical")
// We can assume that the tests will be run on the main run loop
// so the current loop should be the main loop
XCTAssertEqual(mainRunLoop, currentRunLoop, "the current run loop should be the main loop")
}
func test_runLoopRunMode() {
let runLoop = RunLoop.current
let timeInterval = TimeInterval(0.05)
let endDate = Date(timeInterval: timeInterval, since: Date())
var flag = false
let dummyTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { _ in
flag = true
guard let runLoopMode = runLoop.currentMode else {
XCTFail("Run loop mode is not defined")
return
}
XCTAssertEqual(runLoopMode, RunLoopMode.defaultRunLoopMode)
}
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
let result = runLoop.run(mode: .defaultRunLoopMode, before: endDate)
XCTAssertFalse(result) // should be .Finished
XCTAssertTrue(flag)
}
func test_runLoopLimitDate() {
let runLoop = RunLoop.current
let timeInterval = TimeInterval(1)
let expectedTimeInterval = Date(timeInterval: timeInterval, since: Date()).timeIntervalSince1970
let dummyTimer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false) { _ in }
runLoop.add(dummyTimer, forMode: .defaultRunLoopMode)
guard let timerTickInterval = runLoop.limitDate(forMode: .defaultRunLoopMode)?.timeIntervalSince1970 else {
return
}
XCTAssertLessThan(abs(timerTickInterval - expectedTimeInterval), 0.01)
}
}