From 85cf6619ed6f2eb8d8f543fd3f2cce19d79798a5 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 6 Mar 2025 21:06:51 +0000 Subject: [PATCH] Fixes #3956. MessageBox doesn't return the index of IsDefault button (#3958) * Fixes #3956. MessageBox doesn't return the index of IsDefault button * Change to Theory test. * Fix unit test 'Error opening terminal: unknown.' * Remove RunningUnitTests = true because constructor already set it. --------- Co-authored-by: Tig --- Terminal.Gui/Views/MessageBox.cs | 4 ++++ Tests/UnitTests/Dialogs/MessageBoxTests.cs | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/MessageBox.cs b/Terminal.Gui/Views/MessageBox.cs index cd14dfc4f..31b4a41ec 100644 --- a/Terminal.Gui/Views/MessageBox.cs +++ b/Terminal.Gui/Views/MessageBox.cs @@ -374,6 +374,10 @@ public static class MessageBox { Clicked = (int)btn.Data!; } + else + { + Clicked = defaultButton; + } e.Cancel = true; Application.RequestStop (); diff --git a/Tests/UnitTests/Dialogs/MessageBoxTests.cs b/Tests/UnitTests/Dialogs/MessageBoxTests.cs index 50765272f..c873686f7 100644 --- a/Tests/UnitTests/Dialogs/MessageBoxTests.cs +++ b/Tests/UnitTests/Dialogs/MessageBoxTests.cs @@ -504,5 +504,25 @@ public class MessageBoxTests Application.Run (top); top.Dispose (); } -} + [Theory] + [SetupFakeDriver] + [MemberData (nameof (AcceptingKeys))] + public void Button_IsDefault_True_Return_His_Index_On_Accepting (Key key) + { + Application.Init (); + + Application.Iteration += (_, _) => Assert.True (Application.RaiseKeyDownEvent (key)); + int res = MessageBox.Query ("hey", "IsDefault", "Yes", "No"); + + Assert.Equal (0, res); + + Application.Shutdown (); + } + + public static IEnumerable AcceptingKeys () + { + yield return [Key.Enter]; + yield return [Key.Space]; + } +}