ConsoleDriverTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. using Terminal.Gui.Views;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ConsoleDrivers {
  11. public class ConsoleDriverTests {
  12. readonly ITestOutputHelper output;
  13. public ConsoleDriverTests (ITestOutputHelper output)
  14. {
  15. this.output = output;
  16. }
  17. [Fact]
  18. public void Init_Inits ()
  19. {
  20. var driver = new FakeDriver ();
  21. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  22. driver.Init (() => { });
  23. Assert.Equal (80, Console.BufferWidth);
  24. Assert.Equal (25, Console.BufferHeight);
  25. // MockDriver is always 80x25
  26. Assert.Equal (Console.BufferWidth, driver.Cols);
  27. Assert.Equal (Console.BufferHeight, driver.Rows);
  28. driver.End ();
  29. // Shutdown must be called to safely clean up Application if Init has been called
  30. Application.Shutdown ();
  31. }
  32. [Fact]
  33. public void End_Cleans_Up ()
  34. {
  35. var driver = new FakeDriver ();
  36. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  37. driver.Init (() => { });
  38. FakeConsole.ForegroundColor = ConsoleColor.Red;
  39. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  40. FakeConsole.BackgroundColor = ConsoleColor.Green;
  41. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  42. driver.Move (2, 3);
  43. Assert.Equal (2, Console.CursorLeft);
  44. Assert.Equal (3, Console.CursorTop);
  45. driver.End ();
  46. Assert.Equal (0, Console.CursorLeft);
  47. Assert.Equal (0, Console.CursorTop);
  48. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  49. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  50. // Shutdown must be called to safely clean up Application if Init has been called
  51. Application.Shutdown ();
  52. }
  53. [Fact]
  54. public void SetColors_Changes_Colors ()
  55. {
  56. var driver = new FakeDriver ();
  57. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  58. driver.Init (() => { });
  59. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  60. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  61. Console.ForegroundColor = ConsoleColor.Red;
  62. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  63. Console.BackgroundColor = ConsoleColor.Green;
  64. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  65. Console.ResetColor ();
  66. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  67. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  68. driver.End ();
  69. // Shutdown must be called to safely clean up Application if Init has been called
  70. Application.Shutdown ();
  71. }
  72. [Fact]
  73. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses ()
  74. {
  75. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  76. var top = Application.Top;
  77. var view = new View ();
  78. var count = 0;
  79. var wasKeyPressed = false;
  80. view.KeyPress += (e) => {
  81. wasKeyPressed = true;
  82. };
  83. top.Add (view);
  84. Application.Iteration += () => {
  85. count++;
  86. if (count == 10) {
  87. Application.RequestStop ();
  88. }
  89. };
  90. Application.Run ();
  91. Assert.False (wasKeyPressed);
  92. // Shutdown must be called to safely clean up Application if Init has been called
  93. Application.Shutdown ();
  94. }
  95. [Fact]
  96. public void FakeDriver_MockKeyPresses ()
  97. {
  98. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  99. var text = "MockKeyPresses";
  100. var mKeys = new Stack<ConsoleKeyInfo> ();
  101. foreach (var r in text.Reverse ()) {
  102. var ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  103. var cki = new ConsoleKeyInfo (r, ck, false, false, false);
  104. mKeys.Push (cki);
  105. }
  106. FakeConsole.MockKeyPresses = mKeys;
  107. var top = Application.Top;
  108. var view = new View ();
  109. var rText = "";
  110. var idx = 0;
  111. view.KeyPress += (e) => {
  112. Assert.Equal (text [idx], (char)e.KeyEvent.Key);
  113. rText += (char)e.KeyEvent.Key;
  114. Assert.Equal (rText, text.Substring (0, idx + 1));
  115. e.Handled = true;
  116. idx++;
  117. };
  118. top.Add (view);
  119. Application.Iteration += () => {
  120. if (mKeys.Count == 0) {
  121. Application.RequestStop ();
  122. }
  123. };
  124. Application.Run ();
  125. Assert.Equal ("MockKeyPresses", rText);
  126. // Shutdown must be called to safely clean up Application if Init has been called
  127. Application.Shutdown ();
  128. }
  129. [Fact]
  130. public void SendKeys_Test ()
  131. {
  132. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  133. var top = Application.Top;
  134. var view = new View ();
  135. var shift = false; var alt = false; var control = false;
  136. Key key = default;
  137. Key lastKey = default;
  138. List<Key> keyEnums = GetKeys ();
  139. int i = 0;
  140. int idxKey = 0;
  141. var PushIterations = 0;
  142. var PopIterations = 0;
  143. List<Key> GetKeys ()
  144. {
  145. List<Key> keys = new List<Key> ();
  146. foreach (Key k in Enum.GetValues (typeof (Key))) {
  147. if ((uint)k <= 0xff) {
  148. keys.Add (k);
  149. } else if ((uint)k > 0xff) {
  150. break;
  151. }
  152. }
  153. return keys;
  154. }
  155. view.KeyPress += (e) => {
  156. e.Handled = true;
  157. PopIterations++;
  158. var rMk = new KeyModifiers () {
  159. Shift = e.KeyEvent.IsShift,
  160. Alt = e.KeyEvent.IsAlt,
  161. Ctrl = e.KeyEvent.IsCtrl
  162. };
  163. lastKey = ShortcutHelper.GetModifiersKey (new KeyEvent (e.KeyEvent.Key, rMk));
  164. Assert.Equal (key, lastKey);
  165. };
  166. top.Add (view);
  167. Application.Iteration += () => {
  168. switch (i) {
  169. case 0:
  170. SendKeys ();
  171. break;
  172. case 1:
  173. shift = true;
  174. SendKeys ();
  175. break;
  176. case 2:
  177. alt = true;
  178. SendKeys ();
  179. break;
  180. case 3:
  181. control = true;
  182. SendKeys ();
  183. break;
  184. }
  185. if (PushIterations == keyEnums.Count * 4) {
  186. Application.RequestStop ();
  187. }
  188. };
  189. void SendKeys ()
  190. {
  191. var k = keyEnums [idxKey];
  192. var c = (char)k;
  193. var ck = char.IsLetter (c) ? (ConsoleKey)char.ToUpper (c) : (ConsoleKey)c;
  194. var mk = new KeyModifiers () {
  195. Shift = shift,
  196. Alt = alt,
  197. Ctrl = control
  198. };
  199. key = ShortcutHelper.GetModifiersKey (new KeyEvent (k, mk));
  200. Application.Driver.SendKeys (c, ck, shift, alt, control);
  201. PushIterations++;
  202. if (idxKey + 1 < keyEnums.Count) {
  203. idxKey++;
  204. } else {
  205. idxKey = 0;
  206. i++;
  207. }
  208. }
  209. Application.Run ();
  210. Assert.Equal (key, lastKey);
  211. // Shutdown must be called to safely clean up Application if Init has been called
  212. Application.Shutdown ();
  213. }
  214. [Fact]
  215. public void TerminalResized_Simulation ()
  216. {
  217. var driver = new FakeDriver ();
  218. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  219. var wasTerminalResized = false;
  220. Application.Resized = (e) => {
  221. wasTerminalResized = true;
  222. Assert.Equal (120, e.Cols);
  223. Assert.Equal (40, e.Rows);
  224. };
  225. Assert.Equal (80, Console.BufferWidth);
  226. Assert.Equal (25, Console.BufferHeight);
  227. // MockDriver is by default 80x25
  228. Assert.Equal (Console.BufferWidth, driver.Cols);
  229. Assert.Equal (Console.BufferHeight, driver.Rows);
  230. Assert.False (wasTerminalResized);
  231. // MockDriver will now be sets to 120x40
  232. driver.SetBufferSize (120, 40);
  233. Assert.Equal (120, Application.Driver.Cols);
  234. Assert.Equal (40, Application.Driver.Rows);
  235. Assert.True (wasTerminalResized);
  236. // MockDriver will still be 120x40
  237. wasTerminalResized = false;
  238. Application.HeightAsBuffer = true;
  239. driver.SetWindowSize (40, 20);
  240. Assert.Equal (120, Application.Driver.Cols);
  241. Assert.Equal (40, Application.Driver.Rows);
  242. Assert.Equal (120, Console.BufferWidth);
  243. Assert.Equal (40, Console.BufferHeight);
  244. Assert.Equal (40, Console.WindowWidth);
  245. Assert.Equal (20, Console.WindowHeight);
  246. Assert.True (wasTerminalResized);
  247. Application.Shutdown ();
  248. }
  249. [Fact]
  250. public void HeightAsBuffer_Is_False_Left_And_Top_Is_Always_Zero ()
  251. {
  252. var driver = new FakeDriver ();
  253. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  254. Assert.False (Application.HeightAsBuffer);
  255. Assert.Equal (0, Console.WindowLeft);
  256. Assert.Equal (0, Console.WindowTop);
  257. driver.SetWindowPosition (5, 5);
  258. Assert.Equal (0, Console.WindowLeft);
  259. Assert.Equal (0, Console.WindowTop);
  260. Application.Shutdown ();
  261. }
  262. [Fact]
  263. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_WindowWidth ()
  264. {
  265. var driver = new FakeDriver ();
  266. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  267. Application.HeightAsBuffer = true;
  268. Assert.True (Application.HeightAsBuffer);
  269. driver.SetWindowPosition (81, 25);
  270. Assert.Equal (0, Console.WindowLeft);
  271. Assert.Equal (0, Console.WindowTop);
  272. Application.Shutdown ();
  273. }
  274. [Fact]
  275. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_BufferWidth_Minus_WindowWidth ()
  276. {
  277. var driver = new FakeDriver ();
  278. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  279. Application.HeightAsBuffer = true;
  280. Assert.True (Application.HeightAsBuffer);
  281. driver.SetWindowPosition (81, 25);
  282. Assert.Equal (0, Console.WindowLeft);
  283. Assert.Equal (0, Console.WindowTop);
  284. // MockDriver will now be sets to 120x25
  285. driver.SetBufferSize (120, 25);
  286. Assert.Equal (120, Application.Driver.Cols);
  287. Assert.Equal (25, Application.Driver.Rows);
  288. Assert.Equal (120, Console.BufferWidth);
  289. Assert.Equal (25, Console.BufferHeight);
  290. Assert.Equal (80, Console.WindowWidth);
  291. Assert.Equal (25, Console.WindowHeight);
  292. driver.SetWindowPosition (121, 25);
  293. Assert.Equal (40, Console.WindowLeft);
  294. Assert.Equal (0, Console.WindowTop);
  295. driver.SetWindowSize (90, 25);
  296. Assert.Equal (120, Application.Driver.Cols);
  297. Assert.Equal (25, Application.Driver.Rows);
  298. Assert.Equal (120, Console.BufferWidth);
  299. Assert.Equal (25, Console.BufferHeight);
  300. Assert.Equal (90, Console.WindowWidth);
  301. Assert.Equal (25, Console.WindowHeight);
  302. driver.SetWindowPosition (121, 25);
  303. Assert.Equal (30, Console.WindowLeft);
  304. Assert.Equal (0, Console.WindowTop);
  305. Application.Shutdown ();
  306. }
  307. [Fact]
  308. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_WindowHeight ()
  309. {
  310. var driver = new FakeDriver ();
  311. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  312. Application.HeightAsBuffer = true;
  313. Assert.True (Application.HeightAsBuffer);
  314. driver.SetWindowPosition (80, 26);
  315. Assert.Equal (0, Console.WindowLeft);
  316. Assert.Equal (0, Console.WindowTop);
  317. Application.Shutdown ();
  318. }
  319. [Fact]
  320. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_BufferHeight_Minus_WindowHeight ()
  321. {
  322. var driver = new FakeDriver ();
  323. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  324. Application.HeightAsBuffer = true;
  325. Assert.True (Application.HeightAsBuffer);
  326. driver.SetWindowPosition (80, 26);
  327. Assert.Equal (0, Console.WindowLeft);
  328. Assert.Equal (0, Console.WindowTop);
  329. // MockDriver will now be sets to 80x40
  330. driver.SetBufferSize (80, 40);
  331. Assert.Equal (80, Application.Driver.Cols);
  332. Assert.Equal (40, Application.Driver.Rows);
  333. Assert.Equal (80, Console.BufferWidth);
  334. Assert.Equal (40, Console.BufferHeight);
  335. Assert.Equal (80, Console.WindowWidth);
  336. Assert.Equal (25, Console.WindowHeight);
  337. Assert.Equal (0, Console.WindowLeft);
  338. Assert.Equal (0, Console.WindowTop);
  339. driver.SetWindowPosition (80, 40);
  340. Assert.Equal (0, Console.WindowLeft);
  341. Assert.Equal (15, Console.WindowTop);
  342. driver.SetWindowSize (80, 20);
  343. Assert.Equal (80, Application.Driver.Cols);
  344. Assert.Equal (40, Application.Driver.Rows);
  345. Assert.Equal (80, Console.BufferWidth);
  346. Assert.Equal (40, Console.BufferHeight);
  347. Assert.Equal (80, Console.WindowWidth);
  348. Assert.Equal (20, Console.WindowHeight);
  349. Assert.Equal (0, Console.WindowLeft);
  350. Assert.Equal (15, Console.WindowTop);
  351. driver.SetWindowPosition (80, 41);
  352. Assert.Equal (0, Console.WindowLeft);
  353. Assert.Equal (20, Console.WindowTop);
  354. Application.Shutdown ();
  355. }
  356. [Fact]
  357. public void Internal_Tests ()
  358. {
  359. var cs = new ColorScheme ();
  360. Assert.Equal ("", cs.caller);
  361. }
  362. [Fact]
  363. [AutoInitShutdown]
  364. public void KeyModifiers_Resetting_At_New_Keystrokes ()
  365. {
  366. bool? okInitialFocused = null;
  367. bool? cancelInitialFocused = null;
  368. var okClicked = false;
  369. var closing = false;
  370. var cursorRight = false;
  371. var endingKeyPress = false;
  372. var closed = false;
  373. var top = Application.Top;
  374. var ok = new Button ("Ok");
  375. ok.Clicked += () => {
  376. if (!okClicked) {
  377. okClicked = true;
  378. Application.RequestStop ();
  379. }
  380. };
  381. var cancel = new Button ("Cancel");
  382. var d = new Dialog ("Quit", cancel, ok);
  383. d.KeyPress += (e) => {
  384. if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  385. if (!okClicked && !closing) {
  386. okInitialFocused = ok.HasFocus;
  387. cancelInitialFocused = cancel.HasFocus;
  388. closing = true;
  389. var mKeys = new Stack<ConsoleKeyInfo> ();
  390. var cki = new ConsoleKeyInfo ('\0', ConsoleKey.Enter, false, false, false);
  391. mKeys.Push (cki);
  392. cki = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, false, false, false);
  393. mKeys.Push (cki);
  394. FakeConsole.MockKeyPresses = mKeys;
  395. }
  396. e.Handled = true;
  397. } else if (e.KeyEvent.Key == Key.CursorRight) {
  398. if (!cursorRight) {
  399. cursorRight = true;
  400. } else if (ok.HasFocus) {
  401. e.Handled = endingKeyPress = true;
  402. }
  403. }
  404. };
  405. d.Loaded += () => {
  406. var mKeys = new Stack<ConsoleKeyInfo> ();
  407. var cki = new ConsoleKeyInfo ('q', ConsoleKey.Q, false, false, true);
  408. mKeys.Push (cki);
  409. FakeConsole.MockKeyPresses = mKeys;
  410. };
  411. d.Closed += (_) => {
  412. if (okClicked && closing) {
  413. closed = true;
  414. }
  415. };
  416. top.Ready += () => Application.Run (d);
  417. Application.Iteration += () => {
  418. if (closed) {
  419. Application.RequestStop ();
  420. }
  421. };
  422. Application.Run ();
  423. Assert.False (okInitialFocused);
  424. Assert.True (cancelInitialFocused);
  425. Assert.True (okClicked);
  426. Assert.True (closing);
  427. Assert.True (cursorRight);
  428. Assert.True (endingKeyPress);
  429. Assert.True (closed);
  430. Assert.Empty (FakeConsole.MockKeyPresses);
  431. }
  432. [Fact, AutoInitShutdown]
  433. public void AddRune_On_Clip_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  434. {
  435. var tv = new TextView () {
  436. Width = Dim.Fill (),
  437. Height = Dim.Fill (),
  438. Text = @"これは広いルーンラインです。
  439. これは広いルーンラインです。
  440. これは広いルーンラインです。
  441. これは広いルーンラインです。
  442. これは広いルーンラインです。
  443. これは広いルーンラインです。
  444. これは広いルーンラインです。
  445. これは広いルーンラインです。"
  446. };
  447. var win = new Window ("ワイドルーン") { Width = Dim.Fill (), Height = Dim.Fill () };
  448. win.Add (tv);
  449. Application.Top.Add (win);
  450. var lbl = new Label ("ワイドルーン。");
  451. var dg = new Dialog ("テスト", 14, 4, new Button ("選ぶ"));
  452. dg.Add (lbl);
  453. Application.Begin (Application.Top);
  454. Application.Begin (dg);
  455. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  456. var expected = @"
  457. ┌ ワイドルーン ──────────────┐
  458. │これは広いルーンラインです。│
  459. │これは広いルーンラインです。│
  460. │これは ┌ テスト ────┐ です。│
  461. │これは │ワイドルーン│ です。│
  462. │これは │ [ 選ぶ ] │ です。│
  463. │これは └────────────┘ です。│
  464. │これは広いルーンラインです。│
  465. │これは広いルーンラインです。│
  466. └────────────────────────────┘
  467. ";
  468. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  469. Assert.Equal (new Rect (0, 0, 30, 10), pos);
  470. }
  471. [Fact, AutoInitShutdown]
  472. public void Write_Do_Not_Change_On_ProcessKey ()
  473. {
  474. var win = new Window ();
  475. Application.Begin (win);
  476. ((FakeDriver)Application.Driver).SetBufferSize (20, 8);
  477. System.Threading.Tasks.Task.Run (() => {
  478. System.Threading.Tasks.Task.Delay (500).Wait ();
  479. Application.MainLoop.Invoke (() => {
  480. var lbl = new Label ("Hello World") { X = Pos.Center () };
  481. var dlg = new Dialog ("Test", new Button ("Ok"));
  482. dlg.Add (lbl);
  483. Application.Begin (dlg);
  484. var expected = @"
  485. ┌──────────────────┐
  486. │┌ Test ─────────┐ │
  487. ││ Hello World │ │
  488. ││ │ │
  489. ││ │ │
  490. ││ [ Ok ] │ │
  491. │└───────────────┘ │
  492. └──────────────────┘
  493. ";
  494. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  495. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  496. Assert.True (dlg.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  497. dlg.Redraw (dlg.Bounds);
  498. expected = @"
  499. ┌──────────────────┐
  500. │┌ Test ─────────┐ │
  501. ││ Hello World │ │
  502. ││ │ │
  503. ││ │ │
  504. ││ [ Ok ] │ │
  505. │└───────────────┘ │
  506. └──────────────────┘
  507. ";
  508. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  509. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  510. win.RequestStop ();
  511. });
  512. });
  513. Application.Run (win);
  514. Application.Shutdown ();
  515. }
  516. }
  517. }