ConsoleDriverTests.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.FakeConsole;
  5. namespace Terminal.Gui.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 (NetDriver))]
  17. //[InlineData (typeof (ANSIDriver))]
  18. [InlineData (typeof (WindowsDriver))]
  19. [InlineData (typeof (CursesDriver))]
  20. public void End_Cleans_Up (Type driverType)
  21. {
  22. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  23. driver.Init ();
  24. driver.End ();
  25. }
  26. [Theory]
  27. [InlineData (typeof (FakeDriver))]
  28. public void FakeDriver_MockKeyPresses (Type driverType)
  29. {
  30. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  31. Application.Init (driver);
  32. var text = "MockKeyPresses";
  33. Stack<ConsoleKeyInfo> mKeys = new ();
  34. foreach (char r in text.Reverse ())
  35. {
  36. ConsoleKey ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  37. var cki = new ConsoleKeyInfo (r, ck, char.IsUpper(r), false, false);
  38. mKeys.Push (cki);
  39. }
  40. Console.MockKeyPresses = mKeys;
  41. Toplevel top = new ();
  42. var view = new View { CanFocus = true };
  43. var rText = "";
  44. var idx = 0;
  45. view.KeyDown += (s, e) =>
  46. {
  47. Assert.Equal (new Rune(text [idx]), e.AsRune);
  48. rText += e.AsRune;
  49. Assert.Equal (rText, text.Substring (0, idx + 1));
  50. e.Handled = true;
  51. idx++;
  52. };
  53. top.Add (view);
  54. Application.Iteration += (s, a) =>
  55. {
  56. if (mKeys.Count == 0)
  57. {
  58. Application.RequestStop ();
  59. }
  60. };
  61. Application.Run (top);
  62. Assert.Equal ("MockKeyPresses", rText);
  63. top.Dispose ();
  64. // Shutdown must be called to safely clean up Application if Init has been called
  65. Application.Shutdown ();
  66. }
  67. [Theory]
  68. [InlineData (typeof (FakeDriver))]
  69. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses (Type driverType)
  70. {
  71. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  72. Application.Init (driver);
  73. Toplevel top = new ();
  74. var view = new View { CanFocus = true };
  75. var count = 0;
  76. var wasKeyPressed = false;
  77. view.KeyDown += (s, e) => { wasKeyPressed = true; };
  78. top.Add (view);
  79. Application.Iteration += (s, a) =>
  80. {
  81. count++;
  82. if (count == 10)
  83. {
  84. Application.RequestStop ();
  85. }
  86. };
  87. Application.Run (top);
  88. Assert.False (wasKeyPressed);
  89. top.Dispose ();
  90. // Shutdown must be called to safely clean up Application if Init has been called
  91. Application.Shutdown ();
  92. }
  93. [Theory]
  94. [InlineData (typeof (FakeDriver))]
  95. [InlineData (typeof (NetDriver))]
  96. //[InlineData (typeof (ANSIDriver))]
  97. [InlineData (typeof (WindowsDriver))]
  98. [InlineData (typeof (CursesDriver))]
  99. public void Init_Inits (Type driverType)
  100. {
  101. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  102. MainLoop ml = driver.Init ();
  103. Assert.NotNull (ml);
  104. Assert.NotNull (driver.Clipboard);
  105. Console.ForegroundColor = ConsoleColor.Red;
  106. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  107. Console.BackgroundColor = ConsoleColor.Green;
  108. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  109. driver.End ();
  110. }
  111. //[Theory]
  112. //[InlineData (typeof (FakeDriver))]
  113. //public void FakeDriver_MockKeyPresses_Press_AfterTimeOut (Type driverType)
  114. //{
  115. // var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  116. // Application.Init (driver);
  117. // // Simulating pressing of QuitKey after a short period of time
  118. // uint quitTime = 100;
  119. // Func<MainLoop, bool> closeCallback = (MainLoop loop) => {
  120. // // Prove the scenario is using Application.QuitKey correctly
  121. // output.WriteLine ($" {quitTime}ms elapsed; Simulating keypresses...");
  122. // FakeConsole.PushMockKeyPress (Key.F);
  123. // FakeConsole.PushMockKeyPress (Key.U);
  124. // FakeConsole.PushMockKeyPress (Key.C);
  125. // FakeConsole.PushMockKeyPress (Key.K);
  126. // return false;
  127. // };
  128. // output.WriteLine ($"Add timeout to simulate key presses after {quitTime}ms");
  129. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (quitTime), closeCallback);
  130. // // If Top doesn't quit within abortTime * 5 (500ms), this will force it
  131. // uint abortTime = quitTime * 5;
  132. // Func<MainLoop, bool> forceCloseCallback = (MainLoop loop) => {
  133. // Application.RequestStop ();
  134. // Assert.Fail ($" failed to Quit after {abortTime}ms. Force quit.");
  135. // return false;
  136. // };
  137. // output.WriteLine ($"Add timeout to force quit after {abortTime}ms");
  138. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), forceCloseCallback);
  139. // Key key = Key.Unknown;
  140. // Application.Top.KeyPress += (e) => {
  141. // key = e.Key;
  142. // output.WriteLine ($" Application.Top.KeyPress: {key}");
  143. // e.Handled = true;
  144. // };
  145. // int iterations = 0;
  146. // Application.Iteration += (s, a) => {
  147. // output.WriteLine ($" iteration {++iterations}");
  148. // if (Console.MockKeyPresses.Count == 0) {
  149. // output.WriteLine ($" No more MockKeyPresses; RequestStop");
  150. // Application.RequestStop ();
  151. // }
  152. // };
  153. // Application.Run ();
  154. // // Shutdown must be called to safely clean up Application if Init has been called
  155. // Application.Shutdown ();
  156. //}
  157. [Theory]
  158. [InlineData (typeof (FakeDriver))]
  159. [InlineData (typeof (NetDriver))]
  160. //[InlineData (typeof (ANSIDriver))]
  161. [InlineData (typeof (WindowsDriver))]
  162. [InlineData (typeof (CursesDriver))]
  163. public void TerminalResized_Simulation (Type driverType)
  164. {
  165. var driver = (IConsoleDriver)Activator.CreateInstance (driverType);
  166. driver?.Init ();
  167. driver.Cols = 80;
  168. driver.Rows = 25;
  169. var wasTerminalResized = false;
  170. driver.SizeChanged += (s, e) =>
  171. {
  172. wasTerminalResized = true;
  173. Assert.Equal (120, e.Size.GetValueOrDefault ().Width);
  174. Assert.Equal (40, e.Size.GetValueOrDefault ().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. ((ConsoleDriver)driver).OnSizeChanged (new SizeChangedEventArgs (new (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 (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 (0, 0, 20, 8), pos);
  227. // win.RequestStop ();
  228. // });
  229. // });
  230. // Application.Run (win);
  231. // Application.Shutdown ();
  232. // }
  233. }