Keep macOS client running in the menu bar when its window closes
Retain the main Flutter window, add reopen and explicit quit actions, and preserve its controller while hidden. Fix native test host paths and cover the window and menu lifecycle with XCTest.
This commit is contained in:
@@ -1,12 +1,49 @@
|
||||
import Cocoa
|
||||
import FlutterMacOS
|
||||
import XCTest
|
||||
@testable import MeshPort
|
||||
|
||||
class RunnerTests: XCTestCase {
|
||||
|
||||
func testExample() {
|
||||
// If you add code to the Runner application, consider adding tests here.
|
||||
// See https://developer.apple.com/documentation/xctest for more information about using XCTest.
|
||||
func testCloseRetainsControllerAndCanReopenSameWindow() {
|
||||
let window = MainFlutterWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
|
||||
styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
||||
window.isReleasedWhenClosed = false
|
||||
let controller = NSViewController()
|
||||
controller.view = NSView(frame: window.contentLayoutRect)
|
||||
window.contentViewController = controller
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
XCTAssertTrue(window.isVisible)
|
||||
window.performClose(nil)
|
||||
XCTAssertFalse(window.isVisible)
|
||||
XCTAssertTrue(window.contentViewController === controller)
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
XCTAssertTrue(window.isVisible)
|
||||
XCTAssertTrue(window.contentViewController === controller)
|
||||
window.orderOut(nil)
|
||||
}
|
||||
|
||||
func testMenuReopensRetainedWindowAndQuitIsExplicit() {
|
||||
let delegate = AppDelegate()
|
||||
let window = MainFlutterWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
|
||||
styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
||||
delegate.mainFlutterWindow = window
|
||||
delegate.applicationDidFinishLaunching(Notification(name: NSApplication.didFinishLaunchingNotification))
|
||||
XCTAssertFalse(delegate.applicationShouldTerminateAfterLastWindowClosed(NSApp))
|
||||
guard let menu = delegate.statusItem?.menu, let open = menu.items.first,
|
||||
let action = open.action, let quit = menu.items.last else {
|
||||
return XCTFail("Menu bar actions missing")
|
||||
}
|
||||
XCTAssertEqual(open.title, "打开 MeshPort")
|
||||
window.close()
|
||||
XCTAssertFalse(window.isVisible)
|
||||
XCTAssertTrue(NSApp.sendAction(action, to: open.target, from: open))
|
||||
XCTAssertTrue(window.isVisible)
|
||||
XCTAssertTrue(delegate.mainFlutterWindow === window)
|
||||
XCTAssertEqual(quit.title, "退出 MeshPort")
|
||||
XCTAssertTrue(quit.target === NSApp)
|
||||
XCTAssertEqual(quit.action, #selector(NSApplication.terminate(_:)))
|
||||
window.orderOut(nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user