Fixes #4361 - Consolidate FakeDriver into library and refactor driver architecture (#4362)

* Initial plan

* Add ScreenChanged event, SetScreenSize method, and fix FakeDriver buffer initialization

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

* Add comprehensive tests for ScreenChanged event and buffer integrity

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

* Replace obsolete SizeChanged usage with ScreenChanged in core and tests

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

* Refactor terminal size event handling

Replaced `ScreenChanged` with `SizeChanged` across the codebase to standardize naming and improve clarity. Updated event handling logic, including subscriptions, unsubscriptions, and raising methods. Removed deprecated `ScreenChanged` event and backward compatibility code.

Refactored driver initialization to handle legacy `IConsoleDriver` types separately. Updated tests and mock implementations to align with the new `SizeChanged` event. Improved documentation and comments to reflect these changes.

These updates enhance maintainability, consistency, and modernize the architecture.

* Refactor & Code Cleanup: Replace IWindowSizeMonitor with IConsoleSizeMonitor

Renamed `IWindowSizeMonitor` to `IConsoleSizeMonitor` across the codebase, updating all references, method signatures, and event handlers. Replaced the `WindowSizeMonitor` class with the new `ConsoleSizeMonitor` implementation, which includes improved terminal size change handling via the `Poll` method.

Enabled nullable reference types in several files to enhance code safety. Updated test cases to reflect the new `IConsoleSizeMonitor` interface. Removed redundant code, simplified null checks, and corrected minor typos in comments. Streamlined the codebase by removing the obsolete `WindowSizeMonitor` class and its interface.

* Code cleanup - Refactor and enhance ShadowView and FakeDriverTests

Updated ShadowView.cs to use null-conditional operators and added null checks for safer access to `ScreenContents`. Refined XML documentation in View.Layout.cs for clarity and consistency.

Refactored FakeDriverTests.cs to leverage modern C# features, including shorthand object instantiation, inline lambdas, and tuple-like syntax for `Size` and `Rectangle`. Removed redundant tests and improved test readability and reliability.

Enhanced error handling with null checks and ensured backward compatibility for deprecated events. Improved test coverage for resizing, clipboard operations, and invalid coordinates. Verified buffer integrity and screen updates after resizing.

General improvements include replacing explicit type declarations with `var`, removing unused imports, and aligning code formatting for better readability.

Refactor and improve code quality and test coverage

Updated `ShadowView` for null safety using null-conditional operators. Simplified object initializations and modernized syntax across the codebase, including shorthand initializations and inline lambdas. Enhanced event handling logic and ensured compatibility with obsolete members.

Refactored `FakeDriverTests` by removing redundant code, standardizing formatting, and improving test setup. Suppressed obsolete warnings where necessary. Improved XML documentation in `View.Layout.cs` for clarity and removed outdated references.

Performed general cleanup, including removing unused namespaces, redundant comments, and ensuring consistent formatting. These changes enhance readability, maintainability, and runtime safety.

* Code cleanup

Refactor TimedEventsTests for readability and consistency

Improved code readability and maintainability:
- Enabled nullable reference types with `#nullable enable`.
- Removed unused `using System.Diagnostics;`.
- Updated namespace to `UnitTests.ApplicationTests`.
- Replaced `Terminal.Gui.App.TimedEvents` with `TimedEvents`.
- Reformatted XML documentation comments for alignment.
- Used `var` and target-typed new expressions for consistency.
- Reformatted `Parallel.For` loops and lambdas for clarity.
- Added `Thread.Sleep(10)` to prevent excessive CPU usage in tests.
- Improved assertions and event handler formatting in tests.

Aligned with modern C# coding practices.

* Code Cleanup - No more driver warnings.

Refactor codebase and introduce FakeClipboard

- Adjusted `.editorconfig` to change severity levels for CS0612, CS0618, and CS0672 diagnostics.
- Replaced `FakeDriver.FakeClipboard` with a new `FakeClipboard` class for testing purposes, supporting exception handling and clipboard data manipulation.
- Removed redundant methods (`MakeColor`, `MapKey`) and unused classes (`MockConsoleDriver`) to streamline the codebase.
- Refactored `ConsoleDriverFacade` and `FakeDriver` to simplify logic and improve maintainability.
- Updated tests to use `CreateFakeDriver` and removed or commented out obsolete tests.
- Reformatted and cleaned up code for readability across multiple files.

* Refactor FakeDriver - Code Cleanup

Standardized console size management by replacing `WindowSizeMonitor` with `ConsoleSizeMonitor` across the codebase. Updated methods `GetWindowSize` and `SetWindowSize` to `GetSize` and `SetSize` for consistency.

