|
| 1 | +import Cocoa |
| 2 | +import XCTest |
| 3 | +@testable import ChatUI |
| 4 | + |
| 5 | +private final class PointerPassthroughView: NSView { |
| 6 | + override func hitTest(_ point: NSPoint) -> NSView? { nil } |
| 7 | +} |
| 8 | + |
| 9 | +final class ChatLayerPointerVisibilityTests: XCTestCase { |
| 10 | + private var window: NSWindow! |
| 11 | + private var host: NSView! |
| 12 | + private var scene: NSView! |
| 13 | + private let point = NSPoint(x: 20, y: 25) |
| 14 | + |
| 15 | + override func setUp() { |
| 16 | + super.setUp() |
| 17 | + _ = NSApplication.shared |
| 18 | + window = NSWindow(contentRect: NSRect(x: 40, y: 30, width: 400, height: 300), styleMask: .borderless, backing: .buffered, defer: false) |
| 19 | + window.isReleasedWhenClosed = false |
| 20 | + let content = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 300)) |
| 21 | + window.contentView = content |
| 22 | + host = NSView(frame: NSRect(x: 31, y: 47, width: 330, height: 230)) |
| 23 | + content.addSubview(host) |
| 24 | + scene = NSView(frame: NSRect(x: 13, y: 17, width: 260, height: 180)) |
| 25 | + host.addSubview(scene) |
| 26 | + } |
| 27 | + |
| 28 | + override func tearDown() { |
| 29 | + window.contentView = nil |
| 30 | + window.close() |
| 31 | + window = nil |
| 32 | + host = nil |
| 33 | + scene = nil |
| 34 | + super.tearDown() |
| 35 | + } |
| 36 | + |
| 37 | + private func cover() -> NSView { |
| 38 | + let overlay = NSView(frame: scene.frame) |
| 39 | + host.addSubview(overlay, positioned: .above, relativeTo: scene) |
| 40 | + return overlay |
| 41 | + } |
| 42 | + |
| 43 | + func testUncoveredMessageCanReceiveHover() { |
| 44 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 45 | + } |
| 46 | + |
| 47 | + func testSuggestionsCoverMessageEvenWhenItsOwnHitTestSucceeds() { |
| 48 | + _ = cover() |
| 49 | + XCTAssertTrue(scene.hitTest(scene.convert(point, to: host)) === scene) |
| 50 | + XCTAssertFalse(chatLayerPointerIsExposed(scene, at: point)) |
| 51 | + } |
| 52 | + |
| 53 | + func testCoverAppearingAtStationaryPointerInvalidatesHover() { |
| 54 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 55 | + let overlay = cover() |
| 56 | + XCTAssertFalse(chatLayerPointerIsExposed(scene, at: point)) |
| 57 | + overlay.removeFromSuperview() |
| 58 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 59 | + } |
| 60 | + |
| 61 | + func testHiddenSuggestionsDoNotBlockMessage() { |
| 62 | + let overlay = cover() |
| 63 | + overlay.isHidden = true |
| 64 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 65 | + } |
| 66 | + |
| 67 | + func testPassthroughOverlayDoesNotBlockMessage() { |
| 68 | + host.addSubview(PointerPassthroughView(frame: scene.frame)) |
| 69 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 70 | + } |
| 71 | + |
| 72 | + func testNativeChildOfSceneRemainsExposed() { |
| 73 | + scene.addSubview(NSView(frame: NSRect(x: 10, y: 10, width: 50, height: 50))) |
| 74 | + XCTAssertTrue(chatLayerPointerIsExposed(scene, at: point)) |
| 75 | + } |
| 76 | + |
| 77 | + func testHiddenAndDetachedMessageCannotReceiveHover() { |
| 78 | + scene.isHidden = true |
| 79 | + XCTAssertFalse(chatLayerPointerIsExposed(scene, at: point)) |
| 80 | + scene.isHidden = false |
| 81 | + scene.removeFromSuperview() |
| 82 | + XCTAssertFalse(chatLayerPointerIsExposed(scene, at: point)) |
| 83 | + } |
| 84 | + |
| 85 | + func testPointerOutsideClippedHostCannotReceiveHover() { |
| 86 | + scene.setFrameSize(NSSize(width: 400, height: 180)) |
| 87 | + XCTAssertFalse(chatLayerPointerIsExposed(scene, at: NSPoint(x: 350, y: 25))) |
| 88 | + } |
| 89 | +} |
0 commit comments