-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenboomApp.swift
More file actions
450 lines (390 loc) · 17.8 KB
/
Copy pathScreenboomApp.swift
File metadata and controls
450 lines (390 loc) · 17.8 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import SwiftUI
import AVFoundation
@main
struct ScreenboomApp: App {
private let container: AppContainer
@State private var project: Project
@State private var recorderPanelManager = RecorderPanelManager()
@State private var exportPanelManager = ExportPanelManager()
@State private var testRecorderFlow: RecorderFlowController?
init() {
let environment = ProcessInfo.processInfo.environment
let container: AppContainer
if environment["XCTestConfigurationFilePath"] != nil || environment["SCREENBOOM_UI_TEST_MODE"] != nil {
let mode = environment["SCREENBOOM_UI_TEST_MODE"] ?? "default"
let uiTestStoreDirectory = FileManager.default.temporaryDirectory
.appendingPathComponent("screenboom-uitest-store-\(mode)", isDirectory: true)
try? FileManager.default.removeItem(at: uiTestStoreDirectory)
container = AppContainer.live(baseDirectory: uiTestStoreDirectory)
} else {
container = AppContainer.live()
}
self.container = container
let project = container.makeProject()
Self.bootstrapUITestStateIfNeeded(project, store: container.projectStore)
_project = State(initialValue: project)
// Recorder harness: embed RecorderBarView directly in main window
// (NSPanel is outside XCUI accessibility tree)
if let flow = Self.makeRecorderFlowIfNeeded() {
_testRecorderFlow = State(initialValue: flow)
}
}
var body: some Scene {
WindowGroup {
ZStack {
if let flow = testRecorderFlow {
// UI test harness: embed RecorderBarView directly in main window
// (NSPanel is outside XCUI accessibility tree)
VStack {
Spacer()
RecorderBarView(
flow: flow,
onComplete: { _ in },
onDismiss: {}
)
.padding()
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.sbPageBackground()
} else {
ContentView(project: project, store: container.projectStore)
}
}
.frame(minWidth: 1100, minHeight: 700)
.environment(recorderPanelManager)
.environment(exportPanelManager)
.onAppear {
container.projectStore.migrateIfNeeded()
let store = container.projectStore
project.onVideoLoaded = { id, size, duration in
store.update(id) { info in
info.videoWidth = Int(size.width)
info.videoHeight = Int(size.height)
info.videoDurationSeconds = duration
}
}
}
}
.defaultSize(width: 1400, height: 900)
.commands {
CommandGroup(replacing: .undoRedo) {
Button("Undo") { project.undo() }
.keyboardShortcut("z", modifiers: .command)
.disabled(!project.canUndo)
Button("Redo") { project.redo() }
.keyboardShortcut("z", modifiers: [.command, .shift])
.disabled(!project.canRedo)
}
}
}
// MARK: - UI Test Bootstrap
private static func bootstrapUITestStateIfNeeded(_ project: Project, store: ProjectStore) {
guard let mode = ProcessInfo.processInfo.environment["SCREENBOOM_UI_TEST_MODE"] else { return }
switch mode {
// ── Welcome Modes ──────────────────────────────────────────────
case "welcome":
resetToWelcome(project)
case "welcome_projects":
// Welcome screen with synthetic projects in the store grid
resetToWelcome(project)
let now = Date()
store.add(ProjectInfo(
name: "Demo Recording",
createdAt: now.addingTimeInterval(-3600),
lastOpenedAt: now.addingTimeInterval(-600),
videoWidth: 1920, videoHeight: 1080,
videoDurationSeconds: 45.2,
sourceFileName: "demo.mp4"
))
store.add(ProjectInfo(
name: "Product Tour",
createdAt: now.addingTimeInterval(-86400),
lastOpenedAt: now.addingTimeInterval(-7200),
videoWidth: 2560, videoHeight: 1440,
videoDurationSeconds: 120.0,
sourceFileName: "tour.mp4"
))
store.add(ProjectInfo(
name: "Bug Report",
createdAt: now.addingTimeInterval(-172800),
lastOpenedAt: now.addingTimeInterval(-86400),
videoWidth: 3840, videoHeight: 2160,
videoDurationSeconds: 8.5,
sourceFileName: "bug.mp4"
))
// ── Editor Modes (real video pipeline) ────────────────────────
case "editor", "editor_cursor":
bootstrapEditorMode(mode, project: project, store: store)
// ── Editor Modes (synchronous state for deterministic UI testing) ──
case "editor_selected", "editor_split",
"editor_cursor_zoom_selected":
bootstrapEditorFallback(mode, project: project)
default:
return
}
}
// MARK: - Recorder Harness
/// Creates a RecorderFlowController pre-set to a specific state for UI testing.
/// Returns nil for non-recorder modes.
private static func makeRecorderFlowIfNeeded() -> RecorderFlowController? {
guard let mode = ProcessInfo.processInfo.environment["SCREENBOOM_UI_TEST_MODE"] else { return nil }
switch mode {
case "recorder_configuring":
let flow = RecorderFlowController()
flow._setFlowStateForTesting(.configuring)
flow.captureMode = .display
return flow
case "recorder_recording":
let flow = RecorderFlowController()
flow._setFlowStateForTesting(.recording)
return flow
case "recorder_countdown":
let flow = RecorderFlowController()
flow._setFlowStateForTesting(.countdown(3))
return flow
case "recorder_failed":
let flow = RecorderFlowController()
flow._setFlowStateForTesting(.failed("Screen recording permission denied"))
return flow
default:
return nil
}
}
// MARK: - Editor Bootstrap (Real Video Pipeline)
private static func bootstrapEditorMode(_ mode: String, project: Project, store: ProjectStore) {
// Inject cursor metadata BEFORE video loads — hasCursorData must be true
// when the tab bar first renders, otherwise cursor/zoom tabs won't appear
let cursorModes = ["editor_cursor", "editor_cursor_zoom_selected"]
if cursorModes.contains(mode) {
injectCursorMetadata(project: project)
}
// Generate a real synthetic MP4 through AVAssetWriter (small + cached for speed)
let videoURL = generateTestVideo(
width: 640, height: 360,
duration: 2.0, frameRate: 15
)
guard let videoURL else {
// Fallback: set state directly (same as legacy harness)
bootstrapEditorFallback(mode, project: project)
return
}
// Route through real DI pipeline: createProject → loadVideo → AVAsset → segments
project.createProject(url: videoURL, store: store)
// Post-load setup: runs after loadVideo finishes asynchronously
let originalCallback = project.onVideoLoaded
project.onVideoLoaded = { id, size, duration in
originalCallback?(id, size, duration)
Self.applyPostLoadState(mode: mode, project: project)
}
}
/// Called after video finishes loading — sets mode-specific state
private static func applyPostLoadState(mode: String, project: Project) {
switch mode {
case "editor":
// Base editor — split once to get 2 segments
let midpoint = project.duration.seconds / 2
project.addSplit(at: midpoint)
case "editor_selected":
// Editor with segment pre-selected (shows Disable/Speed controls)
let midpoint = project.duration.seconds / 2
project.addSplit(at: midpoint)
if let firstSeg = project.segments.first {
project.selectedSegmentId = firstSeg.id
}
case "editor_split":
// Editor with 3 segments (has splitPoints → Remove Split available)
let dur = project.duration.seconds
project.addSplit(at: dur * 0.33)
project.addSplit(at: dur * 0.66)
if let secondSeg = project.segments.dropFirst().first {
project.selectedSegmentId = secondSeg.id
}
case "editor_cursor":
// Editor with cursor data — all 6 tabs visible (cursor metadata already injected)
let midpoint = project.duration.seconds / 2
project.addSplit(at: midpoint)
case "editor_cursor_zoom_selected":
// Editor with cursor data + zoom region pre-selected (cursor metadata already injected)
let midpoint = project.duration.seconds / 2
project.addSplit(at: midpoint)
// Add and select a zoom region
project.addZoomRegion(at: midpoint)
if let firstRegion = project.zoomRegions.first {
project.selectedZoomRegionID = firstRegion.id
project.selectedSegmentId = nil
}
default:
break
}
}
// MARK: - Synthetic Video Generator
/// Creates a real MP4 file using AVAssetWriter — routes through the full AVFoundation pipeline.
/// Cached: generates once per test suite run, reuses across launches for speed.
private static func generateTestVideo(
width: Int, height: Int,
duration: Double, frameRate: Int
) -> URL? {
let outputURL = FileManager.default.temporaryDirectory
.appendingPathComponent("screenboom-test-video-cached.mp4")
// Cache: reuse if already generated in this test session
if FileManager.default.fileExists(atPath: outputURL.path) {
return outputURL
}
guard let writer = try? AVAssetWriter(outputURL: outputURL, fileType: .mp4) else { return nil }
let videoSettings: [String: Any] = [
AVVideoCodecKey: AVVideoCodecType.h264,
AVVideoWidthKey: width,
AVVideoHeightKey: height,
]
let writerInput = AVAssetWriterInput(mediaType: .video, outputSettings: videoSettings)
let adaptor = AVAssetWriterInputPixelBufferAdaptor(
assetWriterInput: writerInput,
sourcePixelBufferAttributes: [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB,
kCVPixelBufferWidthKey as String: width,
kCVPixelBufferHeightKey as String: height,
]
)
writer.add(writerInput)
guard writer.startWriting() else { return nil }
writer.startSession(atSourceTime: .zero)
let totalFrames = Int(duration * Double(frameRate))
let frameDuration = CMTime(value: 1, timescale: CMTimeScale(frameRate))
for frameIndex in 0..<totalFrames {
while !writerInput.isReadyForMoreMediaData {
Thread.sleep(forTimeInterval: 0.01)
}
guard let pixelBuffer = createTestFrame(
width: width, height: height,
frameIndex: frameIndex, totalFrames: totalFrames
) else { continue }
let presentationTime = CMTimeMultiply(frameDuration, multiplier: Int32(frameIndex))
adaptor.append(pixelBuffer, withPresentationTime: presentationTime)
}
writerInput.markAsFinished()
let semaphore = DispatchSemaphore(value: 0)
writer.finishWriting { semaphore.signal() }
semaphore.wait()
return writer.status == .completed ? outputURL : nil
}
/// Creates a single frame — gradient from dark blue to coral, shifts per frame for visual variety
private static func createTestFrame(
width: Int, height: Int,
frameIndex: Int, totalFrames: Int
) -> CVPixelBuffer? {
var pixelBuffer: CVPixelBuffer?
let status = CVPixelBufferCreate(
kCFAllocatorDefault, width, height,
kCVPixelFormatType_32ARGB, nil, &pixelBuffer
)
guard status == kCVReturnSuccess, let buffer = pixelBuffer else { return nil }
CVPixelBufferLockBaseAddress(buffer, [])
defer { CVPixelBufferUnlockBaseAddress(buffer, []) }
guard let baseAddress = CVPixelBufferGetBaseAddress(buffer) else { return nil }
let bytesPerRow = CVPixelBufferGetBytesPerRow(buffer)
let ptr = baseAddress.assumingMemoryBound(to: UInt8.self)
let progress = Double(frameIndex) / Double(max(1, totalFrames - 1))
for y in 0..<height {
let rowFraction = Double(y) / Double(height)
// Gradient: dark blue at top → coral at bottom, shifts with time
let shift = progress * 0.3
let r = UInt8(min(255, max(0, (0.1 + rowFraction * 0.9 + shift) * 255)))
let g = UInt8(min(255, max(0, (0.08 + rowFraction * 0.27) * 255)))
let b = UInt8(min(255, max(0, (0.2 + (1.0 - rowFraction) * 0.5) * 255)))
for x in 0..<width {
let offset = y * bytesPerRow + x * 4
ptr[offset] = 255 // A
ptr[offset + 1] = r // R
ptr[offset + 2] = g // G
ptr[offset + 3] = b // B
}
}
return buffer
}
// MARK: - Cursor Metadata Injection
private static func injectCursorMetadata(project: Project) {
let metadata = CursorMetadataFile(
frameRate: 60,
sourceSize: .init(width: 640, height: 360),
captureOrigin: .init(x: 0, y: 0),
displayHeight: 360,
backingScaleFactor: 1.0,
events: [
CursorEvent(timestamp: 0.2, x: 100, y: 200, type: .move, button: nil),
CursorEvent(timestamp: 0.5, x: 150, y: 180, type: .click, button: .left),
CursorEvent(timestamp: 0.8, x: 300, y: 150, type: .keyDown, button: nil),
CursorEvent(timestamp: 1.2, x: 400, y: 100, type: .move, button: nil),
CursorEvent(timestamp: 1.5, x: 320, y: 180, type: .click, button: .left),
CursorEvent(timestamp: 1.8, x: 200, y: 100, type: .move, button: nil),
]
)
let metadataURL = FileManager.default.temporaryDirectory
.appendingPathComponent("screenboom-uitest-cursor-cached.json")
if let data = try? JSONEncoder().encode(metadata) {
try? data.write(to: metadataURL, options: .atomic)
project.cursorMetadataURL = metadataURL
project.loadCursorMetadata()
}
// Enable auto-zoom so zoom tab shows expanded controls in tests
project.cursorSettings.autoZoomEnabled = true
}
// MARK: - Helpers
private static func resetToWelcome(_ project: Project) {
project.currentProjectID = nil
project.sourceURL = nil
project.asset = nil
project.player = nil
project.playerItem = nil
project.videoSize = .zero
project.duration = .zero
project.splitPoints = []
project.segments = []
project.selectedSegmentId = nil
project.zoomRegions = []
project.selectedZoomRegionID = nil
project.thumbnails = []
project.currentTime = 0
project.playheadTime = 0
project.isLoadingProject = false
project.isClosingProject = false
}
/// Legacy fallback: sets state directly without a real video file
private static func bootstrapEditorFallback(_ mode: String, project: Project) {
project.currentProjectID = UUID()
project.videoSize = CGSize(width: 1920, height: 1080)
project.duration = CMTime(seconds: 12, preferredTimescale: 600)
project.segments = [
Segment(startTime: 0, endTime: 6, speed: 1.0, isEnabled: true),
Segment(startTime: 6, endTime: 12, speed: 1.0, isEnabled: true),
]
project.splitPoints = [6.0]
project.currentTime = 2
project.playheadTime = 2
project.isLoadingProject = false
project.isClosingProject = false
switch mode {
case "editor_selected":
project.selectedSegmentId = project.segments.first?.id
case "editor_split":
project.segments = [
Segment(startTime: 0, endTime: 4, speed: 1.0, isEnabled: true),
Segment(startTime: 4, endTime: 8, speed: 1.0, isEnabled: true),
Segment(startTime: 8, endTime: 12, speed: 1.0, isEnabled: true),
]
project.splitPoints = [4.0, 8.0]
project.selectedSegmentId = project.segments[1].id
case "editor_cursor", "editor_cursor_zoom_selected":
injectCursorMetadata(project: project)
if mode == "editor_cursor_zoom_selected" {
project.addZoomRegion(at: 3.0)
if let region = project.zoomRegions.first {
project.selectedZoomRegionID = region.id
project.selectedSegmentId = nil
}
}
default:
break
}
}
}