|
| 1 | +// |
| 2 | +// NSHostingViewRootTransformTests.swift |
| 3 | +// OpenSwiftUITests |
| 4 | + |
| 5 | +#if os(macOS) |
| 6 | +import AppKit |
| 7 | +import OpenAttributeGraphShims |
| 8 | +@_spi(ForOpenSwiftUIOnly) |
| 9 | +@testable import OpenSwiftUI |
| 10 | +@_spi(ForOpenSwiftUIOnly) |
| 11 | +import OpenSwiftUICore |
| 12 | +import OpenSwiftUITestsSupport |
| 13 | +import Testing |
| 14 | + |
| 15 | +@MainActor |
| 16 | +@Suite(.disabled(if: attributeGraphVendor == .oag), .tags(.aigc)) |
| 17 | +struct NSHostingViewRootTransformTests { |
| 18 | + @Test |
| 19 | + func detachedHostProvidesIdentityTransform() throws { |
| 20 | + let host = NSHostingView(rootView: EmptyView()) |
| 21 | + let provider = try #require(host.as(RootTransformProvider.self)) |
| 22 | + let transform = provider.rootTransform() |
| 23 | + #expect(transform.convert(.globalToSpace(.local), point: CGPoint(x: 20, y: 15)) == CGPoint(x: 20, y: 15)) |
| 24 | + } |
| 25 | + |
| 26 | + @Test(arguments: [ |
| 27 | + (Semantics.v2, CGPoint(x: 160, y: 175)), |
| 28 | + (Semantics.v3, CGPoint(x: 160, y: 225)), |
| 29 | + (Semantics.v6, CGPoint(x: 160, y: 225)), |
| 30 | + ]) |
| 31 | + func windowCoordinatesReachHost(semantics: Semantics, globalPoint: CGPoint) throws { |
| 32 | + try semantics.test { |
| 33 | + let fixture = Fixture(rootView: EmptyView()) |
| 34 | + defer { fixture.window.close() } |
| 35 | + _ = try #require(fixture.host.as(RootTransformProvider.self)) |
| 36 | + let transform = fixture.host.updateViewGraph { $0.transform } |
| 37 | + #expect(transform.convert(.globalToSpace(.local), point: globalPoint) == CGPoint(x: 20, y: 15)) |
| 38 | + #expect(transform.convert(.localToSpace(.global), point: CGPoint(x: 20, y: 15)) == globalPoint) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + func geometryChangesInvalidateRootTransform() throws { |
| 44 | + try Semantics.v6.test { |
| 45 | + let fixture = Fixture(rootView: EmptyView()) |
| 46 | + defer { fixture.window.close() } |
| 47 | + _ = try #require(fixture.host.as(RootTransformProvider.self)) |
| 48 | + let initial = fixture.host.updateViewGraph { $0.transform } |
| 49 | + #expect(initial.convert(.localToSpace(.global), point: .zero) == CGPoint(x: 140, y: 210)) |
| 50 | + |
| 51 | + fixture.host.setFrameOrigin(CGPoint(x: 60, y: 40)) |
| 52 | + #expect(fixture.host.propertiesNeedingUpdate.contains(.transform)) |
| 53 | + let moved = fixture.host.updateViewGraph { $0.transform } |
| 54 | + #expect(moved.convert(.localToSpace(.global), point: .zero) == CGPoint(x: 160, y: 200)) |
| 55 | + |
| 56 | + fixture.parent.setFrameOrigin(CGPoint(x: 150, y: 100)) |
| 57 | + #expect(fixture.host.propertiesNeedingUpdate.contains(.transform)) |
| 58 | + let ancestorMoved = fixture.host.updateViewGraph { $0.transform } |
| 59 | + #expect(ancestorMoved.convert(.localToSpace(.global), point: .zero) == CGPoint(x: 210, y: 180)) |
| 60 | + |
| 61 | + fixture.host.removeFromSuperview() |
| 62 | + let detached = fixture.host.updateViewGraph { $0.transform } |
| 63 | + #expect(detached.convert(.localToSpace(.global), point: CGPoint(x: 20, y: 15)) == CGPoint(x: 20, y: 15)) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + func windowMouseEventBindsAfterAncestorMovement() throws { |
| 69 | + try Semantics.v6.test { |
| 70 | + let fixture = Fixture(rootView: Color.red.onTapGesture {}) |
| 71 | + defer { fixture.window.close() } |
| 72 | + let root = try #require(fixture.host.updateViewGraph { |
| 73 | + $0.instantiateIfNeeded() |
| 74 | + return $0.responderNode |
| 75 | + }) |
| 76 | + let event = MouseEvent( |
| 77 | + timestamp: .zero, |
| 78 | + button: .primary, |
| 79 | + phase: .began, |
| 80 | + location: .zero, |
| 81 | + globalLocation: CGPoint(x: 160, y: 225), |
| 82 | + modifiers: [] |
| 83 | + ) |
| 84 | + #expect(root.bindEvent(event) != nil) |
| 85 | + |
| 86 | + fixture.parent.setFrameOrigin(CGPoint(x: 300, y: 100)) |
| 87 | + let movedRoot = try #require(fixture.host.updateViewGraph { $0.responderNode }) |
| 88 | + #expect(movedRoot.bindEvent(event) == nil) |
| 89 | + var movedEvent = event |
| 90 | + movedEvent.globalLocation = CGPoint(x: 360, y: 205) |
| 91 | + #expect(movedRoot.bindEvent(movedEvent) != nil) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + func geometryObserverDoesNotRetainHost() throws { |
| 97 | + weak var weakHost: NSHostingView<EmptyView>? |
| 98 | + try autoreleasepool { |
| 99 | + let host = NSHostingView(rootView: EmptyView()) |
| 100 | + weakHost = host |
| 101 | + let provider = try #require(host.as(RootTransformProvider.self)) |
| 102 | + _ = provider.rootTransform() |
| 103 | + } |
| 104 | + #expect(weakHost == nil) |
| 105 | + } |
| 106 | + |
| 107 | + @Test(arguments: [NSWindow.StyleMask.borderless, .titled]) |
| 108 | + func nativeMouseClickRunsTapAction(styleMask: NSWindow.StyleMask) throws { |
| 109 | + try Semantics.v6.test { |
| 110 | + var tapCount = 0 |
| 111 | + let fixture = Fixture(rootView: Color.red.onTapGesture { tapCount += 1 }, styleMask: styleMask) |
| 112 | + defer { fixture.window.close() } |
| 113 | + fixture.host.updateViewGraph { $0.instantiateIfNeeded() } |
| 114 | + let recognizer = try #require(fixture.host.gestureRecognizers.first as? AppKitGestureRecognizer) |
| 115 | + let location = fixture.host.convert(CGPoint(x: 20, y: 15), to: nil) |
| 116 | + let down = try #require(NSEvent.mouseEvent( |
| 117 | + with: .leftMouseDown, |
| 118 | + location: location, |
| 119 | + modifierFlags: [], |
| 120 | + timestamp: 1, |
| 121 | + windowNumber: fixture.window.windowNumber, |
| 122 | + context: nil, |
| 123 | + eventNumber: 1, |
| 124 | + clickCount: 1, |
| 125 | + pressure: 1 |
| 126 | + )) |
| 127 | + let up = try #require(NSEvent.mouseEvent( |
| 128 | + with: .leftMouseUp, |
| 129 | + location: location, |
| 130 | + modifierFlags: [], |
| 131 | + timestamp: 1.1, |
| 132 | + windowNumber: fixture.window.windowNumber, |
| 133 | + context: nil, |
| 134 | + eventNumber: 2, |
| 135 | + clickCount: 1, |
| 136 | + pressure: 0 |
| 137 | + )) |
| 138 | + Update.perform { |
| 139 | + recognizer.mouseDown(with: down) |
| 140 | + recognizer.mouseUp(with: up) |
| 141 | + } |
| 142 | + #expect(tapCount == 1) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @MainActor |
| 147 | + private struct Fixture<Content: View> { |
| 148 | + let window: NSWindow |
| 149 | + let parent: NSView |
| 150 | + let host: NSHostingView<Content> |
| 151 | + |
| 152 | + init(rootView: Content, styleMask: NSWindow.StyleMask = .borderless) { |
| 153 | + _ = NSApplication.shared |
| 154 | + window = NSWindow( |
| 155 | + contentRect: CGRect(x: 0, y: 0, width: 600, height: 400), |
| 156 | + styleMask: styleMask, |
| 157 | + backing: .buffered, |
| 158 | + defer: false |
| 159 | + ) |
| 160 | + window.isReleasedWhenClosed = false |
| 161 | + let container = NSView(frame: CGRect(x: 0, y: 0, width: 600, height: 400)) |
| 162 | + window.contentView = container |
| 163 | + parent = NSView(frame: CGRect(x: 100, y: 80, width: 400, height: 260)) |
| 164 | + container.addSubview(parent) |
| 165 | + host = NSHostingView(rootView: rootView) |
| 166 | + host.sizingOptions = [] |
| 167 | + host.frame = CGRect(x: 40, y: 30, width: 100, height: 80) |
| 168 | + parent.addSubview(host) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | +#endif |
0 commit comments