Files
Terminal.Gui/Tests/UnitTestsParallelizable/Application/Application.NavigationTests.cs
Tig 5da7e59aa2 Fixes #4456 - Clear MouseGrabView in App.End (#4460)
* Fixed MouseGrabView bug.

Added extensive test coverage for `Keyboard`, `Mouse`, `Timeout`, and `Popover` functionalities, including edge cases and concurrent access. Introduced parameterized and data-driven tests to reduce redundancy and improve clarity.

Refactored codebase for modularity and maintainability,
introducing new namespaces and reorganizing classes. Enhanced `MouseImpl`, `KeyboardImpl`, and `Runnable` implementations with improved event handling, thread safety, and support for the Terminal.Gui Cancellable Work Pattern (CWP).

Removed deprecated code and legacy tests, such as `LogarithmicTimeout` and `SmoothAcceleratingTimeout`. Fixed bugs related to mouse grabbing during drag operations and unbalanced `ApplicationImpl.Begin/End` calls. Improved documentation and code readability with modern C# features.

* Code cleanup.

* Update Tests/UnitTestsParallelizable/Application/Runnable/RunnableIntegrationTests.cs

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

* Improve null handling and simplify test setup

In `MouseImpl.cs`, added an early `return` after the `UngrabMouse()`
call within the `if (view is null)` block to prevent further execution
when `view` is `null`, improving null reference handling.

In `RunnableIntegrationTests.cs`, removed the initialization of the
`IApplication` object (`app`) from the `MultipleRunnables_IndependentResults`
test method, simplifying the test setup and focusing on runnable behavior.

* Code cleanup

* API doc link cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-07 13:01:19 -07:00

150 lines
3.9 KiB
C#

using Xunit.Abstractions;
namespace ApplicationTests.Navigation;
public class ApplicationNavigationTests (ITestOutputHelper output)
{
private readonly ITestOutputHelper _output = output;
[Theory]
[InlineData (TabBehavior.NoStop)]
[InlineData (TabBehavior.TabStop)]
[InlineData (TabBehavior.TabGroup)]
public void Begin_SetsFocus_On_Deepest_Focusable_View (TabBehavior behavior)
{
using IApplication? application = Application.Create ();
Runnable<bool> runnable = new()
{
TabStop = behavior,
CanFocus = true
};
View subView = new ()
{
CanFocus = true,
TabStop = behavior
};
runnable.Add (subView);
View subSubView = new ()
{
CanFocus = true,
TabStop = TabBehavior.NoStop
};
subView.Add (subSubView);
SessionToken? rs = application.Begin (runnable);
Assert.True (runnable.HasFocus);
Assert.True (subView.HasFocus);
Assert.True (subSubView.HasFocus);
runnable.Dispose ();
}
[Fact]
public void Begin_SetsFocus_On_Top ()
{
using IApplication? application = Application.Create ();
Runnable<bool> runnable = new () { CanFocus = true };
Assert.False (runnable.HasFocus);
application.Begin (runnable);
Assert.True (runnable.HasFocus);
runnable.Dispose ();
}
[Fact]
public void Focused_Change_Raises_FocusedChanged ()
{
using IApplication? application = Application.Create ();
var raised = false;
application.Navigation!.FocusedChanged += ApplicationNavigationOnFocusedChanged;
application.Navigation.SetFocused (new () { CanFocus = true, HasFocus = true });
Assert.True (raised);
application.Navigation.FocusedChanged -= ApplicationNavigationOnFocusedChanged;
return;
void ApplicationNavigationOnFocusedChanged (object? sender, EventArgs e) { raised = true; }
}
[Fact]
public void GetFocused_Returns_Focused_View ()
{
using IApplication app = Application.Create ();
app.Begin (
new Runnable<bool>
{
Id = "top",
CanFocus = true,
App = app
});
var subView1 = new View
{
Id = "subView1",
CanFocus = true
};
var subView2 = new View
{
Id = "subView2",
CanFocus = true
};
app.TopRunnableView?.Add (subView1, subView2);
subView1.SetFocus ();
//app.TopRunnableView?.SetFocus ();
Assert.True (subView1.HasFocus);
Assert.Equal (subView1, app.Navigation?.GetFocused ());
app.Navigation?.AdvanceFocus (NavigationDirection.Forward, null);
Assert.Equal (subView2, app.Navigation?.GetFocused ());
}
[Fact]
public void GetFocused_Returns_Null_If_No_Focused_View ()
{
using IApplication app = Application.Create ();
app.Begin (
new Runnable<bool>
{
Id = "top",
CanFocus = true,
App = app
});
var subView1 = new View
{
Id = "subView1",
CanFocus = true
};
app.TopRunnableView!.Add (subView1);
app.TopRunnableView.SetFocus ();
Assert.True (subView1.HasFocus);
Assert.Equal (subView1, app.Navigation!.GetFocused ());
subView1.HasFocus = false;
Assert.False (subView1.HasFocus);
Assert.True (app.TopRunnableView.HasFocus);
Assert.Equal (app.TopRunnableView, app.Navigation.GetFocused ());
app.TopRunnableView.HasFocus = false;
Assert.False (app.TopRunnableView.HasFocus);
Assert.Null (app.Navigation.GetFocused ());
}
}