Files
plak/Tests/PlakUITests/WindowTests.swift
T

96 lines
4.4 KiB
Swift

import AppKit
@testable import PlakUI
import Testing
@MainActor
@Suite struct WindowTests {
@Test("the title bar double-click setting: Minimize minimises, None does nothing, anything else zooms")
func doubleClickSetting() {
#expect(TitleBarDoubleClick(setting: "Minimize") == .minimize)
#expect(TitleBarDoubleClick(setting: "None") == .none)
#expect(TitleBarDoubleClick(setting: "Maximize") == .zoom)
#expect(TitleBarDoubleClick(setting: nil) == .zoom)
}
@Test("the drag strip covers the top 28 pt over the full width, above the text, also after a resize")
func stripFrame() throws {
let page = try TestPage("one\n")
let strip = page.page.dragStrip
let root = page.page.view
for width: CGFloat in [900, 520] {
page.controller.window?.setFrame(NSRect(x: 0, y: 0, width: width, height: 640), display: false)
root.layoutSubtreeIfNeeded()
#expect(strip.frame == NSRect(x: 0, y: root.bounds.height - 28, width: root.bounds.width, height: 28))
#expect(root.hitTest(NSPoint(x: root.bounds.midX, y: root.bounds.height - 10)) === strip)
#expect(root.hitTest(NSPoint(x: root.bounds.midX, y: root.bounds.height - 40)) !== strip)
}
#expect(strip.scrollTarget === page.page.scrollView)
#expect(!strip.acceptsFirstResponder)
}
@Test("the drag strip leaves the window buttons alone and hands drops to the text view")
func stripPassesThrough() throws {
let page = try TestPage("one\n")
let strip = page.page.dragStrip
let root = page.page.view
let button = try #require(page.controller.window?.standardWindowButton(.closeButton))
let center = root.convert(NSPoint(x: button.frame.midX, y: button.frame.midY), from: button.superview)
#expect(strip.hitTest(center) == nil)
#expect(strip.dropTarget === page.textView)
#expect(Set(strip.registeredDraggedTypes).isSuperset(of: [.fileURL, .png, .tiff, .string]))
}
@Test("the pointer is the arrow over the drag strip and the I-beam below it, also after a resize")
func stripPointer() throws {
let page = try TestPage("one\n")
let root = page.page.view
let textView = page.textView
#expect(textView.dragStrip === page.page.dragStrip)
for width: CGFloat in [900, 520] {
page.controller.window?.setFrame(NSRect(x: 0, y: 0, width: width, height: 640), display: false)
root.layoutSubtreeIfNeeded()
for x in [4, root.bounds.midX, root.bounds.maxX - 4] {
#expect(textView.isUnderDragStrip(root.convert(NSPoint(x: x, y: root.bounds.height - 2), to: nil)))
#expect(textView.isUnderDragStrip(root.convert(NSPoint(x: x, y: root.bounds.height - 27), to: nil)))
#expect(!textView.isUnderDragStrip(root.convert(NSPoint(x: x, y: root.bounds.height - 29), to: nil)))
}
let strip = try #require(textView.dragStripRect)
#expect(strip.minY == textView.visibleRect.minY)
#expect(strip.height == 28)
}
}
@Test("the window follows you to the active desktop and shows over a fullscreen app")
func followsSpaces() throws {
let page = try TestPage("one\n")
let behavior = try #require(page.controller.window?.collectionBehavior)
#expect(behavior.contains(.moveToActiveSpace))
#expect(behavior.contains(.fullScreenAuxiliary))
}
@Test("Esc hides Plak, but during an input-method composition it only cancels the composition")
func escHides() throws {
let page = try TestPage("one\n")
var hides = 0
page.controller.onHide = { hides += 1 }
page.caretAtEnd()
page.textView.setMarkedText("ka", selectedRange: NSRange(location: 2, length: 0), replacementRange: page.textView.selectedRange())
#expect(page.textView.hasMarkedText())
page.textView.cancelOperation(nil)
#expect(hides == 0)
#expect(!page.textView.hasMarkedText())
page.textView.cancelOperation(nil)
#expect(hides == 1)
}
@Test("the close button and ⌘W hide Plak the same way as Esc")
func closeHides() throws {
let page = try TestPage("one\n")
var hides = 0
page.controller.onHide = { hides += 1 }
let window = try #require(page.controller.window)
#expect(!page.controller.windowShouldClose(window))
#expect(hides == 1)
}
}