ApplicationTests.cs 13 KB

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