-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60335c6
commit 17b2be4
Showing
13 changed files
with
524 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// DemoWatchPreviewTest.swift | ||
// Demo Watch AppTests | ||
// | ||
// Created by Noah Martin on 8/10/24. | ||
// | ||
|
||
import XCTest | ||
import SnapshottingTests | ||
|
||
final class DemoWatchPreviewTest: PreviewLayoutTest { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// DemoWatchSnapshotTest.swift | ||
// Demo Watch AppTests | ||
// | ||
// Created by Noah Martin on 8/10/24. | ||
// | ||
|
||
import Foundation | ||
import SnapshottingTests | ||
|
||
final class DemoWatchSnapshotTest: SnapshotTest { | ||
|
||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// DemoAppPreviewTest.swift | ||
// DemoAppTests | ||
// | ||
// Created by Noah Martin on 8/9/24. | ||
// | ||
|
||
import XCTest | ||
import SnapshottingTests | ||
|
||
final class DemoAppPreviewTest: PreviewLayoutTest { | ||
override func snapshotPreviews() -> [String]? { | ||
return nil | ||
} | ||
|
||
override func excludedSnapshotPreviews() -> [String]? { | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// DemoAppSnapshotTest.swift | ||
// DemoAppTests | ||
// | ||
// Created by Noah Martin on 8/10/24. | ||
// | ||
|
||
import Foundation | ||
import SnapshottingTests | ||
|
||
class DemoAppSnapshotTest: SnapshotTest { | ||
override func snapshotPreviews() -> [String]? { | ||
return nil | ||
} | ||
|
||
override func excludedSnapshotPreviews() -> [String]? { | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Sources/SnapshottingTests/EMGPreviewBaseTest+PreviewFilters.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// EMGPreviewBaseTest+PreviewFilters.swift | ||
// | ||
// | ||
// Created by Noah Martin on 8/9/24. | ||
// | ||
|
||
import Foundation | ||
import SnapshotPreviewsCore | ||
import SnapshottingTestsObjc | ||
|
||
extension PreviewFilters where Self: EMGPreviewBaseTest { | ||
@MainActor | ||
static func matchingPreviewTypes() -> [PreviewType] { | ||
let instance = self.create() | ||
return FindPreviews.findPreviews(included: instance.snapshotPreviews(), excluded: instance.excludedSnapshotPreviews()) | ||
} | ||
} | ||
|
||
extension EMGDiscoveredPreview { | ||
static func from(previewType: PreviewType) -> EMGDiscoveredPreview { | ||
let d = EMGDiscoveredPreview() | ||
d.typeName = previewType.typeName | ||
d.displayName = previewType.displayName | ||
d.numberOfPreviews = NSNumber(value: previewType.previews.count) | ||
return d | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// PreviewFilters.swift | ||
// | ||
// | ||
// Created by Noah Martin on 8/9/24. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol PreviewFilters { | ||
// Override to return a list of previews that should be snapshotted. | ||
// The default is null, which snapshots all previews. | ||
// Elements should be the type name of the preview, like "MyModule.MyView_Previews" | ||
func snapshotPreviews() -> [String]? | ||
|
||
func excludedSnapshotPreviews() -> [String]? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// PreviewLayoutTest.swift | ||
// | ||
// | ||
// Created by Noah Martin on 8/9/24. | ||
// | ||
|
||
import Foundation | ||
import SnapshotPreviewsCore | ||
import SnapshottingTestsObjc | ||
import SwiftUI | ||
|
||
// Test Xcode previews by forcing a layout pass of each one | ||
open class PreviewLayoutTest: EMGPreviewBaseTest, PreviewFilters { | ||
|
||
open func snapshotPreviews() -> [String]? { | ||
nil | ||
} | ||
|
||
open func excludedSnapshotPreviews() -> [String]? { | ||
nil | ||
} | ||
|
||
static private var previews: [PreviewType] = [] | ||
|
||
@MainActor | ||
open override class func discoverPreviews() -> [EMGDiscoveredPreview] { | ||
previews = matchingPreviewTypes() | ||
return previews.map { EMGDiscoveredPreview.from(previewType: $0) } | ||
} | ||
|
||
@MainActor | ||
open override func test(_ preview: EMGPreview) { | ||
let previewType = Self.previews.first { $0.typeName == preview.preview.typeName } | ||
guard let preview = previewType?.previews[preview.index.intValue] else { | ||
XCTFail("Preview not found") | ||
return | ||
} | ||
|
||
#if canImport(UIKit) && !os(watchOS) | ||
let hostingVC = UIHostingController(rootView: AnyView(preview.view())) | ||
#if os(visionOS) || os(watchOS) | ||
hostingVC.view.sizeThatFits(CGSize(width: 100, height: CGFloat.greatestFiniteMagnitude)) | ||
#else | ||
hostingVC.view.sizeThatFits(UIScreen.main.bounds.size) | ||
#endif | ||
#elseif canImport(AppKit) | ||
let hostingVC = NSHostingController(rootView: AnyView(preview.view())) | ||
_ = hostingVC.sizeThatFits(in: NSScreen.main!.frame.size) | ||
#else | ||
_ = ImageRenderer(content: AnyView(preview.view())).uiImage | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// SnapshotTest.swift | ||
// | ||
// | ||
// Created by Noah Martin on 8/9/24. | ||
// | ||
|
||
import Foundation | ||
import SnapshotPreviewsCore | ||
import SnapshottingTestsObjc | ||
|
||
// Generate snapshots of Xcode previews | ||
open class SnapshotTest: EMGPreviewBaseTest, PreviewFilters { | ||
|
||
open func snapshotPreviews() -> [String]? { | ||
nil | ||
} | ||
|
||
open func excludedSnapshotPreviews() -> [String]? { | ||
nil | ||
} | ||
|
||
private static func getRenderingStrategy() -> RenderingStrategy { | ||
#if canImport(UIKit) && !os(watchOS) && !os(visionOS) && !os(tvOS) | ||
return UIKitRenderingStrategy() | ||
#else | ||
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, visionOS 1.0, *) { | ||
SwiftUIRenderingStrategy() | ||
} else { | ||
preconditionFailure("Cannot snapshot on this device/os") | ||
} | ||
#endif | ||
} | ||
private let renderingStrategy = getRenderingStrategy() | ||
|
||
static private var previews: [SnapshotPreviewsCore.PreviewType] = [] | ||
|
||
@MainActor | ||
open override class func discoverPreviews() -> [EMGDiscoveredPreview] { | ||
previews = matchingPreviewTypes() | ||
return previews.map { EMGDiscoveredPreview.from(previewType: $0) } | ||
} | ||
|
||
@MainActor | ||
open override func test(_ preview: EMGPreview) { | ||
let previewType = Self.previews.first { $0.typeName == preview.preview.typeName } | ||
guard let preview = previewType?.previews[preview.index.intValue] else { | ||
XCTFail("Preview not found") | ||
return | ||
} | ||
|
||
var result: SnapshotResult? = nil | ||
let expectation = XCTestExpectation() | ||
renderingStrategy.render(preview: preview) { snapshotResult in | ||
result = snapshotResult | ||
expectation.fulfill() | ||
} | ||
wait(for: [expectation], timeout: 10) | ||
guard let result else { | ||
XCTFail("Did not render") | ||
return | ||
} | ||
do { | ||
let attachment = try XCTAttachment(image: result.image.get()) | ||
attachment.name = preview.displayName | ||
attachment.lifetime = .keepAlways | ||
add(attachment) | ||
} catch { | ||
XCTFail("Error \(error)") | ||
} | ||
} | ||
} |