| index b83c3e8..1f2867d 100644 |
| --- a/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift |
| +++ b/native/apple/Tests/DocsWindowTests/DocsWindowCloserTests.swift |
| @@ -1,5 +1,6 @@ |
| #if os(macOS) |
| import AppKit |
| +import SwiftUI |
| import XCTest |
| @testable import DocsKit |
| |
| @@ -41,5 +42,55 @@ import XCTest |
| 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 |