|
| 1 | +// |
| 2 | +// TimerUtilsTests.swift |
| 3 | +// OpenSwiftUICoreTests |
| 4 | + |
| 5 | +import OpenSwiftUICore |
| 6 | +import Testing |
| 7 | +import Foundation |
| 8 | + |
| 9 | +// MARK: - TimerUtilsTests |
| 10 | + |
| 11 | +private let isXCTestBackendEnabled = { |
| 12 | + let isXCTestBackendEnabled: Bool |
| 13 | + #if canImport(Darwin) |
| 14 | + // Even we do not use XCTest explicitly, swift-testing will still trigger XCTestMain under the hood on Apple platforms. |
| 15 | + isXCTestBackendEnabled = true |
| 16 | + #else |
| 17 | + isXCTestBackendEnabled = false |
| 18 | + #endif |
| 19 | + return isXCTestBackendEnabled |
| 20 | +}() |
| 21 | + |
| 22 | +@MainActor |
| 23 | +@Suite(.enabled(if: isXCTestBackendEnabled, "swift-testing does not pumping the run loop like XCTest which will call CFRunLoopRunInMode repeatedly.")) |
| 24 | +struct TimerUtilsTests { |
| 25 | + @Test |
| 26 | + func withDelayExecutesAfterSpecifiedTime() async throws { |
| 27 | + try await confirmation { confirmation in |
| 28 | + var callbackExecuted = false |
| 29 | + |
| 30 | + let startTime = Date() |
| 31 | + let delayInterval: TimeInterval = 0.2 |
| 32 | + |
| 33 | + let timer = withDelay(delayInterval) { |
| 34 | + callbackExecuted = true |
| 35 | + confirmation() |
| 36 | + } |
| 37 | + #expect(timer.isValid == true) |
| 38 | + #expect(callbackExecuted == false) |
| 39 | + try await Task.sleep(nanoseconds: 300_000_000) |
| 40 | + |
| 41 | + #expect(callbackExecuted == true) |
| 42 | + let elapsedTime = Date().timeIntervalSince(startTime) |
| 43 | + #expect(elapsedTime >= delayInterval) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + func withDelayCanBeCancelled() async throws { |
| 49 | + var callbackExecuted = false |
| 50 | + let timer = withDelay(0.2) { |
| 51 | + callbackExecuted = true |
| 52 | + } |
| 53 | + #expect(timer.isValid == true) |
| 54 | + |
| 55 | + timer.invalidate() |
| 56 | + try await Task.sleep(nanoseconds: 300_000_000) |
| 57 | + |
| 58 | + #expect(timer.isValid == false) |
| 59 | + #expect(callbackExecuted == false) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + func withDelayRunsOnMainRunLoop() async throws { |
| 64 | + try await confirmation { confirmation in |
| 65 | + let _ = withDelay(0.2) { |
| 66 | + #expect(Thread.isMainThread) |
| 67 | + confirmation() |
| 68 | + } |
| 69 | + try await Task.sleep(nanoseconds: 300_000_000) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +import XCTest |
| 75 | +final class TimerUtilsXCTests: XCTestCase { |
| 76 | + func testWithDelayExecutesAfterSpecifiedTime() async throws { |
| 77 | + let expectation = expectation(description: "withDelay body call") |
| 78 | + var callbackExecuted = false |
| 79 | + |
| 80 | + let startTime = Date() |
| 81 | + let delayInterval: TimeInterval = 0.2 |
| 82 | + |
| 83 | + let timer = withDelay(delayInterval) { |
| 84 | + callbackExecuted = true |
| 85 | + expectation.fulfill() |
| 86 | + } |
| 87 | + XCTAssertTrue(timer.isValid) |
| 88 | + XCTAssertFalse(callbackExecuted) |
| 89 | + await fulfillment(of: [expectation], timeout: 0.3) |
| 90 | + |
| 91 | + XCTAssertTrue(callbackExecuted) |
| 92 | + let elapsedTime = Date().timeIntervalSince(startTime) |
| 93 | + print("Elapsed time: \(elapsedTime)") |
| 94 | + #if !canImport(Darwin) |
| 95 | + // FIXE: The elapsed time is somehow not correct on non-Darwin platform |
| 96 | + throw XCTSkip("The elapsed time is somehow not correct on non-Darwin platform") |
| 97 | + #endif |
| 98 | + XCTAssertTrue(elapsedTime >= delayInterval) |
| 99 | + } |
| 100 | + |
| 101 | + func testWithDelayCanBeCancelled() async throws { |
| 102 | + var callbackExecuted = false |
| 103 | + let timer = withDelay(0.2) { |
| 104 | + callbackExecuted = true |
| 105 | + } |
| 106 | + XCTAssertTrue(timer.isValid) |
| 107 | + |
| 108 | + timer.invalidate() |
| 109 | + try await Task.sleep(nanoseconds: 300_000_000) |
| 110 | + XCTAssertFalse(timer.isValid) |
| 111 | + XCTAssertFalse(callbackExecuted) |
| 112 | + } |
| 113 | + |
| 114 | + func testWithDelayRunsOnMainRunLoop() async throws { |
| 115 | + let expectation = expectation(description: "withDelay body call") |
| 116 | + let _ = withDelay(0.2) { |
| 117 | + XCTAssertTrue(Thread.isMainThread) |
| 118 | + expectation.fulfill() |
| 119 | + } |
| 120 | + await fulfillment(of: [expectation], timeout: 0.3) |
| 121 | + } |
| 122 | +} |
0 commit comments