ConsoleDriverTests.cs 8.4 KB

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