Refactored `FakeDriver` to use `SetScreenSize` and removed redundant methods. Simplified driver initialization by removing legacy `InternalInit` logic.

Standardized ANSI escape sequences by replacing `CSI_ReportTerminalSizeInChars` with `CSI_ReportWindowSizeInChars`.

Updated test cases to align with the new `ConsoleSizeMonitor` and `SetScreenSize` methods. Removed obsolete test utilities like `FakeSizeMonitor` and `FakeWindowSizeMonitor`.

Performed general code cleanup, including removing unused classes, redundant code, and improving formatting. Fixed resizing logic issues and improved exception handling in driver methods.

* Update Terminal.Gui/Drivers/OutputBuffer.cs

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

* Update Terminal.Gui/Drivers/MouseButtonStateEx.cs

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

* Update Terminal.Gui/App/MainLoop/IApplicationMainLoop.cs

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

* Update Tests/UnitTests/Views/ToplevelTests.cs

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

* Update Terminal.Gui/ViewBase/View.Layout.cs

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

* Moved all Drawing tests to Paralleizable - proving Fakedriver works

Enhanced `Ruler` and `Thickness` classes by adding an optional `IConsoleDriver? driver` parameter to decouple rendering from the default `Application.Driver`, improving flexibility and testability. Updated `DriverAssert` to support nullable drivers and added defensive checks.

Refactored and expanded test cases for `Ruler`, `Thickness`, `LineCanvas`, and `StraightLineExtensions` to ensure comprehensive coverage, including zero-length intersections, line rendering, and exclusion logic. Migrated rendering-dependent tests to a parallelizable namespace.

Removed redundant tests, improved naming conventions, and updated documentation for better maintainability and clarity.

* Fixed Run<T> startup hang.

Refactor: Simplify driver logic and update SetSize methods

Removed FakeDriver safeguard in unit tests to simplify
CreateDriver logic. Updated SetSize methods in NetOutput,
UnixOutput, and WindowsOutput to do nothing instead of
throwing NotImplementedException. Modified SizeChanged
event in ConsoleDriverFacade to call SetScreenSize directly.
Commented out unnecessary debug validation in DimAuto.
These changes improve maintainability and reduce complexity.

* Fixed intermittent unit test bug.

Refactored `_cachedRunStateToplevel` to `CachedRunStateToplevel` as an internal static property, delegating its management to `ApplicationImpl` for improved encapsulation. Updated all references to use the new property and centralized its handling in `ApplicationImpl`.

Removed the `MouseGrabHandler` property from `ApplicationImpl` and simplified driver-related assignments by replacing `Application.ForceDriver` and `Application.Screen` with direct references.

Reset `CachedRunStateToplevel` during cleanup to ensure proper state management. Updated the `Invoke` method to use `Top` directly, aligning with the refactored design. Improved debug assertions and performed general cleanup to enhance code readability and maintainability.

* Fixed intermittent bug an massive code cleanup of warnings.

Refactor and enhance codebase for maintainability

- Applied null-conditional operator (`!`) to improve null-safety.
- Refactored tests for clarity, added new cases, and removed redundancies.
- Introduced `FakeApplicationFactory` and `FakeSizeMonitor` for testing.
- Removed unused code, including legacy `DecodeEscSeq` logic.
- Reformatted code for readability and consistency.
- Updated assertions for more accurate validation in tests.
- Fixed potential null reference issues across multiple files.
- Improved event handling with proper null checks.
- Enhanced documentation for new classes and methods.
- Modernized code with C# features like `record struct` and `nullable enable`.

---------

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>
This commit is contained in:
Copilot
2025-10-27 23:34:14 -06:00
committed by GitHub
parent f3fc20306e
commit e6a0ec64ca
158 changed files with 3359 additions and 7523 deletions

View File

@@ -16,11 +16,11 @@ public class FakeApplicationFactory
var cts = new CancellationTokenSource ();
var fakeInput = new FakeNetInput (cts.Token);
FakeOutput output = new ();
output.Size = new (25, 25);
output.Size = new (80, 25);
IApplication origApp = ApplicationImpl.Instance;
var sizeMonitor = new FakeSizeMonitor ();
var sizeMonitor = new FakeSizeMonitor (output, output.LastBuffer!);
var impl = new ApplicationImpl (new FakeNetComponentFactory (fakeInput, output, sizeMonitor));
@@ -32,13 +32,13 @@ public class FakeApplicationFactory
// Handle different facade types - cast to common interface instead
var d = (IConsoleDriverFacade)Application.Driver!;
sizeMonitor.SizeChanging += (_, e) =>
sizeMonitor.SizeChanged += (_, e) =>
{
if (e.Size != null)
{
Size s = e.Size.Value;
output.Size = s;
d.OutputBuffer.SetWindowSize (s.Width, s.Height);
d.OutputBuffer.SetSize (s.Width, s.Height);
}
};

