Files
Terminal.Gui/Tests/TerminalGuiFluentTesting/FakeDriverV2.cs
BDisp ad8ebc9890 Fixes #4200. ExtendedCharInfo needs be enhanced to properly deal with all codepoints (#4202)
* Fixes #4196. Application.Begin doesn't refresh the screen at start

* Fixes #4198. Application.Invoke isn't wakeup the driver if idle

* Reformatting to run CI again

* Revert "Reformatting to run CI again"

This reverts commit ef639c1e64.

* Trying fix an issue where sometimes subview variable is null running unit tests

* Replace ExtendedCharInfo.Char with char array

* Replace IsWindowsTerminal with IsVirtualTerminal

* Add a lastSize parameter to process resize automatically

* Handling surrogate pairs in input

* Implement SetConsoleTextAttribute

* Prevent select true color is not supported

* Fix null exception

* Revert GetWindowSize and add SetWindowSize

* Fix unit tests

* Revert all v2 changes except the one related with the ExtendedCharInfo

* Revert newlines and FakeOutput

* Prevents null reference

* Add gnome-terminal to launch settings

* Fixes issue on restore window size after maximize causing width shrinking

* Add ; exec bash to stay in terminal

* Fixes issue on restore window size after maximize causing width shrinking

* Tidying up input and output console modes

* Fixes uninitialized screen buffer.

* Revert "Fixes issue on restore window size after maximize causing width shrinking"

This reverts commit e5edad79f6.

* Reset console after sending escape sequences

* Remove unnecessary code only for buggy VSDebugConsole

* Fix more annoying exceptions

* Ensure flush the input buffer before reset the console

* Remove unnecessary ENABLE_VIRTUAL_TERMINAL_INPUT

* Remove unnecessary error handles

* Fix CI warnings

* Fix more CI warnings

* Fix more CI warnings

* Fixes #2796. CursesDriver doesn't render wide codepoints correctly

---------

Co-authored-by: Tig <tig@users.noreply.github.com>
2025-09-11 13:37:03 -06:00

160 lines
4.9 KiB
C#

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;
public class FakeApplicationFactory
{
/// <summary>
/// Creates an initialized fake application which will be cleaned up when result object
/// is disposed.
/// </summary>
/// <returns></returns>
public IDisposable SetupFakeApplication ()
{
var cts = new CancellationTokenSource ();
var fakeInput = new FakeNetInput (cts.Token);
FakeOutput output = new ();
output.Size = new (25, 25);
IApplication origApp = ApplicationImpl.Instance;
var sizeMonitor = new FakeSizeMonitor ();
var v2 = new ApplicationV2 (new FakeNetComponentFactory (fakeInput, output, sizeMonitor));
ApplicationImpl.ChangeInstance (v2);
v2.Init (null,"v2net");
var d = (ConsoleDriverFacade<ConsoleKeyInfo>)Application.Driver!;
sizeMonitor.SizeChanging += (_, e) =>
{
if (e.Size != null)
{
var s = e.Size.Value;
output.Size = s;
d.OutputBuffer.SetWindowSize (s.Width, s.Height);
}
};
return new FakeApplicationLifecycle (origApp,cts);
}
}
class FakeApplicationLifecycle : IDisposable
{
private readonly IApplication _origApp;
private readonly CancellationTokenSource _hardStop;
public FakeApplicationLifecycle (IApplication origApp, CancellationTokenSource hardStop)
{
_origApp = origApp;
_hardStop = hardStop;
}
/// <inheritdoc />
public void Dispose ()
{
_hardStop.Cancel();
Application.Top?.Dispose ();
Application.Shutdown ();
ApplicationImpl.ChangeInstance (_origApp);
}
}
public class FakeDriverFactory
{
/// <summary>
/// Creates a new instance of <see cref="FakeDriverV2"/> using default options
/// </summary>
/// <returns></returns>
public IFakeDriverV2 Create ()
{
return new FakeDriverV2 (
new ConcurrentQueue<ConsoleKeyInfo> (),
new OutputBuffer (),
new FakeOutput (),
() => DateTime.Now,
new FakeSizeMonitor ());
}
}
public interface IFakeDriverV2 : IConsoleDriver, IConsoleDriverFacade
{
void SetBufferSize (int width, int height);
}
/// <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>
class FakeDriverV2 : ConsoleDriverFacade<ConsoleKeyInfo>, IFakeDriverV2
{
public ConcurrentQueue<ConsoleKeyInfo> InputBuffer { get; }
public FakeSizeMonitor SizeMonitor { get; }
public new OutputBuffer OutputBuffer { get; }
public IConsoleOutput ConsoleOutput { get; }
private FakeOutput _fakeOutput;
internal FakeDriverV2 (
ConcurrentQueue<ConsoleKeyInfo> inputBuffer,
OutputBuffer outputBuffer,
FakeOutput fakeOutput,
Func<DateTime> datetimeFunc,
FakeSizeMonitor sizeMonitor) :
base (new NetInputProcessor (inputBuffer),
outputBuffer,
fakeOutput,
new (new AnsiResponseParser (), datetimeFunc),
sizeMonitor)
{
InputBuffer = inputBuffer;
SizeMonitor = sizeMonitor;
OutputBuffer = outputBuffer;
ConsoleOutput = _fakeOutput = fakeOutput;
SizeChanged += (_, e) =>
{
if (e.Size != null)
{
var s = e.Size.Value;
_fakeOutput.Size = s;
OutputBuffer.SetWindowSize (s.Width,s.Height);
}
};
}
public void SetBufferSize (int width, int height)
{
SizeMonitor.RaiseSizeChanging (new Size (width,height));
OutputBuffer.SetWindowSize (width,height);
}
}
public class FakeSizeMonitor : IWindowSizeMonitor
{
/// <inheritdoc />
public event EventHandler<SizeChangedEventArgs>? SizeChanging;
/// <inheritdoc />
public bool Poll ()
{
return false;
}
/// <summary>
/// Raises the <see cref="SizeChanging"/> event.
/// </summary>
/// <param name="newSize"></param>
public void RaiseSizeChanging (Size newSize)
{
SizeChanging?.Invoke (this,new (newSize));
}
}