Files
Terminal.Gui/Tests/UnitTestsParallelizable/Application/Timeouts/LogarithmicTimeoutTests.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

81 lines
2.6 KiB
C#

namespace ApplicationTests.Timeout;
public class LogarithmicTimeoutTests
{
[Fact]
public void Span_Should_Return_BaseDelay_When_Stage_Is_Zero ()
{
var baseDelay = TimeSpan.FromMilliseconds (1000);
var timeout = new LogarithmicTimeout (baseDelay, () => true);
Assert.Equal (TimeSpan.Zero, timeout.Span);
}
[Fact]
public void Span_Should_Increase_Logarithmically ()
{
var baseDelay = TimeSpan.FromMilliseconds (1000);
var timeout = new LogarithmicTimeout (baseDelay, () => true);
var stage0 = timeout.Span;
timeout.AdvanceStage (); // stage = 1
var stage1 = timeout.Span;
timeout.AdvanceStage (); // stage = 2
var stage2 = timeout.Span;
timeout.AdvanceStage (); // stage = 3
var stage3 = timeout.Span;
Assert.True (stage1 > stage0, "Stage 1 should be greater than stage 0");
Assert.True (stage2 > stage1, "Stage 2 should be greater than stage 1");
Assert.True (stage3 > stage2, "Stage 3 should be greater than stage 2");
}
[Theory]
[MemberData (nameof (GetLogarithmicTestData))]
public void Span_Should_Match_Expected_Logarithmic_Value (
double baseDelayMs, int stage, double expectedMs)
{
var baseDelay = TimeSpan.FromMilliseconds (baseDelayMs);
var timeout = new LogarithmicTimeout (baseDelay, () => true);
for (int i = 0; i < stage; i++)
{
timeout.AdvanceStage ();
}
double actualMs = timeout.Span.TotalMilliseconds;
double tolerance = 0.001; // Allow minor rounding error
Assert.InRange (actualMs, expectedMs - tolerance, expectedMs + tolerance);
}
public static IEnumerable<object []> GetLogarithmicTestData ()
{
// baseDelayMs, stage, expectedSpanMs
double baseMs = 1000;
yield return new object [] { baseMs, 0, 0 };
yield return new object [] { baseMs, 1, baseMs * Math.Log (2) };
yield return new object [] { baseMs, 2, baseMs * Math.Log (3) };
yield return new object [] { baseMs, 5, baseMs * Math.Log (6) };
yield return new object [] { baseMs, 10, baseMs * Math.Log (11) };
}
[Fact]
public void Reset_Should_Set_Stage_Back_To_Zero ()
{
var baseDelay = TimeSpan.FromMilliseconds (1000);
var timeout = new LogarithmicTimeout (baseDelay, () => true);
timeout.AdvanceStage ();
timeout.AdvanceStage ();
Assert.NotEqual (baseDelay, timeout.Span);
timeout.Reset ();
Assert.Equal (TimeSpan.Zero, timeout.Span);
}
}