FakeDriverV2.cs 4.8 KB

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