ConsoleDriverTests.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.SetScreenSize(80, 25);
  110. var wasTerminalResized = false;
  111. driver.SizeChanged += (s, e) =>
  112. {
  113. wasTerminalResized = true;
  114. Assert.Equal (120, e.Size.GetValueOrDefault ().Width);
  115. Assert.Equal (40, e.Size.GetValueOrDefault ().Height);
  116. };
  117. Assert.Equal (80, driver.Cols);
  118. Assert.Equal (25, driver.Rows);
  119. Assert.False (wasTerminalResized);
  120. driver.SetScreenSize(120, 40);
  121. ((ConsoleDriver)driver).OnSizeChanged (new SizeChangedEventArgs (new (driver.Cols, driver.Rows)));
  122. Assert.Equal (120, driver.Cols);
  123. Assert.Equal (40, driver.Rows);
  124. Assert.True (wasTerminalResized);
  125. driver.End ();
  126. }
  127. // Disabled due to test error - Change Task.Delay to an await
  128. // [Fact, AutoInitShutdown]
  129. // public void Write_Do_Not_Change_On_ProcessKey ()
  130. // {
  131. // var win = new Window ();
  132. // Application.Begin (win);
  133. // AutoInitShutdownAttribute.FakeResize(new Size ( (20, 8);
  134. // System.Threading.Tasks.Task.Run (() => {
  135. // System.Threading.Tasks.Task.Delay (500).Wait ();
  136. // Application.Invoke (() => {
  137. // var lbl = new Label ("Hello World") { X = Pos.Center () };
  138. // var dlg = new Dialog ();
  139. // dlg.Add (lbl);
  140. // Application.Begin (dlg);
  141. // var expected = @"
  142. //┌──────────────────┐
  143. //│┌───────────────┐ │
  144. //││ Hello World │ │
  145. //││ │ │
  146. //││ │ │
  147. //││ │ │
  148. //│└───────────────┘ │
  149. //└──────────────────┘
  150. //";
  151. // var pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  152. // Assert.Equal (new (0, 0, 20, 8), pos);
  153. // Assert.True (dlg.ProcessKey (new (Key.Tab)));
  154. // dlg.Draw ();
  155. // expected = @"
  156. //┌──────────────────┐
  157. //│┌───────────────┐ │
  158. //││ Hello World │ │
  159. //││ │ │
  160. //││ │ │
  161. //││ │ │
  162. //│└───────────────┘ │
  163. //└──────────────────┘
  164. //";
  165. // pos = DriverAsserts.AssertDriverContentsWithFrameAre (expected, output);
  166. // Assert.Equal (new (0, 0, 20, 8), pos);
  167. // win.RequestStop ();
  168. // });
  169. // });
  170. // Application.Run (win);
  171. // Application.Shutdown ();
  172. // }
  173. // NOTE: This test was removed because:
  174. // 1. It hangs indefinitely - the Application.Run loop never exits properly with modern architecture
  175. // 2. It's testing general surrogate pair/input handling, not FakeDriver-specific functionality
  176. // 3. It uses legacy FakeDriver patterns (Console.MockKeyPresses) that don't work correctly with modern architecture
  177. // 4. Surrogate pair handling should be tested in input processor tests, not driver tests
  178. // 5. The test accesses private field _highSurrogate which is an implementation detail
  179. //
  180. // [Theory]
  181. // [InlineData ('\ud83d', '\udcc4')] // This seems right sequence but Stack is LIFO
  182. // [InlineData ('\ud83d', '\ud83d')]
  183. // [InlineData ('\udcc4', '\udcc4')]
  184. // public void FakeDriver_IsValidInput_Wrong_Surrogate_Sequence (char c1, char c2)
  185. // NOTE: This test was also removed for the same reasons as FakeDriver_IsValidInput_Wrong_Surrogate_Sequence:
  186. // 1. It hangs indefinitely - the Application.Run loop never exits properly with modern architecture
  187. // 2. It's testing general surrogate pair/input handling, not FakeDriver-specific functionality
  188. // 3. It uses legacy FakeDriver patterns (Console.MockKeyPresses) that don't work correctly with modern architecture
  189. // 4. Surrogate pair handling should be tested in input processor tests, not driver tests
  190. //
  191. // [Fact]
  192. // public void FakeDriver_IsValidInput_Correct_Surrogate_Sequence ()
  193. /// <summary>
  194. /// Tests that when ConsoleDriver.RunningUnitTests is true, Application.Init() without
  195. /// parameters uses FakeDriver instead of platform-specific drivers.
  196. /// This prevents intermittent failures on macOS where kernel32.dll might be referenced.
  197. /// </summary>
  198. [Fact]
  199. public void Application_Init_Without_Params_Uses_FakeDriver_When_RunningUnitTests ()
  200. {
  201. // Arrange
  202. ConsoleDriver.RunningUnitTests = true;
  203. Application.ResetState (true);
  204. // Act
  205. Application.Init ();
  206. // Assert
  207. Assert.NotNull (Application.Driver);
  208. // In the modern v2 architecture, the driver will be a ConsoleDriverFacade wrapping FakeDriver
  209. // The key is that it's not attempting to use Windows/Unix platform-specific drivers
  210. Assert.True (Application.Initialized);
  211. // Cleanup
  212. Application.Shutdown ();
  213. Application.ResetState (true);
  214. }
  215. }