FakeDriver.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #nullable enable
  2. //
  3. // FakeDriver.cs: A fake IConsoleDriver for unit tests.
  4. //
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. namespace Terminal.Gui.Drivers;
  9. /// <summary>
  10. /// Implements a mock <see cref="IConsoleDriver"/> for unit testing. This driver simulates console behavior
  11. /// without requiring a real terminal, allowing for deterministic testing of Terminal.Gui applications.
  12. /// </summary>
  13. /// <remarks>
  14. /// <para>
  15. /// <see cref="FakeDriver"/> extends the legacy <see cref="ConsoleDriver"/> base class and is designed
  16. /// for backward compatibility with existing tests. It provides programmatic control over console state,
  17. /// including screen size, keyboard input, and output verification.
  18. /// </para>
  19. /// <para>
  20. /// <strong>Key Features:</strong>
  21. /// </para>
  22. /// <list type="bullet">
  23. /// <item>Programmatic screen resizing via <see cref="SetBufferSize"/> and <see cref="SetWindowSize"/></item>
  24. /// <item>Keyboard input simulation via <see cref="FakeConsole.PushMockKeyPress"/></item>
  25. /// <item>Mouse input simulation via <see cref="FakeConsole"/> methods</item>
  26. /// <item>Output verification via <see cref="ConsoleDriver.Contents"/> buffer inspection</item>
  27. /// <item>Event firing for resize, keyboard, and mouse events</item>
  28. /// </list>
  29. /// <para>
  30. /// <strong>Usage:</strong> Most tests should use <see cref="AutoInitShutdownAttribute"/> which automatically
  31. /// initializes Application with FakeDriver. For more control, create and configure FakeDriver instances directly.
  32. /// </para>
  33. /// <para>
  34. /// <strong>Thread Safety:</strong> FakeDriver is not thread-safe. Tests using this driver should not run
  35. /// in parallel with other tests that access driver state.
  36. /// </para>
  37. /// <para>
  38. /// For detailed usage examples and patterns, see the README.md file in this directory.
  39. /// </para>
  40. /// </remarks>
  41. /// <seealso cref="AutoInitShutdownAttribute"/>
  42. /// <seealso cref="FakeConsole"/>
  43. /// <seealso cref="FakeComponentFactory"/>
  44. public class FakeDriver : ConsoleDriver
  45. {
  46. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  47. public class Behaviors
  48. {
  49. public Behaviors (
  50. bool useFakeClipboard = false,
  51. bool fakeClipboardAlwaysThrowsNotSupportedException = false,
  52. bool fakeClipboardIsSupportedAlwaysTrue = false
  53. )
  54. {
  55. UseFakeClipboard = useFakeClipboard;
  56. FakeClipboardAlwaysThrowsNotSupportedException = fakeClipboardAlwaysThrowsNotSupportedException;
  57. FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
  58. // double check usage is correct
  59. Debug.Assert (useFakeClipboard == false && fakeClipboardAlwaysThrowsNotSupportedException == false);
  60. Debug.Assert (useFakeClipboard == false && fakeClipboardIsSupportedAlwaysTrue == false);
  61. }
  62. public bool FakeClipboardAlwaysThrowsNotSupportedException { get; internal set; }
  63. public bool FakeClipboardIsSupportedAlwaysFalse { get; internal set; }
  64. public bool UseFakeClipboard { get; internal set; }
  65. }
  66. public static Behaviors FakeBehaviors { get; } = new ();
  67. public override bool SupportsTrueColor => false;
  68. /// <inheritdoc />
  69. public override void WriteRaw (string ansi)
  70. {
  71. }
  72. public FakeDriver ()
  73. {
  74. // FakeDriver implies UnitTests
  75. RunningUnitTests = true;
  76. base.Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
  77. base.Rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
  78. if (FakeBehaviors.UseFakeClipboard)
  79. {
  80. Clipboard = new FakeClipboard (
  81. FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException,
  82. FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse
  83. );
  84. }
  85. else
  86. {
  87. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))
  88. {
  89. Clipboard = new WindowsClipboard ();
  90. }
  91. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  92. {
  93. Clipboard = new MacOSXClipboard ();
  94. }
  95. else
  96. {
  97. if (PlatformDetection.IsWSLPlatform ())
  98. {
  99. Clipboard = new WSLClipboard ();
  100. }
  101. else
  102. {
  103. Clipboard = new UnixClipboard ();
  104. }
  105. }
  106. }
  107. }
  108. public override void End ()
  109. {
  110. FakeConsole.ResetColor ();
  111. FakeConsole.Clear ();
  112. }
  113. public override void Init ()
  114. {
  115. FakeConsole.MockKeyPresses.Clear ();
  116. Cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
  117. Rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
  118. FakeConsole.Clear ();
  119. ResizeScreen ();
  120. CurrentAttribute = new Attribute (Color.White, Color.Black);
  121. }
  122. public override bool UpdateScreen ()
  123. {
  124. bool updated = false;
  125. int savedRow = FakeConsole.CursorTop;
  126. int savedCol = FakeConsole.CursorLeft;
  127. bool savedCursorVisible = FakeConsole.CursorVisible;
  128. var top = 0;
  129. var left = 0;
  130. int rows = Rows;
  131. int cols = Cols;
  132. var output = new StringBuilder ();
  133. var redrawAttr = new Attribute ();
  134. int lastCol = -1;
  135. for (int row = top; row < rows; row++)
  136. {
  137. if (!_dirtyLines! [row])
  138. {
  139. continue;
  140. }
  141. updated = true;
  142. FakeConsole.CursorTop = row;
  143. FakeConsole.CursorLeft = 0;
  144. _dirtyLines [row] = false;
  145. output.Clear ();
  146. for (int col = left; col < cols; col++)
  147. {
  148. lastCol = -1;
  149. var outputWidth = 0;
  150. for (; col < cols; col++)
  151. {
  152. if (!Contents! [row, col].IsDirty)
  153. {
  154. if (output.Length > 0)
  155. {
  156. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  157. }
  158. else if (lastCol == -1)
  159. {
  160. lastCol = col;
  161. }
  162. if (lastCol + 1 < cols)
  163. {
  164. lastCol++;
  165. }
  166. continue;
  167. }
  168. if (lastCol == -1)
  169. {
  170. lastCol = col;
  171. }
  172. Attribute attr = Contents [row, col].Attribute!.Value;
  173. // Performance: Only send the escape sequence if the attribute has changed.
  174. if (attr != redrawAttr)
  175. {
  176. redrawAttr = attr;
  177. FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor16 ();
  178. FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor16 ();
  179. }
  180. outputWidth++;
  181. Rune rune = Contents [row, col].Rune;
  182. output.Append (rune.ToString ());
  183. if (rune.IsSurrogatePair () && rune.GetColumns () < 2)
  184. {
  185. WriteToConsole (output, ref lastCol, row, ref outputWidth);
  186. FakeConsole.CursorLeft--;
  187. }
  188. Contents [row, col].IsDirty = false;
  189. }
  190. }
  191. if (output.Length > 0)
  192. {
  193. FakeConsole.CursorTop = row;
  194. FakeConsole.CursorLeft = lastCol;
  195. foreach (char c in output.ToString ())
  196. {
  197. FakeConsole.Write (c);
  198. }
  199. }
  200. }
  201. FakeConsole.CursorTop = 0;
  202. FakeConsole.CursorLeft = 0;
  203. //SetCursorVisibility (savedVisibility);
  204. void WriteToConsole (StringBuilder outputSb, ref int lastColumn, int row, ref int outputWidth)
  205. {
  206. FakeConsole.CursorTop = row;
  207. FakeConsole.CursorLeft = lastColumn;
  208. foreach (char c in outputSb.ToString ())
  209. {
  210. FakeConsole.Write (c);
  211. }
  212. outputSb.Clear ();
  213. lastColumn += outputWidth;
  214. outputWidth = 0;
  215. }
  216. FakeConsole.CursorTop = savedRow;
  217. FakeConsole.CursorLeft = savedCol;
  218. FakeConsole.CursorVisible = savedCursorVisible;
  219. return updated;
  220. }
  221. #region Color Handling
  222. ///// <remarks>
  223. ///// In the FakeDriver, colors are encoded as an int; same as DotNetDriver
  224. ///// However, the foreground color is stored in the most significant 16 bits,
  225. ///// and the background color is stored in the least significant 16 bits.
  226. ///// </remarks>
  227. //public override Attribute MakeColor (Color foreground, Color background)
  228. //{
  229. // // Encode the colors into the int value.
  230. // return new Attribute (
  231. // foreground: foreground,
  232. // background: background
  233. // );
  234. //}
  235. #endregion
  236. private KeyCode MapKey (ConsoleKeyInfo keyInfo)
  237. {
  238. switch (keyInfo.Key)
  239. {
  240. case ConsoleKey.Escape:
  241. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Esc);
  242. case ConsoleKey.Tab:
  243. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Tab);
  244. case ConsoleKey.Clear:
  245. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Clear);
  246. case ConsoleKey.Home:
  247. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Home);
  248. case ConsoleKey.End:
  249. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.End);
  250. case ConsoleKey.LeftArrow:
  251. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorLeft);
  252. case ConsoleKey.RightArrow:
  253. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorRight);
  254. case ConsoleKey.UpArrow:
  255. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorUp);
  256. case ConsoleKey.DownArrow:
  257. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorDown);
  258. case ConsoleKey.PageUp:
  259. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageUp);
  260. case ConsoleKey.PageDown:
  261. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageDown);
  262. case ConsoleKey.Enter:
  263. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Enter);
  264. case ConsoleKey.Spacebar:
  265. return ConsoleKeyMapping.MapToKeyCodeModifiers (
  266. keyInfo.Modifiers,
  267. keyInfo.KeyChar == 0
  268. ? KeyCode.Space
  269. : (KeyCode)keyInfo.KeyChar
  270. );
  271. case ConsoleKey.Backspace:
  272. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Backspace);
  273. case ConsoleKey.Delete:
  274. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Delete);
  275. case ConsoleKey.Insert:
  276. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Insert);
  277. case ConsoleKey.PrintScreen:
  278. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PrintScreen);
  279. case ConsoleKey.Oem1:
  280. case ConsoleKey.Oem2:
  281. case ConsoleKey.Oem3:
  282. case ConsoleKey.Oem4:
  283. case ConsoleKey.Oem5:
  284. case ConsoleKey.Oem6:
  285. case ConsoleKey.Oem7:
  286. case ConsoleKey.Oem8:
  287. case ConsoleKey.Oem102:
  288. case ConsoleKey.OemPeriod:
  289. case ConsoleKey.OemComma:
  290. case ConsoleKey.OemPlus:
  291. case ConsoleKey.OemMinus:
  292. if (keyInfo.KeyChar == 0)
  293. {
  294. return KeyCode.Null;
  295. }
  296. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
  297. }
  298. ConsoleKey key = keyInfo.Key;
  299. if (key >= ConsoleKey.A && key <= ConsoleKey.Z)
  300. {
  301. int delta = key - ConsoleKey.A;
  302. if (keyInfo.KeyChar != (uint)key)
  303. {
  304. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.Key);
  305. }
  306. if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control)
  307. || keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt)
  308. || keyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift))
  309. {
  310. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.A + delta));
  311. }
  312. char alphaBase = keyInfo.Modifiers != ConsoleModifiers.Shift ? 'A' : 'a';
  313. return (KeyCode)((uint)alphaBase + delta);
  314. }
  315. return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
  316. }
  317. private CursorVisibility _savedCursorVisibility;
  318. /// <inheritdoc/>
  319. public override bool GetCursorVisibility (out CursorVisibility visibility)
  320. {
  321. visibility = FakeConsole.CursorVisible
  322. ? CursorVisibility.Default
  323. : CursorVisibility.Invisible;
  324. return FakeConsole.CursorVisible;
  325. }
  326. /// <inheritdoc/>
  327. public override bool SetCursorVisibility (CursorVisibility visibility)
  328. {
  329. _savedCursorVisibility = visibility;
  330. return FakeConsole.CursorVisible = visibility == CursorVisibility.Default;
  331. }
  332. /// <inheritdoc/>
  333. private bool EnsureCursorVisibility ()
  334. {
  335. if (!(Col >= 0 && Row >= 0 && Col < Cols && Row < Rows))
  336. {
  337. GetCursorVisibility (out CursorVisibility cursorVisibility);
  338. _savedCursorVisibility = cursorVisibility;
  339. SetCursorVisibility (CursorVisibility.Invisible);
  340. return false;
  341. }
  342. SetCursorVisibility (_savedCursorVisibility);
  343. return FakeConsole.CursorVisible;
  344. }
  345. private AnsiResponseParser _parser = new ();
  346. /// <inheritdoc />
  347. internal override IAnsiResponseParser GetParser () => _parser;
  348. /// <summary>
  349. /// Sets the size of the fake console screen/buffer for testing purposes. This method updates
  350. /// the driver's dimensions (<see cref="ConsoleDriver.Cols"/> and <see cref="ConsoleDriver.Rows"/>),
  351. /// clears the contents, and fires the <see cref="ConsoleDriver.SizeChanged"/> event.
  352. /// </summary>
  353. /// <remarks>
  354. /// <para>
  355. /// This method is intended for use in unit tests to simulate terminal resize events.
  356. /// For FakeDriver, the buffer size and window size are always the same (there is no scrollback).
  357. /// </para>
  358. /// <para>
  359. /// When called, this method:
  360. /// <list type="number">
  361. /// <item>Updates the <see cref="FakeConsole"/> buffer size</item>
  362. /// <item>Sets <see cref="ConsoleDriver.Cols"/> and <see cref="ConsoleDriver.Rows"/> to the new dimensions</item>
  363. /// <item>Updates the window size to match</item>
  364. /// <item>Clears the screen contents</item>
  365. /// <item>Fires the <see cref="ConsoleDriver.SizeChanged"/> event</item>
  366. /// </list>
  367. /// </para>
  368. /// <para>
  369. /// <strong>Thread Safety:</strong> This method is not thread-safe. Tests using this method
  370. /// should ensure they are not accessing the driver concurrently.
  371. /// </para>
  372. /// <para>
  373. /// <strong>Relationship to Screen property:</strong> After calling this method,
  374. /// <see cref="ConsoleDriver.Screen"/> will return a rectangle with origin (0,0) and size (width, height).
  375. /// </para>
  376. /// </remarks>
  377. /// <param name="width">The new width in columns.</param>
  378. /// <param name="height">The new height in rows.</param>
  379. /// <example>
  380. /// <code>
  381. /// // Simulate a terminal resize to 120x30
  382. /// var driver = new FakeDriver();
  383. /// driver.SetBufferSize(120, 30);
  384. /// Assert.Equal(120, driver.Cols);
  385. /// Assert.Equal(30, driver.Rows);
  386. /// </code>
  387. /// </example>
  388. public void SetBufferSize (int width, int height)
  389. {
  390. FakeConsole.SetBufferSize (width, height);
  391. Cols = width;
  392. Rows = height;
  393. SetWindowSize (width, height);
  394. ProcessResize ();
  395. }
  396. /// <summary>
  397. /// Sets the window size of the fake console. For FakeDriver, this is functionally equivalent to
  398. /// <see cref="SetBufferSize"/> as the fake console does not support scrollback (window size == buffer size).
  399. /// </summary>
  400. /// <remarks>
  401. /// <para>
  402. /// This method exists for API compatibility with real console drivers, but in FakeDriver,
  403. /// the window size and buffer size are always kept in sync. Calling this method will update
  404. /// both the window and buffer to the specified size.
  405. /// </para>
  406. /// <para>
  407. /// Prefer using <see cref="SetBufferSize"/> for clarity in test code, as it more accurately
  408. /// describes what's happening (setting the entire screen size for the fake driver).
  409. /// </para>
  410. /// </remarks>
  411. /// <param name="width">The new width in columns.</param>
  412. /// <param name="height">The new height in rows.</param>
  413. /// <seealso cref="SetBufferSize"/>
  414. public void SetWindowSize (int width, int height)
  415. {
  416. FakeConsole.SetWindowSize (width, height);
  417. if (width != Cols || height != Rows)
  418. {
  419. SetBufferSize (width, height);
  420. Cols = width;
  421. Rows = height;
  422. }
  423. ProcessResize ();
  424. }
  425. public void SetWindowPosition (int left, int top)
  426. {
  427. if (Left > 0 || Top > 0)
  428. {
  429. Left = 0;
  430. Top = 0;
  431. }
  432. FakeConsole.SetWindowPosition (Left, Top);
  433. }
  434. private void ProcessResize ()
  435. {
  436. ResizeScreen ();
  437. ClearContents ();
  438. OnSizeChanged (new SizeChangedEventArgs (new (Cols, Rows)));
  439. }
  440. public virtual void ResizeScreen ()
  441. {
  442. if (FakeConsole.WindowHeight > 0)
  443. {
  444. // Can raise an exception while it is still resizing.
  445. try
  446. {
  447. FakeConsole.CursorTop = 0;
  448. FakeConsole.CursorLeft = 0;
  449. FakeConsole.WindowTop = 0;
  450. FakeConsole.WindowLeft = 0;
  451. }
  452. catch (IOException)
  453. {
  454. return;
  455. }
  456. catch (ArgumentOutOfRangeException)
  457. {
  458. return;
  459. }
  460. }
  461. // CONCURRENCY: Unsynchronized access to Clip is not safe.
  462. Clip = new (Screen);
  463. }
  464. public override void UpdateCursor ()
  465. {
  466. if (!EnsureCursorVisibility ())
  467. {
  468. return;
  469. }
  470. // Prevents the exception of size changing during resizing.
  471. try
  472. {
  473. // BUGBUG: Why is this using BufferWidth/Height and now Cols/Rows?
  474. if (Col >= 0 && Col < FakeConsole.BufferWidth && Row >= 0 && Row < FakeConsole.BufferHeight)
  475. {
  476. FakeConsole.SetCursorPosition (Col, Row);
  477. }
  478. }
  479. catch (IOException)
  480. { }
  481. catch (ArgumentOutOfRangeException)
  482. { }
  483. }
  484. #region Not Implemented
  485. public override void Suspend ()
  486. {
  487. //throw new NotImplementedException ();
  488. }
  489. #endregion
  490. public class FakeClipboard : ClipboardBase
  491. {
  492. public Exception? FakeException { get; set; }
  493. private readonly bool _isSupportedAlwaysFalse;
  494. private string _contents = string.Empty;
  495. public FakeClipboard (
  496. bool fakeClipboardThrowsNotSupportedException = false,
  497. bool isSupportedAlwaysFalse = false
  498. )
  499. {
  500. _isSupportedAlwaysFalse = isSupportedAlwaysFalse;
  501. if (fakeClipboardThrowsNotSupportedException)
  502. {
  503. FakeException = new NotSupportedException ("Fake clipboard exception");
  504. }
  505. }
  506. public override bool IsSupported => !_isSupportedAlwaysFalse;
  507. protected override string GetClipboardDataImpl ()
  508. {
  509. if (FakeException is { })
  510. {
  511. throw FakeException;
  512. }
  513. return _contents;
  514. }
  515. protected override void SetClipboardDataImpl (string? text)
  516. {
  517. if (FakeException is { })
  518. {
  519. throw FakeException;
  520. }
  521. _contents = text ?? throw new ArgumentNullException (nameof (text));
  522. }
  523. }
  524. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  525. }