Class1.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections.Concurrent;
  2. using System.Drawing;
  3. using Terminal.Gui;
  4. namespace TerminalGuiFluentAssertions;
  5. class FakeInput<T> : IConsoleInput<T>
  6. {
  7. /// <inheritdoc />
  8. public void Dispose () { }
  9. /// <inheritdoc />
  10. public void Initialize (ConcurrentQueue<T> inputBuffer) { }
  11. /// <inheritdoc />
  12. public void Run (CancellationToken token)
  13. {
  14. // Simulate an infinite loop that checks for cancellation
  15. token.WaitHandle.WaitOne (); // Blocks until the token is cancelled
  16. }
  17. }
  18. class FakeNetInput : FakeInput<ConsoleKeyInfo>, INetInput
  19. {
  20. }
  21. class FakeWindowsInput : FakeInput<WindowsConsole.InputRecord>, IWindowsInput
  22. {
  23. }
  24. class FakeOutput : IConsoleOutput
  25. {
  26. public Size Size { get; set; }
  27. /// <inheritdoc />
  28. public void Dispose ()
  29. {
  30. }
  31. /// <inheritdoc />
  32. public void Write (ReadOnlySpan<char> text)
  33. {
  34. }
  35. /// <inheritdoc />
  36. public void Write (IOutputBuffer buffer)
  37. {
  38. }
  39. /// <inheritdoc />
  40. public Size GetWindowSize ()
  41. {
  42. return Size;
  43. }
  44. /// <inheritdoc />
  45. public void SetCursorVisibility (CursorVisibility visibility)
  46. {
  47. }
  48. /// <inheritdoc />
  49. public void SetCursorPosition (int col, int row)
  50. {
  51. }
  52. }
  53. /// <summary>
  54. /// Entry point to fluent assertions.
  55. /// </summary>
  56. public static class With
  57. {
  58. /// <summary>
  59. /// Entrypoint to fluent assertions
  60. /// </summary>
  61. /// <param name="width"></param>
  62. /// <param name="height"></param>
  63. /// <returns></returns>
  64. public static GuiTestContext<T> A<T> (int width, int height) where T : Toplevel, new ()
  65. {
  66. return new GuiTestContext<T> (width,height);
  67. }
  68. }
  69. public class GuiTestContext<T> where T : Toplevel, new()
  70. {
  71. private readonly CancellationTokenSource _cts;
  72. private readonly Task _runTask;
  73. internal GuiTestContext (int width, int height)
  74. {
  75. IApplication origApp = ApplicationImpl.Instance;
  76. var netInput = new FakeNetInput ();
  77. var winInput = new FakeWindowsInput ();
  78. var output = new FakeOutput ();
  79. output.Size = new (width, height);
  80. var v2 = new ApplicationV2(
  81. () => netInput,
  82. ()=>output,
  83. () => winInput,
  84. () => output);
  85. // Create a cancellation token
  86. _cts = new ();
  87. // Start the application in a background thread
  88. _runTask = Task.Run (() =>
  89. {
  90. try
  91. {
  92. ApplicationImpl.ChangeInstance (v2);
  93. v2.Init ();
  94. Application.Run<T> (); // This will block, but it's on a background thread now
  95. Application.Shutdown ();
  96. }
  97. catch (OperationCanceledException)
  98. {
  99. }
  100. finally
  101. {
  102. ApplicationImpl.ChangeInstance (origApp);
  103. }
  104. }, _cts.Token);
  105. Application.Shutdown ();
  106. }
  107. /// <summary>
  108. /// Stops the application and waits for the background thread to exit.
  109. /// </summary>
  110. public void Stop ()
  111. {
  112. _cts.Cancel ();
  113. Application.Invoke (()=>Application.RequestStop());
  114. _runTask.Wait (); // Ensure the background thread exits
  115. }
  116. // Cleanup to avoid state bleed between tests
  117. public void Dispose ()
  118. {
  119. }
  120. }