Files
Terminal.Gui/Tests/UnitTests/Views/CheckBoxTests.cs
Copilot e7a4df492d Fixes #4050. Rename Command.Select and Selecting to Activate/Activating (#4470)
* Initial plan

* Rename Command.Select to Command.Activate and Selecting to Activating

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add Activating event propagation to SuperView

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update all comments and docs referencing Select to Activate

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix event log messages in examples to use Activating/Activate

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Revert automatic Activating event propagation that broke tests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update docfx documentation to use Activate/Activating terminology

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* renames

* Revert "Add Activating event propagation to SuperView"

This reverts commit 6d82bee9ad.

* added command diagrams

* mermaid

* updated level 3

* again

* Select->Activate in MouseTests.cs

* Update Terminal.Gui/Views/Selectors/FlagSelector.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Refactor: Rename Selecting to Activating in View APIs

Renamed the `Selecting` event and `OnSelecting` method to
`Activating` and `OnActivating` to better reflect their purpose.
Updated all related comments, test method names, variables,
and assertions in `View` and `ViewCommandTests` to align with
the new terminology.

Improved code clarity by using `_` for unused parameters in
lambda expressions. Renamed properties like `HandleSelecting`
to `HandleActivating` and adjusted naming conventions for
consistency (e.g., `OnactivatingCount` to `OnActivatingCount`).

These changes enhance readability, maintainability, and
terminology consistency across the codebase.

* Update Terminal.Gui/Views/Selectors/OptionSelector.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Typos

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-09 12:42:34 -07:00

321 lines
10 KiB
C#

