Skip to content

Commit 02c70b3

Browse files
committed
fix: make recorder timestamp test deterministic
1 parent 40c2c56 commit 02c70b3

2 files changed

Lines changed: 25 additions & 72 deletions

File tree

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,4 @@
1-
import CoreGraphics
2-
import Foundation
31
import XCTest
4-
#if canImport(AppKit)
5-
import AppKit
6-
#elseif canImport(UIKit)
7-
import UIKit
8-
#endif
9-
10-
private func makeRecordingTestImage() -> RunnerImage {
11-
let context = CGContext(
12-
data: nil,
13-
width: 2,
14-
height: 2,
15-
bitsPerComponent: 8,
16-
bytesPerRow: 0,
17-
space: CGColorSpaceCreateDeviceRGB(),
18-
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
19-
)!
20-
context.setFillColor(CGColor(red: 0.2, green: 0.4, blue: 0.6, alpha: 1))
21-
context.fill(CGRect(x: 0, y: 0, width: 2, height: 2))
22-
let cgImage = context.makeImage()!
23-
#if canImport(AppKit)
24-
return NSImage(cgImage: cgImage, size: NSSize(width: 2, height: 2))
25-
#else
26-
return UIImage(cgImage: cgImage)
27-
#endif
28-
}
292

303
extension RunnerTests {
314
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
@@ -43,27 +16,12 @@ extension RunnerTests {
4316
}
4417
}
4518

46-
func testScreenRecorderClampsNonMonotonicFrameTimestamps() throws {
47-
let outputPath = URL(fileURLWithPath: NSTemporaryDirectory())
48-
.appendingPathComponent("agent-device-screen-recorder-\(UUID().uuidString).mp4")
49-
.path
50-
defer { try? FileManager.default.removeItem(atPath: outputPath) }
51-
52-
let recorder = ScreenRecorder(outputPath: outputPath, fps: 30)
53-
let image = makeRecordingTestImage()
54-
try recorder.startForTesting(image: image)
55-
defer { try? recorder.stop() }
19+
func testScreenRecorderClampsNonMonotonicFrameTimestamps() {
20+
let recorder = ScreenRecorder(outputPath: "", fps: 30)
5621

57-
XCTAssertEqual(
58-
recorder.appendForTesting(image: image, timestampValue: 100),
59-
100
60-
)
61-
// Non-vacuity: without the monotonicity guard, this append records 0 or fails instead of 101.
62-
XCTAssertEqual(
63-
recorder.appendForTesting(image: image, timestampValue: 0),
64-
101
65-
)
66-
XCTAssertNoThrow(try recorder.stop())
22+
XCTAssertEqual(recorder.allocateTimestampForTesting(100), 100)
23+
// Non-vacuity: the allocator must advance an accepted frame past the prior timestamp.
24+
XCTAssertEqual(recorder.allocateTimestampForTesting(0), 101)
6725
}
6826
#endif
6927
}

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+ScreenRecorder.swift‎

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,8 @@ extension RunnerTests {
199199
}
200200
guard input.isReadyForMoreMediaData else { return }
201201
guard let pixelBuffer = makePixelBuffer(from: cgImage) else { return }
202-
let nowUptime = ProcessInfo.processInfo.systemUptime
203-
if recordingStartUptime == nil {
204-
recordingStartUptime = nowUptime
205-
}
206-
let elapsed = max(0, nowUptime - (recordingStartUptime ?? nowUptime))
202+
let timestampValue = timestampValue(for: ProcessInfo.processInfo.systemUptime)
207203
let timescale = effectiveFps
208-
var timestampValue = Int64((elapsed * Double(timescale)).rounded(.down))
209-
if timestampValue <= lastTimestampValue {
210-
timestampValue = lastTimestampValue + 1
211-
}
212204
let timestamp = CMTime(value: timestampValue, timescale: timescale)
213205
if !adaptor.append(pixelBuffer, withPresentationTime: timestamp) {
214206
startError = writer.error ?? NSError(
@@ -221,6 +213,18 @@ extension RunnerTests {
221213
lastTimestampValue = timestampValue
222214
}
223215

216+
private func timestampValue(for nowUptime: TimeInterval) -> Int64 {
217+
if recordingStartUptime == nil {
218+
recordingStartUptime = nowUptime
219+
}
220+
let elapsed = max(0, nowUptime - (recordingStartUptime ?? nowUptime))
221+
let timestampValue = Int64((elapsed * Double(effectiveFps)).rounded(.down))
222+
if timestampValue <= lastTimestampValue {
223+
return lastTimestampValue + 1
224+
}
225+
return timestampValue
226+
}
227+
224228
private func shouldStop() -> Bool {
225229
lock.lock()
226230
defer { lock.unlock() }
@@ -264,25 +268,16 @@ extension RunnerTests {
264268

265269
#if AGENT_DEVICE_RUNNER_UNIT_TESTS
266270
extension RunnerTests.ScreenRecorder {
267-
func startForTesting(image: RunnerImage) throws {
268-
try start { image }
269-
lock.lock()
270-
timer?.cancel()
271-
timer = nil
272-
lock.unlock()
273-
}
274-
275271
@discardableResult
276-
func appendForTesting(image: RunnerImage, timestampValue: Int64) -> Int64 {
272+
func allocateTimestampForTesting(_ requestedTimestampValue: Int64) -> Int64 {
277273
lock.lock()
274+
defer { lock.unlock() }
275+
let nowUptime = ProcessInfo.processInfo.systemUptime
278276
recordingStartUptime =
279-
ProcessInfo.processInfo.systemUptime - Double(timestampValue) / Double(effectiveFps)
280-
lock.unlock()
281-
append(image: image)
282-
lock.lock()
283-
let appendedTimestamp = lastTimestampValue
284-
lock.unlock()
285-
return appendedTimestamp
277+
nowUptime - Double(requestedTimestampValue) / Double(effectiveFps)
278+
let allocatedTimestamp = timestampValue(for: nowUptime)
279+
lastTimestampValue = allocatedTimestamp
280+
return allocatedTimestamp
286281
}
287282
}
288283
#endif

0 commit comments

Comments
 (0)