From 1cfe81a28a87bf8d52cdc74f3c3bae300e0aff10 Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 2 Oct 2024 13:32:32 -0600 Subject: [PATCH] Fixed Dialog/MessageBox tests --- Terminal.Gui/View/View.Drawing.cs | 4 ++-- Terminal.Gui/View/View.Keyboard.cs | 2 +- Terminal.Gui/Views/RadioGroup.cs | 30 ++++++++++++++------------ UnitTests/Application/MainLoopTests.cs | 4 ++-- UnitTests/Dialogs/DialogTests.cs | 8 +++---- UnitTests/Dialogs/MessageBoxTests.cs | 26 ++++++++++++++++++---- 6 files changed, 47 insertions(+), 27 deletions(-) diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index d2628cd93..06cd33890 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -344,8 +344,8 @@ public partial class View // Drawing APIs { DrawHotString ( text, - Enabled ? GetHotNormalColor () : ColorScheme.Disabled, - Enabled ? GetNormalColor () : ColorScheme.Disabled + Enabled ? GetHotNormalColor () : ColorScheme!.Disabled, + Enabled ? GetNormalColor () : ColorScheme!.Disabled ); } } diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index 7ede131bc..8db31c656 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -47,7 +47,7 @@ public partial class View // Keyboard APIs /// opened. /// /// - /// View subclasses can use< see cref="View.AddCommand(Command,Func{CommandContext,System.Nullable{bool}})"/> to + /// View subclasses can use to /// define the /// behavior of the hot key. /// diff --git a/Terminal.Gui/Views/RadioGroup.cs b/Terminal.Gui/Views/RadioGroup.cs index 301e62d98..6129bef54 100644 --- a/Terminal.Gui/Views/RadioGroup.cs +++ b/Terminal.Gui/Views/RadioGroup.cs @@ -130,8 +130,10 @@ public class RadioGroup : View, IDesignable, IOrientation return true; }); - _orientationHelper = new (this); - _orientationHelper.Orientation = Orientation.Vertical; + _orientationHelper = new (this) + { + Orientation = Orientation.Vertical + }; _orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e); _orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e); @@ -181,7 +183,7 @@ public class RadioGroup : View, IDesignable, IOrientation /// public bool DoubleClickAccepts { get; set; } = true; - private void RadioGroup_MouseClick (object sender, MouseEventEventArgs e) + private void RadioGroup_MouseClick (object? sender, MouseEventEventArgs e) { if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) { @@ -191,13 +193,13 @@ public class RadioGroup : View, IDesignable, IOrientation int pos = Orientation == Orientation.Horizontal ? viewportX : viewportY; int rCount = Orientation == Orientation.Horizontal - ? _horizontal.Last ().pos + _horizontal.Last ().length + ? _horizontal!.Last ().pos + _horizontal!.Last ().length : _radioLabels.Count; if (pos < rCount) { int c = Orientation == Orientation.Horizontal - ? _horizontal.FindIndex (x => x.pos <= viewportX && x.pos + x.length - 2 >= viewportX) + ? _horizontal!.FindIndex (x => x.pos <= viewportX && x.pos + x.length - 2 >= viewportX) : viewportY; if (c > -1) @@ -229,7 +231,7 @@ public class RadioGroup : View, IDesignable, IOrientation } - private List<(int pos, int length)> _horizontal; + private List<(int pos, int length)>? _horizontal; private int _horizontalSpace = 2; /// @@ -344,7 +346,7 @@ public class RadioGroup : View, IDesignable, IOrientation break; case Orientation.Horizontal: - Move (_horizontal [i].pos, 0); + Move (_horizontal! [i].pos, 0); break; } @@ -366,7 +368,7 @@ public class RadioGroup : View, IDesignable, IOrientation { Application.Driver?.SetAttribute ( HasFocus - ? ColorScheme.HotFocus + ? ColorScheme!.HotFocus : GetHotNormalColor () ); } @@ -388,7 +390,7 @@ public class RadioGroup : View, IDesignable, IOrientation { Application.Driver?.SetAttribute ( HasFocus - ? ColorScheme.HotFocus + ? ColorScheme!.HotFocus : GetHotNormalColor () ); } @@ -424,10 +426,10 @@ public class RadioGroup : View, IDesignable, IOrientation private readonly OrientationHelper _orientationHelper; /// - public event EventHandler> OrientationChanging; + public event EventHandler>? OrientationChanging; /// - public event EventHandler> OrientationChanged; + public event EventHandler>? OrientationChanged; /// Called when has changed. /// @@ -469,7 +471,7 @@ public class RadioGroup : View, IDesignable, IOrientation break; case Orientation.Horizontal: - if (_horizontal.Count > 0) + if (_horizontal!.Count > 0) { x = _horizontal [Cursor].pos; } @@ -490,7 +492,7 @@ public class RadioGroup : View, IDesignable, IOrientation // TODO: This should use StateEventArgs and should be cancelable. /// Invoked when the selected radio label has changed. - public event EventHandler SelectedItemChanged; + public event EventHandler? SelectedItemChanged; private bool MoveDownRight () { @@ -523,7 +525,7 @@ public class RadioGroup : View, IDesignable, IOrientation return false; } - private void RadioGroup_LayoutStarted (object sender, EventArgs e) { SetContentSize (); } + private void RadioGroup_LayoutStarted (object? sender, EventArgs e) { SetContentSize (); } private void SetContentSize () { diff --git a/UnitTests/Application/MainLoopTests.cs b/UnitTests/Application/MainLoopTests.cs index 71e8517a0..7dc4f122f 100644 --- a/UnitTests/Application/MainLoopTests.cs +++ b/UnitTests/Application/MainLoopTests.cs @@ -697,7 +697,7 @@ public class MainLoopTests { Assert.Null (btn); Assert.Equal (zero, total); - Assert.True (btnLaunch.NewKeyDownEvent (Key.Space)); + Assert.False (btnLaunch.NewKeyDownEvent (Key.Space)); if (btn == null) { @@ -714,7 +714,7 @@ public class MainLoopTests { Assert.Equal (clickMe, btn.Text); Assert.Equal (zero, total); - Assert.True (btn.NewKeyDownEvent (Key.Space)); + Assert.False (btn.NewKeyDownEvent (Key.Space)); Assert.Equal (cancel, btn.Text); Assert.Equal (one, total); } diff --git a/UnitTests/Dialogs/DialogTests.cs b/UnitTests/Dialogs/DialogTests.cs index 897d59855..ca272cdcb 100644 --- a/UnitTests/Dialogs/DialogTests.cs +++ b/UnitTests/Dialogs/DialogTests.cs @@ -1095,7 +1095,7 @@ public class DialogTests if (iterations == 0) { - Assert.True (btn1.NewKeyDownEvent (Key.Space)); + Assert.False (btn1.NewKeyDownEvent (Key.Space)); } else if (iterations == 1) { @@ -1110,7 +1110,7 @@ public class DialogTests └───────────────────────┘"; TestHelpers.AssertDriverContentsWithFrameAre (expected, _output); - Assert.True (btn2.NewKeyDownEvent (Key.Space)); + Assert.False (btn2.NewKeyDownEvent (Key.Space)); } else if (iterations == 2) { @@ -1127,13 +1127,13 @@ public class DialogTests _output ); - Assert.True (Top!.NewKeyDownEvent (Key.Enter)); + Assert.False (Top!.NewKeyDownEvent (Key.Enter)); } else if (iterations == 3) { TestHelpers.AssertDriverContentsWithFrameAre (expected, _output); - Assert.True (btn3.NewKeyDownEvent (Key.Space)); + Assert.False (btn3.NewKeyDownEvent (Key.Space)); } else if (iterations == 4) { diff --git a/UnitTests/Dialogs/MessageBoxTests.cs b/UnitTests/Dialogs/MessageBoxTests.cs index 565284a98..e5ab51578 100644 --- a/UnitTests/Dialogs/MessageBoxTests.cs +++ b/UnitTests/Dialogs/MessageBoxTests.cs @@ -11,12 +11,14 @@ public class MessageBoxTests [Fact] [AutoInitShutdown] - public void KeyBindings_Enter_Causes_Focused_Button_Click () + public void KeyBindings_Enter_Causes_Focused_Button_Click_No_Accept () { int result = -1; var iteration = 0; + int btnAcceptCount = 0; + Application.Iteration += (s, a) => { iteration++; @@ -32,6 +34,12 @@ public class MessageBoxTests case 2: // Tab to btn2 Application.OnKeyDown (Key.Tab); + + Button btn = Application.Navigation!.GetFocused () as Button; + + btn.Accept += (sender, e) => { btnAcceptCount++; }; + + // Click Application.OnKeyDown (Key.Enter); break; @@ -44,7 +52,8 @@ public class MessageBoxTests }; Application.Run ().Dispose (); - Assert.Equal (1, result); + Assert.Equal (0, result); + Assert.Equal (btnAcceptCount, 1); } [Fact] @@ -85,12 +94,14 @@ public class MessageBoxTests [Fact] [AutoInitShutdown] - public void KeyBindings_Space_Causes_Focused_Button_Click () + public void KeyBindings_Space_Causes_Focused_Button_Click_No_Accept () { int result = -1; var iteration = 0; + int btnAcceptCount = 0; + Application.Iteration += (s, a) => { iteration++; @@ -106,6 +117,11 @@ public class MessageBoxTests case 2: // Tab to btn2 Application.OnKeyDown (Key.Tab); + + Button btn = Application.Navigation!.GetFocused () as Button; + + btn.Accept += (sender, e) => { btnAcceptCount++; }; + Application.OnKeyDown (Key.Space); break; @@ -118,7 +134,9 @@ public class MessageBoxTests }; Application.Run ().Dispose (); - Assert.Equal (1, result); + Assert.Equal (0, result); + Assert.Equal (btnAcceptCount, 1); + } [Theory]