using System.ComponentModel;
using UnitTests;
using Xunit.Abstractions;
// ReSharper disable AccessToModifiedClosure
namespace UnitTests.ViewsTests;
public class CheckBoxTests (ITestOutputHelper output)
{
private static readonly Size _size25x1 = new (25, 1);
#region Mouse Tests
#endregion Mouse Tests
[Fact]
[AutoInitShutdown]
public void TextAlignment_Centered ()
{
var checkBox = new CheckBox
{
X = 1,
Y = Pos.Center (),
Text = "Check this out 你",
TextAlignment = Alignment.Center,
Width = 25
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
win.Add (checkBox);
var top = new Runnable ();
top.Add (win);
Application.Begin (top);
Application.Driver?.SetScreenSize (30, 5);
Application.LayoutAndDraw ();
Assert.Equal (Alignment.Center, checkBox.TextAlignment);
Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
var expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateUnChecked} Check this out 你 │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
checkBox.CheckedState = CheckState.Checked;
Application.LayoutAndDraw ();
expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateChecked} Check this out 你 │
│ │
└────────────────────────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void TextAlignment_Justified ()
{
var checkBox1 = new CheckBox
{
X = 1,
Y = Pos.Center (),
Text = "Check first out 你",
TextAlignment = Alignment.Fill,
Width = 25
};
var checkBox2 = new CheckBox
{
X = 1,
Y = Pos.Bottom (checkBox1),
Text = "Check second out 你",
TextAlignment = Alignment.Fill,
Width = 25
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
win.Add (checkBox1, checkBox2);
var top = new Runnable ();
top.Add (win);
SessionToken rs = Application.Begin (top);
Application.Driver!.SetScreenSize (30, 6);
Application.LayoutAndDraw ();
Assert.Equal (Alignment.Fill, checkBox1.TextAlignment);
Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
Assert.Equal (Alignment.Fill, checkBox2.TextAlignment);
Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
var expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateUnChecked} Check first out 你 │
│ {Glyphs.CheckStateUnChecked} Check second out 你 │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 6), pos);
checkBox1.CheckedState = CheckState.Checked;
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (1, 1, 25, 1), checkBox1.Frame);
Assert.Equal (_size25x1, checkBox1.TextFormatter.ConstrainToSize);
checkBox2.CheckedState = CheckState.Checked;
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (1, 2, 25, 1), checkBox2.Frame);
Assert.Equal (_size25x1, checkBox2.TextFormatter.ConstrainToSize);
AutoInitShutdownAttribute.RunIteration ();
expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateChecked} Check first out 你 │
│ {Glyphs.CheckStateChecked} Check second out 你 │
│ │
└────────────────────────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 6), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void TextAlignment_Left ()
{
var checkBox = new CheckBox
{
X = 1,
Y = Pos.Center (),
Text = "Check this out 你",
Width = 25
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
win.Add (checkBox);
var top = new Runnable ();
top.Add (win);
Application.Begin (top);
Application.Driver!.SetScreenSize (30, 5);
Application.LayoutAndDraw ();
Assert.Equal (Alignment.Start, checkBox.TextAlignment);
Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
var expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateUnChecked} Check this out 你 │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
checkBox.CheckedState = CheckState.Checked;
AutoInitShutdownAttribute.RunIteration ();
expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ {Glyphs.CheckStateChecked} Check this out 你 │
│ │
└────────────────────────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void TextAlignment_Right ()
{
var checkBox = new CheckBox
{
X = 1,
Y = Pos.Center (),
Text = "Check this out 你",
TextAlignment = Alignment.End,
Width = 25
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill (), Title = "Test Demo 你" };
win.Add (checkBox);
var top = new Runnable ();
top.Add (win);
Application.Begin (top);
Application.Driver!.SetScreenSize (30, 5);
Application.LayoutAndDraw ();
Assert.Equal (Alignment.End, checkBox.TextAlignment);
Assert.Equal (new (1, 1, 25, 1), checkBox.Frame);
Assert.Equal (_size25x1, checkBox.TextFormatter.ConstrainToSize);
var expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ Check this out 你 {Glyphs.CheckStateUnChecked} │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
checkBox.CheckedState = CheckState.Checked;
AutoInitShutdownAttribute.RunIteration ();
expected = @$"
┌┤Test Demo 你├──────────────┐
│ │
│ Check this out 你 {Glyphs.CheckStateChecked} │
│ │
└────────────────────────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Fact]
public void HotKey_Command_Does_Not_Fire_Accept ()
{
var cb = new CheckBox ();
var accepted = false;
cb.Accepting += CheckBoxOnAccept;
cb.InvokeCommand (Command.HotKey);
Assert.False (accepted);
return;
void CheckBoxOnAccept (object sender, CommandEventArgs e) { accepted = true; }
}
[Theory]
[InlineData (CheckState.Checked)]
[InlineData (CheckState.UnChecked)]
[InlineData (CheckState.None)]
public void Activated_Handle_Event_Prevents_Change (CheckState initialState)
{
var ckb = new CheckBox { AllowCheckStateNone = true };
var checkedInvoked = false;
ckb.CheckedState = initialState;
ckb.Activating += OnActivating;
Assert.Equal (initialState, ckb.CheckedState);
bool? ret = ckb.InvokeCommand (Command.Activate);
Assert.True (ret);
Assert.True (checkedInvoked);
Assert.Equal (initialState, ckb.CheckedState);
return;
void OnActivating (object sender, CommandEventArgs e)
{
checkedInvoked = true;
e.Handled = true;
}
}
[Theory]
[InlineData (CheckState.Checked)]
[InlineData (CheckState.UnChecked)]
[InlineData (CheckState.None)]
public void CheckedStateChanging_Cancel_Event_Prevents_Change (CheckState initialState)
{
var ckb = new CheckBox { AllowCheckStateNone = true };
var checkedInvoked = false;
ckb.CheckedState = initialState;
ckb.CheckedStateChanging += OnCheckedStateChanging;
Assert.Equal (initialState, ckb.CheckedState);
// AdvanceCheckState returns false if the state was changed, true if it was cancelled, null if it was not changed
bool? ret = ckb.AdvanceCheckState ();
Assert.True (ret);
Assert.True (checkedInvoked);
Assert.Equal (initialState, ckb.CheckedState);
return;
void OnCheckedStateChanging (object sender, ResultEventArgs<CheckState> e)
{
checkedInvoked = true;
e.Handled = true;
}
}
}