#if os(macOS) import AppKit import SwiftUI import XCTest @testable import DocsKit /// Close on the "No longer available" callout (Fable on #169, finding 11) /// goes through the window's own close, so DocsCloseGuard asks about unsent /// changes rather than the window simply going away. @MainActor final class DocsWindowCloserTests: XCTestCase { func testCloseAsksTheCloseGuardWhenChangesAreUnsent() async throws { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.isReleasedWhenClosed = false let content = NSView(frame: window.contentLayoutRect) window.contentView = content let guardView = DocsCloseGuardView() guardView.hasUnsaved = { true } content.addSubview(guardView) window.orderFrontRegardless() defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil) } XCTAssertTrue(window.delegate === guardView, "the guard is the window's delegate") let closer = DocsWindowCloser() closer.window = window closer.close() try await Task.sleep(for: .milliseconds(300)) XCTAssertTrue(window.isVisible, "unsent changes: the window stays") XCTAssertNotNil(window.attachedSheet, "and the close guard asks") } func testCloseClosesAWindowWithNothingUnsent() async throws { let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.isReleasedWhenClosed = false let content = NSView(frame: window.contentLayoutRect) window.contentView = content let guardView = DocsCloseGuardView() content.addSubview(guardView) window.orderFrontRegardless() let closer = DocsWindowCloser() closer.window = window closer.close() try await Task.sleep(for: .milliseconds(200)) XCTAssertFalse(window.isVisible) } /// Delete… (Fable on #178, finding 2): the confirmation is a sheet, and a /// fast server answers before it has gone. A close asked for while a /// sheet is still attached must still close the window, not beep and /// leave it empty. Here the dialog's own button closes at once, the /// fastest a delete can come back. func testCloseFromAConfirmationDialogClosesTheWindow() async throws { let closer = DocsWindowCloser() let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.isReleasedWhenClosed = false window.contentView = NSHostingView(rootView: DialogHost(closer: closer)) window.orderFrontRegardless() defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil); window.contentView = nil } try await until("the confirmation is up") { window.attachedSheet != nil } let sheet = try XCTUnwrap(window.attachedSheet) let confirm = try XCTUnwrap(Self.button("Delete document", in: sheet.contentView), "the dialog's Delete document button") XCTAssertTrue(closer.window === window) confirm.performClick(nil) try await until("the window closed") { !window.isVisible } } private struct Timeout: Error { let what: String } private func until(_ what: String, _ condition: () -> Bool) async throws { let deadline = Date().addingTimeInterval(5) while !condition() { guard Date() < deadline else { XCTFail("timed out waiting for \(what)"); throw Timeout(what: what) } while let event = NSApp.nextEvent(matching: .any, until: Date(), inMode: .default, dequeue: true) { NSApp.sendEvent(event) } try await Task.sleep(for: .milliseconds(10)) } } private static func button(_ title: String, in view: NSView?) -> NSButton? { guard let view else { return nil } if let button = view as? NSButton, button.title == title { return button } return view.subviews.lazy.compactMap { button(title, in: $0) }.first } } private struct DialogHost: View { let closer: DocsWindowCloser @State private var asking = false var body: some View { Color.clear.frame(width: 400, height: 300) .background { DocsWindowReader(closer: closer).frame(width: 0, height: 0) } .task { asking = true } .confirmationDialog(Text(verbatim: "Delete?"), isPresented: $asking, titleVisibility: .visible) { Button(role: .destructive) { closer.close() } label: { Text(verbatim: "Delete document") } Button(role: .cancel) {} label: { Text(verbatim: "Cancel") } } } } #endif