From 3be5b7f28ef4eff6cd65d0132261c6da3a679772 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 19 Jan 2025 14:54:53 -0800 Subject: [PATCH] Extract BaseTests class --- .../BaseTests.swift | 50 +++++++++++++++++++ .../SJSAssetExportSessionTests.swift | 40 +-------------- 2 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 Tests/SJSAssetExportSessionTests/BaseTests.swift diff --git a/Tests/SJSAssetExportSessionTests/BaseTests.swift b/Tests/SJSAssetExportSessionTests/BaseTests.swift new file mode 100644 index 0000000..fcb62cd --- /dev/null +++ b/Tests/SJSAssetExportSessionTests/BaseTests.swift @@ -0,0 +1,50 @@ +// +// BaseTests.swift +// SJSAssetExportSession +// +// Created by Sami Samhuri on 2025-01-19. +// + +import AVFoundation +import Foundation +import Testing + +class BaseTests { + func resourceURL(named name: String) -> URL { + Bundle.module.resourceURL!.appending(component: name) + } + + func makeAsset(url: URL) -> sending AVAsset { + AVURLAsset(url: url, options: [ + AVURLAssetPreferPreciseDurationAndTimingKey: true, + ]) + } + + func makeTemporaryURL(function: String = #function) -> AutoDestructingURL { + let timestamp = Int(Date.now.timeIntervalSince1970) + let f = function.replacing(/[\(\)]/, with: { _ in "" }) + let filename = "\(Self.self)_\(f)_\(timestamp).mp4" + let url = URL.temporaryDirectory.appending(component: filename) + return AutoDestructingURL(url: url) + } + + func makeVideoComposition( + assetURL: URL, + size: CGSize? = nil, + fps: Int? = nil + ) async throws -> sending AVMutableVideoComposition { + let asset = makeAsset(url: assetURL) + let videoComposition = try await AVMutableVideoComposition.videoComposition( + withPropertiesOf: asset + ) + if let size { + videoComposition.renderSize = size + } + if let fps { + let seconds = 1.0 / TimeInterval(fps) + videoComposition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid + videoComposition.frameDuration = CMTime(seconds: seconds, preferredTimescale: 600) + } + return videoComposition + } +} diff --git a/Tests/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift b/Tests/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift index f476d71..1bf63c5 100644 --- a/Tests/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift +++ b/Tests/SJSAssetExportSessionTests/SJSAssetExportSessionTests.swift @@ -10,45 +10,7 @@ import CoreLocation import SJSAssetExportSession import Testing -final class ExportSessionTests { - private func resourceURL(named name: String) -> URL { - Bundle.module.resourceURL!.appending(component: name) - } - - private func makeAsset(url: URL) -> sending AVAsset { - AVURLAsset(url: url, options: [ - AVURLAssetPreferPreciseDurationAndTimingKey: true, - ]) - } - - private func makeTemporaryURL(function: String = #function) -> AutoDestructingURL { - let timestamp = Int(Date.now.timeIntervalSince1970) - let f = function.replacing(/[\(\)]/, with: { _ in "" }) - let filename = "\(Self.self)_\(f)_\(timestamp).mp4" - let url = URL.temporaryDirectory.appending(component: filename) - return AutoDestructingURL(url: url) - } - - private func makeVideoComposition( - assetURL: URL, - size: CGSize? = nil, - fps: Int? = nil - ) async throws -> sending AVMutableVideoComposition { - let asset = makeAsset(url: assetURL) - let videoComposition = try await AVMutableVideoComposition.videoComposition( - withPropertiesOf: asset - ) - if let size { - videoComposition.renderSize = size - } - if let fps { - let seconds = 1.0 / TimeInterval(fps) - videoComposition.sourceTrackIDForFrameTiming = kCMPersistentTrackID_Invalid - videoComposition.frameDuration = CMTime(seconds: seconds, preferredTimescale: 600) - } - return videoComposition - } - +final class ExportSessionTests: BaseTests { @Test func test_sugary_export_720p_h264_24fps() async throws { let sourceURL = resourceURL(named: "test-4k-hdr-hevc-30fps.mov") let destinationURL = makeTemporaryURL()