ConsoleDriverTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui.Views;
  7. using Xunit;
  8. using Xunit.Abstractions;
  9. // Alias Console to MockConsole so we don't accidentally use Console
  10. using Console = Terminal.Gui.FakeConsole;
  11. namespace Terminal.Gui.ConsoleDrivers {
  12. public class ConsoleDriverTests {
  13. readonly ITestOutputHelper output;
  14. public ConsoleDriverTests (ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. [Theory]
  19. [InlineData (typeof (FakeDriver))]
  20. //[InlineData (typeof (NetDriver))]
  21. //[InlineData (typeof (CursesDriver))]
  22. //[InlineData (typeof (WindowsDriver))]
  23. public void Init_Inits (Type driverType)
  24. {
  25. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  26. Application.Init (driver);
  27. driver.Init (() => { });
  28. Assert.Equal (80, Console.BufferWidth);
  29. Assert.Equal (25, Console.BufferHeight);
  30. // MockDriver is always 80x25
  31. Assert.Equal (Console.BufferWidth, driver.Cols);
  32. Assert.Equal (Console.BufferHeight, driver.Rows);
  33. driver.End ();
  34. // Shutdown must be called to safely clean up Application if Init has been called
  35. Application.Shutdown ();
  36. }
  37. [Theory]
  38. [InlineData (typeof (FakeDriver))]
  39. //[InlineData (typeof (NetDriver))]
  40. //[InlineData (typeof (CursesDriver))]
  41. //[InlineData (typeof (WindowsDriver))]
  42. public void End_Cleans_Up (Type driverType)
  43. {
  44. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  45. Application.Init (driver);
  46. driver.Init (() => { });
  47. FakeConsole.ForegroundColor = ConsoleColor.Red;
  48. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  49. FakeConsole.BackgroundColor = ConsoleColor.Green;
  50. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  51. driver.Move (2, 3);
  52. Assert.Equal (2, Console.CursorLeft);
  53. Assert.Equal (3, Console.CursorTop);
  54. driver.End ();
  55. Assert.Equal (0, Console.CursorLeft);
  56. Assert.Equal (0, Console.CursorTop);
  57. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  58. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  59. // Shutdown must be called to safely clean up Application if Init has been called
  60. Application.Shutdown ();
  61. }
  62. [Theory]
  63. [InlineData (typeof (FakeDriver))]
  64. //[InlineData (typeof (NetDriver))]
  65. //[InlineData (typeof (CursesDriver))]
  66. //[InlineData (typeof (WindowsDriver))]
  67. public void SetColors_Changes_Colors (Type driverType)
  68. {
  69. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  70. Application.Init (driver);
  71. driver.Init (() => { });
  72. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  73. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  74. Console.ForegroundColor = ConsoleColor.Red;
  75. Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
  76. Console.BackgroundColor = ConsoleColor.Green;
  77. Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
  78. Console.ResetColor ();
  79. Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
  80. Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
  81. driver.End ();
  82. // Shutdown must be called to safely clean up Application if Init has been called
  83. Application.Shutdown ();
  84. }
  85. [Theory]
  86. [InlineData (typeof (FakeDriver))]
  87. public void FakeDriver_Only_Sends_Keystrokes_Through_MockKeyPresses (Type driverType)
  88. {
  89. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  90. Application.Init (driver);
  91. var top = Application.Top;
  92. var view = new View ();
  93. var count = 0;
  94. var wasKeyPressed = false;
  95. view.KeyPress += (e) => {
  96. wasKeyPressed = true;
  97. };
  98. top.Add (view);
  99. Application.Iteration += () => {
  100. count++;
  101. if (count == 10) {
  102. Application.RequestStop ();
  103. }
  104. };
  105. Application.Run ();
  106. Assert.False (wasKeyPressed);
  107. // Shutdown must be called to safely clean up Application if Init has been called
  108. Application.Shutdown ();
  109. }
  110. [Theory]
  111. [InlineData (typeof (FakeDriver))]
  112. public void FakeDriver_MockKeyPresses (Type driverType)
  113. {
  114. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  115. Application.Init (driver);
  116. var text = "MockKeyPresses";
  117. var mKeys = new Stack<ConsoleKeyInfo> ();
  118. foreach (var r in text.Reverse ()) {
  119. var ck = char.IsLetter (r) ? (ConsoleKey)char.ToUpper (r) : (ConsoleKey)r;
  120. var cki = new ConsoleKeyInfo (r, ck, false, false, false);
  121. mKeys.Push (cki);
  122. }
  123. FakeConsole.MockKeyPresses = mKeys;
  124. var top = Application.Top;
  125. var view = new View ();
  126. var rText = "";
  127. var idx = 0;
  128. view.KeyPress += (e) => {
  129. Assert.Equal (text [idx], (char)e.KeyEvent.Key);
  130. rText += (char)e.KeyEvent.Key;
  131. Assert.Equal (rText, text.Substring (0, idx + 1));
  132. e.Handled = true;
  133. idx++;
  134. };
  135. top.Add (view);
  136. Application.Iteration += () => {
  137. if (mKeys.Count == 0) {
  138. Application.RequestStop ();
  139. }
  140. };
  141. Application.Run ();
  142. Assert.Equal ("MockKeyPresses", rText);
  143. // Shutdown must be called to safely clean up Application if Init has been called
  144. Application.Shutdown ();
  145. }
  146. [Theory]
  147. [InlineData (typeof (FakeDriver))]
  148. public void SendKeys_Test (Type driverType)
  149. {
  150. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  151. Application.Init (driver);
  152. var top = Application.Top;
  153. var view = new View ();
  154. var shift = false; var alt = false; var control = false;
  155. Key key = default;
  156. Key lastKey = default;
  157. List<Key> keyEnums = GetKeys ();
  158. int i = 0;
  159. int idxKey = 0;
  160. var PushIterations = 0;
  161. var PopIterations = 0;
  162. List<Key> GetKeys ()
  163. {
  164. List<Key> keys = new List<Key> ();
  165. foreach (Key k in Enum.GetValues (typeof (Key))) {
  166. if ((uint)k <= 0xff) {
  167. keys.Add (k);
  168. } else if ((uint)k > 0xff) {
  169. break;
  170. }
  171. }
  172. return keys;
  173. }
  174. view.KeyPress += (e) => {
  175. e.Handled = true;
  176. PopIterations++;
  177. var rMk = new KeyModifiers () {
  178. Shift = e.KeyEvent.IsShift,
  179. Alt = e.KeyEvent.IsAlt,
  180. Ctrl = e.KeyEvent.IsCtrl
  181. };
  182. lastKey = ShortcutHelper.GetModifiersKey (new KeyEvent (e.KeyEvent.Key, rMk));
  183. Assert.Equal (key, lastKey);
  184. };
  185. top.Add (view);
  186. Application.Iteration += () => {
  187. switch (i) {
  188. case 0:
  189. SendKeys ();
  190. break;
  191. case 1:
  192. shift = true;
  193. SendKeys ();
  194. break;
  195. case 2:
  196. alt = true;
  197. SendKeys ();
  198. break;
  199. case 3:
  200. control = true;
  201. SendKeys ();
  202. break;
  203. }
  204. if (PushIterations == keyEnums.Count * 4) {
  205. Application.RequestStop ();
  206. }
  207. };
  208. void SendKeys ()
  209. {
  210. var k = shift && char.IsLetter ((char)keyEnums [idxKey]) && char.IsLower ((char)keyEnums [idxKey])
  211. ? (Key)char.ToUpper ((char)keyEnums [idxKey]) : keyEnums [idxKey];
  212. var c = (char)k;
  213. var ck = char.IsLetter (c) ? (ConsoleKey)char.ToUpper (c) : (ConsoleKey)c;
  214. var mk = new KeyModifiers () {
  215. Shift = shift,
  216. Alt = alt,
  217. Ctrl = control
  218. };
  219. key = ShortcutHelper.GetModifiersKey (new KeyEvent (k, mk));
  220. Application.Driver.SendKeys (c, ck, shift, alt, control);
  221. PushIterations++;
  222. if (idxKey + 1 < keyEnums.Count) {
  223. idxKey++;
  224. } else {
  225. idxKey = 0;
  226. i++;
  227. }
  228. }
  229. Application.Run ();
  230. Assert.Equal (key, lastKey);
  231. // Shutdown must be called to safely clean up Application if Init has been called
  232. Application.Shutdown ();
  233. }
  234. [Theory]
  235. [InlineData (typeof (FakeDriver))]
  236. public void TerminalResized_Simulation (Type driverType)
  237. {
  238. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  239. Application.Init (driver);
  240. var wasTerminalResized = false;
  241. Application.Resized = (e) => {
  242. wasTerminalResized = true;
  243. Assert.Equal (120, e.Cols);
  244. Assert.Equal (40, e.Rows);
  245. };
  246. Assert.Equal (80, Console.BufferWidth);
  247. Assert.Equal (25, Console.BufferHeight);
  248. // MockDriver is by default 80x25
  249. Assert.Equal (Console.BufferWidth, driver.Cols);
  250. Assert.Equal (Console.BufferHeight, driver.Rows);
  251. Assert.False (wasTerminalResized);
  252. // MockDriver will now be sets to 120x40
  253. driver.SetBufferSize (120, 40);
  254. Assert.Equal (120, Application.Driver.Cols);
  255. Assert.Equal (40, Application.Driver.Rows);
  256. Assert.True (wasTerminalResized);
  257. // MockDriver will still be 120x40
  258. wasTerminalResized = false;
  259. Application.HeightAsBuffer = true;
  260. driver.SetWindowSize (40, 20);
  261. Assert.Equal (120, Application.Driver.Cols);
  262. Assert.Equal (40, Application.Driver.Rows);
  263. Assert.Equal (120, Console.BufferWidth);
  264. Assert.Equal (40, Console.BufferHeight);
  265. Assert.Equal (40, Console.WindowWidth);
  266. Assert.Equal (20, Console.WindowHeight);
  267. Assert.True (wasTerminalResized);
  268. Application.Shutdown ();
  269. }
  270. [Theory]
  271. [InlineData (typeof (FakeDriver))]
  272. public void HeightAsBuffer_Is_False_Left_And_Top_Is_Always_Zero (Type driverType)
  273. {
  274. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  275. Application.Init (driver);
  276. Assert.False (Application.HeightAsBuffer);
  277. Assert.Equal (0, Console.WindowLeft);
  278. Assert.Equal (0, Console.WindowTop);
  279. driver.SetWindowPosition (5, 5);
  280. Assert.Equal (0, Console.WindowLeft);
  281. Assert.Equal (0, Console.WindowTop);
  282. Application.Shutdown ();
  283. }
  284. [Theory]
  285. [InlineData (typeof (FakeDriver))]
  286. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_WindowWidth (Type driverType)
  287. {
  288. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  289. Application.Init (driver);
  290. Application.HeightAsBuffer = true;
  291. Assert.True (Application.HeightAsBuffer);
  292. driver.SetWindowPosition (81, 25);
  293. Assert.Equal (0, Console.WindowLeft);
  294. Assert.Equal (0, Console.WindowTop);
  295. Application.Shutdown ();
  296. }
  297. [Theory]
  298. [InlineData (typeof (FakeDriver))]
  299. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_BufferWidth_Minus_WindowWidth (Type driverType)
  300. {
  301. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  302. Application.Init (driver);
  303. Application.HeightAsBuffer = true;
  304. Assert.True (Application.HeightAsBuffer);
  305. driver.SetWindowPosition (81, 25);
  306. Assert.Equal (0, Console.WindowLeft);
  307. Assert.Equal (0, Console.WindowTop);
  308. // MockDriver will now be sets to 120x25
  309. driver.SetBufferSize (120, 25);
  310. Assert.Equal (120, Application.Driver.Cols);
  311. Assert.Equal (25, Application.Driver.Rows);
  312. Assert.Equal (120, Console.BufferWidth);
  313. Assert.Equal (25, Console.BufferHeight);
  314. Assert.Equal (80, Console.WindowWidth);
  315. Assert.Equal (25, Console.WindowHeight);
  316. driver.SetWindowPosition (121, 25);
  317. Assert.Equal (40, Console.WindowLeft);
  318. Assert.Equal (0, Console.WindowTop);
  319. driver.SetWindowSize (90, 25);
  320. Assert.Equal (120, Application.Driver.Cols);
  321. Assert.Equal (25, Application.Driver.Rows);
  322. Assert.Equal (120, Console.BufferWidth);
  323. Assert.Equal (25, Console.BufferHeight);
  324. Assert.Equal (90, Console.WindowWidth);
  325. Assert.Equal (25, Console.WindowHeight);
  326. driver.SetWindowPosition (121, 25);
  327. Assert.Equal (30, Console.WindowLeft);
  328. Assert.Equal (0, Console.WindowTop);
  329. Application.Shutdown ();
  330. }
  331. [Theory]
  332. [InlineData (typeof (FakeDriver))]
  333. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_WindowHeight (Type driverType)
  334. {
  335. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  336. Application.Init (driver);
  337. Application.HeightAsBuffer = true;
  338. Assert.True (Application.HeightAsBuffer);
  339. driver.SetWindowPosition (80, 26);
  340. Assert.Equal (0, Console.WindowLeft);
  341. Assert.Equal (0, Console.WindowTop);
  342. Application.Shutdown ();
  343. }
  344. [Theory]
  345. [InlineData (typeof (FakeDriver))]
  346. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_BufferHeight_Minus_WindowHeight (Type driverType)
  347. {
  348. var driver = (FakeDriver)Activator.CreateInstance (driverType);
  349. Application.Init (driver);
  350. Application.HeightAsBuffer = true;
  351. Assert.True (Application.HeightAsBuffer);
  352. driver.SetWindowPosition (80, 26);
  353. Assert.Equal (0, Console.WindowLeft);
  354. Assert.Equal (0, Console.WindowTop);
  355. // MockDriver will now be sets to 80x40
  356. driver.SetBufferSize (80, 40);
  357. Assert.Equal (80, Application.Driver.Cols);
  358. Assert.Equal (40, Application.Driver.Rows);
  359. Assert.Equal (80, Console.BufferWidth);
  360. Assert.Equal (40, Console.BufferHeight);
  361. Assert.Equal (80, Console.WindowWidth);
  362. Assert.Equal (25, Console.WindowHeight);
  363. Assert.Equal (0, Console.WindowLeft);
  364. Assert.Equal (0, Console.WindowTop);
  365. driver.SetWindowPosition (80, 40);
  366. Assert.Equal (0, Console.WindowLeft);
  367. Assert.Equal (15, Console.WindowTop);
  368. driver.SetWindowSize (80, 20);
  369. Assert.Equal (80, Application.Driver.Cols);
  370. Assert.Equal (40, Application.Driver.Rows);
  371. Assert.Equal (80, Console.BufferWidth);
  372. Assert.Equal (40, Console.BufferHeight);
  373. Assert.Equal (80, Console.WindowWidth);
  374. Assert.Equal (20, Console.WindowHeight);
  375. Assert.Equal (0, Console.WindowLeft);
  376. Assert.Equal (15, Console.WindowTop);
  377. driver.SetWindowPosition (80, 41);
  378. Assert.Equal (0, Console.WindowLeft);
  379. Assert.Equal (20, Console.WindowTop);
  380. Application.Shutdown ();
  381. }
  382. [Fact]
  383. public void Internal_Tests ()
  384. {
  385. var cs = new ColorScheme ();
  386. Assert.Equal ("", cs.caller);
  387. }
  388. [Fact]
  389. [AutoInitShutdown]
  390. public void KeyModifiers_Resetting_At_New_Keystrokes ()
  391. {
  392. bool? okInitialFocused = null;
  393. bool? cancelInitialFocused = null;
  394. var okClicked = false;
  395. var closing = false;
  396. var cursorRight = false;
  397. var endingKeyPress = false;
  398. var closed = false;
  399. var top = Application.Top;
  400. var ok = new Button ("Ok");
  401. ok.Clicked += () => {
  402. if (!okClicked) {
  403. okClicked = true;
  404. Application.RequestStop ();
  405. }
  406. };
  407. var cancel = new Button ("Cancel");
  408. var d = new Dialog ("Quit", cancel, ok);
  409. d.KeyPress += (e) => {
  410. if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  411. if (!okClicked && !closing) {
  412. okInitialFocused = ok.HasFocus;
  413. cancelInitialFocused = cancel.HasFocus;
  414. closing = true;
  415. var mKeys = new Stack<ConsoleKeyInfo> ();
  416. var cki = new ConsoleKeyInfo ('\0', ConsoleKey.Enter, false, false, false);
  417. mKeys.Push (cki);
  418. cki = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, false, false, false);
  419. mKeys.Push (cki);
  420. FakeConsole.MockKeyPresses = mKeys;
  421. }
  422. e.Handled = true;
  423. } else if (e.KeyEvent.Key == Key.CursorRight) {
  424. if (!cursorRight) {
  425. cursorRight = true;
  426. } else if (ok.HasFocus) {
  427. e.Handled = endingKeyPress = true;
  428. }
  429. }
  430. };
  431. d.Loaded += () => {
  432. var mKeys = new Stack<ConsoleKeyInfo> ();
  433. var cki = new ConsoleKeyInfo ('q', ConsoleKey.Q, false, false, true);
  434. mKeys.Push (cki);
  435. FakeConsole.MockKeyPresses = mKeys;
  436. };
  437. d.Closed += (_) => {
  438. if (okClicked && closing) {
  439. closed = true;
  440. }
  441. };
  442. top.Ready += () => Application.Run (d);
  443. Application.Iteration += () => {
  444. if (closed) {
  445. Application.RequestStop ();
  446. }
  447. };
  448. Application.Run ();
  449. Assert.False (okInitialFocused);
  450. Assert.True (cancelInitialFocused);
  451. Assert.True (okClicked);
  452. Assert.True (closing);
  453. Assert.True (cursorRight);
  454. Assert.True (endingKeyPress);
  455. Assert.True (closed);
  456. Assert.Empty (FakeConsole.MockKeyPresses);
  457. }
  458. [Fact, AutoInitShutdown]
  459. public void AddRune_On_Clip_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  460. {
  461. var tv = new TextView () {
  462. Width = Dim.Fill (),
  463. Height = Dim.Fill (),
  464. Text = @"これは広いルーンラインです。
  465. これは広いルーンラインです。
  466. これは広いルーンラインです。
  467. これは広いルーンラインです。
  468. これは広いルーンラインです。
  469. これは広いルーンラインです。
  470. これは広いルーンラインです。
  471. これは広いルーンラインです。"
  472. };
  473. var win = new Window ("ワイドルーン") { Width = Dim.Fill (), Height = Dim.Fill () };
  474. win.Add (tv);
  475. Application.Top.Add (win);
  476. var lbl = new Label ("ワイドルーン。");
  477. var dg = new Dialog ("テスト", 14, 4, new Button ("選ぶ"));
  478. dg.Add (lbl);
  479. Application.Begin (Application.Top);
  480. Application.Begin (dg);
  481. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  482. var expected = @"
  483. ┌ ワイドルーン ──────────────┐
  484. │これは広いルーンラインです。│
  485. │これは広いルーンラインです。│
  486. │これは ┌ テスト ────┐ です。│
  487. │これは │ワイドルーン│ です。│
  488. │これは │ [ 選ぶ ] │ です。│
  489. │これは └────────────┘ です。│
  490. │これは広いルーンラインです。│
  491. │これは広いルーンラインです。│
  492. └────────────────────────────┘
  493. ";
  494. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  495. Assert.Equal (new Rect (0, 0, 30, 10), pos);
  496. }
  497. [Fact, AutoInitShutdown]
  498. public void Write_Do_Not_Change_On_ProcessKey ()
  499. {
  500. var win = new Window ();
  501. Application.Begin (win);
  502. ((FakeDriver)Application.Driver).SetBufferSize (20, 8);
  503. System.Threading.Tasks.Task.Run (() => {
  504. System.Threading.Tasks.Task.Delay (500).Wait ();
  505. Application.MainLoop.Invoke (() => {
  506. var lbl = new Label ("Hello World") { X = Pos.Center () };
  507. var dlg = new Dialog ("Test", new Button ("Ok"));
  508. dlg.Add (lbl);
  509. Application.Begin (dlg);
  510. var expected = @"
  511. ┌──────────────────┐
  512. │┌ Test ─────────┐ │
  513. ││ Hello World │ │
  514. ││ │ │
  515. ││ │ │
  516. ││ [ Ok ] │ │
  517. │└───────────────┘ │
  518. └──────────────────┘
  519. ";
  520. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  521. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  522. Assert.True (dlg.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  523. dlg.Redraw (dlg.Bounds);
  524. expected = @"
  525. ┌──────────────────┐
  526. │┌ Test ─────────┐ │
  527. ││ Hello World │ │
  528. ││ │ │
  529. ││ │ │
  530. ││ [ Ok ] │ │
  531. │└───────────────┘ │
  532. └──────────────────┘
  533. ";
  534. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  535. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  536. win.RequestStop ();
  537. });
  538. });
  539. Application.Run (win);
  540. Application.Shutdown ();
  541. }
  542. [Theory]
  543. [InlineData (0x0000001F, 0x241F)]
  544. [InlineData (0x0000007F, 0x247F)]
  545. [InlineData (0x0000009F, 0x249F)]
  546. [InlineData (0x0001001A, 0x1001A)]
  547. public void MakePrintable_Converts_Control_Chars_To_Proper_Unicode (uint code, uint expected)
  548. {
  549. var actual = ConsoleDriver.MakePrintable (code);
  550. Assert.Equal (expected, actual.Value);
  551. }
  552. [Theory]
  553. [InlineData (0x20)]
  554. [InlineData (0x7E)]
  555. [InlineData (0xA0)]
  556. [InlineData (0x010020)]
  557. public void MakePrintable_Does_Not_Convert_Ansi_Chars_To_Unicode (uint code)
  558. {
  559. var actual = ConsoleDriver.MakePrintable (code);
  560. Assert.Equal (code, actual.Value);
  561. }
  562. /// <summary>
  563. /// Sometimes when using remote tools EventKeyRecord sends 'virtual keystrokes'.
  564. /// These are indicated with the wVirtualKeyCode of 231. When we see this code
  565. /// then we need to look to the unicode character (UnicodeChar) instead of the key
  566. /// when telling the rest of the framework what button was pressed. For full details
  567. /// see: https://github.com/gui-cs/Terminal.Gui/issues/2008
  568. /// </summary>
  569. [Theory, AutoInitShutdown]
  570. [ClassData (typeof (PacketTest))]
  571. public void TestVKPacket (uint unicodeCharacter, bool shift, bool alt, bool control, uint initialVirtualKey, uint initialScanCode, Key expectedRemapping, uint expectedVirtualKey, uint expectedScanCode)
  572. {
  573. ConsoleModifiers modifiers = new ConsoleModifiers ();
  574. if (shift) {
  575. modifiers |= ConsoleModifiers.Shift;
  576. }
  577. if (alt) {
  578. modifiers |= ConsoleModifiers.Alt;
  579. }
  580. if (control) {
  581. modifiers |= ConsoleModifiers.Control;
  582. }
  583. var mappedConsoleKey = ConsoleKeyMapping.GetConsoleKeyFromKey (unicodeCharacter, modifiers, out uint scanCode, out uint outputChar);
  584. if ((scanCode > 0 || mappedConsoleKey == 0) && mappedConsoleKey == initialVirtualKey) {
  585. Assert.Equal (mappedConsoleKey, initialVirtualKey);
  586. } else {
  587. Assert.Equal (mappedConsoleKey, outputChar < 0xff ? (uint)(outputChar & 0xff | 0xff << 8) : outputChar);
  588. }
  589. Assert.Equal (scanCode, initialScanCode);
  590. var keyChar = ConsoleKeyMapping.GetKeyCharFromConsoleKey (mappedConsoleKey, modifiers, out uint consoleKey, out scanCode);
  591. //if (scanCode > 0 && consoleKey == keyChar && consoleKey > 48 && consoleKey > 57 && consoleKey < 65 && consoleKey > 91) {
  592. if (scanCode > 0 && keyChar == 0 && consoleKey == mappedConsoleKey) {
  593. Assert.Equal (0, (double)keyChar);
  594. } else {
  595. Assert.Equal (keyChar, unicodeCharacter);
  596. }
  597. Assert.Equal (consoleKey, expectedVirtualKey);
  598. Assert.Equal (scanCode, expectedScanCode);
  599. var top = Application.Top;
  600. top.KeyPress += (e) => {
  601. var after = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  602. Assert.Equal (expectedRemapping, after);
  603. e.Handled = true;
  604. Application.RequestStop ();
  605. };
  606. var iterations = -1;
  607. Application.Iteration += () => {
  608. iterations++;
  609. if (iterations == 0) {
  610. Application.Driver.SendKeys ((char)mappedConsoleKey, ConsoleKey.Packet, shift, alt, control);
  611. }
  612. };
  613. Application.Run ();
  614. Application.Shutdown ();
  615. }
  616. public class PacketTest : IEnumerable, IEnumerable<object []> {
  617. public IEnumerator<object []> GetEnumerator ()
  618. {
  619. yield return new object [] { 'a', false, false, false, 'A', 30, Key.a, 'A', 30 };
  620. yield return new object [] { 'A', true, false, false, 'A', 30, Key.A | Key.ShiftMask, 'A', 30 };
  621. yield return new object [] { 'A', true, true, false, 'A', 30, Key.A | Key.ShiftMask | Key.AltMask, 'A', 30 };
  622. yield return new object [] { 'A', true, true, true, 'A', 30, Key.A | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 'A', 30 };
  623. yield return new object [] { 'z', false, false, false, 'Z', 44, Key.z, 'Z', 44 };
  624. yield return new object [] { 'Z', true, false, false, 'Z', 44, Key.Z | Key.ShiftMask, 'Z', 44 };
  625. yield return new object [] { 'Z', true, true, false, 'Z', 44, Key.Z | Key.ShiftMask | Key.AltMask, 'Z', 44 };
  626. yield return new object [] { 'Z', true, true, true, 'Z', 44, Key.Z | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 'Z', 44 };
  627. yield return new object [] { '英', false, false, false, '\0', 0, (Key)'英', '\0', 0 };
  628. yield return new object [] { '英', true, false, false, '\0', 0, (Key)'英' | Key.ShiftMask, '\0', 0 };
  629. yield return new object [] { '英', true, true, false, '\0', 0, (Key)'英' | Key.ShiftMask | Key.AltMask, '\0', 0 };
  630. yield return new object [] { '英', true, true, true, '\0', 0, (Key)'英' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '\0', 0 };
  631. yield return new object [] { '+', false, false, false, 187, 26, (Key)'+', 187, 26 };
  632. yield return new object [] { '*', true, false, false, 187, 26, (Key)'*' | Key.ShiftMask, 187, 26 };
  633. yield return new object [] { '+', true, true, false, 187, 26, (Key)'+' | Key.ShiftMask | Key.AltMask, 187, 26 };
  634. yield return new object [] { '+', true, true, true, 187, 26, (Key)'+' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 187, 26 };
  635. yield return new object [] { '1', false, false, false, '1', 2, Key.D1, '1', 2 };
  636. yield return new object [] { '!', true, false, false, '1', 2, (Key)'!' | Key.ShiftMask, '1', 2 };
  637. yield return new object [] { '1', true, true, false, '1', 2, Key.D1 | Key.ShiftMask | Key.AltMask, '1', 2 };
  638. yield return new object [] { '1', true, true, true, '1', 2, Key.D1 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '1', 2 };
  639. yield return new object [] { '1', false, true, true, '1', 2, Key.D1 | Key.AltMask | Key.CtrlMask, '1', 2 };
  640. yield return new object [] { '2', false, false, false, '2', 3, Key.D2, '2', 3 };
  641. yield return new object [] { '"', true, false, false, '2', 3, (Key)'"' | Key.ShiftMask, '2', 3 };
  642. yield return new object [] { '2', true, true, false, '2', 3, Key.D2 | Key.ShiftMask | Key.AltMask, '2', 3 };
  643. yield return new object [] { '2', true, true, true, '2', 3, Key.D2 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '2', 3 };
  644. yield return new object [] { '@', false, true, true, '2', 3, (Key)'@' | Key.AltMask | Key.CtrlMask, '2', 3 };
  645. yield return new object [] { '3', false, false, false, '3', 4, Key.D3, '3', 4 };
  646. yield return new object [] { '#', true, false, false, '3', 4, (Key)'#' | Key.ShiftMask, '3', 4 };
  647. yield return new object [] { '3', true, true, false, '3', 4, Key.D3 | Key.ShiftMask | Key.AltMask, '3', 4 };
  648. yield return new object [] { '3', true, true, true, '3', 4, Key.D3 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '3', 4 };
  649. yield return new object [] { '£', false, true, true, '3', 4, (Key)'£' | Key.AltMask | Key.CtrlMask, '3', 4 };
  650. yield return new object [] { '4', false, false, false, '4', 5, Key.D4, '4', 5 };
  651. yield return new object [] { '$', true, false, false, '4', 5, (Key)'$' | Key.ShiftMask, '4', 5 };
  652. yield return new object [] { '4', true, true, false, '4', 5, Key.D4 | Key.ShiftMask | Key.AltMask, '4', 5 };
  653. yield return new object [] { '4', true, true, true, '4', 5, Key.D4 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '4', 5 };
  654. yield return new object [] { '§', false, true, true, '4', 5, (Key)'§' | Key.AltMask | Key.CtrlMask, '4', 5 };
  655. yield return new object [] { '5', false, false, false, '5', 6, Key.D5, '5', 6 };
  656. yield return new object [] { '%', true, false, false, '5', 6, (Key)'%' | Key.ShiftMask, '5', 6 };
  657. yield return new object [] { '5', true, true, false, '5', 6, Key.D5 | Key.ShiftMask | Key.AltMask, '5', 6 };
  658. yield return new object [] { '5', true, true, true, '5', 6, Key.D5 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '5', 6 };
  659. yield return new object [] { '€', false, true, true, '5', 6, (Key)'€' | Key.AltMask | Key.CtrlMask, '5', 6 };
  660. yield return new object [] { '6', false, false, false, '6', 7, Key.D6, '6', 7 };
  661. yield return new object [] { '&', true, false, false, '6', 7, (Key)'&' | Key.ShiftMask, '6', 7 };
  662. yield return new object [] { '6', true, true, false, '6', 7, Key.D6 | Key.ShiftMask | Key.AltMask, '6', 7 };
  663. yield return new object [] { '6', true, true, true, '6', 7, Key.D6 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '6', 7 };
  664. yield return new object [] { '6', false, true, true, '6', 7, Key.D6 | Key.AltMask | Key.CtrlMask, '6', 7 };
  665. yield return new object [] { '7', false, false, false, '7', 8, Key.D7, '7', 8 };
  666. yield return new object [] { '/', true, false, false, '7', 8, (Key)'/' | Key.ShiftMask, '7', 8 };
  667. yield return new object [] { '7', true, true, false, '7', 8, Key.D7 | Key.ShiftMask | Key.AltMask, '7', 8 };
  668. yield return new object [] { '7', true, true, true, '7', 8, Key.D7 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '7', 8 };
  669. yield return new object [] { '{', false, true, true, '7', 8, (Key)'{' | Key.AltMask | Key.CtrlMask, '7', 8 };
  670. yield return new object [] { '8', false, false, false, '8', 9, Key.D8, '8', 9 };
  671. yield return new object [] { '(', true, false, false, '8', 9, (Key)'(' | Key.ShiftMask, '8', 9 };
  672. yield return new object [] { '8', true, true, false, '8', 9, Key.D8 | Key.ShiftMask | Key.AltMask, '8', 9 };
  673. yield return new object [] { '8', true, true, true, '8', 9, Key.D8 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '8', 9 };
  674. yield return new object [] { '[', false, true, true, '8', 9, (Key)'[' | Key.AltMask | Key.CtrlMask, '8', 9 };
  675. yield return new object [] { '9', false, false, false, '9', 10, Key.D9, '9', 10 };
  676. yield return new object [] { ')', true, false, false, '9', 10, (Key)')' | Key.ShiftMask, '9', 10 };
  677. yield return new object [] { '9', true, true, false, '9', 10, Key.D9 | Key.ShiftMask | Key.AltMask, '9', 10 };
  678. yield return new object [] { '9', true, true, true, '9', 10, Key.D9 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '9', 10 };
  679. yield return new object [] { ']', false, true, true, '9', 10, (Key)']' | Key.AltMask | Key.CtrlMask, '9', 10 };
  680. yield return new object [] { '0', false, false, false, '0', 11, Key.D0, '0', 11 };
  681. yield return new object [] { '=', true, false, false, '0', 11, (Key)'=' | Key.ShiftMask, '0', 11 };
  682. yield return new object [] { '0', true, true, false, '0', 11, Key.D0 | Key.ShiftMask | Key.AltMask, '0', 11 };
  683. yield return new object [] { '0', true, true, true, '0', 11, Key.D0 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '0', 11 };
  684. yield return new object [] { '}', false, true, true, '0', 11, (Key)'}' | Key.AltMask | Key.CtrlMask, '0', 11 };
  685. yield return new object [] { '\'', false, false, false, 219, 12, (Key)'\'', 219, 12 };
  686. yield return new object [] { '?', true, false, false, 219, 12, (Key)'?' | Key.ShiftMask, 219, 12 };
  687. yield return new object [] { '\'', true, true, false, 219, 12, (Key)'\'' | Key.ShiftMask | Key.AltMask, 219, 12 };
  688. yield return new object [] { '\'', true, true, true, 219, 12, (Key)'\'' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 219, 12 };
  689. yield return new object [] { '«', false, false, false, 221, 13, (Key)'«', 221, 13 };
  690. yield return new object [] { '»', true, false, false, 221, 13, (Key)'»' | Key.ShiftMask, 221, 13 };
  691. yield return new object [] { '«', true, true, false, 221, 13, (Key)'«' | Key.ShiftMask | Key.AltMask, 221, 13 };
  692. yield return new object [] { '«', true, true, true, 221, 13, (Key)'«' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 221, 13 };
  693. yield return new object [] { 'á', false, false, false, 'á', 0, (Key)'á', 'A', 30 };
  694. yield return new object [] { 'Á', true, false, false, 'Á', 0, (Key)'Á' | Key.ShiftMask, 'A', 30 };
  695. yield return new object [] { 'à', false, false, false, 'à', 0, (Key)'à', 'A', 30 };
  696. yield return new object [] { 'À', true, false, false, 'À', 0, (Key)'À' | Key.ShiftMask, 'A', 30 };
  697. yield return new object [] { 'é', false, false, false, 'é', 0, (Key)'é', 'E', 18 };
  698. yield return new object [] { 'É', true, false, false, 'É', 0, (Key)'É' | Key.ShiftMask, 'E', 18 };
  699. yield return new object [] { 'è', false, false, false, 'è', 0, (Key)'è', 'E', 18 };
  700. yield return new object [] { 'È', true, false, false, 'È', 0, (Key)'È' | Key.ShiftMask, 'E', 18 };
  701. yield return new object [] { 'í', false, false, false, 'í', 0, (Key)'í', 'I', 23 };
  702. yield return new object [] { 'Í', true, false, false, 'Í', 0, (Key)'Í' | Key.ShiftMask, 'I', 23 };
  703. yield return new object [] { 'ì', false, false, false, 'ì', 0, (Key)'ì', 'I', 23 };
  704. yield return new object [] { 'Ì', true, false, false, 'Ì', 0, (Key)'Ì' | Key.ShiftMask, 'I', 23 };
  705. yield return new object [] { 'ó', false, false, false, 'ó', 0, (Key)'ó', 'O', 24 };
  706. yield return new object [] { 'Ó', true, false, false, 'Ó', 0, (Key)'Ó' | Key.ShiftMask, 'O', 24 };
  707. yield return new object [] { 'ò', false, false, false, 'Ó', 0, (Key)'ò', 'O', 24 };
  708. yield return new object [] { 'Ò', true, false, false, 'Ò', 0, (Key)'Ò' | Key.ShiftMask, 'O', 24 };
  709. yield return new object [] { 'ú', false, false, false, 'ú', 0, (Key)'ú', 'U', 22 };
  710. yield return new object [] { 'Ú', true, false, false, 'Ú', 0, (Key)'Ú' | Key.ShiftMask, 'U', 22 };
  711. yield return new object [] { 'ù', false, false, false, 'ù', 0, (Key)'ù', 'U', 22 };
  712. yield return new object [] { 'Ù', true, false, false, 'Ù', 0, (Key)'Ù' | Key.ShiftMask, 'U', 22 };
  713. yield return new object [] { 'ö', false, false, false, 'ó', 0, (Key)'ö', 'O', 24 };
  714. yield return new object [] { 'Ö', true, false, false, 'Ó', 0, (Key)'Ö' | Key.ShiftMask, 'O', 24 };
  715. yield return new object [] { '<', false, false, false, 226, 86, (Key)'<', 226, 86 };
  716. yield return new object [] { '>', true, false, false, 226, 86, (Key)'>' | Key.ShiftMask, 226, 86 };
  717. yield return new object [] { '<', true, true, false, 226, 86, (Key)'<' | Key.ShiftMask | Key.AltMask, 226, 86 };
  718. yield return new object [] { '<', true, true, true, 226, 86, (Key)'<' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 226, 86 };
  719. yield return new object [] { 'ç', false, false, false, 192, 39, (Key)'ç', 192, 39 };
  720. yield return new object [] { 'Ç', true, false, false, 192, 39, (Key)'Ç' | Key.ShiftMask, 192, 39 };
  721. yield return new object [] { 'ç', true, true, false, 192, 39, (Key)'ç' | Key.ShiftMask | Key.AltMask, 192, 39 };
  722. yield return new object [] { 'ç', true, true, true, 192, 39, (Key)'ç' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 192, 39 };
  723. yield return new object [] { '¨', false, true, true, 187, 26, (Key)'¨' | Key.AltMask | Key.CtrlMask, 187, 26 };
  724. yield return new object [] { (uint)Key.PageUp, false, false, false, 33, 73, Key.PageUp, 33, 73 };
  725. yield return new object [] { (uint)Key.PageUp, true, false, false, 33, 73, Key.PageUp | Key.ShiftMask, 33, 73 };
  726. yield return new object [] { (uint)Key.PageUp, true, true, false, 33, 73, Key.PageUp | Key.ShiftMask | Key.AltMask, 33, 73 };
  727. yield return new object [] { (uint)Key.PageUp, true, true, true, 33, 73, Key.PageUp | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 33, 73 };
  728. }
  729. IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
  730. }
  731. }
  732. }