ConsoleDriverTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  23. [Fact]
  24. public void End_Cleans_Up ()
  25. {
  26. var driver = new FakeDriver ();
  27. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  28. driver.Init (() => { });
  29. FakeConsole.ForegroundColor = ConsoleColor.Red;
  30. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  31. FakeConsole.BackgroundColor = ConsoleColor.Green;
  32. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  33. driver.Move (2, 3);
  34. Assert.Equal (2, Console.CursorLeft);
  35. Assert.Equal (3, Console.CursorTop);
  36. driver.End ();
  37. Assert.Equal (0, Console.CursorLeft);
  38. Assert.Equal (0, Console.CursorTop);
  39. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  40. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  41. }
  42. [Fact]
  43. public void SetColors_Changes_Colors ()
  44. {
  45. var driver = new FakeDriver ();
  46. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  47. driver.Init (() => { });
  48. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  49. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  50. Console.ForegroundColor = ConsoleColor.Red;
  51. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  52. Console.BackgroundColor = ConsoleColor.Green;
  53. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  54. Console.ResetColor ();
  55. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  56. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  57. driver.End ();
  58. }
  59. [Fact]
  60. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses ()
  61. {
  62. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  63. var top = Application.Top;
  64. var view = new View ();
  65. var count = 0;
  66. var wasKeyPressed = false;
  67. view.KeyPress += (e) => {
  68. wasKeyPressed = true;
  69. };
  70. top.Add (view);
  71. Application.Iteration += () => {
  72. count++;
  73. if (count == 10) {
  74. Application.RequestStop ();
  75. }
  76. };
  77. Application.Run ();
  78. Assert.False (wasKeyPressed);
  79. }
  80. [Fact]
  81. public void FakeDriver_MockKeyPresses ()
  82. {
  83. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  84. var text = "MockKeyPresses";
  85. var mKeys = new Stack<ConsoleKeyInfo> ();
  86. foreach (var r in text.Reverse ()) {
  87. var ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  88. var cki = new ConsoleKeyInfo (r, ck, false, false, false);
  89. mKeys.Push (cki);
  90. }
  91. FakeConsole.MockKeyPresses = mKeys;
  92. var top = Application.Top;
  93. var view = new View ();
  94. var rText = "";
  95. var idx = 0;
  96. view.KeyPress += (e) => {
  97. Assert.Equal (text [idx], (char)e.KeyEvent.Key);
  98. rText += (char)e.KeyEvent.Key;
  99. Assert.Equal (rText, text.Substring (0, idx + 1));
  100. e.Handled = true;
  101. idx++;
  102. };
  103. top.Add (view);
  104. Application.Iteration += () => {
  105. if (mKeys.Count == 0) {
  106. Application.RequestStop ();
  107. }
  108. };
  109. Application.Run ();
  110. Assert.Equal ("MockKeyPresses", rText);
  111. }
  112. [Fact]
  113. public void SendKeys_Test ()
  114. {
  115. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  116. var top = Application.Top;
  117. var view = new View ();
  118. var shift = false; var alt = false; var control = false;
  119. Key key = default;
  120. Key lastKey = default;
  121. List<Key> keyEnums = GetKeys ();
  122. int i = 0;
  123. int idxKey = 0;
  124. var PushIterations = 0;
  125. var PopIterations = 0;
  126. List<Key> GetKeys ()
  127. {
  128. List<Key> keys = new List<Key> ();
  129. foreach (Key k in Enum.GetValues (typeof (Key))) {
  130. if ((uint)k <= 0xff) {
  131. keys.Add (k);
  132. } else if ((uint)k > 0xff) {
  133. break;
  134. }
  135. }
  136. return keys;
  137. }
  138. view.KeyPress += (e) => {
  139. e.Handled = true;
  140. PopIterations++;
  141. var rMk = new KeyModifiers () {
  142. Shift = e.KeyEvent.IsShift,
  143. Alt = e.KeyEvent.IsAlt,
  144. Ctrl = e.KeyEvent.IsCtrl
  145. };
  146. lastKey = ShortcutHelper.GetModifiersKey (new KeyEvent (e.KeyEvent.Key, rMk));
  147. Assert.Equal (key, lastKey);
  148. };
  149. top.Add (view);
  150. Application.Iteration += () => {
  151. switch (i) {
  152. case 0:
  153. SendKeys ();
  154. break;
  155. case 1:
  156. shift = true;
  157. SendKeys ();
  158. break;
  159. case 2:
  160. alt = true;
  161. SendKeys ();
  162. break;
  163. case 3:
  164. control = true;
  165. SendKeys ();
  166. break;
  167. }
  168. if (PushIterations == keyEnums.Count * 4) {
  169. Application.RequestStop ();
  170. }
  171. };
  172. void SendKeys ()
  173. {
  174. var k = keyEnums [idxKey];
  175. var c = (char)k;
  176. var ck = char.IsLetter (c) ? (ConsoleKey)char.ToUpper (c) : (ConsoleKey)c;
  177. var mk = new KeyModifiers () {
  178. Shift = shift,
  179. Alt = alt,
  180. Ctrl = control
  181. };
  182. key = ShortcutHelper.GetModifiersKey (new KeyEvent (k, mk));
  183. Application.Driver.SendKeys (c, ck, shift, alt, control);
  184. PushIterations++;
  185. if (idxKey + 1 < keyEnums.Count) {
  186. idxKey++;
  187. } else {
  188. idxKey = 0;
  189. i++;
  190. }
  191. }
  192. Application.Run ();
  193. Assert.Equal (key, lastKey);
  194. }
  195. }
  196. }