| 1 | #if os(macOS) |
| 2 | import AppKit |
| 3 | import SwiftUI |
| 4 | import XCTest |
| 5 | @testable import DocsKit |
| 6 | |
| 7 | /// Close on the "No longer available" callout (Fable on #169, finding 11) |
| 8 | /// goes through the window's own close, so DocsCloseGuard asks about unsent |
| 9 | /// changes rather than the window simply going away. |
| 10 | @MainActor final class DocsWindowCloserTests: XCTestCase { |
| 11 | func testCloseAsksTheCloseGuardWhenChangesAreUnsent() async throws { |
| 12 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| 13 | window.isReleasedWhenClosed = false |
| 14 | let content = NSView(frame: window.contentLayoutRect) |
| 15 | window.contentView = content |
| 16 | let guardView = DocsCloseGuardView() |
| 17 | guardView.hasUnsaved = { true } |
| 18 | content.addSubview(guardView) |
| 19 | window.orderFrontRegardless() |
| 20 | defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil) } |
| 21 | XCTAssertTrue(window.delegate === guardView, "the guard is the window's delegate") |
| 22 | |
| 23 | let closer = DocsWindowCloser() |
| 24 | closer.window = window |
| 25 | closer.close() |
| 26 | try await Task.sleep(for: .milliseconds(300)) |
| 27 | XCTAssertTrue(window.isVisible, "unsent changes: the window stays") |
| 28 | XCTAssertNotNil(window.attachedSheet, "and the close guard asks") |
| 29 | } |
| 30 | |
| 31 | func testCloseClosesAWindowWithNothingUnsent() async throws { |
| 32 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| 33 | window.isReleasedWhenClosed = false |
| 34 | let content = NSView(frame: window.contentLayoutRect) |
| 35 | window.contentView = content |
| 36 | let guardView = DocsCloseGuardView() |
| 37 | content.addSubview(guardView) |
| 38 | window.orderFrontRegardless() |
| 39 | let closer = DocsWindowCloser() |
| 40 | closer.window = window |
| 41 | closer.close() |
| 42 | try await Task.sleep(for: .milliseconds(200)) |
| 43 | XCTAssertFalse(window.isVisible) |
| 44 | } |
| 45 | |
| 46 | /// Delete… (Fable on #178, finding 2): the confirmation is a sheet, and a |
| 47 | /// fast server answers before it has gone. A close asked for while a |
| 48 | /// sheet is still attached must still close the window, not beep and |
| 49 | /// leave it empty. Here the dialog's own button closes at once, the |
| 50 | /// fastest a delete can come back. |
| 51 | func testCloseFromAConfirmationDialogClosesTheWindow() async throws { |
| 52 | let closer = DocsWindowCloser() |
| 53 | let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| 54 | window.isReleasedWhenClosed = false |
| 55 | window.contentView = NSHostingView(rootView: DialogHost(closer: closer)) |
| 56 | window.orderFrontRegardless() |
| 57 | defer { if let sheet = window.attachedSheet { window.endSheet(sheet) }; window.orderOut(nil); window.contentView = nil } |
| 58 | try await until("the confirmation is up") { window.attachedSheet != nil } |
| 59 | let sheet = try XCTUnwrap(window.attachedSheet) |
| 60 | let confirm = try XCTUnwrap(Self.button("Delete document", in: sheet.contentView), "the dialog's Delete document button") |
| 61 | XCTAssertTrue(closer.window === window) |
| 62 | confirm.performClick(nil) |
| 63 | try await until("the window closed") { !window.isVisible } |
| 64 | } |
| 65 | |
| 66 | private struct Timeout: Error { let what: String } |
| 67 | private func until(_ what: String, _ condition: () -> Bool) async throws { |
| 68 | let deadline = Date().addingTimeInterval(5) |
| 69 | while !condition() { |
| 70 | guard Date() < deadline else { XCTFail("timed out waiting for \(what)"); throw Timeout(what: what) } |
| 71 | while let event = NSApp.nextEvent(matching: .any, until: Date(), inMode: .default, dequeue: true) { NSApp.sendEvent(event) } |
| 72 | try await Task.sleep(for: .milliseconds(10)) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private static func button(_ title: String, in view: NSView?) -> NSButton? { |
| 77 | guard let view else { return nil } |
| 78 | if let button = view as? NSButton, button.title == title { return button } |
| 79 | return view.subviews.lazy.compactMap { button(title, in: $0) }.first |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private struct DialogHost: View { |
| 84 | let closer: DocsWindowCloser |
| 85 | @State private var asking = false |
| 86 | var body: some View { |
| 87 | Color.clear.frame(width: 400, height: 300) |
| 88 | .background { DocsWindowReader(closer: closer).frame(width: 0, height: 0) } |
| 89 | .task { asking = true } |
| 90 | .confirmationDialog(Text(verbatim: "Delete?"), isPresented: $asking, titleVisibility: .visible) { |
| 91 | Button(role: .destructive) { closer.close() } label: { Text(verbatim: "Delete document") } |
| 92 | Button(role: .cancel) {} label: { Text(verbatim: "Cancel") } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | #endif |
| 97 | |