Skip to content

Commit

Permalink
Fix so that tests can be run in parallell with Testing (#12)
Browse files Browse the repository at this point in the history
* Fix so that tests can be run in parallell with Testing

* Fix

* Fix

* Fix

* Rename window

* Fix

* Add log

* Fix

* Clean
  • Loading branch information
materik authored Jan 15, 2025
1 parent 5a65b8b commit 11f85a1
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 9 deletions.
55 changes: 47 additions & 8 deletions Sources/SnapshotTestCase/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,48 @@ private extension Snapshot {
}
}

private class SnapshotWindow {
static var shared = SnapshotWindow()

@Published var window: UIWindow?

@MainActor
func new() async throws -> Self {
if window == nil {
window = UIWindow()
return self
}
for await window in $window.values {
if window == nil {
break
} else {
try await Task.sleep(for: .milliseconds(10))
}
}
return try await new()
}

func frame(_ frame: CGRect) -> Self {
window?.frame = frame
return self
}

func rootViewController(_ viewController: UIViewController) -> Self {
window?.rootViewController = viewController
return self
}

@MainActor
func render(_ render: () async throws -> UIImage) async throws -> UIImage {
window?.isHidden = false
let snapshot = try await render()
window?.isHidden = true
window?.removeFromSuperview()
window = nil
return snapshot
}
}

private extension Snapshot.TestCase {
private func frame(size: CGSize) -> CGRect {
CGRect(
Expand All @@ -190,11 +232,10 @@ private extension Snapshot.TestCase {
private func takeSnapshot(with config: SnapshotConfig.Config) async throws -> UIImage {
let size = config.size + CGSize(width: 0, height: Snapshot.renderOffsetY)
let (viewController, view) = try create(with: config, in: size)
let window = UIWindow(frame: CGRect(origin: .zero, size: size))
window.rootViewController = viewController
window.makeKeyAndVisible()
defer { window.removeFromSuperview() }
var snapshot = try await renderSnapshot(view: view, in: size)
var snapshot = try await SnapshotWindow.shared.new()
.frame(CGRect(origin: .zero, size: size))
.rootViewController(viewController)
.render { try await renderSnapshot(view: view, in: size) }
snapshot = try await crop(snapshot, to: size)
snapshot = try await resize(snapshot, to: Snapshot.renderScale)
return snapshot
Expand All @@ -208,9 +249,7 @@ private extension Snapshot.TestCase {
}

try await Task.sleep(for: .seconds(renderDelay))
await MainActor.run {
view.layer.render(in: context)
}
view.layer.render(in: context)

let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
55 changes: 55 additions & 0 deletions Tests/SnapshotTestCaseTests/TestingSnapshotTestCaseTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import SnapshotTestCase
import SwiftUI
import Testing

@Suite
class TestingSnapshotTestCaseTests: SnapshotTestCase {
@Test
func test() async throws {
try await verifySnapshot {
TestView()
}
}

@Test
func test2() async throws {
try await verifySnapshot(
config: SnapshotConfig().add(device: .d6dot1, interfaceStyle: .dark)
) {
let viewController = UIHostingController(rootView: TestView())
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let subRootView = Rectangle()
.foregroundColor(.green)
.clipShape(RoundedRectangle(cornerSize: .init(width: 32, height: 32)))
.edgesIgnoringSafeArea(.all)
guard let subview = UIHostingController(rootView: subRootView).view else {
return
}
viewController.view.addSubview(subview)
subview.frame = viewController.view.bounds
}
return viewController
}
}
}

private struct TestView: View {
@Environment(\.colorScheme) var colorScheme

var body: some View {
VStack {
switch colorScheme {
case .light: Text("Light")
case .dark: Text("Dark")
@unknown default: Text("Unknown")
}

Rectangle()
.foregroundColor(.blue)
.clipShape(RoundedRectangle(cornerSize: .init(width: 32, height: 32)))

Circle().foregroundColor(.yellow)
}
.edgesIgnoringSafeArea(.all)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SnapshotTestCaseTests: XCTestCase, SnapshotTestCase {
}
}

struct TestView: View {
private struct TestView: View {
@Environment(\.colorScheme) var colorScheme

var body: some View {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 11f85a1

Please sign in to comment.