Files
cutdex/packages/cutdex_native/tool/macos/file_picker_test.swift
lingjhf abc1868550
Gitea CI / Check code and tests (push) Successful in 6m37s
Gitea CI / Build Windows x64 app (push) Successful in 3m18s
Gitea CI / Build Android APK (push) Successful in 3m47s
Gitea CI / Audit and package Windows x64 (push) Successful in 1m7s
Gitea CI / Assemble and validate CI artifacts (push) Successful in 50s
Gitea CI / Gitea CI passed (push) Successful in 2s
Gitea Release / Prepare draft Release (push) Successful in 9s
Gitea Release / Upload Android to draft Release (push) Successful in 16s
Gitea Release / Upload Windows to draft Release (push) Successful in 22s
Gitea Release / Verify and publish Release (push) Successful in 14s
feat(workspace): 桌面工作区、主题与交互更新,发布 0.1.2
2026-10-08 18:17:12 +00:00

76 lines
2.8 KiB
Swift

import Cocoa
import FlutterMacOS
final class TestOpenPanel: NSOpenPanel {
var owner: NSWindow?
var reply: ((NSApplication.ModalResponse) -> Void)?
override var urls: [URL] { [URL(fileURLWithPath: "/tmp/a b.mp4")] }
override func beginSheetModal(for window: NSWindow,
completionHandler handler: @escaping (NSApplication.ModalResponse) -> Void) {
owner = window
reply = handler
}
override func runModal() -> NSApplication.ModalResponse {
fatalError("The picker must not block the application event loop")
}
}
@main
struct FilePickerTest {
static func main() {
_ = NSApplication.shared
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 640, height: 480),
styleMask: [.titled], backing: .buffered, defer: false)
let view = NSView()
window.contentView = view
let panel = TestOpenPanel()
let picker = NativeFilePickerBridge(view: view, makePanel: { panel })
let args = ["allowedExtensions": ["mp4", "wav"]]
func waitUntil(_ condition: () -> Bool) {
let deadline = Date().addingTimeInterval(2)
while !condition(), Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
precondition(condition(), "The asynchronous picker did not respond")
}
var replies = 0
var selected: [String]?
picker.pickFiles(arguments: args) { result in
replies += 1
selected = result as? [String]
}
waitUntil { panel.reply != nil }
precondition(panel.owner === window && replies == 0)
precondition(panel.canChooseFiles && !panel.canChooseDirectories)
precondition(panel.allowsMultipleSelection && !panel.allowedContentTypes.isEmpty)
var duplicateError: FlutterError?
picker.pickFiles(arguments: args) { duplicateError = $0 as? FlutterError }
waitUntil { duplicateError != nil }
precondition(duplicateError?.code == "picker_active" && replies == 0)
// Completion may arrive from AppKit off the main queue.
let reply = panel.reply!
DispatchQueue.global().async { reply(.OK) }
waitUntil { replies == 1 }
precondition(selected == ["/tmp/a b.mp4"])
panel.reply = nil
picker.pickFiles(arguments: args) { result in
precondition(Thread.isMainThread && result == nil)
replies += 1
}
waitUntil { panel.reply != nil }
panel.reply?(.cancel)
waitUntil { replies == 2 }
let detached = NSView()
let orphanPicker = NativeFilePickerBridge(view: detached)
var missingWindowError: FlutterError?
orphanPicker.pickFiles(arguments: args) { missingWindowError = $0 as? FlutterError }
waitUntil { missingWindowError != nil }
precondition(missingWindowError?.code == "window_unavailable")
print("Native file picker ownership, asynchronous completion, cancellation and busy guards passed")
}
}