ConsoleDriverTests.cs 8.0 KB

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