ApplicationTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace ApplicationTests;
  4. /// <summary>
  5. /// Parallelizable tests for IApplication that don't require the main event loop.
  6. /// Tests using the modern non-static IApplication API.
  7. /// </summary>
  8. public class ApplicationTests (ITestOutputHelper output)
  9. {
  10. private readonly ITestOutputHelper _output = output;
  11. [Fact]
  12. public void AddTimeout_Fires ()
  13. {
  14. IApplication app = Application.Create ();
  15. app.Init ("fake");
  16. uint timeoutTime = 100;
  17. var timeoutFired = false;
  18. // Setup a timeout that will fire
  19. app.AddTimeout (
  20. TimeSpan.FromMilliseconds (timeoutTime),
  21. () =>
  22. {
  23. timeoutFired = true;
  24. // Return false so the timer does not repeat
  25. return false;
  26. }
  27. );
  28. // The timeout has not fired yet
  29. Assert.False (timeoutFired);
  30. // Block the thread to prove the timeout does not fire on a background thread
  31. Thread.Sleep ((int)timeoutTime * 2);
  32. Assert.False (timeoutFired);
  33. app.StopAfterFirstIteration = true;
  34. app.Run<Runnable> ();
  35. // The timeout should have fired
  36. Assert.True (timeoutFired);
  37. app.Dispose ();
  38. }
  39. [Fact]
  40. public void Begin_Null_Runnable_Throws ()
  41. {
  42. IApplication app = Application.Create ();
  43. app.Init ("fake");
  44. // Test null Runnable
  45. Assert.Throws<ArgumentNullException> (() => app.Begin (null!));
  46. app.Dispose ();
  47. }
  48. [Fact]
  49. public void Begin_Sets_Application_Top_To_Console_Size ()
  50. {
  51. IApplication app = Application.Create ();
  52. app.Init ("fake");
  53. Assert.Null (app.TopRunnableView);
  54. app.Driver!.SetScreenSize (80, 25);
  55. Runnable top = new ();
  56. SessionToken? token = app.Begin (top);
  57. Assert.Equal (new (0, 0, 80, 25), app.TopRunnableView!.Frame);
  58. app.Driver!.SetScreenSize (5, 5);
  59. app.LayoutAndDraw ();
  60. Assert.Equal (new (0, 0, 5, 5), app.TopRunnableView!.Frame);
  61. if (token is { })
  62. {
  63. app.End (token);
  64. }
  65. top.Dispose ();
  66. app.Dispose ();
  67. }
  68. [Fact]
  69. public void Init_Null_Driver_Should_Pick_A_Driver ()
  70. {
  71. IApplication app = Application.Create ();
  72. app.Init ();
  73. Assert.NotNull (app.Driver);
  74. app.Dispose ();
  75. }
  76. [Fact]
  77. public void Init_Dispose_Cleans_Up ()
  78. {
  79. IApplication app = Application.Create ();
  80. app.Init ("fake");
  81. app.Dispose ();
  82. #if DEBUG_IDISPOSABLE
  83. // Validate there are no outstanding Responder-based instances
  84. // after cleanup
  85. // Note: We can't check View.Instances in parallel tests as it's a static field
  86. // that would be shared across parallel test runs
  87. #endif
  88. }
  89. [Fact]
  90. public void Init_Dispose_Fire_InitializedChanged ()
  91. {
  92. var initialized = false;
  93. var Dispose = false;
  94. IApplication app = Application.Create ();
  95. app.InitializedChanged += OnApplicationOnInitializedChanged;
  96. app.Init (driverName: "fake");
  97. Assert.True (initialized);
  98. Assert.False (Dispose);
  99. app.Dispose ();
  100. Assert.True (initialized);
  101. Assert.True (Dispose);
  102. app.InitializedChanged -= OnApplicationOnInitializedChanged;
  103. return;
  104. void OnApplicationOnInitializedChanged (object? s, EventArgs<bool> a)
  105. {
  106. if (a.Value)
  107. {
  108. initialized = true;
  109. }
  110. else
  111. {
  112. Dispose = true;
  113. }
  114. }
  115. }
  116. [Fact]
  117. public void Init_KeyBindings_Are_Not_Reset ()
  118. {
  119. IApplication app = Application.Create ();
  120. // Set via Keyboard property (modern API)
  121. app.Keyboard.QuitKey = Key.Q;
  122. Assert.Equal (Key.Q, app.Keyboard.QuitKey);
  123. app.Init ("fake");
  124. Assert.Equal (Key.Q, app.Keyboard.QuitKey);
  125. app.Dispose ();
  126. }
  127. [Fact]
  128. public void Init_NoParam_ForceDriver_Works ()
  129. {
  130. using IApplication app = Application.Create ();
  131. app.ForceDriver = "fake";
  132. // Note: Init() without params picks up driver configuration
  133. app.Init ();
  134. Assert.Equal ("fake", app.Driver!.GetName ());
  135. }
  136. [Fact]
  137. public void Init_Dispose_Resets_Instance_Properties ()
  138. {
  139. IApplication app = Application.Create ();
  140. // Init the app
  141. app.Init (driverName: "fake");
  142. // Verify initialized
  143. Assert.True (app.Initialized);
  144. Assert.NotNull (app.Driver);
  145. // Dispose cleans up
  146. app.Dispose ();
  147. // Check reset state on the instance
  148. CheckReset (app);
  149. // Create a new instance and set values
  150. app = Application.Create ();
  151. app.Init ("fake");
  152. app.StopAfterFirstIteration = true;
  153. app.Keyboard.PrevTabGroupKey = Key.A;
  154. app.Keyboard.NextTabGroupKey = Key.B;
  155. app.Keyboard.QuitKey = Key.C;
  156. app.Keyboard.KeyBindings.Add (Key.D, Command.Cancel);
  157. app.Mouse.CachedViewsUnderMouse.Clear ();
  158. app.Mouse.LastMousePosition = new Point (1, 1);
  159. // Dispose and check reset
  160. app.Dispose ();
  161. CheckReset (app);
  162. return;
  163. void CheckReset (IApplication application)
  164. {
  165. // Check that all fields and properties are reset on the instance
  166. // Public Properties
  167. Assert.Null (application.TopRunnableView);
  168. Assert.Null (application.Mouse.MouseGrabView);
  169. Assert.Null (application.Driver);
  170. Assert.False (application.StopAfterFirstIteration);
  171. // Internal properties
  172. Assert.False (application.Initialized);
  173. Assert.Null (application.MainThreadId);
  174. Assert.Empty (application.Mouse.CachedViewsUnderMouse);
  175. }
  176. }
  177. [Fact]
  178. public void Internal_Properties_Correct ()
  179. {
  180. IApplication app = Application.Create ();
  181. app.Init ("fake");
  182. Assert.True (app.Initialized);
  183. Assert.Null (app.TopRunnableView);
  184. SessionToken? rs = app.Begin (new Runnable<bool> ());
  185. Assert.Equal (app.TopRunnable, rs!.Runnable);
  186. Assert.Null (app.Mouse.MouseGrabView); // public
  187. app.Dispose ();
  188. }
  189. [Fact]
  190. public void Invoke_Adds_Idle ()
  191. {
  192. IApplication app = Application.Create ();
  193. app.Init ("fake");
  194. Runnable top = new ();
  195. SessionToken? rs = app.Begin (top);
  196. var actionCalled = 0;
  197. app.Invoke ((_) => { actionCalled++; });
  198. app.TimedEvents!.RunTimers ();
  199. Assert.Equal (1, actionCalled);
  200. top.Dispose ();
  201. app.Dispose ();
  202. }
  203. [Fact]
  204. public void Run_Iteration_Fires ()
  205. {
  206. var iteration = 0;
  207. IApplication app = Application.Create ();
  208. app.Init ("fake");
  209. app.Iteration += Application_Iteration;
  210. app.Run<Runnable> ();
  211. app.Iteration -= Application_Iteration;
  212. Assert.Equal (1, iteration);
  213. app.Dispose ();
  214. return;
  215. void Application_Iteration (object? sender, EventArgs<IApplication?> e)
  216. {
  217. if (iteration > 0)
  218. {
  219. Assert.Fail ();
  220. }
  221. iteration++;
  222. app.RequestStop ();
  223. }
  224. }
  225. [Fact]
  226. public void Screen_Size_Changes ()
  227. {
  228. IApplication app = Application.Create ();
  229. app.Init ("fake");
  230. IDriver? driver = app.Driver;
  231. app.Driver!.SetScreenSize (80, 25);
  232. Assert.Equal (new (0, 0, 80, 25), driver!.Screen);
  233. Assert.Equal (new (0, 0, 80, 25), app.Screen);
  234. // TODO: Should not be possible to manually change these at whim!
  235. driver.Cols = 100;
  236. driver.Rows = 30;
  237. app.Driver!.SetScreenSize (100, 30);
  238. Assert.Equal (new (0, 0, 100, 30), driver.Screen);
  239. app.Screen = new (0, 0, driver.Cols, driver.Rows);
  240. Assert.Equal (new (0, 0, 100, 30), driver.Screen);
  241. app.Dispose ();
  242. }
  243. [Fact]
  244. public void Dispose_Alone_Does_Nothing ()
  245. {
  246. IApplication app = Application.Create ();
  247. app.Dispose ();
  248. }
  249. #region RunTests
  250. [Fact]
  251. public void Run_T_After_InitWithDriver_with_Runnable_and_Driver_Does_Not_Throw ()
  252. {
  253. IApplication app = Application.Create ();
  254. app.StopAfterFirstIteration = true;
  255. // Run<Runnable<bool>> when already initialized or not with a Driver will not throw (because Window is derived from Runnable)
  256. // Using another type not derived from Runnable will throws at compile time
  257. app.Run<Window> (null, "fake");
  258. // Run<Runnable<bool>> when already initialized or not with a Driver will not throw (because Dialog is derived from Runnable)
  259. app.Run<Dialog> (null, "fake");
  260. app.Dispose ();
  261. }
  262. [Fact]
  263. public void Run_T_After_Init_Does_Not_Disposes_Application_Top ()
  264. {
  265. IApplication app = Application.Create ();
  266. app.Init ("fake");
  267. // Init doesn't create a Runnable and assigned it to app.TopRunnable
  268. // but Begin does
  269. var initTop = new Runnable ();
  270. app.Iteration += OnApplicationOnIteration;
  271. app.Run<Runnable> ();
  272. app.Iteration -= OnApplicationOnIteration;
  273. #if DEBUG_IDISPOSABLE
  274. Assert.False (initTop.WasDisposed);
  275. initTop.Dispose ();
  276. Assert.True (initTop.WasDisposed);
  277. #endif
  278. initTop.Dispose ();
  279. app.Dispose ();
  280. return;
  281. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  282. {
  283. Assert.NotEqual (initTop, app.TopRunnableView);
  284. #if DEBUG_IDISPOSABLE
  285. Assert.False (initTop.WasDisposed);
  286. #endif
  287. app.RequestStop ();
  288. }
  289. }
  290. [Fact]
  291. public void Run_T_After_InitWithDriver_with_TestRunnable_DoesNotThrow ()
  292. {
  293. IApplication app = Application.Create ();
  294. app.Init ("fake");
  295. app.StopAfterFirstIteration = true;
  296. // Init has been called and we're passing no driver to Run<TestRunnable>. This is ok.
  297. app.Run<Window> ();
  298. app.Dispose ();
  299. }
  300. [Fact]
  301. public void Run_T_After_InitNullDriver_with_TestRunnable_DoesNotThrow ()
  302. {
  303. IApplication app = Application.Create ();
  304. app.Init ("fake");
  305. app.StopAfterFirstIteration = true;
  306. // Init has been called, selecting FakeDriver; we're passing no driver to Run<TestRunnable>. Should be fine.
  307. app.Run<Window> ();
  308. app.Dispose ();
  309. }
  310. [Fact]
  311. public void Run_T_NoInit_DoesNotThrow ()
  312. {
  313. IApplication app = Application.Create ();
  314. app.StopAfterFirstIteration = true;
  315. app.Run<Window> ();
  316. app.Dispose ();
  317. }
  318. [Fact]
  319. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  320. {
  321. IApplication app = Application.Create ();
  322. app.StopAfterFirstIteration = true;
  323. // Init has NOT been called and we're passing a valid driver to Run<TestRunnable>. This is ok.
  324. app.Run<Runnable> (null, "fake");
  325. app.Dispose ();
  326. }
  327. [Fact]
  328. public void Run_Sets_Running_True ()
  329. {
  330. IApplication app = Application.Create ();
  331. app.Init ("fake");
  332. var top = new Runnable ();
  333. SessionToken? rs = app.Begin (top);
  334. Assert.NotNull (rs);
  335. app.Iteration += OnApplicationOnIteration;
  336. app.Run (top);
  337. app.Iteration -= OnApplicationOnIteration;
  338. top.Dispose ();
  339. app.Dispose ();
  340. return;
  341. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  342. {
  343. Assert.True (top.IsRunning);
  344. top.RequestStop ();
  345. }
  346. }
  347. [Fact]
  348. public void Run_A_Modal_Runnable_Refresh_Background_On_Moving ()
  349. {
  350. IApplication app = Application.Create ();
  351. app.Init ("fake");
  352. // Don't use Dialog here as it has more layout logic. Use Window instead.
  353. var w = new Window
  354. {
  355. Width = 5, Height = 5,
  356. Arrangement = ViewArrangement.Movable
  357. };
  358. app.Driver!.SetScreenSize (10, 10);
  359. SessionToken? rs = app.Begin (w);
  360. // Don't use visuals to test as style of border can change over time.
  361. Assert.Equal (new (0, 0), w.Frame.Location);
  362. app.Mouse.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed });
  363. Assert.Equal (w.Border, app.Mouse.MouseGrabView);
  364. Assert.Equal (new (0, 0), w.Frame.Location);
  365. // Move down and to the right.
  366. app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
  367. Assert.Equal (new (1, 1), w.Frame.Location);
  368. app.End (rs!);
  369. w.Dispose ();
  370. app.Dispose ();
  371. }
  372. [Fact]
  373. public void Run_T_Creates_Top_Without_Init ()
  374. {
  375. IApplication app = Application.Create ();
  376. app.StopAfterFirstIteration = true;
  377. app.SessionEnded += OnApplicationOnSessionEnded;
  378. app.Run<Window> (null, "fake");
  379. Assert.Null (app.TopRunnableView);
  380. app.Dispose ();
  381. Assert.Null (app.TopRunnableView);
  382. return;
  383. void OnApplicationOnSessionEnded (object? sender, SessionTokenEventArgs e)
  384. {
  385. app.SessionEnded -= OnApplicationOnSessionEnded;
  386. e.State.Result = (e.State.Runnable as IRunnable<object?>)?.Result;
  387. }
  388. }
  389. #endregion
  390. #region DisposeTests
  391. [Fact]
  392. public async Task Dispose_Allows_Async ()
  393. {
  394. var isCompletedSuccessfully = false;
  395. async Task TaskWithAsyncContinuation ()
  396. {
  397. await Task.Yield ();
  398. await Task.Yield ();
  399. isCompletedSuccessfully = true;
  400. }
  401. IApplication app = Application.Create ();
  402. app.Dispose ();
  403. Assert.False (isCompletedSuccessfully);
  404. await TaskWithAsyncContinuation ();
  405. Thread.Sleep (100);
  406. Assert.True (isCompletedSuccessfully);
  407. }
  408. [Fact]
  409. public void Dispose_Resets_SyncContext ()
  410. {
  411. IApplication app = Application.Create ();
  412. app.Dispose ();
  413. Assert.Null (SynchronizationContext.Current);
  414. }
  415. #endregion
  416. }