Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/AppKitBackend/AppKitBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,16 @@ class NSSplitViewResizingDelegate: NSObject, NSSplitViewDelegate {
public class NSCustomWindow: NSWindow {
var resizeDelegate = ResizeDelegate()

/// Allows the backing scale factor to be overridden. Useful for keeping
/// UI tests consistent across devices.
///
/// Idea from https://github.com/pointfreeco/swift-snapshot-testing/pull/533
public var backingScaleFactorOverride: CGFloat?

public override var backingScaleFactor: CGFloat {
backingScaleFactorOverride ?? super.backingScaleFactor
}

class ResizeDelegate: NSObject, NSWindowDelegate {
var resizeHandler: ((SIMD2<Int>) -> Void)?

Expand Down
9 changes: 7 additions & 2 deletions Tests/SwiftCrossUITests/SwiftCrossUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ final class SwiftCrossUITests: XCTestCase {
func testBasicLayout() throws {
let backend = AppKitBackend()
let window = backend.createWindow(withDefaultSize: SIMD2(200, 200))

// Idea taken from https://github.com/pointfreeco/swift-snapshot-testing/pull/533
// and implemented in AppKitBackend.
window.backingScaleFactorOverride = 1
window.colorSpace = .genericRGB

let environment = EnvironmentValues(backend: backend)
.with(\.window, window)
let viewGraph = ViewGraph(
Expand All @@ -88,15 +94,14 @@ final class SwiftCrossUITests: XCTestCase {

XCTAssertEqual(
result.size,
ViewSize(fixedSize: SIMD2(94, 95)),
ViewSize(fixedSize: SIMD2(88, 95)),
"View update result mismatch"
)

XCTAssert(
result.preferences.onOpenURL == nil,
"onOpenURL not nil"
)

}

static func snapshotView(_ view: NSView) throws -> Data {
Expand Down
Loading