ConsoleDriverTests.cs 9.0 KB

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