ConsoleDriverTests.cs 7.9 KB

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