diff --git a/crates/ui-egui/tests/it/dialog_focus.rs b/crates/ui-egui/tests/it/dialog_focus.rs index 559bdbe3d..ccbed7746 100644 --- a/crates/ui-egui/tests/it/dialog_focus.rs +++ b/crates/ui-egui/tests/it/dialog_focus.rs @@ -261,7 +261,11 @@ fn failed_rename_enter_keeps_error_visible() { h.run_steps(3); assert!(h.state().dialog.is_some(), "failed dialog stays open"); - assert!(h.state().message.as_ref().is_some_and(|(_, text)| !text.is_empty()), "Enter must leave the error visible: {:?}", h.state().message); + assert!( + h.state().message.as_ref().is_some_and(|(_, text)| text.contains("A sheet name can't contain any of these characters: : \\ / ? * [ ]")), + "Enter must leave the invalid-sheet-name error visible: {:?}", + h.state().message + ); assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Sheet1", "invalid rename must not change the sheet"); @@ -271,6 +275,15 @@ fn failed_rename_enter_keeps_error_visible() { assert!(h.state().message.is_none(), "second Enter dismisses error"); assert!(h.state().dialog.is_some(), "rename dialog stays editable"); + + // After dismissing the error, the dialog remains usable for a valid rename. + h.state_mut().dialog.as_mut().unwrap().values.insert("name".into(), json!("Renamed")); + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().dialog.is_none(), "valid rename confirms and closes dialog"); + assert!(h.state().message.is_none(), "valid rename leaves no error"); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Renamed"); } /// Mouse OK still shows the error; Escape dismisses only the message. @@ -294,3 +307,73 @@ fn failed_rename_click_keeps_error_until_escape() { assert!(h.state().message.is_none(), "Escape dismisses error"); assert!(h.state().dialog.is_some(), "Escape keeps dialog open"); } + +/// #2189: Clicking the message box's OK button dismisses only the error and leaves the dialog usable. +#[test] +fn failed_rename_message_box_ok_dismisses_error_and_recovers() { + let mut h = harness(); + h.state_mut().open_dialog("renameSheet", json!({})); + h.run_steps(4); + + h.state_mut().dialog.as_mut().unwrap().values.insert("name".into(), json!("a/b")); + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().message.is_some(), "invalid rename shows message box"); + assert!(h.state().dialog.is_some(), "rename dialog remains open behind message box"); + + // `message_box` renders `" OK "` while `dialogs::show::ok_cancel` renders `" OK "`. + assert_eq!(h.query_all_by_label(" OK ").count(), 1, "message box OK button is uniquely selectable"); + h.get_by_label(" OK ").click(); + h.run_steps(3); + + assert!(h.state().message.is_none(), "clicking message box OK dismisses the error"); + assert!(h.state().dialog.is_some(), "rename dialog stays open after dismissing message box"); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Sheet1"); + + // Verify recovery after dismissing via mouse OK. + h.state_mut().dialog.as_mut().unwrap().values.insert("name".into(), json!("Recovered")); + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().dialog.is_none(), "recovered rename closes dialog"); + assert!(h.state().message.is_none()); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Recovered"); +} + +/// #2189: A pre-existing `state.message` consumes Enter and Escape before the underlying dialog. +#[test] +fn existing_message_takes_enter_and_escape_before_open_dialog() { + let mut h = harness(); + h.state_mut().open_dialog("renameSheet", json!({})); + h.run_steps(4); + + h.state_mut().dialog.as_mut().unwrap().values.insert("name".into(), json!("LaterName")); + h.state_mut().message = Some(("Alert".into(), "Existing alert".into())); + h.run_steps(2); + + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().message.is_none(), "Enter dismisses pre-existing message"); + assert!(h.state().dialog.is_some(), "dialog stays open because message owned Enter"); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Sheet1", "draft name was not submitted"); + + h.state_mut().message = Some(("Alert".into(), "Second alert".into())); + h.run_steps(2); + + h.key_press(Key::Escape); + h.run_steps(3); + + assert!(h.state().message.is_none(), "Escape dismisses pre-existing message"); + assert!(h.state().dialog.is_some(), "dialog stays open because message owned Escape"); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "Sheet1"); + + // Once the message is gone, Enter confirms the still-open dialog. + h.key_press(Key::Enter); + h.run_steps(3); + + assert!(h.state().dialog.is_none(), "Enter confirms dialog after message dismissal"); + assert!(h.state().message.is_none()); + assert_eq!(h.state().session.active().unwrap().wb.active().unwrap().name, "LaterName"); +}