ConsoleDriverTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  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 = shift && char.IsLetter ((char)keyEnums [idxKey]) && char.IsLower ((char)keyEnums [idxKey])
  192. ? (Key)char.ToUpper ((char)keyEnums [idxKey]) : keyEnums [idxKey];
  193. var c = (char)k;
  194. var ck = char.IsLetter (c) ? (ConsoleKey)char.ToUpper (c) : (ConsoleKey)c;
  195. var mk = new KeyModifiers () {
  196. Shift = shift,
  197. Alt = alt,
  198. Ctrl = control
  199. };
  200. key = ShortcutHelper.GetModifiersKey (new KeyEvent (k, mk));
  201. Application.Driver.SendKeys (c, ck, shift, alt, control);
  202. PushIterations++;
  203. if (idxKey + 1 < keyEnums.Count) {
  204. idxKey++;
  205. } else {
  206. idxKey = 0;
  207. i++;
  208. }
  209. }
  210. Application.Run ();
  211. Assert.Equal (key, lastKey);
  212. // Shutdown must be called to safely clean up Application if Init has been called
  213. Application.Shutdown ();
  214. }
  215. [Fact]
  216. public void TerminalResized_Simulation ()
  217. {
  218. var driver = new FakeDriver ();
  219. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  220. var wasTerminalResized = false;
  221. Application.Resized = (e) => {
  222. wasTerminalResized = true;
  223. Assert.Equal (120, e.Cols);
  224. Assert.Equal (40, e.Rows);
  225. };
  226. Assert.Equal (80, Console.BufferWidth);
  227. Assert.Equal (25, Console.BufferHeight);
  228. // MockDriver is by default 80x25
  229. Assert.Equal (Console.BufferWidth, driver.Cols);
  230. Assert.Equal (Console.BufferHeight, driver.Rows);
  231. Assert.False (wasTerminalResized);
  232. // MockDriver will now be sets to 120x40
  233. driver.SetBufferSize (120, 40);
  234. Assert.Equal (120, Application.Driver.Cols);
  235. Assert.Equal (40, Application.Driver.Rows);
  236. Assert.True (wasTerminalResized);
  237. // MockDriver will still be 120x40
  238. wasTerminalResized = false;
  239. Application.HeightAsBuffer = true;
  240. driver.SetWindowSize (40, 20);
  241. Assert.Equal (120, Application.Driver.Cols);
  242. Assert.Equal (40, Application.Driver.Rows);
  243. Assert.Equal (120, Console.BufferWidth);
  244. Assert.Equal (40, Console.BufferHeight);
  245. Assert.Equal (40, Console.WindowWidth);
  246. Assert.Equal (20, Console.WindowHeight);
  247. Assert.True (wasTerminalResized);
  248. Application.Shutdown ();
  249. }
  250. [Fact]
  251. public void HeightAsBuffer_Is_False_Left_And_Top_Is_Always_Zero ()
  252. {
  253. var driver = new FakeDriver ();
  254. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  255. Assert.False (Application.HeightAsBuffer);
  256. Assert.Equal (0, Console.WindowLeft);
  257. Assert.Equal (0, Console.WindowTop);
  258. driver.SetWindowPosition (5, 5);
  259. Assert.Equal (0, Console.WindowLeft);
  260. Assert.Equal (0, Console.WindowTop);
  261. Application.Shutdown ();
  262. }
  263. [Fact]
  264. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_WindowWidth ()
  265. {
  266. var driver = new FakeDriver ();
  267. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  268. Application.HeightAsBuffer = true;
  269. Assert.True (Application.HeightAsBuffer);
  270. driver.SetWindowPosition (81, 25);
  271. Assert.Equal (0, Console.WindowLeft);
  272. Assert.Equal (0, Console.WindowTop);
  273. Application.Shutdown ();
  274. }
  275. [Fact]
  276. public void HeightAsBuffer_Is_True_Left_Cannot_Be_Greater_Than_BufferWidth_Minus_WindowWidth ()
  277. {
  278. var driver = new FakeDriver ();
  279. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  280. Application.HeightAsBuffer = true;
  281. Assert.True (Application.HeightAsBuffer);
  282. driver.SetWindowPosition (81, 25);
  283. Assert.Equal (0, Console.WindowLeft);
  284. Assert.Equal (0, Console.WindowTop);
  285. // MockDriver will now be sets to 120x25
  286. driver.SetBufferSize (120, 25);
  287. Assert.Equal (120, Application.Driver.Cols);
  288. Assert.Equal (25, Application.Driver.Rows);
  289. Assert.Equal (120, Console.BufferWidth);
  290. Assert.Equal (25, Console.BufferHeight);
  291. Assert.Equal (80, Console.WindowWidth);
  292. Assert.Equal (25, Console.WindowHeight);
  293. driver.SetWindowPosition (121, 25);
  294. Assert.Equal (40, Console.WindowLeft);
  295. Assert.Equal (0, Console.WindowTop);
  296. driver.SetWindowSize (90, 25);
  297. Assert.Equal (120, Application.Driver.Cols);
  298. Assert.Equal (25, Application.Driver.Rows);
  299. Assert.Equal (120, Console.BufferWidth);
  300. Assert.Equal (25, Console.BufferHeight);
  301. Assert.Equal (90, Console.WindowWidth);
  302. Assert.Equal (25, Console.WindowHeight);
  303. driver.SetWindowPosition (121, 25);
  304. Assert.Equal (30, Console.WindowLeft);
  305. Assert.Equal (0, Console.WindowTop);
  306. Application.Shutdown ();
  307. }
  308. [Fact]
  309. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_WindowHeight ()
  310. {
  311. var driver = new FakeDriver ();
  312. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  313. Application.HeightAsBuffer = true;
  314. Assert.True (Application.HeightAsBuffer);
  315. driver.SetWindowPosition (80, 26);
  316. Assert.Equal (0, Console.WindowLeft);
  317. Assert.Equal (0, Console.WindowTop);
  318. Application.Shutdown ();
  319. }
  320. [Fact]
  321. public void HeightAsBuffer_Is_True_Top_Cannot_Be_Greater_Than_BufferHeight_Minus_WindowHeight ()
  322. {
  323. var driver = new FakeDriver ();
  324. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  325. Application.HeightAsBuffer = true;
  326. Assert.True (Application.HeightAsBuffer);
  327. driver.SetWindowPosition (80, 26);
  328. Assert.Equal (0, Console.WindowLeft);
  329. Assert.Equal (0, Console.WindowTop);
  330. // MockDriver will now be sets to 80x40
  331. driver.SetBufferSize (80, 40);
  332. Assert.Equal (80, Application.Driver.Cols);
  333. Assert.Equal (40, Application.Driver.Rows);
  334. Assert.Equal (80, Console.BufferWidth);
  335. Assert.Equal (40, Console.BufferHeight);
  336. Assert.Equal (80, Console.WindowWidth);
  337. Assert.Equal (25, Console.WindowHeight);
  338. Assert.Equal (0, Console.WindowLeft);
  339. Assert.Equal (0, Console.WindowTop);
  340. driver.SetWindowPosition (80, 40);
  341. Assert.Equal (0, Console.WindowLeft);
  342. Assert.Equal (15, Console.WindowTop);
  343. driver.SetWindowSize (80, 20);
  344. Assert.Equal (80, Application.Driver.Cols);
  345. Assert.Equal (40, Application.Driver.Rows);
  346. Assert.Equal (80, Console.BufferWidth);
  347. Assert.Equal (40, Console.BufferHeight);
  348. Assert.Equal (80, Console.WindowWidth);
  349. Assert.Equal (20, Console.WindowHeight);
  350. Assert.Equal (0, Console.WindowLeft);
  351. Assert.Equal (15, Console.WindowTop);
  352. driver.SetWindowPosition (80, 41);
  353. Assert.Equal (0, Console.WindowLeft);
  354. Assert.Equal (20, Console.WindowTop);
  355. Application.Shutdown ();
  356. }
  357. [Fact]
  358. public void Internal_Tests ()
  359. {
  360. var cs = new ColorScheme ();
  361. Assert.Equal ("", cs.caller);
  362. }
  363. [Fact]
  364. [AutoInitShutdown]
  365. public void KeyModifiers_Resetting_At_New_Keystrokes ()
  366. {
  367. bool? okInitialFocused = null;
  368. bool? cancelInitialFocused = null;
  369. var okClicked = false;
  370. var closing = false;
  371. var cursorRight = false;
  372. var endingKeyPress = false;
  373. var closed = false;
  374. var top = Application.Top;
  375. var ok = new Button ("Ok");
  376. ok.Clicked += () => {
  377. if (!okClicked) {
  378. okClicked = true;
  379. Application.RequestStop ();
  380. }
  381. };
  382. var cancel = new Button ("Cancel");
  383. var d = new Dialog ("Quit", cancel, ok);
  384. d.KeyPress += (e) => {
  385. if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  386. if (!okClicked && !closing) {
  387. okInitialFocused = ok.HasFocus;
  388. cancelInitialFocused = cancel.HasFocus;
  389. closing = true;
  390. var mKeys = new Stack<ConsoleKeyInfo> ();
  391. var cki = new ConsoleKeyInfo ('\0', ConsoleKey.Enter, false, false, false);
  392. mKeys.Push (cki);
  393. cki = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, false, false, false);
  394. mKeys.Push (cki);
  395. FakeConsole.MockKeyPresses = mKeys;
  396. }
  397. e.Handled = true;
  398. } else if (e.KeyEvent.Key == Key.CursorRight) {
  399. if (!cursorRight) {
  400. cursorRight = true;
  401. } else if (ok.HasFocus) {
  402. e.Handled = endingKeyPress = true;
  403. }
  404. }
  405. };
  406. d.Loaded += () => {
  407. var mKeys = new Stack<ConsoleKeyInfo> ();
  408. var cki = new ConsoleKeyInfo ('q', ConsoleKey.Q, false, false, true);
  409. mKeys.Push (cki);
  410. FakeConsole.MockKeyPresses = mKeys;
  411. };
  412. d.Closed += (_) => {
  413. if (okClicked && closing) {
  414. closed = true;
  415. }
  416. };
  417. top.Ready += () => Application.Run (d);
  418. Application.Iteration += () => {
  419. if (closed) {
  420. Application.RequestStop ();
  421. }
  422. };
  423. Application.Run ();
  424. Assert.False (okInitialFocused);
  425. Assert.True (cancelInitialFocused);
  426. Assert.True (okClicked);
  427. Assert.True (closing);
  428. Assert.True (cursorRight);
  429. Assert.True (endingKeyPress);
  430. Assert.True (closed);
  431. Assert.Empty (FakeConsole.MockKeyPresses);
  432. }
  433. [Fact, AutoInitShutdown]
  434. public void AddRune_On_Clip_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  435. {
  436. var tv = new TextView () {
  437. Width = Dim.Fill (),
  438. Height = Dim.Fill (),
  439. Text = @"これは広いルーンラインです。
  440. これは広いルーンラインです。
  441. これは広いルーンラインです。
  442. これは広いルーンラインです。
  443. これは広いルーンラインです。
  444. これは広いルーンラインです。
  445. これは広いルーンラインです。
  446. これは広いルーンラインです。"
  447. };
  448. var win = new Window ("ワイドルーン") { Width = Dim.Fill (), Height = Dim.Fill () };
  449. win.Add (tv);
  450. Application.Top.Add (win);
  451. var lbl = new Label ("ワイドルーン。");
  452. var dg = new Dialog ("テスト", 14, 4, new Button ("選ぶ"));
  453. dg.Add (lbl);
  454. Application.Begin (Application.Top);
  455. Application.Begin (dg);
  456. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  457. var expected = @"
  458. ┌ ワイドルーン ──────────────┐
  459. │これは広いルーンラインです。│
  460. │これは広いルーンラインです。│
  461. │これは ┌ テスト ────┐ です。│
  462. │これは │ワイドルーン│ です。│
  463. │これは │ [ 選ぶ ] │ です。│
  464. │これは └────────────┘ です。│
  465. │これは広いルーンラインです。│
  466. │これは広いルーンラインです。│
  467. └────────────────────────────┘
  468. ";
  469. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  470. Assert.Equal (new Rect (0, 0, 30, 10), pos);
  471. }
  472. [Fact, AutoInitShutdown]
  473. public void Write_Do_Not_Change_On_ProcessKey ()
  474. {
  475. var win = new Window ();
  476. Application.Begin (win);
  477. ((FakeDriver)Application.Driver).SetBufferSize (20, 8);
  478. System.Threading.Tasks.Task.Run (() => {
  479. System.Threading.Tasks.Task.Delay (500).Wait ();
  480. Application.MainLoop.Invoke (() => {
  481. var lbl = new Label ("Hello World") { X = Pos.Center () };
  482. var dlg = new Dialog ("Test", new Button ("Ok"));
  483. dlg.Add (lbl);
  484. Application.Begin (dlg);
  485. var expected = @"
  486. ┌──────────────────┐
  487. │┌ Test ─────────┐ │
  488. ││ Hello World │ │
  489. ││ │ │
  490. ││ │ │
  491. ││ [ Ok ] │ │
  492. │└───────────────┘ │
  493. └──────────────────┘
  494. ";
  495. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  496. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  497. Assert.True (dlg.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ())));
  498. dlg.Redraw (dlg.Bounds);
  499. expected = @"
  500. ┌──────────────────┐
  501. │┌ Test ─────────┐ │
  502. ││ Hello World │ │
  503. ││ │ │
  504. ││ │ │
  505. ││ [ Ok ] │ │
  506. │└───────────────┘ │
  507. └──────────────────┘
  508. ";
  509. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  510. Assert.Equal (new Rect (0, 0, 20, 8), pos);
  511. win.RequestStop ();
  512. });
  513. });
  514. Application.Run (win);
  515. Application.Shutdown ();
  516. }
  517. [Theory]
  518. [InlineData (0x0000001F, 0x241F)]
  519. [InlineData (0x0000007F, 0x247F)]
  520. [InlineData (0x0000009F, 0x249F)]
  521. [InlineData (0x0001001A, 0x1001A)]
  522. public void MakePrintable_Converts_Control_Chars_To_Proper_Unicode (uint code, uint expected)
  523. {
  524. var actual = ConsoleDriver.MakePrintable (code);
  525. Assert.Equal (expected, actual.Value);
  526. }
  527. [Theory]
  528. [InlineData (0x20)]
  529. [InlineData (0x7E)]
  530. [InlineData (0xA0)]
  531. [InlineData (0x010020)]
  532. public void MakePrintable_Does_Not_Convert_Ansi_Chars_To_Unicode (uint code)
  533. {
  534. var actual = ConsoleDriver.MakePrintable (code);
  535. Assert.Equal (code, actual.Value);
  536. }
  537. /// <summary>
  538. /// Sometimes when using remote tools EventKeyRecord sends 'virtual keystrokes'.
  539. /// These are indicated with the wVirtualKeyCode of 231. When we see this code
  540. /// then we need to look to the unicode character (UnicodeChar) instead of the key
  541. /// when telling the rest of the framework what button was pressed. For full details
  542. /// see: https://github.com/gui-cs/Terminal.Gui/issues/2008
  543. /// </summary>
  544. [Theory, AutoInitShutdown]
  545. [ClassData (typeof (PacketTest))]
  546. public void TestVKPacket (uint unicodeCharacter, bool shift, bool alt, bool control, uint initialVirtualKey, uint initialScanCode, Key expectedRemapping, uint expectedVirtualKey, uint expectedScanCode)
  547. {
  548. ConsoleModifiers modifiers = new ConsoleModifiers ();
  549. if (shift) {
  550. modifiers |= ConsoleModifiers.Shift;
  551. }
  552. if (alt) {
  553. modifiers |= ConsoleModifiers.Alt;
  554. }
  555. if (control) {
  556. modifiers |= ConsoleModifiers.Control;
  557. }
  558. var mappedConsoleKey = ConsoleKeyMapping.GetConsoleKeyFromKey (unicodeCharacter, modifiers, out uint scanCode, out uint outputChar);
  559. if ((scanCode > 0 || mappedConsoleKey == 0) && mappedConsoleKey == initialVirtualKey) {
  560. Assert.Equal (mappedConsoleKey, initialVirtualKey);
  561. } else {
  562. Assert.Equal (mappedConsoleKey, outputChar < 0xff ? (uint)(outputChar & 0xff | 0xff << 8) : outputChar);
  563. }
  564. Assert.Equal (scanCode, initialScanCode);
  565. var keyChar = ConsoleKeyMapping.GetKeyCharFromConsoleKey (mappedConsoleKey, modifiers, out uint consoleKey, out scanCode);
  566. //if (scanCode > 0 && consoleKey == keyChar && consoleKey > 48 && consoleKey > 57 && consoleKey < 65 && consoleKey > 91) {
  567. if (scanCode > 0 && keyChar == 0 && consoleKey == mappedConsoleKey) {
  568. Assert.Equal (0, (double)keyChar);
  569. } else {
  570. Assert.Equal (keyChar, unicodeCharacter);
  571. }
  572. Assert.Equal (consoleKey, expectedVirtualKey);
  573. Assert.Equal (scanCode, expectedScanCode);
  574. var top = Application.Top;
  575. top.KeyPress += (e) => {
  576. var after = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  577. Assert.Equal (expectedRemapping, after);
  578. e.Handled = true;
  579. Application.RequestStop ();
  580. };
  581. var iterations = -1;
  582. Application.Iteration += () => {
  583. iterations++;
  584. if (iterations == 0) {
  585. Application.Driver.SendKeys ((char)mappedConsoleKey, ConsoleKey.Packet, shift, alt, control);
  586. }
  587. };
  588. Application.Run ();
  589. Application.Shutdown ();
  590. }
  591. public class PacketTest : IEnumerable, IEnumerable<object []> {
  592. public IEnumerator<object []> GetEnumerator ()
  593. {
  594. yield return new object [] { 'a', false, false, false, 'A', 30, Key.a, 'A', 30 };
  595. yield return new object [] { 'A', true, false, false, 'A', 30, Key.A | Key.ShiftMask, 'A', 30 };
  596. yield return new object [] { 'A', true, true, false, 'A', 30, Key.A | Key.ShiftMask | Key.AltMask, 'A', 30 };
  597. yield return new object [] { 'A', true, true, true, 'A', 30, Key.A | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 'A', 30 };
  598. yield return new object [] { 'z', false, false, false, 'Z', 44, Key.z, 'Z', 44 };
  599. yield return new object [] { 'Z', true, false, false, 'Z', 44, Key.Z | Key.ShiftMask, 'Z', 44 };
  600. yield return new object [] { 'Z', true, true, false, 'Z', 44, Key.Z | Key.ShiftMask | Key.AltMask, 'Z', 44 };
  601. yield return new object [] { 'Z', true, true, true, 'Z', 44, Key.Z | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 'Z', 44 };
  602. yield return new object [] { '英', false, false, false, '\0', 0, (Key)'英', '\0', 0 };
  603. yield return new object [] { '英', true, false, false, '\0', 0, (Key)'英' | Key.ShiftMask, '\0', 0 };
  604. yield return new object [] { '英', true, true, false, '\0', 0, (Key)'英' | Key.ShiftMask | Key.AltMask, '\0', 0 };
  605. yield return new object [] { '英', true, true, true, '\0', 0, (Key)'英' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '\0', 0 };
  606. yield return new object [] { '+', false, false, false, 187, 26, (Key)'+', 187, 26 };
  607. yield return new object [] { '*', true, false, false, 187, 26, (Key)'*' | Key.ShiftMask, 187, 26 };
  608. yield return new object [] { '+', true, true, false, 187, 26, (Key)'+' | Key.ShiftMask | Key.AltMask, 187, 26 };
  609. yield return new object [] { '+', true, true, true, 187, 26, (Key)'+' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 187, 26 };
  610. yield return new object [] { '1', false, false, false, '1', 2, Key.D1, '1', 2 };
  611. yield return new object [] { '!', true, false, false, '1', 2, (Key)'!' | Key.ShiftMask, '1', 2 };
  612. yield return new object [] { '1', true, true, false, '1', 2, Key.D1 | Key.ShiftMask | Key.AltMask, '1', 2 };
  613. yield return new object [] { '1', true, true, true, '1', 2, Key.D1 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '1', 2 };
  614. yield return new object [] { '1', false, true, true, '1', 2, Key.D1 | Key.AltMask | Key.CtrlMask, '1', 2 };
  615. yield return new object [] { '2', false, false, false, '2', 3, Key.D2, '2', 3 };
  616. yield return new object [] { '"', true, false, false, '2', 3, (Key)'"' | Key.ShiftMask, '2', 3 };
  617. yield return new object [] { '2', true, true, false, '2', 3, Key.D2 | Key.ShiftMask | Key.AltMask, '2', 3 };
  618. yield return new object [] { '2', true, true, true, '2', 3, Key.D2 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '2', 3 };
  619. yield return new object [] { '@', false, true, true, '2', 3, (Key)'@' | Key.AltMask | Key.CtrlMask, '2', 3 };
  620. yield return new object [] { '3', false, false, false, '3', 4, Key.D3, '3', 4 };
  621. yield return new object [] { '#', true, false, false, '3', 4, (Key)'#' | Key.ShiftMask, '3', 4 };
  622. yield return new object [] { '3', true, true, false, '3', 4, Key.D3 | Key.ShiftMask | Key.AltMask, '3', 4 };
  623. yield return new object [] { '3', true, true, true, '3', 4, Key.D3 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '3', 4 };
  624. yield return new object [] { '£', false, true, true, '3', 4, (Key)'£' | Key.AltMask | Key.CtrlMask, '3', 4 };
  625. yield return new object [] { '4', false, false, false, '4', 5, Key.D4, '4', 5 };
  626. yield return new object [] { '$', true, false, false, '4', 5, (Key)'$' | Key.ShiftMask, '4', 5 };
  627. yield return new object [] { '4', true, true, false, '4', 5, Key.D4 | Key.ShiftMask | Key.AltMask, '4', 5 };
  628. yield return new object [] { '4', true, true, true, '4', 5, Key.D4 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '4', 5 };
  629. yield return new object [] { '§', false, true, true, '4', 5, (Key)'§' | Key.AltMask | Key.CtrlMask, '4', 5 };
  630. yield return new object [] { '5', false, false, false, '5', 6, Key.D5, '5', 6 };
  631. yield return new object [] { '%', true, false, false, '5', 6, (Key)'%' | Key.ShiftMask, '5', 6 };
  632. yield return new object [] { '5', true, true, false, '5', 6, Key.D5 | Key.ShiftMask | Key.AltMask, '5', 6 };
  633. yield return new object [] { '5', true, true, true, '5', 6, Key.D5 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '5', 6 };
  634. yield return new object [] { '€', false, true, true, '5', 6, (Key)'€' | Key.AltMask | Key.CtrlMask, '5', 6 };
  635. yield return new object [] { '6', false, false, false, '6', 7, Key.D6, '6', 7 };
  636. yield return new object [] { '&', true, false, false, '6', 7, (Key)'&' | Key.ShiftMask, '6', 7 };
  637. yield return new object [] { '6', true, true, false, '6', 7, Key.D6 | Key.ShiftMask | Key.AltMask, '6', 7 };
  638. yield return new object [] { '6', true, true, true, '6', 7, Key.D6 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '6', 7 };
  639. yield return new object [] { '6', false, true, true, '6', 7, Key.D6 | Key.AltMask | Key.CtrlMask, '6', 7 };
  640. yield return new object [] { '7', false, false, false, '7', 8, Key.D7, '7', 8 };
  641. yield return new object [] { '/', true, false, false, '7', 8, (Key)'/' | Key.ShiftMask, '7', 8 };
  642. yield return new object [] { '7', true, true, false, '7', 8, Key.D7 | Key.ShiftMask | Key.AltMask, '7', 8 };
  643. yield return new object [] { '7', true, true, true, '7', 8, Key.D7 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '7', 8 };
  644. yield return new object [] { '{', false, true, true, '7', 8, (Key)'{' | Key.AltMask | Key.CtrlMask, '7', 8 };
  645. yield return new object [] { '8', false, false, false, '8', 9, Key.D8, '8', 9 };
  646. yield return new object [] { '(', true, false, false, '8', 9, (Key)'(' | Key.ShiftMask, '8', 9 };
  647. yield return new object [] { '8', true, true, false, '8', 9, Key.D8 | Key.ShiftMask | Key.AltMask, '8', 9 };
  648. yield return new object [] { '8', true, true, true, '8', 9, Key.D8 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '8', 9 };
  649. yield return new object [] { '[', false, true, true, '8', 9, (Key)'[' | Key.AltMask | Key.CtrlMask, '8', 9 };
  650. yield return new object [] { '9', false, false, false, '9', 10, Key.D9, '9', 10 };
  651. yield return new object [] { ')', true, false, false, '9', 10, (Key)')' | Key.ShiftMask, '9', 10 };
  652. yield return new object [] { '9', true, true, false, '9', 10, Key.D9 | Key.ShiftMask | Key.AltMask, '9', 10 };
  653. yield return new object [] { '9', true, true, true, '9', 10, Key.D9 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '9', 10 };
  654. yield return new object [] { ']', false, true, true, '9', 10, (Key)']' | Key.AltMask | Key.CtrlMask, '9', 10 };
  655. yield return new object [] { '0', false, false, false, '0', 11, Key.D0, '0', 11 };
  656. yield return new object [] { '=', true, false, false, '0', 11, (Key)'=' | Key.ShiftMask, '0', 11 };
  657. yield return new object [] { '0', true, true, false, '0', 11, Key.D0 | Key.ShiftMask | Key.AltMask, '0', 11 };
  658. yield return new object [] { '0', true, true, true, '0', 11, Key.D0 | Key.ShiftMask | Key.AltMask | Key.CtrlMask, '0', 11 };
  659. yield return new object [] { '}', false, true, true, '0', 11, (Key)'}' | Key.AltMask | Key.CtrlMask, '0', 11 };
  660. yield return new object [] { '\'', false, false, false, 219, 12, (Key)'\'', 219, 12 };
  661. yield return new object [] { '?', true, false, false, 219, 12, (Key)'?' | Key.ShiftMask, 219, 12 };
  662. yield return new object [] { '\'', true, true, false, 219, 12, (Key)'\'' | Key.ShiftMask | Key.AltMask, 219, 12 };
  663. yield return new object [] { '\'', true, true, true, 219, 12, (Key)'\'' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 219, 12 };
  664. yield return new object [] { '«', false, false, false, 221, 13, (Key)'«', 221, 13 };
  665. yield return new object [] { '»', true, false, false, 221, 13, (Key)'»' | Key.ShiftMask, 221, 13 };
  666. yield return new object [] { '«', true, true, false, 221, 13, (Key)'«' | Key.ShiftMask | Key.AltMask, 221, 13 };
  667. yield return new object [] { '«', true, true, true, 221, 13, (Key)'«' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 221, 13 };
  668. yield return new object [] { 'á', false, false, false, 'á', 0, (Key)'á', 'A', 30 };
  669. yield return new object [] { 'Á', true, false, false, 'Á', 0, (Key)'Á' | Key.ShiftMask, 'A', 30 };
  670. yield return new object [] { 'à', false, false, false, 'à', 0, (Key)'à', 'A', 30 };
  671. yield return new object [] { 'À', true, false, false, 'À', 0, (Key)'À' | Key.ShiftMask, 'A', 30 };
  672. yield return new object [] { 'é', false, false, false, 'é', 0, (Key)'é', 'E', 18 };
  673. yield return new object [] { 'É', true, false, false, 'É', 0, (Key)'É' | Key.ShiftMask, 'E', 18 };
  674. yield return new object [] { 'è', false, false, false, 'è', 0, (Key)'è', 'E', 18 };
  675. yield return new object [] { 'È', true, false, false, 'È', 0, (Key)'È' | Key.ShiftMask, 'E', 18 };
  676. yield return new object [] { 'í', false, false, false, 'í', 0, (Key)'í', 'I', 23 };
  677. yield return new object [] { 'Í', true, false, false, 'Í', 0, (Key)'Í' | Key.ShiftMask, 'I', 23 };
  678. yield return new object [] { 'ì', false, false, false, 'ì', 0, (Key)'ì', 'I', 23 };
  679. yield return new object [] { 'Ì', true, false, false, 'Ì', 0, (Key)'Ì' | Key.ShiftMask, 'I', 23 };
  680. yield return new object [] { 'ó', false, false, false, 'ó', 0, (Key)'ó', 'O', 24 };
  681. yield return new object [] { 'Ó', true, false, false, 'Ó', 0, (Key)'Ó' | Key.ShiftMask, 'O', 24 };
  682. yield return new object [] { 'ò', false, false, false, 'Ó', 0, (Key)'ò', 'O', 24 };
  683. yield return new object [] { 'Ò', true, false, false, 'Ò', 0, (Key)'Ò' | Key.ShiftMask, 'O', 24 };
  684. yield return new object [] { 'ú', false, false, false, 'ú', 0, (Key)'ú', 'U', 22 };
  685. yield return new object [] { 'Ú', true, false, false, 'Ú', 0, (Key)'Ú' | Key.ShiftMask, 'U', 22 };
  686. yield return new object [] { 'ù', false, false, false, 'ù', 0, (Key)'ù', 'U', 22 };
  687. yield return new object [] { 'Ù', true, false, false, 'Ù', 0, (Key)'Ù' | Key.ShiftMask, 'U', 22 };
  688. yield return new object [] { 'ö', false, false, false, 'ó', 0, (Key)'ö', 'O', 24 };
  689. yield return new object [] { 'Ö', true, false, false, 'Ó', 0, (Key)'Ö' | Key.ShiftMask, 'O', 24 };
  690. yield return new object [] { '<', false, false, false, 226, 86, (Key)'<', 226, 86 };
  691. yield return new object [] { '>', true, false, false, 226, 86, (Key)'>' | Key.ShiftMask, 226, 86 };
  692. yield return new object [] { '<', true, true, false, 226, 86, (Key)'<' | Key.ShiftMask | Key.AltMask, 226, 86 };
  693. yield return new object [] { '<', true, true, true, 226, 86, (Key)'<' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 226, 86 };
  694. yield return new object [] { 'ç', false, false, false, 192, 39, (Key)'ç', 192, 39 };
  695. yield return new object [] { 'Ç', true, false, false, 192, 39, (Key)'Ç' | Key.ShiftMask, 192, 39 };
  696. yield return new object [] { 'ç', true, true, false, 192, 39, (Key)'ç' | Key.ShiftMask | Key.AltMask, 192, 39 };
  697. yield return new object [] { 'ç', true, true, true, 192, 39, (Key)'ç' | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 192, 39 };
  698. yield return new object [] { '¨', false, true, true, 187, 26, (Key)'¨' | Key.AltMask | Key.CtrlMask, 187, 26 };
  699. yield return new object [] { (uint)Key.PageUp, false, false, false, 33, 73, Key.PageUp, 33, 73 };
  700. yield return new object [] { (uint)Key.PageUp, true, false, false, 33, 73, Key.PageUp | Key.ShiftMask, 33, 73 };
  701. yield return new object [] { (uint)Key.PageUp, true, true, false, 33, 73, Key.PageUp | Key.ShiftMask | Key.AltMask, 33, 73 };
  702. yield return new object [] { (uint)Key.PageUp, true, true, true, 33, 73, Key.PageUp | Key.ShiftMask | Key.AltMask | Key.CtrlMask, 33, 73 };
  703. }
  704. IEnumerator IEnumerable.GetEnumerator () => GetEnumerator ();
  705. }
  706. }
  707. }