3084: isolate fake dialog tests from AppKit thread eligibility

This commit is contained in:
Dylan M. Taylor committed 2026-11-14 16:46:11 -05:00
1 parent e8a68671ae
commit 82f7baafb2
1 file changed
+30 -5
+30 -5
View File
@@ -88,7 +88,12 @@ struct Errors {
/// Run blocking dialog `what`; one that can't open sends why to `errors` and counts as
/// cancelled (the blocking hooks can't return an error).
fn blocking<T>(errors: &Errors, what: &str, open: impl FnOnce() -> Option<T>) -> Option<T> {
guarded(what, open).unwrap_or_else(|e| {
blocking_on(on_main_thread(), errors, what, open)
}
// Keep the platform check in the production wrapper; tests inject eligibility only for fake pickers.
fn blocking_on<T>(main_thread: bool, errors: &Errors, what: &str, open: impl FnOnce() -> Option<T>) -> Option<T> {
guarded_on(main_thread, what, open).unwrap_or_else(|e| {
let _ = errors.tx.send(e);
errors.ctx.request_repaint();
None
@@ -175,8 +180,13 @@ fn title(dialog: &FileDialog) -> &'static str {
/// Open `dialog` with `open` through [`guarded`] and wait for its answer on a worker thread.
/// `done` always gets an answer: the paths, or why the dialog couldn't open or answer.
fn show(dialog: FileDialog, done: DialogDone, ctx: egui::Context, open: impl FnOnce(FileDialog) -> Answer) {
show_on(on_main_thread(), dialog, done, ctx, open);
}
// A fake picker has no AppKit object; inject eligibility without relaxing the real entry point.
fn show_on(main_thread: bool, dialog: FileDialog, done: DialogDone, ctx: egui::Context, open: impl FnOnce(FileDialog) -> Answer) {
let what = title(&dialog);
let answer = match guarded(what, || open(dialog)) {
let answer = match guarded_on(main_thread, what, || open(dialog)) {
Ok(answer) => answer,
Err(e) => {
done(Err(e));
@@ -282,7 +292,7 @@ mod tests {
fn answered(dialog: FileDialog, open: impl FnOnce(FileDialog) -> Answer) -> Result<Vec<String>, String> {
let (tx, rx) = channel();
show(dialog, Box::new(move |r| tx.send(r).unwrap()), egui::Context::default(), open);
show_on(true, dialog, Box::new(move |r| tx.send(r).unwrap()), egui::Context::default(), open);
rx.recv_timeout(Duration::from_secs(10)).expect("the dialog answers")
}
@@ -328,12 +338,27 @@ mod tests {
fn a_blocking_picker_that_cannot_open_reports_why() {
let (tx, rx) = channel();
let tx = Errors { tx, ctx: egui::Context::default() };
assert_eq!(blocking(&tx, "Open", || -> Option<u8> { panic!("{NIL_PANEL}") }), None);
assert_eq!(blocking_on(true, &tx, "Open", || -> Option<u8> { panic!("{NIL_PANEL}") }), None);
assert!(rx.try_recv().is_ok_and(|e| e.contains(NIL_PANEL)));
assert_eq!(blocking(&tx, "Open", || Some(3)), Some(3));
assert_eq!(blocking_on(true, &tx, "Open", || Some(3)), Some(3));
assert!(rx.try_recv().is_err(), "nothing to report");
}
/// Both dispatch paths refuse an off-thread request before invoking even a fake picker.
#[test]
fn dispatchers_refuse_off_thread_without_opening_a_picker() {
let (tx, rx) = channel();
show_on(false, FileDialog::OpenProject, Box::new(move |r| tx.send(r).unwrap()), egui::Context::default(), |_| panic!("picker must not open"));
assert!(rx.try_recv().unwrap().unwrap_err().contains("off the main thread"));
assert!(rx.try_recv().is_err(), "the request answers exactly once");
let (tx, rx) = channel();
let errors = Errors { tx, ctx: egui::Context::default() };
assert_eq!(blocking_on(false, &errors, "Open", || -> Option<u8> { panic!("picker must not open") }), None);
assert!(rx.try_recv().unwrap().contains("off the main thread"));
assert!(rx.try_recv().is_err(), "the error is reported exactly once");
}
/// #3084: every rfd dialog in the desktop app goes through this module (and so through
/// [`guarded`]); no other source file opens one directly.
#[test]