ConsoleDriverTests.cs 8.0 KB

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