Skip to content

Commit 9313474

Browse files
authored
Fix basic AppKitBackend layout test in CI (#112)
* Snapshot view to debug UI tests in CI * Always run snapshot upload (to see snapshot on fail) * Standardize AppKitBackend CI rendering env's dpi and color space Credit goes to pointfreeco/swift-snapshot-testing#533
1 parent 1f71097 commit 9313474

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Sources/AppKitBackend/AppKitBackend.swift

+10
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,16 @@ class NSSplitViewResizingDelegate: NSObject, NSSplitViewDelegate {
14021402
public class NSCustomWindow: NSWindow {
14031403
var resizeDelegate = ResizeDelegate()
14041404

1405+
/// Allows the backing scale factor to be overridden. Useful for keeping
1406+
/// UI tests consistent across devices.
1407+
///
1408+
/// Idea from https://github.com/pointfreeco/swift-snapshot-testing/pull/533
1409+
public var backingScaleFactorOverride: CGFloat?
1410+
1411+
public override var backingScaleFactor: CGFloat {
1412+
backingScaleFactorOverride ?? super.backingScaleFactor
1413+
}
1414+
14051415
class ResizeDelegate: NSObject, NSWindowDelegate {
14061416
var resizeHandler: ((SIMD2<Int>) -> Void)?
14071417

Tests/SwiftCrossUITests/SwiftCrossUITests.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ final class SwiftCrossUITests: XCTestCase {
6868
func testBasicLayout() throws {
6969
let backend = AppKitBackend()
7070
let window = backend.createWindow(withDefaultSize: SIMD2(200, 200))
71+
72+
// Idea taken from https://github.com/pointfreeco/swift-snapshot-testing/pull/533
73+
// and implemented in AppKitBackend.
74+
window.backingScaleFactorOverride = 1
75+
window.colorSpace = .genericRGB
76+
7177
let environment = EnvironmentValues(backend: backend)
7278
.with(\.window, window)
7379
let viewGraph = ViewGraph(
@@ -88,15 +94,14 @@ final class SwiftCrossUITests: XCTestCase {
8894

8995
XCTAssertEqual(
9096
result.size,
91-
ViewSize(fixedSize: SIMD2(94, 95)),
97+
ViewSize(fixedSize: SIMD2(88, 95)),
9298
"View update result mismatch"
9399
)
94100

95101
XCTAssert(
96102
result.preferences.onOpenURL == nil,
97103
"onOpenURL not nil"
98104
)
99-
100105
}
101106

102107
static func snapshotView(_ view: NSView) throws -> Data {

0 commit comments

Comments
 (0)