FakeDriverV2.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections.Concurrent;
  2. using System.Drawing;
  3. using TerminalGuiFluentTesting;
  4. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  5. namespace Terminal.Gui.Drivers;
  6. public class FakeApplicationFactory
  7. {
  8. /// <summary>
  9. /// Creates an initialized fake application which will be cleaned up when result object
  10. /// is disposed.
  11. /// </summary>
  12. /// <returns></returns>
  13. public IDisposable SetupFakeApplication ()
  14. {
  15. var cts = new CancellationTokenSource ();
  16. var fakeInput = new FakeNetInput (cts.Token);
  17. FakeOutput output = new ();
  18. output.Size = new (25, 25);
  19. IApplication origApp = ApplicationImpl.Instance;
  20. var sizeMonitor = new FakeSizeMonitor ();
  21. var v2 = new ApplicationV2 (new FakeNetComponentFactory (fakeInput, output, sizeMonitor));
  22. ApplicationImpl.ChangeInstance (v2);
  23. v2.Init (null,"v2net");
  24. var d = (ConsoleDriverFacade<ConsoleKeyInfo>)Application.Driver!;
  25. sizeMonitor.SizeChanging += (_, e) =>
  26. {
  27. if (e.Size != null)
  28. {
  29. var s = e.Size.Value;
  30. output.Size = s;
  31. d.OutputBuffer.SetWindowSize (s.Width, s.Height);
  32. }
  33. };
  34. return new FakeApplicationLifecycle (origApp,cts);
  35. }
  36. }
  37. class FakeApplicationLifecycle : IDisposable
  38. {
  39. private readonly IApplication _origApp;
  40. private readonly CancellationTokenSource _hardStop;
  41. public FakeApplicationLifecycle (IApplication origApp, CancellationTokenSource hardStop)
  42. {
  43. _origApp = origApp;
  44. _hardStop = hardStop;
  45. }
  46. /// <inheritdoc />
  47. public void Dispose ()
  48. {
  49. _hardStop.Cancel();
  50. Application.Top?.Dispose ();
  51. Application.Shutdown ();
  52. ApplicationImpl.ChangeInstance (_origApp);
  53. }
  54. }
  55. public class FakeDriverFactory
  56. {
  57. /// <summary>
  58. /// Creates a new instance of <see cref="FakeDriverV2"/> using default options
  59. /// </summary>
  60. /// <returns></returns>
  61. public IFakeDriverV2 Create ()
  62. {
  63. return new FakeDriverV2 (
  64. new ConcurrentQueue<ConsoleKeyInfo> (),
  65. new OutputBuffer (),
  66. new FakeOutput (),
  67. () => DateTime.Now,
  68. new FakeSizeMonitor ());
  69. }
  70. }
  71. public interface IFakeDriverV2 : IConsoleDriver, IConsoleDriverFacade
  72. {
  73. void SetBufferSize (int width, int height);
  74. }
  75. /// <summary>
  76. /// Implementation of <see cref="IConsoleDriver"/> that uses fake input/output.
  77. /// This is a lightweight alternative to <see cref="GuiTestContext"/> (if you don't
  78. /// need the entire application main loop running).
  79. /// </summary>
  80. class FakeDriverV2 : ConsoleDriverFacade<ConsoleKeyInfo>, IFakeDriverV2
  81. {
  82. public ConcurrentQueue<ConsoleKeyInfo> InputBuffer { get; }
  83. public FakeSizeMonitor SizeMonitor { get; }
  84. public new OutputBuffer OutputBuffer { get; }
  85. public IConsoleOutput ConsoleOutput { get; }
  86. private FakeOutput _fakeOutput;
  87. internal FakeDriverV2 (
  88. ConcurrentQueue<ConsoleKeyInfo> inputBuffer,
  89. OutputBuffer outputBuffer,
  90. FakeOutput fakeOutput,
  91. Func<DateTime> datetimeFunc,
  92. FakeSizeMonitor sizeMonitor) :
  93. base (new NetInputProcessor (inputBuffer),
  94. outputBuffer,
  95. fakeOutput,
  96. new (new AnsiResponseParser (), datetimeFunc),
  97. sizeMonitor)
  98. {
  99. InputBuffer = inputBuffer;
  100. SizeMonitor = sizeMonitor;
  101. OutputBuffer = outputBuffer;
  102. ConsoleOutput = _fakeOutput = fakeOutput;
  103. SizeChanged += (_, e) =>
  104. {
  105. if (e.Size != null)
  106. {
  107. var s = e.Size.Value;
  108. _fakeOutput.Size = s;
  109. OutputBuffer.SetWindowSize (s.Width,s.Height);
  110. }
  111. };
  112. }
  113. public void SetBufferSize (int width, int height)
  114. {
  115. SizeMonitor.RaiseSizeChanging (new Size (width,height));
  116. OutputBuffer.SetWindowSize (width,height);
  117. }
  118. }
  119. public class FakeSizeMonitor : IWindowSizeMonitor
  120. {
  121. /// <inheritdoc />
  122. public event EventHandler<SizeChangedEventArgs>? SizeChanging;
  123. /// <inheritdoc />
  124. public bool Poll ()
  125. {
  126. return false;
  127. }
  128. /// <summary>
  129. /// Raises the <see cref="SizeChanging"/> event.
  130. /// </summary>
  131. /// <param name="newSize"></param>
  132. public void RaiseSizeChanging (Size newSize)
  133. {
  134. SizeChanging?.Invoke (this,new (newSize));
  135. }
  136. }