2
0

ConsoleDriverTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. // Alias Console to MockConsole so we don't accidentally use Console
  4. using Console = Terminal.Gui.FakeConsole;
  5. namespace Terminal.Gui.DriverTests;
  6. public class ConsoleDriverTests
  7. {
  8. private readonly ITestOutputHelper _output;
  9. public ConsoleDriverTests (ITestOutputHelper output)
  10. {
  11. ConsoleDriver.RunningUnitTests = true;
  12. _output = output;
  13. }
  14. [Theory]
  15. [InlineData (typeof (FakeDriver))]
  16. [InlineData (typeof (NetDriver))]
  17. //[InlineData (typeof (ANSIDriver))]
  18. [InlineData (typeof (WindowsDriver))]
  19. [InlineData (typeof (CursesDriver))]
  20. public void End_Cleans_Up (Type driverType)
  21. {
  22. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  23. driver.Init ();
  24. driver.End ();
  25. }
  26. [Theory]
  27. [InlineData (typeof (FakeDriver))]
  28. public void FakeDriver_MockKeyPresses (Type driverType)
  29. {
  30. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  31. Application.Init (driver);
  32. var text = "MockKeyPresses";
  33. Stack<ConsoleKeyInfo> mKeys = new ();
  34. foreach (char r in text.Reverse ())
  35. {
  36. ConsoleKey ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  37. var cki = new ConsoleKeyInfo (r, ck, char.IsUpper(r), false, false);
  38. mKeys.Push (cki);
  39. }
  40. Console.MockKeyPresses = mKeys;
  41. Toplevel top = Application.Top;
  42. var view = new View { CanFocus = true };
  43. var rText = "";
  44. var idx = 0;
  45. view.KeyDown += (s, e) =>
  46. {
  47. Assert.Equal (new Rune(text [idx]), e.AsRune);
  48. rText += e.AsRune;
  49. Assert.Equal (rText, text.Substring (0, idx + 1));
  50. e.Handled = true;
  51. idx++;
  52. };
  53. top.Add (view);
  54. Application.Iteration += (s, a) =>
  55. {
  56. if (mKeys.Count == 0)
  57. {
  58. Application.RequestStop ();
  59. }
  60. };
  61. Application.Run ();
  62. Assert.Equal ("MockKeyPresses", rText);
  63. // Shutdown must be called to safely clean up Application if Init has been called
  64. Application.Shutdown ();
  65. }
  66. [Theory]
  67. [InlineData (typeof (FakeDriver))]
  68. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses (Type driverType)
  69. {
  70. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  71. Application.Init (driver);
  72. Toplevel top = Application.Top;
  73. var view = new View { CanFocus = true };
  74. var count = 0;
  75. var wasKeyPressed = false;
  76. view.KeyDown += (s, e) => { wasKeyPressed = true; };
  77. top.Add (view);
  78. Application.Iteration += (s, a) =>
  79. {
  80. count++;
  81. if (count == 10)
  82. {
  83. Application.RequestStop ();
  84. }
  85. };
  86. Application.Run ();
  87. Assert.False (wasKeyPressed);
  88. // Shutdown must be called to safely clean up Application if Init has been called
  89. Application.Shutdown ();
  90. }
  91. [Theory]
  92. [InlineData (typeof (FakeDriver))]
  93. [InlineData (typeof (NetDriver))]
  94. //[InlineData (typeof (ANSIDriver))]
  95. [InlineData (typeof (WindowsDriver))]
  96. [InlineData (typeof (CursesDriver))]
  97. public void Init_Inits (Type driverType)
  98. {
  99. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  100. MainLoop ml = driver.Init ();
  101. Assert.NotNull (ml);
  102. Assert.NotNull (driver.Clipboard);
  103. Console.ForegroundColor = ConsoleColor.Red;
  104. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  105. Console.BackgroundColor = ConsoleColor.Green;
  106. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  107. driver.End ();
  108. }
  109. //[Theory]
  110. //[InlineData (typeof (FakeDriver))]
  111. //public void FakeDriver_MockKeyPresses_Press_AfterTimeOut (Type driverType)
  112. //{
  113. // var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  114. // Application.Init (driver);
  115. // // Simulating pressing of QuitKey after a short period of time
  116. // uint quitTime = 100;
  117. // Func<MainLoop, bool> closeCallback = (MainLoop loop) => {
  118. // // Prove the scenario is using Application.QuitKey correctly
  119. // output.WriteLine ($" {quitTime}ms elapsed; Simulating keypresses...");
  120. // FakeConsole.PushMockKeyPress (Key.F);
  121. // FakeConsole.PushMockKeyPress (Key.U);
  122. // FakeConsole.PushMockKeyPress (Key.C);
  123. // FakeConsole.PushMockKeyPress (Key.K);
  124. // return false;
  125. // };
  126. // output.WriteLine ($"Add timeout to simulate key presses after {quitTime}ms");
  127. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (quitTime), closeCallback);
  128. // // If Top doesn't quit within abortTime * 5 (500ms), this will force it
  129. // uint abortTime = quitTime * 5;
  130. // Func<MainLoop, bool> forceCloseCallback = (MainLoop loop) => {
  131. // Application.RequestStop ();
  132. // Assert.Fail ($" failed to Quit after {abortTime}ms. Force quit.");
  133. // return false;
  134. // };
  135. // output.WriteLine ($"Add timeout to force quit after {abortTime}ms");
  136. // _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), forceCloseCallback);
  137. // Key key = Key.Unknown;
  138. // Application.Top.KeyPress += (e) => {
  139. // key = e.Key;
  140. // output.WriteLine ($" Application.Top.KeyPress: {key}");
  141. // e.Handled = true;
  142. // };
  143. // int iterations = 0;
  144. // Application.Iteration += (s, a) => {
  145. // output.WriteLine ($" iteration {++iterations}");
  146. // if (Console.MockKeyPresses.Count == 0) {
  147. // output.WriteLine ($" No more MockKeyPresses; RequestStop");
  148. // Application.RequestStop ();
  149. // }
  150. // };
  151. // Application.Run ();
  152. // // Shutdown must be called to safely clean up Application if Init has been called
  153. // Application.Shutdown ();
  154. //}
  155. [Theory]
  156. [InlineData (typeof (FakeDriver))]
  157. [InlineData (typeof (NetDriver))]
  158. //[InlineData (typeof (ANSIDriver))]
  159. [InlineData (typeof (WindowsDriver))]
  160. [InlineData (typeof (CursesDriver))]
  161. public void TerminalResized_Simulation (Type driverType)
  162. {
  163. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  164. driver?.Init ();
  165. driver.Cols = 80;
  166. driver.Rows = 25;
  167. var wasTerminalResized = false;
  168. driver.SizeChanged += (s, e) =>
  169. {
  170. wasTerminalResized = true;
  171. Assert.Equal (120, e.Size.Width);
  172. Assert.Equal (40, e.Size.Height);
  173. };
  174. Assert.Equal (80, driver.Cols);
  175. Assert.Equal (25, driver.Rows);
  176. Assert.False (wasTerminalResized);
  177. driver.Cols = 120;
  178. driver.Rows = 40;
  179. driver.OnSizeChanged (new SizeChangedEventArgs (new (driver.Cols, driver.Rows)));
  180. Assert.Equal (120, driver.Cols);
  181. Assert.Equal (40, driver.Rows);
  182. Assert.True (wasTerminalResized);
  183. driver.End ();
  184. }
  185. // Disabled due to test error - Change Task.Delay to an await
  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.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 (0, 0, 20, 8), pos);
  211. // Assert.True (dlg.ProcessKey (new (Key.Tab)));
  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 (0, 0, 20, 8), pos);
  225. // win.RequestStop ();
  226. // });
  227. // });
  228. // Application.Run (win);
  229. // Application.Shutdown ();
  230. // }
  231. }