ConsoleDriverTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. using Console = Terminal.Gui.FakeConsole;
  4. namespace Terminal.Gui.DriverTests;
  5. public class ConsoleDriverTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public ConsoleDriverTests (ITestOutputHelper output)
  9. {
  10. ConsoleDriver.RunningUnitTests = true;
  11. _output = output;
  12. }
  13. [Theory]
  14. [InlineData (typeof (FakeDriver))]
  15. [InlineData (typeof (NetDriver))]
  16. //[InlineData (typeof (ANSIDriver))]
  17. [InlineData (typeof (WindowsDriver))]
  18. [InlineData (typeof (CursesDriver))]
  19. public void End_Cleans_Up (Type driverType)
  20. {
  21. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  22. driver.Init ();
  23. driver.End ();
  24. }
  25. [Theory]
  26. [InlineData (typeof (FakeDriver))]
  27. public void FakeDriver_MockKeyPresses (Type driverType)
  28. {
  29. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  30. Application.Init (driver);
  31. var text = "MockKeyPresses";
  32. Stack<ConsoleKeyInfo> mKeys = new ();
  33. foreach (char r in text.Reverse ())
  34. {
  35. ConsoleKey ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  36. var cki = new ConsoleKeyInfo (r, ck, false, false, false);
  37. mKeys.Push (cki);
  38. }
  39. Console.MockKeyPresses = mKeys;
  40. Toplevel top = Application.Top;
  41. var view = new View { CanFocus = true };
  42. var rText = "";
  43. var idx = 0;
  44. view.KeyDown += (s, e) =>
  45. {
  46. Assert.Equal (text [idx], (char)e.KeyCode);
  47. rText += (char)e.KeyCode;
  48. Assert.Equal (rText, text.Substring (0, idx + 1));
  49. e.Handled = true;
  50. idx++;
  51. };
  52. top.Add (view);
  53. Application.Iteration += (s, a) =>
  54. {
  55. if (mKeys.Count == 0)
  56. {
  57. Application.RequestStop ();
  58. }
  59. };
  60. Application.Run ();
  61. Assert.Equal ("MockKeyPresses", rText);
  62. // Shutdown must be called to safely clean up Application if Init has been called
  63. Application.Shutdown ();
  64. }
  65. [Theory]
  66. [InlineData (typeof (FakeDriver))]
  67. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses (Type driverType)
  68. {
  69. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  70. Application.Init (driver);
  71. Toplevel top = Application.Top;
  72. var view = new View { CanFocus = true };
  73. var count = 0;
  74. var wasKeyPressed = false;
  75. view.KeyDown += (s, e) => { wasKeyPressed = true; };
  76. top.Add (view);
  77. Application.Iteration += (s, a) =>
  78. {
  79. count++;
  80. if (count == 10)
  81. {
  82. Application.RequestStop ();
  83. }
  84. };
  85. Application.Run ();
  86. Assert.False (wasKeyPressed);
  87. // Shutdown must be called to safely clean up Application if Init has been called
  88. Application.Shutdown ();
  89. }
  90. [Theory]
  91. [InlineData (typeof (FakeDriver))]
  92. [InlineData (typeof (NetDriver))]
  93. //[InlineData (typeof (ANSIDriver))]
  94. [InlineData (typeof (WindowsDriver))]
  95. [InlineData (typeof (CursesDriver))]
  96. public void Init_Inits (Type driverType)
  97. {
  98. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  99. MainLoop ml = driver.Init ();
  100. Assert.NotNull (ml);
  101. Assert.NotNull (driver.Clipboard);
  102. Console.ForegroundColor = ConsoleColor.Red;
  103. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  104. Console.BackgroundColor = ConsoleColor.Green;
  105. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  106. driver.End ();
  107. }
  108. //[Theory]
  109. //[InlineData (typeof (FakeDriver))]
  110. //public void FakeDriver_MockKeyPresses_Press_AfterTimeOut (Type driverType)
  111. //{
  112. // var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  113. // Application.Init (driver);
  114. // // Simulating pressing of QuitKey after a short period of time
  115. // uint quitTime = 100;
  116. // Func<MainLoop, bool> closeCallback = (MainLoop loop) => {
  117. // // Prove the scenario is using Application.QuitKey correctly
  118. // output.WriteLine ($" {quitTime}ms elapsed; Simulating keypresses...");
  119. // FakeConsole.PushMockKeyPress (Key.F);
  120. // FakeConsole.PushMockKeyPress (Key.U);
  121. // FakeConsole.PushMockKeyPress (Key.C);
  122. // FakeConsole.PushMockKeyPress (Key.K);
  123. // return false;
  124. // };
  125. // output.WriteLine ($"Add timeout to simulate key presses after {quitTime}ms");
  126. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (quitTime), closeCallback);
  127. // // If Top doesn't quit within abortTime * 5 (500ms), this will force it
  128. // uint abortTime = quitTime * 5;
  129. // Func<MainLoop, bool> forceCloseCallback = (MainLoop loop) => {
  130. // Application.RequestStop ();
  131. // Assert.Fail ($" failed to Quit after {abortTime}ms. Force quit.");
  132. // return false;
  133. // };
  134. // output.WriteLine ($"Add timeout to force quit after {abortTime}ms");
  135. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), forceCloseCallback);
  136. // Key key = Key.Unknown;
  137. // Application.Top.KeyPress += (e) => {
  138. // key = e.Key;
  139. // output.WriteLine ($" Application.Top.KeyPress: {key}");
  140. // e.Handled = true;
  141. // };
  142. // int iterations = 0;
  143. // Application.Iteration += (s, a) => {
  144. // output.WriteLine ($" iteration {++iterations}");
  145. // if (Console.MockKeyPresses.Count == 0) {
  146. // output.WriteLine ($" No more MockKeyPresses; RequestStop");
  147. // Application.RequestStop ();
  148. // }
  149. // };
  150. // Application.Run ();
  151. // // Shutdown must be called to safely clean up Application if Init has been called
  152. // Application.Shutdown ();
  153. //}
  154. [Theory]
  155. [InlineData (typeof (FakeDriver))]
  156. [InlineData (typeof (NetDriver))]
  157. //[InlineData (typeof (ANSIDriver))]
  158. [InlineData (typeof (WindowsDriver))]
  159. [InlineData (typeof (CursesDriver))]
  160. public void TerminalResized_Simulation (Type driverType)
  161. {
  162. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  163. driver?.Init ();
  164. driver.Cols = 80;
  165. driver.Rows = 25;
  166. var wasTerminalResized = false;
  167. driver.SizeChanged += (s, e) =>
  168. {
  169. wasTerminalResized = true;
  170. Assert.Equal (120, e.Size.Width);
  171. Assert.Equal (40, e.Size.Height);
  172. };
  173. Assert.Equal (80, driver.Cols);
  174. Assert.Equal (25, driver.Rows);
  175. Assert.False (wasTerminalResized);
  176. driver.Cols = 120;
  177. driver.Rows = 40;
  178. driver.OnSizeChanged (new SizeChangedEventArgs (new Size (driver.Cols, driver.Rows)));
  179. Assert.Equal (120, driver.Cols);
  180. Assert.Equal (40, driver.Rows);
  181. Assert.True (wasTerminalResized);
  182. driver.End ();
  183. }
  184. // Disabled due to test error - Change Task.Delay to an await
  185. // [Fact, AutoInitShutdown]
  186. // public void Write_Do_Not_Change_On_ProcessKey ()
  187. // {
  188. // var win = new Window ();
  189. // Application.Begin (win);
  190. // ((FakeDriver)Application.Driver).SetBufferSize (20, 8);
  191. // System.Threading.Tasks.Task.Run (() => {
  192. // System.Threading.Tasks.Task.Delay (500).Wait ();
  193. // Application.Invoke (() => {
  194. // var lbl = new Label ("Hello World") { X = Pos.Center () };
  195. // var dlg = new Dialog ();
  196. // dlg.Add (lbl);
  197. // Application.Begin (dlg);
  198. // var expected = @"
  199. //┌──────────────────┐
  200. //│┌───────────────┐ │
  201. //││ Hello World │ │
  202. //││ │ │
  203. //││ │ │
  204. //││ │ │
  205. //│└───────────────┘ │
  206. //└──────────────────┘
  207. //";
  208. // var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  209. // Assert.Equal (new Rect (0, 0, 20, 8), pos);
  210. // Assert.True (dlg.ProcessKey (new (Key.Tab)));
  211. // dlg.Draw ();
  212. // expected = @"
  213. //┌──────────────────┐
  214. //│┌───────────────┐ │
  215. //││ Hello World │ │
  216. //││ │ │
  217. //││ │ │
  218. //││ │ │
  219. //│└───────────────┘ │
  220. //└──────────────────┘
  221. //";
  222. // pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  223. // Assert.Equal (new Rect (0, 0, 20, 8), pos);
  224. // win.RequestStop ();
  225. // });
  226. // });
  227. // Application.Run (win);
  228. // Application.Shutdown ();
  229. // }
  230. }