mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-02 01:03:29 +01:00
Fixes #3186. HotKeys must be explicitly set (Makes TextFormatter.FindHotKey firstUpperCase default to false) (#3187)
* Removed resharper settings from editorconfig * Fixed FileDialog, Button, and Checkbox * Fixed RadioGroup * code cleanup * Fixed Unicode Scenario * Fixed nonBMP bug in DrawHotString * Fixed nonBMP bug in DrawHotString * Fixed AllViewsTester and Buttons * Fixed Dialogs & MessageBoxes * API docs
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Terminal.Gui.ViewsTests;
|
||||
namespace Terminal.Gui.ViewsTests;
|
||||
|
||||
public class ButtonTests {
|
||||
readonly ITestOutputHelper output;
|
||||
readonly ITestOutputHelper _output;
|
||||
|
||||
public ButtonTests (ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
public ButtonTests (ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [SetupFakeDriver]
|
||||
public void Constructors_Defaults ()
|
||||
{
|
||||
var btn = new Button ();
|
||||
Assert.Equal (string.Empty, btn.Text);
|
||||
Application.Top.Add (btn);
|
||||
var rs = Application.Begin (Application.Top);
|
||||
btn.BeginInit ();
|
||||
btn.EndInit ();
|
||||
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
|
||||
Assert.False (btn.IsDefault);
|
||||
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
|
||||
Assert.Equal ('_', btn.HotKeySpecifier.Value);
|
||||
Assert.True (btn.CanFocus);
|
||||
Assert.Equal (new Rect (0, 0, 4, 1), btn.Bounds);
|
||||
Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
|
||||
Assert.False (btn.IsDefault);
|
||||
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
|
||||
@@ -29,42 +34,50 @@ public class ButtonTests {
|
||||
Assert.Equal (string.Empty, btn.Title);
|
||||
Assert.Equal (KeyCode.Null, btn.HotKey);
|
||||
|
||||
btn.Draw ();
|
||||
|
||||
var expected = @$"
|
||||
{CM.Glyphs.LeftBracket} {CM.Glyphs.RightBracket}
|
||||
";
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
Application.End (rs);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
|
||||
btn = new Button ("ARGS", true) { Text = "Test" };
|
||||
Assert.Equal ("Test", btn.Text);
|
||||
Application.Top.Add (btn);
|
||||
rs = Application.Begin (Application.Top);
|
||||
btn = new Button ("ARGS", true) { Text = "_Test" };
|
||||
btn.BeginInit ();
|
||||
btn.EndInit ();
|
||||
Assert.Equal ('_', btn.HotKeySpecifier.Value);
|
||||
Assert.Equal (Key.T, btn.HotKey);
|
||||
Assert.Equal ("_Test", btn.Text);
|
||||
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Format ());
|
||||
Assert.True (btn.IsDefault);
|
||||
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
|
||||
Assert.Equal ('_', btn.HotKeySpecifier.Value);
|
||||
Assert.True (btn.CanFocus);
|
||||
Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
|
||||
Assert.Equal (new Rect (0, 0, 10, 1), btn.Frame);
|
||||
Assert.Equal (KeyCode.T, btn.HotKey);
|
||||
Application.End (rs);
|
||||
|
||||
btn = new Button (3, 4, "Test", true);
|
||||
Assert.Equal ("Test", btn.Text);
|
||||
Application.Top.Add (btn);
|
||||
rs = Application.Begin (Application.Top);
|
||||
btn = new Button (1, 2, "_abc", true);
|
||||
btn.BeginInit ();
|
||||
btn.EndInit ();
|
||||
Assert.Equal ("_abc", btn.Text);
|
||||
Assert.Equal (Key.A, btn.HotKey);
|
||||
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} Test {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Text);
|
||||
Assert.Equal ($"{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}", btn.TextFormatter.Format ());
|
||||
Assert.True (btn.IsDefault);
|
||||
Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
|
||||
Assert.Equal ('_', btn.HotKeySpecifier.Value);
|
||||
Assert.True (btn.CanFocus);
|
||||
Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
|
||||
Assert.Equal (new Rect (3, 4, 10, 1), btn.Frame);
|
||||
Assert.Equal (KeyCode.T, btn.HotKey);
|
||||
|
||||
Application.End (rs);
|
||||
Application.Driver.ClearContents ();
|
||||
btn.Draw ();
|
||||
|
||||
expected = @$"
|
||||
{CM.Glyphs.LeftBracket}{CM.Glyphs.LeftDefaultIndicator} abc {CM.Glyphs.RightDefaultIndicator}{CM.Glyphs.RightBracket}
|
||||
";
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
|
||||
Assert.Equal (new Rect (0, 0, 10, 1), btn.Bounds);
|
||||
Assert.Equal (new Rect (1, 2, 10, 1), btn.Frame);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -72,21 +85,24 @@ public class ButtonTests {
|
||||
public void KeyBindings_Command ()
|
||||
{
|
||||
var clicked = false;
|
||||
Button btn = new Button ("_Test");
|
||||
var btn = new Button ("_Test");
|
||||
btn.Clicked += (s, e) => clicked = true;
|
||||
Application.Top.Add (btn);
|
||||
Application.Begin (Application.Top);
|
||||
|
||||
// Hot key. Both alone and with alt
|
||||
Assert.Equal (KeyCode.T, btn.HotKey);
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.T)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T | KeyCode.AltMask)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.AltMask)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
Assert.True (btn.NewKeyDownEvent (btn.HotKey));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
@@ -94,32 +110,32 @@ public class ButtonTests {
|
||||
// IsDefault = false
|
||||
// Space and Enter should work
|
||||
Assert.False (btn.IsDefault);
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.Enter)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
// IsDefault = true
|
||||
// Space and Enter should work
|
||||
btn.IsDefault = true;
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.Enter)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
// Toplevel does not handle Enter, so it should get passed on to button
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Enter)));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.Enter)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
// Direct
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Enter)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.Enter)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.Space)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.Space)));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
Assert.True (btn.NewKeyDownEvent (new ((KeyCode)'T')));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key ((KeyCode)'T')));
|
||||
Assert.True (clicked);
|
||||
clicked = false;
|
||||
|
||||
@@ -135,22 +151,22 @@ public class ButtonTests {
|
||||
public void HotKeyChange_Works ()
|
||||
{
|
||||
var clicked = false;
|
||||
Button btn = new Button ("Test");
|
||||
var btn = new Button ("_Test");
|
||||
btn.Clicked += (s, e) => clicked = true;
|
||||
Application.Top.Add (btn);
|
||||
Application.Begin (Application.Top);
|
||||
|
||||
Assert.Equal (KeyCode.T, btn.HotKey);
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.T)));
|
||||
Assert.True (clicked);
|
||||
|
||||
clicked = false;
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.T | KeyCode.AltMask)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.T | KeyCode.AltMask)));
|
||||
Assert.True (clicked);
|
||||
|
||||
clicked = false;
|
||||
btn.HotKey = KeyCode.E;
|
||||
Assert.True (btn.NewKeyDownEvent (new (KeyCode.E | KeyCode.AltMask)));
|
||||
Assert.True (btn.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.AltMask)));
|
||||
Assert.True (clicked);
|
||||
}
|
||||
|
||||
@@ -162,7 +178,7 @@ public class ButtonTests {
|
||||
[AutoInitShutdown]
|
||||
public void KeyBindingExample ()
|
||||
{
|
||||
int pressed = 0;
|
||||
var pressed = 0;
|
||||
var btn = new Button ("Press Me");
|
||||
|
||||
btn.Clicked += (s, e) => pressed++;
|
||||
@@ -171,44 +187,46 @@ public class ButtonTests {
|
||||
Assert.Contains (Command.Default, btn.GetSupportedCommands ());
|
||||
Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
|
||||
|
||||
Application.Top.Add (btn);
|
||||
Application.Begin (Application.Top);
|
||||
Application.Top.Add (btn);
|
||||
Application.Begin (Application.Top);
|
||||
|
||||
// default keybinding is Space which results in keypress
|
||||
Application.OnKeyDown (new ((KeyCode)' '));
|
||||
Application.OnKeyDown (new Key ((KeyCode)' '));
|
||||
Assert.Equal (1, pressed);
|
||||
|
||||
// remove the default keybinding (Space)
|
||||
btn.KeyBindings.Clear (Command.Default, Command.Accept);
|
||||
|
||||
// After clearing the default keystroke the Space button no longer does anything for the Button
|
||||
Application.OnKeyDown (new ((KeyCode)' '));
|
||||
Application.OnKeyDown (new Key ((KeyCode)' '));
|
||||
Assert.Equal (1, pressed);
|
||||
|
||||
// Set a new binding of b for the click (Accept) event
|
||||
btn.KeyBindings.Add (KeyCode.B, Command.Default, Command.Accept);
|
||||
|
||||
// now pressing B should call the button click event
|
||||
Application.OnKeyDown (new (KeyCode.B));
|
||||
Application.OnKeyDown (new Key (KeyCode.B));
|
||||
Assert.Equal (2, pressed);
|
||||
|
||||
// now pressing Shift-B should NOT call the button click event
|
||||
Application.OnKeyDown (new (KeyCode.ShiftMask | KeyCode.B));
|
||||
Application.OnKeyDown (new Key (KeyCode.ShiftMask | KeyCode.B));
|
||||
Assert.Equal (2, pressed);
|
||||
|
||||
// now pressing Alt-B should NOT call the button click event
|
||||
Application.OnKeyDown (new (KeyCode.AltMask | KeyCode.B));
|
||||
Application.OnKeyDown (new Key (KeyCode.AltMask | KeyCode.B));
|
||||
Assert.Equal (2, pressed);
|
||||
|
||||
// now pressing Shift-Alt-B should NOT call the button click event
|
||||
Application.OnKeyDown (new (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.B));
|
||||
Application.OnKeyDown (new Key (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.B));
|
||||
Assert.Equal (2, pressed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestAssignTextToButton ()
|
||||
{
|
||||
View b = new Button () { Text = "heya" };
|
||||
View b = new Button { Text = "heya" };
|
||||
Assert.Equal ("heya", b.Text);
|
||||
Assert.Contains ("heya", b.TextFormatter.Text);
|
||||
b.Text = "heyb";
|
||||
@@ -223,14 +241,17 @@ public class ButtonTests {
|
||||
public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
|
||||
{
|
||||
var super = new View ();
|
||||
var btn = new Button ("Test");
|
||||
var btn = new Button ("_Test");
|
||||
super.Add (btn);
|
||||
super.BeginInit ();
|
||||
super.EndInit ();
|
||||
|
||||
Assert.Equal ("Test", btn.Text);
|
||||
Assert.Equal ("_Test", btn.Text);
|
||||
Assert.Equal (KeyCode.T, btn.HotKey);
|
||||
|
||||
btn.Text = string.Empty;
|
||||
Assert.Equal ("", btn.Text);
|
||||
Assert.Equal (KeyCode.Null, btn.HotKey);
|
||||
btn.Text = string.Empty;
|
||||
Assert.Equal ("", btn.Text);
|
||||
Assert.Equal (KeyCode.Null, btn.HotKey);
|
||||
@@ -247,15 +268,18 @@ public class ButtonTests {
|
||||
X = Pos.Center (),
|
||||
Y = Pos.Center ()
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
win.Add (btn);
|
||||
Application.Top.Add (win);
|
||||
|
||||
Assert.False (btn.IsInitialized);
|
||||
Assert.False (btn.IsInitialized);
|
||||
|
||||
Application.Begin (Application.Top);
|
||||
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
|
||||
Application.Begin (Application.Top);
|
||||
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
|
||||
|
||||
@@ -272,27 +296,30 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
public void Update_Parameterless_Only_On_Or_After_Initialize ()
|
||||
{
|
||||
var btn = new Button () {
|
||||
var btn = new Button {
|
||||
X = Pos.Center (),
|
||||
Y = Pos.Center (),
|
||||
Text = "Say Hello 你"
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
win.Add (btn);
|
||||
Application.Top.Add (win);
|
||||
|
||||
Assert.False (btn.IsInitialized);
|
||||
Assert.False (btn.IsInitialized);
|
||||
|
||||
Application.Begin (Application.Top);
|
||||
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
|
||||
Application.Begin (Application.Top);
|
||||
((FakeDriver)Application.Driver).SetBufferSize (30, 5);
|
||||
|
||||
@@ -309,30 +336,33 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
public void AutoSize_Stays_True_With_EmptyText ()
|
||||
{
|
||||
var btn = new Button () {
|
||||
var btn = new Button {
|
||||
X = Pos.Center (),
|
||||
Y = Pos.Center (),
|
||||
AutoSize = true
|
||||
};
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
win.Add (btn);
|
||||
Application.Top.Add (win);
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
Assert.True (btn.AutoSize);
|
||||
|
||||
btn.Text = "Say Hello 你";
|
||||
btn.Text = "Say Hello 你";
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
Assert.True (btn.AutoSize);
|
||||
|
||||
Application.Begin (Application.Top);
|
||||
@@ -345,25 +375,26 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
public void AutoSize_Stays_True_Center ()
|
||||
{
|
||||
var btn = new Button () {
|
||||
var btn = new Button {
|
||||
X = Pos.Center (),
|
||||
Y = Pos.Center (),
|
||||
Text = "Say Hello 你"
|
||||
};
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
win.Add (btn);
|
||||
Application.Top.Add (win);
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
Assert.True (btn.AutoSize);
|
||||
|
||||
Application.Begin (Application.Top);
|
||||
@@ -376,7 +407,7 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
btn.Text = "Say Hello 你 changed";
|
||||
@@ -390,28 +421,30 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AutoSize_Stays_True_AnchorEnd ()
|
||||
{
|
||||
var btn = new Button () {
|
||||
var btn = new Button {
|
||||
Y = Pos.Center (),
|
||||
Text = "Say Hello 你",
|
||||
AutoSize = true
|
||||
};
|
||||
var btnTxt = $"{CM.Glyphs.LeftBracket} {btn.Text} {CM.Glyphs.RightBracket}";
|
||||
|
||||
btn.X = Pos.AnchorEnd () - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
|
||||
btn.X = Pos.AnchorEnd () - Pos.Function (() => btn.TextFormatter.Text.GetColumns ());
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
win.Add (btn);
|
||||
Application.Top.Add (win);
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
Assert.True (btn.AutoSize);
|
||||
|
||||
Application.Begin (Application.Top);
|
||||
@@ -424,7 +457,7 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
|
||||
Assert.True (btn.AutoSize);
|
||||
btn.Text = "Say Hello 你 changed";
|
||||
@@ -439,10 +472,10 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AutoSize_False_With_Fixed_Width ()
|
||||
{
|
||||
var tab = new View ();
|
||||
@@ -508,13 +541,13 @@ public class ButtonTests {
|
||||
};
|
||||
tab.Add (ckbMatchWholeWord);
|
||||
|
||||
var tabView = new TabView () {
|
||||
var tabView = new TabView {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
tabView.AddTab (new Tab () { DisplayText = "Find", View = tab }, true);
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
@@ -559,10 +592,10 @@ public class ButtonTests {
|
||||
└────────────────────────────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void Pos_Center_Layout_AutoSize_True ()
|
||||
{
|
||||
var button = new Button ("Process keys") {
|
||||
@@ -570,7 +603,7 @@ public class ButtonTests {
|
||||
Y = Pos.Center (),
|
||||
IsDefault = true
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
@@ -591,10 +624,10 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void Pos_Center_Layout_AutoSize_False ()
|
||||
{
|
||||
var button = new Button ("Process keys") {
|
||||
@@ -604,7 +637,7 @@ public class ButtonTests {
|
||||
IsDefault = true,
|
||||
AutoSize = false
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill ()
|
||||
};
|
||||
@@ -623,29 +656,38 @@ public class ButtonTests {
|
||||
└────────────────────────────┘
|
||||
";
|
||||
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
[Fact, AutoInitShutdown]
|
||||
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void Button_HotKeyChanged_EventFires ()
|
||||
{
|
||||
var btn = new Button ("Yar");
|
||||
var btn = new Button ("_Yar");
|
||||
|
||||
object sender = null;
|
||||
KeyChangedEventArgs args = null;
|
||||
|
||||
btn.HotKeyChanged += (s, e) => {
|
||||
sender = s;
|
||||
args = e;
|
||||
btn.HotKeyChanged += (s, e) => {
|
||||
sender = s;
|
||||
args = e;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
btn.HotKey = KeyCode.R;
|
||||
Assert.Same (btn, sender);
|
||||
Assert.Equal (KeyCode.Y, args.OldKey);
|
||||
Assert.Equal (KeyCode.R, args.NewKey);
|
||||
|
||||
btn.HotKey = KeyCode.R;
|
||||
Assert.Same (btn, sender);
|
||||
Assert.Equal (KeyCode.Y, args.OldKey);
|
||||
Assert.Equal (KeyCode.R, args.NewKey);
|
||||
}
|
||||
[Fact, AutoInitShutdown]
|
||||
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void Button_HotKeyChanged_EventFires_WithNone ()
|
||||
{
|
||||
var btn = new Button ();
|
||||
@@ -656,6 +698,7 @@ public class ButtonTests {
|
||||
btn.HotKeyChanged += (s, e) => {
|
||||
sender = s;
|
||||
args = e;
|
||||
|
||||
};
|
||||
|
||||
btn.HotKey = KeyCode.R;
|
||||
@@ -663,4 +706,4 @@ public class ButtonTests {
|
||||
Assert.Equal (KeyCode.Null, args.OldKey);
|
||||
Assert.Equal (KeyCode.R, args.NewKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Terminal.Gui.ViewsTests;
|
||||
|
||||
public class CheckboxTests {
|
||||
readonly ITestOutputHelper _output;
|
||||
|
||||
public CheckboxTests (ITestOutputHelper output)
|
||||
{
|
||||
this._output = output;
|
||||
}
|
||||
public CheckboxTests (ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public void Constructors_Defaults ()
|
||||
@@ -60,7 +53,7 @@ public class CheckboxTests {
|
||||
public void KeyBindings_Command ()
|
||||
{
|
||||
var toggled = false;
|
||||
CheckBox ckb = new CheckBox ();
|
||||
var ckb = new CheckBox ();
|
||||
ckb.Toggled += (s, e) => toggled = true;
|
||||
Application.Top.Add (ckb);
|
||||
Application.Begin (Application.Top);
|
||||
@@ -69,32 +62,32 @@ public class CheckboxTests {
|
||||
Assert.False (toggled);
|
||||
Assert.Equal (KeyCode.Null, ckb.HotKey);
|
||||
|
||||
ckb.Text = "Test";
|
||||
ckb.Text = "_Test";
|
||||
Assert.Equal (KeyCode.T, ckb.HotKey);
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.T)));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.T)));
|
||||
Assert.True (ckb.Checked);
|
||||
Assert.True (toggled);
|
||||
|
||||
ckb.Text = "T_est";
|
||||
toggled = false;
|
||||
Assert.Equal (KeyCode.E, ckb.HotKey);
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.E | KeyCode.AltMask)));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.E | KeyCode.AltMask)));
|
||||
Assert.True (toggled);
|
||||
Assert.False (ckb.Checked);
|
||||
|
||||
toggled = false;
|
||||
Assert.Equal (KeyCode.E, ckb.HotKey);
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.E)));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.E)));
|
||||
Assert.True (toggled);
|
||||
Assert.True (ckb.Checked);
|
||||
|
||||
toggled = false;
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new ((KeyCode)' ')));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key ((KeyCode)' ')));
|
||||
Assert.True (toggled);
|
||||
Assert.False (ckb.Checked);
|
||||
|
||||
toggled = false;
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new (KeyCode.Space)));
|
||||
Assert.True (Application.Top.NewKeyDownEvent (new Key (KeyCode.Space)));
|
||||
Assert.True (toggled);
|
||||
Assert.True (ckb.Checked);
|
||||
Assert.True (ckb.AutoSize);
|
||||
@@ -109,15 +102,15 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 6, 1), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AutoSize_StaysVisible ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Center (),
|
||||
Text = "Check this out 你"
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -186,17 +179,17 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void TextAlignment_Left ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Center (),
|
||||
Text = "Check this out 你",
|
||||
AutoSize = false,
|
||||
Width = 25
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -236,10 +229,10 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void TextAlignment_Centered ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Center (),
|
||||
Text = "Check this out 你",
|
||||
@@ -247,7 +240,7 @@ public class CheckboxTests {
|
||||
AutoSize = false,
|
||||
Width = 25
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -288,10 +281,10 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void TextAlignment_Justified ()
|
||||
{
|
||||
var checkBox1 = new CheckBox () {
|
||||
var checkBox1 = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Center (),
|
||||
Text = "Check first out 你",
|
||||
@@ -299,7 +292,7 @@ public class CheckboxTests {
|
||||
AutoSize = false,
|
||||
Width = 25
|
||||
};
|
||||
var checkBox2 = new CheckBox () {
|
||||
var checkBox2 = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Bottom (checkBox1),
|
||||
Text = "Check second out 你",
|
||||
@@ -307,7 +300,7 @@ public class CheckboxTests {
|
||||
AutoSize = false,
|
||||
Width = 25
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -357,10 +350,10 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 30, 6), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void TextAlignment_Right ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
X = 1,
|
||||
Y = Pos.Center (),
|
||||
Text = "Check this out 你",
|
||||
@@ -368,7 +361,7 @@ public class CheckboxTests {
|
||||
AutoSize = false,
|
||||
Width = 25
|
||||
};
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -409,16 +402,16 @@ public class CheckboxTests {
|
||||
Assert.Equal (new Rect (0, 0, 30, 5), pos);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
Y = Pos.Center (),
|
||||
Text = "Check this out 你"
|
||||
};
|
||||
checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -455,16 +448,16 @@ public class CheckboxTests {
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
|
||||
{
|
||||
var checkBox = new CheckBox () {
|
||||
var checkBox = new CheckBox {
|
||||
Y = Pos.Center (),
|
||||
Text = "C_heck this out 你"
|
||||
};
|
||||
checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetSizeNeededForTextWithoutHotKey ().Width);
|
||||
|
||||
var win = new Window () {
|
||||
var win = new Window {
|
||||
Width = Dim.Fill (),
|
||||
Height = Dim.Fill (),
|
||||
Title = "Test Demo 你"
|
||||
@@ -501,7 +494,7 @@ public class CheckboxTests {
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
[Fact] [AutoInitShutdown]
|
||||
public void AllowNullChecked_Get_Set ()
|
||||
{
|
||||
var checkBox = new CheckBox ("Check this out 你");
|
||||
@@ -510,25 +503,25 @@ public class CheckboxTests {
|
||||
Application.Begin (top);
|
||||
|
||||
Assert.False (checkBox.Checked);
|
||||
Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
|
||||
Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
|
||||
Assert.True (checkBox.Checked);
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.False (checkBox.Checked);
|
||||
|
||||
checkBox.AllowNullChecked = true;
|
||||
Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
|
||||
Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
|
||||
Assert.Null (checkBox.Checked);
|
||||
Application.Refresh ();
|
||||
TestHelpers.AssertDriverContentsWithFrameAre (@$"
|
||||
{CM.Glyphs.NullChecked} Check this out 你", _output);
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.True (checkBox.Checked);
|
||||
Assert.True (checkBox.NewKeyDownEvent (new (KeyCode.Space)));
|
||||
Assert.True (checkBox.NewKeyDownEvent (new Key (KeyCode.Space)));
|
||||
Assert.False (checkBox.Checked);
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.True (checkBox.MouseEvent (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Clicked }));
|
||||
Assert.Null (checkBox.Checked);
|
||||
|
||||
checkBox.AllowNullChecked = false;
|
||||
Assert.False (checkBox.Checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,7 +170,7 @@ public class RadioGroupTests {
|
||||
[Fact]
|
||||
public void KeyBindings_Are_Added_Correctly ()
|
||||
{
|
||||
var rg = new RadioGroup (new string [] { "Left", "Right" });
|
||||
var rg = new RadioGroup (new string [] { "_Left", "_Right" });
|
||||
Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
|
||||
Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.R));
|
||||
|
||||
@@ -185,7 +185,7 @@ public class RadioGroupTests {
|
||||
[Fact]
|
||||
public void KeyBindings_HotKeys ()
|
||||
{
|
||||
var rg = new RadioGroup (new string [] { "Left", "Right", "Cen_tered", "Justified" });
|
||||
var rg = new RadioGroup (new string [] { "_Left", "_Right", "Cen_tered", "_Justified" });
|
||||
Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L));
|
||||
Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.ShiftMask));
|
||||
Assert.NotEmpty (rg.KeyBindings.GetCommands (KeyCode.L | KeyCode.AltMask));
|
||||
|
||||
Reference in New Issue
Block a user