ConsoleDriverTests.cs 33 KB

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