@testable import BaseKit import SwiftUI import XCTest /// Paul's bug in Base Mac 0.1.0: Shift Return sent the message. Return /// sends; Shift and Option Return are a new line at the cursor; an input /// method's Return is its own. final class ComposerReturnTests: XCTestCase { func testWhatReturnDoes() { XCTAssertEqual(ComposerReturn.action([], composing: false), .send) XCTAssertEqual(ComposerReturn.action(.shift, composing: false), .newline) XCTAssertEqual(ComposerReturn.action(.option, composing: false), .newline) XCTAssertEqual(ComposerReturn.action([.shift, .option], composing: false), .newline) XCTAssertEqual(ComposerReturn.action(.command, composing: false), .send) for modifiers: EventModifiers in [[], .shift, .option, .command] { XCTAssertEqual(ComposerReturn.action(modifiers, composing: true), .pass, "an input method's Return finishes its word") } XCTAssertTrue(ComposerReturn.keys.contains(.return)) } } #if os(macOS) import AppKit /// The real composer in a real window, driven by key events as the /// keyboard sends them. @MainActor final class ComposerKeyTests: XCTestCase { private var window: NSWindow! private var model: AskModel! private var transport: GatedTransport! override func setUp() { _ = NSApplication.shared transport = GatedTransport() model = AskModel(client: BaseClient(transport: transport) {}, schedule: { _, _ in }) window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 420, height: 300), styleMask: [.titled], backing: .buffered, defer: false) window.isReleasedWhenClosed = false window.contentView = NSHostingView(rootView: FocusedComposer(model: model).frame(width: 420)) window.makeKeyAndOrderFront(nil) for _ in 0..<40 where editor == nil { RunLoop.main.run(until: Date().addingTimeInterval(0.05)) } XCTAssertNotNil(editor, "the composer never took the keyboard") } override func tearDown() { window.close() } private var editor: NSTextView? { window.firstResponder as? NSTextView } private func type(_ text: String, cursor: Int? = nil, selecting length: Int = 0) throws { let editor = try XCTUnwrap(editor) editor.insertText(text, replacementRange: NSRange(location: 0, length: (editor.string as NSString).length)) editor.setSelectedRange(NSRange(location: cursor ?? (text as NSString).length, length: length)) settle() XCTAssertEqual(model.text, text) } private func press(_ modifiers: NSEvent.ModifierFlags = [], keyCode: UInt16 = 36, characters: String = "\r") throws { let event = try XCTUnwrap(NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: modifiers, timestamp: 0, windowNumber: window.windowNumber, context: nil, characters: characters, charactersIgnoringModifiers: characters, isARepeat: false, keyCode: keyCode)) window.sendEvent(event) settle() } private func settle() { RunLoop.main.run(until: Date().addingTimeInterval(0.15)) } private var sent: Bool { !transport.calls.isEmpty } func testReturnSends() throws { try type("What's on tomorrow?") try press() XCTAssertTrue(sent, "Return sends") } func testKeypadEnterSends() throws { try type("What's on tomorrow?") try press(.numericPad, keyCode: 76, characters: "\u{3}") XCTAssertTrue(sent, "the keypad's Enter sends") } func testShiftReturnIsANewLineAtTheEnd() throws { try type("Three things") try press(.shift) XCTAssertFalse(sent, "Shift Return sent the message") XCTAssertEqual(model.text, "Three things\n") XCTAssertEqual(editor?.selectedRange(), NSRange(location: 13, length: 0)) } func testShiftReturnIsANewLineAtTheCursor() throws { try type("Flour delivery", cursor: 5) try press(.shift) XCTAssertFalse(sent) XCTAssertEqual(model.text, "Flour\n delivery") XCTAssertEqual(editor?.selectedRange(), NSRange(location: 6, length: 0), "the cursor follows the new line") // And typing goes on from there. editor?.insertText("x", replacementRange: editor!.selectedRange()) settle() XCTAssertEqual(model.text, "Flour\nx delivery") } func testShiftReturnReplacesTheSelection() throws { try type("Flour, then Ana", cursor: 5, selecting: 7) try press(.shift) XCTAssertFalse(sent) XCTAssertEqual(model.text, "Flour\nAna") } func testOptionReturnIsANewLine() throws { try type("Flour delivery", cursor: 5) try press(.option) XCTAssertFalse(sent, "Option Return sent the message") XCTAssertEqual(model.text, "Flour\n delivery") } /// Return while an input method composes (marked text) finishes the /// word; nothing is sent until the next Return. func testReturnWhileComposingDoesNotSend() throws { try type("Tomorrow ") let editor = try XCTUnwrap(editor) editor.setMarkedText("あした", selectedRange: NSRange(location: 3, length: 0), replacementRange: NSRange(location: NSNotFound, length: 0)) XCTAssertTrue(editor.hasMarkedText()) try press() XCTAssertFalse(sent, "Return while composing sent the message") XCTAssertFalse(editor.hasMarkedText(), "the composition is finished") XCTAssertTrue(model.text.hasPrefix("Tomorrow あした"), model.text) try press() XCTAssertTrue(sent, "the next Return sends") } } extension ComposerKeyTests { /// The menu's Paste (or Paste and Match Style) as AppKit sends it: /// the item's will- and did-send around the field reading `board`. private func paste(from board: NSPasteboard, action: Selector = #selector(NSText.paste(_:))) throws { let editor = try XCTUnwrap(editor) let item = NSMenuItem(title: "Paste", action: action, keyEquivalent: "v") let menu = NSMenu(title: "Edit") menu.addItem(item) NotificationCenter.default.post(name: NSMenu.willSendActionNotification, object: menu, userInfo: ["MenuItem": item]) XCTAssertTrue(editor.readSelection(from: board)) NotificationCenter.default.post(name: NSMenu.didSendActionNotification, object: menu, userInfo: ["MenuItem": item]) settle() } private func richBoard() -> NSPasteboard { let board = NSPasteboard(name: .init("BaseKitTests.\(UUID().uuidString)")) board.clearContents() board.setString("
The flour comes Thursday: