Files
Terminal.Gui/Tests/UnitTestsParallelizable/Views/AllViewsTests.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

265 lines
7.1 KiB
C#

#nullable enable
using System.Reflection;
using UnitTests;
using Xunit.Abstractions;
namespace ViewsTests;
public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
{
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Layout_Does_Not_Draw (Type viewType)
{
IDriver driver = CreateFakeDriver ();
View? view = CreateInstanceIfNotGeneric (viewType);
if (view is null)
{
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (view is IDesignable designable)
{
designable.EnableForDesign ();
}
var drawContentCount = 0;
view.DrawingContent += (s, e) => drawContentCount++;
var layoutStartedCount = 0;
view.SubViewLayout += (s, e) => layoutStartedCount++;
var layoutCompleteCount = 0;
view.SubViewsLaidOut += (s, e) => layoutCompleteCount++;
view.SetNeedsLayout ();
view.SetNeedsDraw ();
view.Layout ();
Assert.Equal (0, drawContentCount);
Assert.Equal (1, layoutStartedCount);
Assert.Equal (1, layoutCompleteCount);
}
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Center_Properly (Type viewType)
{
IDriver driver = CreateFakeDriver ();
View? view = CreateInstanceIfNotGeneric (viewType);
if (view is null)
{
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (view is IDesignable designable)
{
designable.EnableForDesign ();
}
view.X = Pos.Center ();
view.Y = Pos.Center ();
// Ensure the view has positive dimensions
view.Width = 10;
view.Height = 10;
var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
frame.Add (view);
frame.LayoutSubViews ();
frame.Dispose ();
// What's the natural width/height?
int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
Assert.True (
view.Frame.Left == expectedX,
$"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
);
Assert.True (
view.Frame.Top == expectedY,
$"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
);
}
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Tests_All_Constructors (Type viewType)
{
Assert.True (TestAllConstructorsOfType (viewType));
return;
bool TestAllConstructorsOfType (Type type)
{
foreach (ConstructorInfo ctor in type.GetConstructors ())
{
View? view = CreateViewFromType (type, ctor);
if (view != null)
{
Assert.True (type.FullName == view.GetType ().FullName);
}
view?.Dispose ();
}
return true;
}
}
//[Fact]
//public void AllViews_HotKey_Works ()
//{
// foreach (var type in GetAllViewClasses ()) {
// _output.WriteLine ($"Testing {type.Name}");
// var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
// view.HotKeySpecifier = (Rune)'^';
// view.Text = "^text";
// Assert.Equal(Key.T, view.HotKey);
// }
//}
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Command_Select_Raises_Selecting (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (view is IDesignable designable)
{
designable.EnableForDesign ();
}
var activatingCount = 0;
view.Activating += (s, e) => activatingCount++;
var acceptedCount = 0;
view.Accepting += (s, e) => { acceptedCount++; };
if (view.InvokeCommand (Command.Activate) == true)
{
Assert.Equal (1, activatingCount);
Assert.Equal (0, acceptedCount);
}
view?.Dispose ();
}
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Command_Accept_Raises_Accepting (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (view is IDesignable designable)
{
designable.EnableForDesign ();
}
var activatingCount = 0;
view.Activating += (s, e) => activatingCount++;
var acceptingCount = 0;
view.Accepting += (s, e) => { acceptingCount++; };
if (view.InvokeCommand (Command.Accept) == true)
{
Assert.Equal (0, activatingCount);
Assert.Equal (1, acceptingCount);
}
view?.Dispose ();
}
[Theory]
[MemberData (nameof (AllViewTypes))]
public void AllViews_Command_HotKey_Raises_HandlingHotKey (Type viewType)
{
var view = CreateInstanceIfNotGeneric (viewType);
if (view == null)
{
output.WriteLine ($"Ignoring {viewType} - It's a Generic");
return;
}
if (view is IDesignable designable)
{
designable.EnableForDesign ();
}
else
{
view.HotKey = Key.T;
}
var acceptedCount = 0;
view.Accepting += (s, e) => { acceptedCount++; };
var handlingHotKeyCount = 0;
view.HandlingHotKey += (s, e) => { handlingHotKeyCount++; };
if (view.InvokeCommand (Command.HotKey) == true)
{
Assert.Equal (1, handlingHotKeyCount);
Assert.Equal (0, acceptedCount);
}
view?.Dispose ();
}
//[Theory]
//[MemberData (nameof (AllViewTypes))]
//public void AllViews_Disabled_Draws_Disabled_Or_Faint (Type viewType)
//{
// var view = CreateInstanceIfNotGeneric (viewType);
// if (view == null)
// {
// output.WriteLine ($"Ignoring {viewType} - It's a Generic");
// return;
// }
// if (view is IDesignable designable)
// {
// designable.EnableForDesign ();
// }
// var driver = CreateFakeDriver ();
// driver.AttributeSet += (_, args) =>
// {
// if (args != view.GetAttributeForRole (VisualRole.Disabled) && args.Style != TextStyle.Faint)
// {
// Assert.Fail($"{viewType} with `Enabled == false` tried to SetAttribute to {args}");
// }
// };
// view.Driver = driver;
// view.Enabled = false;
// view.SetNeedsDraw ();
// view.Draw ();
// view?.Dispose ();
//}
}