Gitea CI / Build Android APK (pull_request) Canceled after 0s
Gitea CI / Build Windows x64 app (pull_request) Canceled after 0s
Gitea CI / Audit and package Windows x64 (pull_request) Canceled after 0s
Gitea CI / Assemble and validate CI artifacts (pull_request) Canceled after 0s
Gitea CI / Gitea CI passed (pull_request) Canceled after 0s
Gitea CI / Check code and tests (pull_request) Canceled after 6m29s
76 lines
2.8 KiB
Swift
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")
|
|
}
|
|
}
|