ConsoleDriverTests.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. // Alias Console to MockConsole so we don't accidentally use Console
  4. using Console = Terminal.Gui.Drivers.FakeConsole;
  5. namespace UnitTests.DriverTests;
  6. public class ConsoleDriverTests
  7. {
  8. private readonly ITestOutputHelper _output;
  9. public ConsoleDriverTests (ITestOutputHelper output)
  10. {
  11. ConsoleDriver.RunningUnitTests = true;
  12. _output = output;
  13. }
  14. [Theory]
  15. [InlineData (typeof (FakeDriver))]
  16. //[InlineData (typeof (DotNetDriver))]
  17. //[InlineData (typeof (ANSIDriver))]
  18. //[InlineData (typeof (WindowsDriver))]
  19. //[InlineData (typeof (UnixDriver))]
  20. public void End_Cleans_Up (Type driverType)
  21. {
  22. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  23. driver.Init ();
  24. driver.End ();
  25. }
  26. // NOTE: These tests were removed because they use legacy FakeDriver patterns that don't work with modern architecture:
  27. // 1. They use Console.MockKeyPresses which is a legacy FakeDriver pattern
  28. // 2. Application.Run() with the legacy FakeDriver doesn't properly process MockKeyPresses in modern architecture
  29. // 3. These tests should be rewritten to use the modern FakeComponentFactory with predefined input
  30. // 4. Key press handling should be tested through the input processor layer, not driver tests
  31. //
  32. // [Theory]
  33. // [InlineData (typeof (FakeDriver))]
  34. // public void FakeDriver_MockKeyPresses (Type driverType)
  35. [Theory]
  36. [InlineData (typeof (FakeDriver))]
  37. //[InlineData (typeof (DotNetDriver))]
  38. //[InlineData (typeof (ANSIDriver))]
  39. //[InlineData (typeof (WindowsDriver))]
  40. //[InlineData (typeof (UnixDriver))]
  41. public void Init_Inits (Type driverType)
  42. {
  43. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  44. driver.Init ();
  45. // Note: MainLoop is no longer returned from Init() as part of legacy MainLoop removal
  46. Assert.NotNull (driver.Clipboard);
  47. Console.ForegroundColor = ConsoleColor.Red;
  48. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  49. Console.BackgroundColor = ConsoleColor.Green;
  50. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  51. driver.End ();
  52. }
  53. //[Theory]
  54. //[InlineData (typeof (FakeDriver))]
  55. //public void FakeDriver_MockKeyPresses_Press_AfterTimeOut (Type driverType)
  56. //{
  57. // var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  58. // Application.Init (driver);
  59. // // Simulating pressing of QuitKey after a short period of time
  60. // uint quitTime = 100;
  61. // Func<MainLoop, bool> closeCallback = (MainLoop loop) => {
  62. // // Prove the scenario is using Application.QuitKey correctly
  63. // output.WriteLine ($" {quitTime}ms elapsed; Simulating keypresses...");
  64. // FakeConsole.PushMockKeyPress (Key.F);
  65. // FakeConsole.PushMockKeyPress (Key.U);
  66. // FakeConsole.PushMockKeyPress (Key.C);
  67. // FakeConsole.PushMockKeyPress (Key.K);
  68. // return false;
  69. // };
  70. // output.WriteLine ($"Add timeout to simulate key presses after {quitTime}ms");
  71. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (quitTime), closeCallback);
  72. // // If Top doesn't quit within abortTime * 5 (500ms), this will force it
  73. // uint abortTime = quitTime * 5;
  74. // Func<MainLoop, bool> forceCloseCallback = (MainLoop loop) => {
  75. // Application.RequestStop ();
  76. // Assert.Fail ($" failed to Quit after {abortTime}ms. Force quit.");
  77. // return false;
  78. // };
  79. // output.WriteLine ($"Add timeout to force quit after {abortTime}ms");
  80. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), forceCloseCallback);
  81. // Key key = Key.Unknown;
  82. // Application.Top.KeyPress += (e) => {
  83. // key = e.Key;
  84. // output.WriteLine ($" Application.Top.KeyPress: {key}");
  85. // e.Handled = true;
  86. // };
  87. // int iterations = 0;
  88. // Application.Iteration += (s, a) => {
  89. // output.WriteLine ($" iteration {++iterations}");
  90. // if (Console.MockKeyPresses.Count == 0) {
  91. // output.WriteLine ($" No more MockKeyPresses; RequestStop");
  92. // Application.RequestStop ();
  93. // }
  94. // };
  95. // Application.Run ();
  96. // // Shutdown must be called to safely clean up Application if Init has been called
  97. // Application.Shutdown ();
  98. //}
  99. [Theory]
  100. [InlineData (typeof (FakeDriver))]
  101. //[InlineData (typeof (DotNetDriver))]
  102. //[InlineData (typeof (ANSIDriver))]
  103. //[InlineData (typeof (WindowsDriver))]
  104. //[InlineData (typeof (UnixDriver))]
  105. public void TerminalResized_Simulation (Type driverType)
  106. {
  107. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  108. driver?.Init ();
  109. driver.Cols = 80;
  110. driver.Rows = 25;
  111. var wasTerminalResized = false;
  112. driver.SizeChanged += (s, e) =>
  113. {
  114. wasTerminalResized = true;
  115. Assert.Equal (120, e.Size.GetValueOrDefault ().Width);
  116. Assert.Equal (40, e.Size.GetValueOrDefault ().Height);
  117. };
  118. Assert.Equal (80, driver.Cols);
  119. Assert.Equal (25, driver.Rows);
  120. Assert.False (wasTerminalResized);
  121. driver.Cols = 120;
  122. driver.Rows = 40;
  123. ((ConsoleDriver)driver).OnSizeChanged (new SizeChangedEventArgs (new (driver.Cols, driver.Rows)));
  124. Assert.Equal (120, driver.Cols);
  125. Assert.Equal (40, driver.Rows);
  126. Assert.True (wasTerminalResized);
  127. driver.End ();
  128. }
  129. // Disabled due to test error - Change Task.Delay to an await
  130. // [Fact, AutoInitShutdown]
  131. // public void Write_Do_Not_Change_On_ProcessKey ()
  132. // {
  133. // var win = new Window ();
  134. // Application.Begin (win);
  135. // Application.Driver!.SetScreenSize ( (20, 8);
  136. // System.Threading.Tasks.Task.Run (() => {
  137. // System.Threading.Tasks.Task.Delay (500).Wait ();
  138. // Application.Invoke (() => {
  139. // var lbl = new Label ("Hello World") { X = Pos.Center () };
  140. // var dlg = new Dialog ();
  141. // dlg.Add (lbl);
  142. // Application.Begin (dlg);
  143. // var expected = @"
  144. //┌──────────────────┐
  145. //│┌───────────────┐ │
  146. //││ Hello World │ │
  147. //││ │ │
  148. //││ │ │
  149. //││ │ │
  150. //│└───────────────┘ │
  151. //└──────────────────┘
  152. //";
  153. // var pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  154. // Assert.Equal (new (0, 0, 20, 8), pos);
  155. // Assert.True (dlg.ProcessKey (new (Key.Tab)));
  156. // dlg.Draw ();
  157. // expected = @"
  158. //┌──────────────────┐
  159. //│┌───────────────┐ │
  160. //││ Hello World │ │
  161. //││ │ │
  162. //││ │ │
  163. //││ │ │
  164. //│└───────────────┘ │
  165. //└──────────────────┘
  166. //";
  167. // pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  168. // Assert.Equal (new (0, 0, 20, 8), pos);
  169. // win.RequestStop ();
  170. // });
  171. // });
  172. // Application.Run (win);
  173. // Application.Shutdown ();
  174. // }
  175. // NOTE: This test was removed because:
  176. // 1. It hangs indefinitely - the Application.Run loop never exits properly with modern architecture
  177. // 2. It's testing general surrogate pair/input handling, not FakeDriver-specific functionality
  178. // 3. It uses legacy FakeDriver patterns (Console.MockKeyPresses) that don't work correctly with modern architecture
  179. // 4. Surrogate pair handling should be tested in input processor tests, not driver tests
  180. // 5. The test accesses private field _highSurrogate which is an implementation detail
  181. //
  182. // [Theory]
  183. // [InlineData ('\ud83d', '\udcc4')] // This seems right sequence but Stack is LIFO
  184. // [InlineData ('\ud83d', '\ud83d')]
  185. // [InlineData ('\udcc4', '\udcc4')]
  186. // public void FakeDriver_IsValidInput_Wrong_Surrogate_Sequence (char c1, char c2)
  187. // NOTE: This test was also removed for the same reasons as FakeDriver_IsValidInput_Wrong_Surrogate_Sequence:
  188. // 1. It hangs indefinitely - the Application.Run loop never exits properly with modern architecture
  189. // 2. It's testing general surrogate pair/input handling, not FakeDriver-specific functionality
  190. // 3. It uses legacy FakeDriver patterns (Console.MockKeyPresses) that don't work correctly with modern architecture
  191. // 4. Surrogate pair handling should be tested in input processor tests, not driver tests
  192. //
  193. // [Fact]
  194. // public void FakeDriver_IsValidInput_Correct_Surrogate_Sequence ()
  195. /// <summary>
  196. /// Tests that when ConsoleDriver.RunningUnitTests is true, Application.Init() without
  197. /// parameters uses FakeDriver instead of platform-specific drivers.
  198. /// This prevents intermittent failures on macOS where kernel32.dll might be referenced.
  199. /// </summary>
  200. [Fact]
  201. public void Application_Init_Without_Params_Uses_FakeDriver_When_RunningUnitTests ()
  202. {
  203. // Arrange
  204. ConsoleDriver.RunningUnitTests = true;
  205. Application.ResetState (true);
  206. // Act
  207. Application.Init ();
  208. // Assert
  209. Assert.NotNull (Application.Driver);
  210. // In the modern v2 architecture, the driver will be a ConsoleDriverFacade wrapping FakeDriver
  211. // The key is that it's not attempting to use Windows/Unix platform-specific drivers
  212. Assert.True (Application.Initialized);
  213. // Cleanup
  214. Application.Shutdown ();
  215. Application.ResetState (true);
  216. }
  217. }