ConsoleDriverTests.cs 8.6 KB

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