View File

@@ -1,58 +0,0 @@
#nullable enable
using System.Collections.Concurrent;
using System.Drawing;
using TerminalGuiFluentTesting;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Terminal.Gui.Drivers;
/// <summary>
/// Implementation of <see cref="IConsoleDriver"/> that uses fake input/output.
/// This is a lightweight alternative to <see cref="GuiTestContext"/> (if you don't
/// need the entire application main loop running).
/// </summary>
internal class FakeConsoleDriver : ConsoleDriverFacade<ConsoleKeyInfo>, IFakeConsoleDriver
{
internal FakeConsoleDriver (
ConcurrentQueue<ConsoleKeyInfo> inputBuffer,
OutputBuffer outputBuffer,
FakeOutput fakeOutput,
Func<DateTime> datetimeFunc,
FakeSizeMonitor sizeMonitor
) :
base (
new NetInputProcessor (inputBuffer),
outputBuffer,
fakeOutput,
new (new AnsiResponseParser (), datetimeFunc),
sizeMonitor)
{
FakeOutput fakeOutput1;
InputBuffer = inputBuffer;
SizeMonitor = sizeMonitor;
OutputBuffer = outputBuffer;
ConsoleOutput = fakeOutput1 = fakeOutput;
SizeChanged += (_, e) =>
{
if (e.Size != null)
{
Size s = e.Size.Value;
fakeOutput1.Size = s;
OutputBuffer.SetWindowSize (s.Width, s.Height);
}
};
}
public void SetBufferSize (int width, int height)
{
SizeMonitor.RaiseSizeChanging (new (width, height));
OutputBuffer.SetWindowSize (width, height);
}
public IConsoleOutput ConsoleOutput { get; }
public ConcurrentQueue<ConsoleKeyInfo> InputBuffer { get; }
public new OutputBuffer OutputBuffer { get; }
public FakeSizeMonitor SizeMonitor { get; }
}

View File

@@ -1,20 +0,0 @@
#nullable enable
namespace Terminal.Gui.Drivers;
#pragma warning disable CS1591
public class FakeDriverFactory
{
/// <summary>
/// Creates a new instance of <see cref="FakeConsoleDriver"/> using default options
/// </summary>
/// <returns></returns>
public IFakeConsoleDriver Create ()
{
return new FakeConsoleDriver (
new (),
new (),
new (),
() => DateTime.Now,
new ());
}
}

View File

@@ -17,7 +17,7 @@ internal class FakeOutput : IConsoleOutput
public void Write (IOutputBuffer buffer) { LastBuffer = buffer; }
/// <inheritdoc/>
public Size GetWindowSize () { return Size; }
public Size GetSize () { return Size; }
/// <inheritdoc/>
public void SetCursorVisibility (CursorVisibility visibility) { }
@@ -25,6 +25,12 @@ internal class FakeOutput : IConsoleOutput
/// <inheritdoc/>
public void SetCursorPosition (int col, int row) { CursorPosition = new Point (col, row); }
/// <inheritdoc />
public void SetSize (int width, int height)
{
Size = new (width, height);
}
/// <summary>
/// The last value set by calling <see cref="SetCursorPosition"/>
/// </summary>

View File

@@ -4,17 +4,17 @@ using System.Drawing;
namespace Terminal.Gui.Drivers;
#pragma warning disable CS1591
public class FakeSizeMonitor : IWindowSizeMonitor
public class FakeSizeMonitor (IConsoleOutput consoleOut, IOutputBuffer _) : IConsoleSizeMonitor
{
/// <inheritdoc/>
public event EventHandler<SizeChangedEventArgs>? SizeChanging;
/// <inheritdoc />
public event EventHandler<SizeChangedEventArgs>? SizeChanged;
/// <inheritdoc/>
public bool Poll () { return false; }
/// <summary>
/// Raises the <see cref="SizeChanging"/> event.
/// Raises the <see cref="SizeChanged"/> event.
/// </summary>
/// <param name="newSize"></param>
public void RaiseSizeChanging (Size newSize) { SizeChanging?.Invoke (this, new (newSize)); }
}
public void RaiseSizeChanged (Size newSize) { SizeChanged?.Invoke (this, new (newSize)); }
}

