ConsoleDriverTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. using Xunit;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.ConsoleDrivers {
  9. public class ConsoleDriverTests {
  10. [Fact]
  11. public void Init_Inits ()
  12. {
  13. var driver = new FakeDriver ();
  14. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  15. driver.Init (() => { });
  16. Assert.Equal (80, Console.BufferWidth);
  17. Assert.Equal (25, Console.BufferHeight);
  18. // MockDriver is always 80x25
  19. Assert.Equal (Console.BufferWidth, driver.Cols);
  20. Assert.Equal (Console.BufferHeight, driver.Rows);
  21. driver.End ();
  22. // Shutdown must be called to safely clean up Application if Init has been called
  23. Application.Shutdown ();
  24. }
  25. [Fact]
  26. public void End_Cleans_Up ()
  27. {
  28. var driver = new FakeDriver ();
  29. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  30. driver.Init (() => { });
  31. FakeConsole.ForegroundColor = ConsoleColor.Red;
  32. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  33. FakeConsole.BackgroundColor = ConsoleColor.Green;
  34. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  35. driver.Move (2, 3);
  36. Assert.Equal (2, Console.CursorLeft);
  37. Assert.Equal (3, Console.CursorTop);
  38. driver.End ();
  39. Assert.Equal (0, Console.CursorLeft);
  40. Assert.Equal (0, Console.CursorTop);
  41. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  42. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  43. // Shutdown must be called to safely clean up Application if Init has been called
  44. Application.Shutdown ();
  45. }
  46. [Fact]
  47. public void SetColors_Changes_Colors ()
  48. {
  49. var driver = new FakeDriver ();
  50. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  51. driver.Init (() => { });
  52. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  53. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  54. Console.ForegroundColor = ConsoleColor.Red;
  55. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  56. Console.BackgroundColor = ConsoleColor.Green;
  57. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  58. Console.ResetColor ();
  59. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  60. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  61. driver.End ();
  62. // Shutdown must be called to safely clean up Application if Init has been called
  63. Application.Shutdown ();
  64. }
  65. [Fact]
  66. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses ()
  67. {
  68. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  69. var top = Application.Top;
  70. var view = new View ();
  71. var count = 0;
  72. var wasKeyPressed = false;
  73. view.KeyPress += (e) => {
  74. wasKeyPressed = true;
  75. };
  76. top.Add (view);
  77. Application.Iteration += () => {
  78. count++;
  79. if (count == 10) {
  80. Application.RequestStop ();
  81. }
  82. };
  83. Application.Run ();
  84. Assert.False (wasKeyPressed);
  85. // Shutdown must be called to safely clean up Application if Init has been called
  86. Application.Shutdown ();
  87. }
  88. [Fact]
  89. public void FakeDriver_MockKeyPresses ()
  90. {
  91. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  92. var text = "MockKeyPresses";
  93. var mKeys = new Stack<ConsoleKeyInfo> ();
  94. foreach (var r in text.Reverse ()) {
  95. var ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  96. var cki = new ConsoleKeyInfo (r, ck, false, false, false);
  97. mKeys.Push (cki);
  98. }
  99. FakeConsole.MockKeyPresses = mKeys;
  100. var top = Application.Top;
  101. var view = new View ();
  102. var rText = "";
  103. var idx = 0;
  104. view.KeyPress += (e) => {
  105. Assert.Equal (text [idx], (char)e.KeyEvent.Key);
  106. rText += (char)e.KeyEvent.Key;
  107. Assert.Equal (rText, text.Substring (0, idx + 1));
  108. e.Handled = true;
  109. idx++;
  110. };
  111. top.Add (view);
  112. Application.Iteration += () => {
  113. if (mKeys.Count == 0) {
  114. Application.RequestStop ();
  115. }
  116. };
  117. Application.Run ();
  118. Assert.Equal ("MockKeyPresses", rText);
  119. // Shutdown must be called to safely clean up Application if Init has been called
  120. Application.Shutdown ();
  121. }
  122. [Fact]
  123. public void SendKeys_Test ()
  124. {
  125. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  126. var top = Application.Top;
  127. var view = new View ();
  128. var shift = false; var alt = false; var control = false;
  129. Key key = default;
  130. Key lastKey = default;
  131. List<Key> keyEnums = GetKeys ();
  132. int i = 0;
  133. int idxKey = 0;
  134. var PushIterations = 0;
  135. var PopIterations = 0;
  136. List<Key> GetKeys ()
  137. {
  138. List<Key> keys = new List<Key> ();
  139. foreach (Key k in Enum.GetValues (typeof (Key))) {
  140. if ((uint)k <= 0xff) {
  141. keys.Add (k);
  142. } else if ((uint)k > 0xff) {
  143. break;
  144. }
  145. }
  146. return keys;
  147. }
  148. view.KeyPress += (e) => {
  149. e.Handled = true;
  150. PopIterations++;
  151. var rMk = new KeyModifiers () {
  152. Shift = e.KeyEvent.IsShift,
  153. Alt = e.KeyEvent.IsAlt,
  154. Ctrl = e.KeyEvent.IsCtrl
  155. };
  156. lastKey = ShortcutHelper.GetModifiersKey (new KeyEvent (e.KeyEvent.Key, rMk));
  157. Assert.Equal (key, lastKey);
  158. };
  159. top.Add (view);
  160. Application.Iteration += () => {
  161. switch (i) {
  162. case 0:
  163. SendKeys ();
  164. break;
  165. case 1:
  166. shift = true;
  167. SendKeys ();
  168. break;
  169. case 2:
  170. alt = true;
  171. SendKeys ();
  172. break;
  173. case 3:
  174. control = true;
  175. SendKeys ();
  176. break;
  177. }
  178. if (PushIterations == keyEnums.Count * 4) {
  179. Application.RequestStop ();
  180. }
  181. };
  182. void SendKeys ()
  183. {
  184. var k = keyEnums [idxKey];
  185. var c = (char)k;
  186. var ck = char.IsLetter (c) ? (ConsoleKey)char.ToUpper (c) : (ConsoleKey)c;
  187. var mk = new KeyModifiers () {
  188. Shift = shift,
  189. Alt = alt,
  190. Ctrl = control
  191. };
  192. key = ShortcutHelper.GetModifiersKey (new KeyEvent (k, mk));
  193. Application.Driver.SendKeys (c, ck, shift, alt, control);
  194. PushIterations++;
  195. if (idxKey + 1 < keyEnums.Count) {
  196. idxKey++;
  197. } else {
  198. idxKey = 0;
  199. i++;
  200. }
  201. }
  202. Application.Run ();
  203. Assert.Equal (key, lastKey);
  204. // Shutdown must be called to safely clean up Application if Init has been called
  205. Application.Shutdown ();
  206. }
  207. [Fact]
  208. public void TerminalResized_Simulation ()
  209. {
  210. var driver = new FakeDriver ();
  211. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  212. var wasTerminalResized = false;
  213. Application.Resized = (e) => {
  214. wasTerminalResized = true;
  215. Assert.Equal (120, e.Cols);
  216. Assert.Equal (40, e.Rows);
  217. };
  218. Assert.Equal (80, Console.BufferWidth);
  219. Assert.Equal (25, Console.BufferHeight);
  220. // MockDriver is by default 80x25
  221. Assert.Equal (Console.BufferWidth, driver.Cols);
  222. Assert.Equal (Console.BufferHeight, driver.Rows);
  223. Assert.False (wasTerminalResized);
  224. // MockDriver will now be sets to 120x40
  225. driver.SetBufferSize (120, 40);
  226. Assert.Equal (120, Application.Driver.Cols);
  227. Assert.Equal (40, Application.Driver.Rows);
  228. Assert.True (wasTerminalResized);
  229. // MockDriver will still be 120x40
  230. wasTerminalResized = false;
  231. Application.HeightAsBuffer = true;
  232. driver.SetWindowSize (40, 20);
  233. Assert.Equal (120, Application.Driver.Cols);
  234. Assert.Equal (40, Application.Driver.Rows);
  235. Assert.Equal (120, Console.BufferWidth);
  236. Assert.Equal (40, Console.BufferHeight);
  237. Assert.Equal (40, Console.WindowWidth);
  238. Assert.Equal (20, Console.WindowHeight);
  239. Assert.True (wasTerminalResized);
  240. Application.Shutdown ();
  241. }
  242. [Fact]
  243. public void HeightAsBuffer_Is_False_Left_And_Top_Is_Always_Zero ()
  244. {
  245. var driver = new FakeDriver ();
  246. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  247. Assert.False (Application.HeightAsBuffer);
  248. Assert.Equal (0, Console.WindowLeft);
  249. Assert.Equal (0, Console.WindowTop);
  250. driver.SetWindowPosition (5, 5);
  251. Assert.Equal (0, Console.WindowLeft);
  252. Assert.Equal (0, Console.WindowTop);
  253. Application.Shutdown ();
  254. }
  255. [Fact]
  256. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_WindowWidth ()
  257. {
  258. var driver = new FakeDriver ();
  259. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  260. Application.HeightAsBuffer = true;
  261. Assert.True (Application.HeightAsBuffer);
  262. driver.SetWindowPosition (81, 25);
  263. Assert.Equal (0, Console.WindowLeft);
  264. Assert.Equal (0, Console.WindowTop);
  265. Application.Shutdown ();
  266. }
  267. [Fact]
  268. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_BufferWidth_Minus_WindowWidth ()
  269. {
  270. var driver = new FakeDriver ();
  271. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  272. Application.HeightAsBuffer = true;
  273. Assert.True (Application.HeightAsBuffer);
  274. driver.SetWindowPosition (81, 25);
  275. Assert.Equal (0, Console.WindowLeft);
  276. Assert.Equal (0, Console.WindowTop);
  277. // MockDriver will now be sets to 120x25
  278. driver.SetBufferSize (120, 25);
  279. Assert.Equal (120, Application.Driver.Cols);
  280. Assert.Equal (25, Application.Driver.Rows);
  281. Assert.Equal (120, Console.BufferWidth);
  282. Assert.Equal (25, Console.BufferHeight);
  283. Assert.Equal (120, Console.WindowWidth);
  284. Assert.Equal (25, Console.WindowHeight);
  285. driver.SetWindowPosition (121, 25);
  286. Assert.Equal (0, Console.WindowLeft);
  287. Assert.Equal (0, Console.WindowTop);
  288. driver.SetWindowSize (90, 25);
  289. Assert.Equal (120, Application.Driver.Cols);
  290. Assert.Equal (25, Application.Driver.Rows);
  291. Assert.Equal (120, Console.BufferWidth);
  292. Assert.Equal (25, Console.BufferHeight);
  293. Assert.Equal (90, Console.WindowWidth);
  294. Assert.Equal (25, Console.WindowHeight);
  295. driver.SetWindowPosition (121, 25);
  296. Assert.Equal (30, Console.WindowLeft);
  297. Assert.Equal (0, Console.WindowTop);
  298. Application.Shutdown ();
  299. }
  300. [Fact]
  301. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_WindowHeight ()
  302. {
  303. var driver = new FakeDriver ();
  304. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  305. Application.HeightAsBuffer = true;
  306. Assert.True (Application.HeightAsBuffer);
  307. driver.SetWindowPosition (80, 26);
  308. Assert.Equal (0, Console.WindowLeft);
  309. Assert.Equal (0, Console.WindowTop);
  310. Application.Shutdown ();
  311. }
  312. [Fact]
  313. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_BufferHeight_Minus_WindowHeight ()
  314. {
  315. var driver = new FakeDriver ();
  316. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  317. Application.HeightAsBuffer = true;
  318. Assert.True (Application.HeightAsBuffer);
  319. driver.SetWindowPosition (80, 26);
  320. Assert.Equal (0, Console.WindowLeft);
  321. Assert.Equal (0, Console.WindowTop);
  322. // MockDriver will now be sets to 120x25
  323. driver.SetBufferSize (80, 40);
  324. Assert.Equal (80, Application.Driver.Cols);
  325. Assert.Equal (40, Application.Driver.Rows);
  326. Assert.Equal (80, Console.BufferWidth);
  327. Assert.Equal (40, Console.BufferHeight);
  328. Assert.Equal (80, Console.WindowWidth);
  329. Assert.Equal (40, Console.WindowHeight);
  330. driver.SetWindowPosition (80, 40);
  331. Assert.Equal (0, Console.WindowLeft);
  332. Assert.Equal (0, Console.WindowTop);
  333. driver.SetWindowSize (80, 20);
  334. Assert.Equal (80, Application.Driver.Cols);
  335. Assert.Equal (40, Application.Driver.Rows);
  336. Assert.Equal (80, Console.BufferWidth);
  337. Assert.Equal (40, Console.BufferHeight);
  338. Assert.Equal (80, Console.WindowWidth);
  339. Assert.Equal (20, Console.WindowHeight);
  340. driver.SetWindowPosition (80, 41);
  341. Assert.Equal (0, Console.WindowLeft);
  342. Assert.Equal (20, Console.WindowTop);
  343. Application.Shutdown ();
  344. }
  345. }
  346. }