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 { /// /// Creates an initialized fake application which will be cleaned up when result object /// is disposed. /// /// 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)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; } /// public void Dispose () { _hardStop.Cancel(); Application.Top?.Dispose (); Application.Shutdown (); ApplicationImpl.ChangeInstance (_origApp); } } public class FakeDriverFactory { /// /// Creates a new instance of using default options /// /// public IFakeDriverV2 Create () { return new FakeDriverV2 ( new ConcurrentQueue (), new OutputBuffer (), new FakeOutput (), () => DateTime.Now, new FakeSizeMonitor ()); } } public interface IFakeDriverV2 : IConsoleDriver, IConsoleDriverFacade { void SetBufferSize (int width, int height); } /// /// Implementation of that uses fake input/output. /// This is a lightweight alternative to (if you don't /// need the entire application main loop running). /// class FakeDriverV2 : ConsoleDriverFacade, IFakeDriverV2 { public ConcurrentQueue InputBuffer { get; } public FakeSizeMonitor SizeMonitor { get; } public new OutputBuffer OutputBuffer { get; } public IConsoleOutput ConsoleOutput { get; } private FakeOutput _fakeOutput; internal FakeDriverV2 ( ConcurrentQueue inputBuffer, OutputBuffer outputBuffer, FakeOutput fakeOutput, Func 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 { /// public event EventHandler? SizeChanging; /// public bool Poll () { return false; } /// /// Raises the event. /// /// public void RaiseSizeChanging (Size newSize) { SizeChanging?.Invoke (this,new (newSize)); } }