GuiTestContext.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System.Diagnostics;
  2. using System.Drawing;
  3. using System.Text;
  4. using Microsoft.Extensions.Logging;
  5. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  6. namespace TerminalGuiFluentTesting;
  7. /// <summary>
  8. /// Fluent API context for testing a Terminal.Gui application. Create
  9. /// an instance using <see cref="With"/> static class.
  10. /// </summary>
  11. public partial class GuiTestContext : IDisposable
  12. {
  13. // ===== Threading & Synchronization =====
  14. private readonly CancellationTokenSource _runCancellationTokenSource = new ();
  15. private readonly CancellationTokenSource? _timeoutCts;
  16. private readonly Task? _runTask;
  17. private readonly SemaphoreSlim _booting;
  18. private readonly object _cancellationLock = new ();
  19. private volatile bool _finished;
  20. // ===== Exception Handling =====
  21. private readonly object _backgroundExceptionLock = new ();
  22. private Exception? _backgroundException;
  23. // ===== Driver & Application State =====
  24. private readonly FakeInput _fakeInput = new ();
  25. private IOutput? _output;
  26. private SizeMonitorImpl? _sizeMonitor;
  27. private ApplicationImpl? _applicationImpl;
  28. private TestDriver _driverType;
  29. // ===== Application State Preservation (for restoration) =====
  30. private IApplication? _originalApplicationInstance;
  31. private ILogger? _originalLogger;
  32. // ===== Test Configuration =====
  33. private readonly bool _runApplication;
  34. private TimeSpan _timeout;
  35. // ===== Logging =====
  36. private readonly object _logsLock = new ();
  37. private readonly TextWriter? _logWriter;
  38. private StringBuilder? _logsSb;
  39. /// <summary>
  40. /// Constructor for tests that only need Application.Init without running the main loop.
  41. /// Uses the driver's default screen size instead of forcing a specific size.
  42. /// </summary>
  43. public GuiTestContext (TestDriver driver, TextWriter? logWriter = null, TimeSpan? timeout = null)
  44. {
  45. _logWriter = logWriter;
  46. _runApplication = false;
  47. _booting = new (0, 1);
  48. _timeoutCts = new CancellationTokenSource (timeout ?? TimeSpan.FromSeconds (10)); // NEW
  49. // Don't force a size - let the driver determine it
  50. CommonInit (0, 0, driver, timeout);
  51. try
  52. {
  53. InitializeApplication ();
  54. _booting.Release ();
  55. // After Init, Application.Screen should be set by the driver
  56. if (Application.Screen == Rectangle.Empty)
  57. {
  58. throw new InvalidOperationException (
  59. "Driver bug: Application.Screen is empty after Init. The driver should set the screen size during Init.");
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. lock (_backgroundExceptionLock) // NEW: Thread-safe exception handling
  65. {
  66. _backgroundException = ex;
  67. }
  68. if (_logWriter != null)
  69. {
  70. WriteOutLogs (_logWriter);
  71. }
  72. throw new ("Application initialization failed", ex);
  73. }
  74. lock (_backgroundExceptionLock) // NEW: Thread-safe check
  75. {
  76. if (_backgroundException != null)
  77. {
  78. throw new ("Application initialization failed", _backgroundException);
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Constructor for tests that need to run the application with Application.Run.
  84. /// </summary>
  85. internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height, TestDriver driver, TextWriter? logWriter = null, TimeSpan? timeout = null)
  86. {
  87. _logWriter = logWriter;
  88. _runApplication = true;
  89. _booting = new (0, 1);
  90. CommonInit (width, height, driver, timeout);
  91. // Start the application in a background thread
  92. _runTask = Task.Run (
  93. () =>
  94. {
  95. try
  96. {
  97. InitializeApplication ();
  98. _booting.Release ();
  99. Toplevel t = topLevelBuilder ();
  100. t.Closed += (s, e) => { Finished = true; };
  101. Application.Run (t); // This will block, but it's on a background thread now
  102. t.Dispose ();
  103. Logging.Trace ("Application.Run completed");
  104. Application.Shutdown ();
  105. _runCancellationTokenSource.Cancel ();
  106. }
  107. catch (OperationCanceledException)
  108. { }
  109. catch (Exception ex)
  110. {
  111. _backgroundException = ex;
  112. _fakeInput.ExternalCancellationTokenSource!.Cancel ();
  113. }
  114. finally
  115. {
  116. CleanupApplication ();
  117. if (_logWriter != null)
  118. {
  119. WriteOutLogs (_logWriter);
  120. }
  121. }
  122. },
  123. _runCancellationTokenSource.Token);
  124. // Wait for booting to complete with a timeout to avoid hangs
  125. if (!_booting.WaitAsync (_timeout).Result)
  126. {
  127. throw new TimeoutException ($"Application failed to start within {_timeout}ms.");
  128. }
  129. ResizeConsole (width, height);
  130. if (_backgroundException is { })
  131. {
  132. throw new ("Application crashed", _backgroundException);
  133. }
  134. }
  135. private void InitializeApplication ()
  136. {
  137. ApplicationImpl.ChangeInstance (_applicationImpl);
  138. _applicationImpl?.Init (null, GetDriverName ());
  139. }
  140. /// <summary>
  141. /// Common initialization for both constructors.
  142. /// </summary>
  143. private void CommonInit (int width, int height, TestDriver driverType, TimeSpan? timeout)
  144. {
  145. _timeout = timeout ?? TimeSpan.FromSeconds (10);
  146. _originalApplicationInstance = ApplicationImpl.Instance;
  147. _originalLogger = Logging.Logger;
  148. _logsSb = new ();
  149. _driverType = driverType;
  150. ILogger logger = LoggerFactory.Create (builder =>
  151. builder.SetMinimumLevel (LogLevel.Trace)
  152. .AddProvider (
  153. new TextWriterLoggerProvider (
  154. new ThreadSafeStringWriter (_logsSb, _logsLock))))
  155. .CreateLogger ("Test Logging");
  156. Logging.Logger = logger;
  157. // ✅ Link _runCancellationTokenSource with a timeout
  158. // This creates a token that responds to EITHER the run cancellation OR timeout
  159. _fakeInput.ExternalCancellationTokenSource =
  160. CancellationTokenSource.CreateLinkedTokenSource (
  161. _runCancellationTokenSource.Token,
  162. new CancellationTokenSource (_timeout).Token);
  163. // Now when InputImpl.Run receives this ExternalCancellationTokenSource,
  164. // it will create ANOTHER linked token internally that combines:
  165. // - Its own runCancellationToken parameter
  166. // - The ExternalCancellationTokenSource (which is already linked)
  167. // This creates a chain: any of these triggers will stop input:
  168. // 1. _runCancellationTokenSource.Cancel() (normal stop)
  169. // 2. Timeout expires (test timeout)
  170. // 3. Direct cancel of ExternalCancellationTokenSource (hard stop/error)
  171. // Remove frame limit
  172. Application.MaximumIterationsPerSecond = ushort.MaxValue;
  173. //// Only set size if explicitly provided (width and height > 0)
  174. //if (width > 0 && height > 0)
  175. //{
  176. // _output.SetSize (width, height);
  177. //}
  178. IComponentFactory? cf = null;
  179. // TODO: As each drivers' IInput/IOutput implementations are made testable (e.g.
  180. // TODO: safely injectable/mocked), we can expand this switch to use them.
  181. switch (driverType)
  182. {
  183. case TestDriver.DotNet:
  184. _output = new FakeOutput ();
  185. _sizeMonitor = new (_output);
  186. cf = new FakeComponentFactory (_fakeInput, _output, _sizeMonitor);
  187. break;
  188. case TestDriver.Windows:
  189. _output = new FakeOutput ();
  190. _sizeMonitor = new (_output);
  191. cf = new FakeComponentFactory (_fakeInput, _output, _sizeMonitor);
  192. break;
  193. case TestDriver.Unix:
  194. _output = new FakeOutput ();
  195. _sizeMonitor = new (_output);
  196. cf = new FakeComponentFactory (_fakeInput, _output, _sizeMonitor);
  197. break;
  198. case TestDriver.Fake:
  199. _output = new FakeOutput ();
  200. _sizeMonitor = new (_output);
  201. cf = new FakeComponentFactory (_fakeInput, _output, _sizeMonitor);
  202. break;
  203. }
  204. _applicationImpl = new (cf!);
  205. Logging.Trace ($"Driver: {GetDriverName ()}. Timeout: {_timeout}");
  206. }
  207. private string GetDriverName ()
  208. {
  209. return _driverType switch
  210. {
  211. TestDriver.Windows => "windows",
  212. TestDriver.DotNet => "dotnet",
  213. TestDriver.Unix => "unix",
  214. TestDriver.Fake => "fake",
  215. _ =>
  216. throw new ArgumentOutOfRangeException ()
  217. };
  218. }
  219. /// <summary>
  220. /// Gets whether the application has finished running; aka Stop has been called and the main loop has exited.
  221. /// </summary>
  222. public bool Finished
  223. {
  224. get => _finished;
  225. private set => _finished = value;
  226. }
  227. /// <summary>
  228. /// Performs the supplied <paramref name="doAction"/> immediately.
  229. /// Enables running commands without breaking the Fluent API calls.
  230. /// </summary>
  231. /// <param name="doAction"></param>
  232. /// <returns></returns>
  233. public GuiTestContext Then (Action doAction)
  234. {
  235. try
  236. {
  237. Logging.Trace ($"Invoking action via WaitIteration");
  238. WaitIteration (doAction);
  239. }
  240. catch (Exception ex)
  241. {
  242. _backgroundException = ex;
  243. HardStop ();
  244. throw;
  245. }
  246. return this;
  247. }
  248. /// <summary>
  249. /// Waits until the end of the current iteration of the main loop. Optionally
  250. /// running a given <paramref name="action"/> action on the UI thread at that time.
  251. /// </summary>
  252. /// <param name="action"></param>
  253. /// <returns></returns>
  254. public GuiTestContext WaitIteration (Action? action = null)
  255. {
  256. // If application has already exited don't wait!
  257. if (Finished || _runCancellationTokenSource.Token.IsCancellationRequested || _fakeInput.ExternalCancellationTokenSource!.Token.IsCancellationRequested)
  258. {
  259. Logging.Warning ("WaitIteration called after context was stopped");
  260. return this;
  261. }
  262. if (Thread.CurrentThread.ManagedThreadId == Application.MainThreadId)
  263. {
  264. throw new NotSupportedException ("Cannot WaitIteration during Invoke");
  265. }
  266. Logging.Trace ($"WaitIteration started");
  267. action ??= () => { };
  268. CancellationTokenSource ctsActionCompleted = new ();
  269. Application.Invoke (() =>
  270. {
  271. try
  272. {
  273. action ();
  274. //Logging.Trace ("Action completed");
  275. ctsActionCompleted.Cancel ();
  276. }
  277. catch (Exception e)
  278. {
  279. Logging.Warning ($"Action failed with exception: {e}");
  280. _backgroundException = e;
  281. _fakeInput.ExternalCancellationTokenSource?.Cancel ();
  282. }
  283. });
  284. // Blocks until either the token or the hardStopToken is cancelled.
  285. // With linked tokens, we only need to wait on _runCancellationTokenSource and ctsLocal
  286. // ExternalCancellationTokenSource is redundant because it's linked to _runCancellationTokenSource
  287. WaitHandle.WaitAny (
  288. [
  289. _runCancellationTokenSource.Token.WaitHandle,
  290. ctsActionCompleted.Token.WaitHandle
  291. ]);
  292. // Logging.Trace ($"Return from WaitIteration");
  293. return this;
  294. }
  295. public GuiTestContext WaitUntil (Func<bool> condition)
  296. {
  297. GuiTestContext? c = null;
  298. var sw = Stopwatch.StartNew ();
  299. //Logging.Trace ($"WaitUntil started with timeout {_timeout}");
  300. while (!condition ())
  301. {
  302. if (sw.Elapsed > _timeout)
  303. {
  304. throw new TimeoutException ($"Failed to reach condition within {_timeout}ms");
  305. }
  306. c = WaitIteration ();
  307. }
  308. return c ?? this;
  309. }
  310. /// <summary>
  311. /// Returns the last set position of the cursor.
  312. /// </summary>
  313. /// <returns></returns>
  314. public Point GetCursorPosition () { return _output!.GetCursorPosition (); }
  315. /// <summary>
  316. /// Simulates changing the console size e.g. by resizing window in your operating system
  317. /// </summary>
  318. /// <param name="width">new Width for the console.</param>
  319. /// <param name="height">new Height for the console.</param>
  320. /// <returns></returns>
  321. public GuiTestContext ResizeConsole (int width, int height) { return WaitIteration (() => { Application.Driver!.SetScreenSize (width, height); }); }
  322. public GuiTestContext ScreenShot (string title, TextWriter? writer)
  323. {
  324. //Logging.Trace ($"{title}");
  325. return WaitIteration (() =>
  326. {
  327. writer?.WriteLine (title + ":");
  328. var text = Application.ToString ();
  329. writer?.WriteLine (text);
  330. });
  331. }
  332. /// <summary>
  333. /// Stops the application and waits for the background thread to exit.
  334. /// </summary>
  335. public GuiTestContext Stop ()
  336. {
  337. Logging.Trace ($"Stopping application for driver: {GetDriverName ()}");
  338. if (_runTask is null || _runTask.IsCompleted)
  339. {
  340. // If we didn't run the application, just cleanup
  341. if (!_runApplication && !Finished)
  342. {
  343. try
  344. {
  345. Application.Shutdown ();
  346. }
  347. catch
  348. {
  349. // Ignore errors during shutdown
  350. }
  351. CleanupApplication ();
  352. }
  353. return this;
  354. }
  355. WaitIteration (() => { Application.RequestStop (); });
  356. // Wait for the application to stop, but give it a 1-second timeout
  357. const int WAIT_TIMEOUT_MS = 1000;
  358. if (!_runTask.Wait (TimeSpan.FromMilliseconds (WAIT_TIMEOUT_MS)))
  359. {
  360. _runCancellationTokenSource.Cancel ();
  361. // No need to manually cancel ExternalCancellationTokenSource
  362. // App is having trouble shutting down, try sending some more shutdown stuff from this thread.
  363. // If this doesn't work there will be test failures as the main loop continues to run during next test.
  364. try
  365. {
  366. Application.RequestStop ();
  367. Application.Shutdown ();
  368. }
  369. catch (Exception ex)
  370. {
  371. Logging.Critical ($"Application failed to stop in {WAIT_TIMEOUT_MS}. Then shutdown threw {ex}");
  372. }
  373. finally
  374. {
  375. Logging.Critical ($"Application failed to stop in {WAIT_TIMEOUT_MS}. Exception was thrown: {_backgroundException}");
  376. }
  377. }
  378. _runCancellationTokenSource.Cancel ();
  379. if (_backgroundException != null)
  380. {
  381. Logging.Critical ($"Exception occurred: {_backgroundException}");
  382. //throw _ex; // Propagate any exception that happened in the background task
  383. }
  384. return this;
  385. }
  386. /// <summary>
  387. /// Hard stops the application and waits for the background thread to exit.HardStop is used by the source generator for
  388. /// wrapping Xunit assertions.
  389. /// </summary>
  390. public void HardStop (Exception? ex = null)
  391. {
  392. if (ex != null)
  393. {
  394. _backgroundException = ex;
  395. }
  396. Logging.Critical ($"HardStop called with exception: {_backgroundException}");
  397. // With linked tokens, just cancelling ExternalCancellationTokenSource
  398. // will cascade to stop everything
  399. _fakeInput.ExternalCancellationTokenSource?.Cancel ();
  400. WriteOutLogs (_logWriter);
  401. Stop ();
  402. }
  403. /// <summary>
  404. /// Writes all Terminal.Gui engine logs collected so far to the <paramref name="writer"/>
  405. /// </summary>
  406. /// <param name="writer"></param>
  407. /// <returns></returns>
  408. public GuiTestContext WriteOutLogs (TextWriter? writer)
  409. {
  410. if (writer is null)
  411. {
  412. return this;
  413. }
  414. lock (_logsLock)
  415. {
  416. writer.WriteLine (_logsSb!.ToString ());
  417. }
  418. return this; //WaitIteration();
  419. }
  420. internal void Fail (string reason)
  421. {
  422. Logging.Error ($"{reason}");
  423. throw new (reason);
  424. }
  425. private void CleanupApplication ()
  426. {
  427. Logging.Trace ("CleanupApplication");
  428. _fakeInput.ExternalCancellationTokenSource = null;
  429. Application.ResetState (true);
  430. ApplicationImpl.ChangeInstance (_originalApplicationInstance);
  431. Logging.Logger = _originalLogger;
  432. Finished = true;
  433. Application.MaximumIterationsPerSecond = Application.DefaultMaximumIterationsPerSecond;
  434. }
  435. /// <summary>
  436. /// Cleanup to avoid state bleed between tests
  437. /// </summary>
  438. public void Dispose ()
  439. {
  440. Logging.Trace ($"Disposing GuiTestContext");
  441. Stop ();
  442. bool shouldThrow = false;
  443. Exception? exToThrow = null;
  444. lock (_cancellationLock) // NEW: Thread-safe check
  445. {
  446. if (_fakeInput.ExternalCancellationTokenSource is { IsCancellationRequested: true })
  447. {
  448. shouldThrow = true;
  449. lock (_backgroundExceptionLock)
  450. {
  451. exToThrow = _backgroundException;
  452. }
  453. }
  454. // ✅ Dispose the linked token source
  455. _fakeInput.ExternalCancellationTokenSource?.Dispose ();
  456. }
  457. _timeoutCts?.Dispose (); // NEW: Dispose timeout CTS
  458. _runCancellationTokenSource?.Dispose ();
  459. _fakeInput.Dispose ();
  460. _output?.Dispose ();
  461. _booting.Dispose ();
  462. if (shouldThrow)
  463. {
  464. throw new ("Application was hard stopped...", exToThrow);
  465. }
  466. }
  467. }