View File

@@ -1,8 +0,0 @@
#nullable enable
namespace Terminal.Gui.Drivers;
#pragma warning disable CS1591
public interface IFakeConsoleDriver : IConsoleDriver, IConsoleDriverFacade
{
void SetBufferSize (int width, int height);
}

View File

@@ -43,7 +43,7 @@ public class GuiTestContext : IDisposable
_winInput = new (_cts.Token);
_output.Size = new (width, height);
_fakeSizeMonitor = new ();
_fakeSizeMonitor = new (_output, _output.LastBuffer!);
IComponentFactory cf = driver == TestDriver.DotNet
? new FakeNetComponentFactory (_netInput, _output, _fakeSizeMonitor)
@@ -238,11 +238,7 @@ public class GuiTestContext : IDisposable
return WaitIteration (
() =>
{
_output.Size = new (width, height);
_fakeSizeMonitor.RaiseSizeChanging (_output.Size);
var d = (IConsoleDriverFacade)Application.Driver!;
d.OutputBuffer.SetWindowSize (width, height);
Application.Driver!.SetScreenSize(width, height);
});
}
@@ -938,48 +934,35 @@ public class GuiTestContext : IDisposable
public Point GetCursorPosition () { return _output.CursorPosition; }
}
internal class FakeWindowsComponentFactory : WindowsComponentFactory
internal class FakeWindowsComponentFactory (FakeWindowsInput winInput, FakeOutput output, FakeSizeMonitor fakeSizeMonitor)
: WindowsComponentFactory
{
private readonly FakeWindowsInput _winInput;
private readonly FakeOutput _output;
private readonly FakeSizeMonitor _fakeSizeMonitor;
/// <inheritdoc/>
public override IConsoleInput<WindowsConsole.InputRecord> CreateInput () { return winInput; }
public FakeWindowsComponentFactory (FakeWindowsInput winInput, FakeOutput output, FakeSizeMonitor fakeSizeMonitor)
/// <inheritdoc/>
public override IConsoleOutput CreateOutput () { return output; }
/// <inheritdoc/>
public override IConsoleSizeMonitor CreateConsoleSizeMonitor (IConsoleOutput consoleOutput, IOutputBuffer outputBuffer)
{
_winInput = winInput;
_output = output;
_fakeSizeMonitor = fakeSizeMonitor;
outputBuffer.SetSize (consoleOutput.GetSize ().Width, consoleOutput.GetSize ().Height);
return fakeSizeMonitor;
}
/// <inheritdoc/>
public override IConsoleInput<WindowsConsole.InputRecord> CreateInput () { return _winInput; }
/// <inheritdoc/>
public override IConsoleOutput CreateOutput () { return _output; }
/// <inheritdoc/>
public override IWindowSizeMonitor CreateWindowSizeMonitor (IConsoleOutput consoleOutput, IOutputBuffer outputBuffer) { return _fakeSizeMonitor; }
}
internal class FakeNetComponentFactory : NetComponentFactory
internal class FakeNetComponentFactory (FakeNetInput netInput, FakeOutput output, FakeSizeMonitor fakeSizeMonitor) : NetComponentFactory
{
private readonly FakeNetInput _netInput;
private readonly FakeOutput _output;
private readonly FakeSizeMonitor _fakeSizeMonitor;
/// <inheritdoc/>
public override IConsoleInput<ConsoleKeyInfo> CreateInput () { return netInput; }
public FakeNetComponentFactory (FakeNetInput netInput, FakeOutput output, FakeSizeMonitor fakeSizeMonitor)
/// <inheritdoc/>
public override IConsoleOutput CreateOutput () { return output; }
/// <inheritdoc/>
public override IConsoleSizeMonitor CreateConsoleSizeMonitor (IConsoleOutput consoleOutput, IOutputBuffer outputBuffer)
{
_netInput = netInput;
_output = output;
_fakeSizeMonitor = fakeSizeMonitor;
outputBuffer.SetSize (consoleOutput.GetSize ().Width, consoleOutput.GetSize ().Height);
return fakeSizeMonitor;
}
/// <inheritdoc/>
public override IConsoleInput<ConsoleKeyInfo> CreateInput () { return _netInput; }
/// <inheritdoc/>
public override IConsoleOutput CreateOutput () { return _output; }
/// <inheritdoc/>
public override IWindowSizeMonitor CreateWindowSizeMonitor (IConsoleOutput consoleOutput, IOutputBuffer outputBuffer) { return _fakeSizeMonitor; }
}