2
0

ApplicationTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. using Xunit.Abstractions;
  10. // Alias Console to MockConsole so we don't accidentally use Console
  11. using Console = Terminal.Gui.FakeConsole;
  12. namespace Terminal.Gui.ApplicationTests;
  13. public class ApplicationTests {
  14. readonly ITestOutputHelper _output;
  15. public ApplicationTests (ITestOutputHelper output)
  16. {
  17. _output = output;
  18. ConsoleDriver.RunningUnitTests = true;
  19. #if DEBUG_IDISPOSABLE
  20. Responder.Instances.Clear ();
  21. RunState.Instances.Clear ();
  22. #endif
  23. }
  24. void Pre_Init_State ()
  25. {
  26. Assert.Null (Application.Driver);
  27. Assert.Null (Application.Top);
  28. Assert.Null (Application.Current);
  29. Assert.Null (Application.MainLoop);
  30. }
  31. void Post_Init_State ()
  32. {
  33. Assert.NotNull (Application.Driver);
  34. Assert.NotNull (Application.Top);
  35. Assert.NotNull (Application.Current);
  36. Assert.NotNull (Application.MainLoop);
  37. // FakeDriver is always 80x25
  38. Assert.Equal (80, Application.Driver.Cols);
  39. Assert.Equal (25, Application.Driver.Rows);
  40. }
  41. void Init ()
  42. {
  43. Application.Init (new FakeDriver ());
  44. Assert.NotNull (Application.Driver);
  45. Assert.NotNull (Application.MainLoop);
  46. Assert.NotNull (SynchronizationContext.Current);
  47. }
  48. void Shutdown () => Application.Shutdown ();
  49. [Fact]
  50. public void Init_Shutdown_Cleans_Up ()
  51. {
  52. // Verify initial state is per spec
  53. //Pre_Init_State ();
  54. Application.Init (new FakeDriver ());
  55. // Verify post-Init state is correct
  56. //Post_Init_State ();
  57. Application.Shutdown ();
  58. // Verify state is back to initial
  59. //Pre_Init_State ();
  60. #if DEBUG_IDISPOSABLE
  61. // Validate there are no outstanding Responder-based instances
  62. // after a scenario was selected to run. This proves the main UI Catalog
  63. // 'app' closed cleanly.
  64. Assert.Empty (Responder.Instances);
  65. #endif
  66. }
  67. [Fact]
  68. public void Init_Unbalanced_Throws ()
  69. {
  70. Application.Init (new FakeDriver ());
  71. Toplevel topLevel = null;
  72. Assert.Throws<InvalidOperationException> (() => Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ()));
  73. Shutdown ();
  74. Assert.Null (Application.Top);
  75. Assert.Null (Application.MainLoop);
  76. Assert.Null (Application.Driver);
  77. // Now try the other way
  78. topLevel = null;
  79. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  80. Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
  81. Shutdown ();
  82. Assert.Null (Application.Top);
  83. Assert.Null (Application.MainLoop);
  84. Assert.Null (Application.Driver);
  85. }
  86. class TestToplevel : Toplevel {
  87. public TestToplevel () => IsOverlappedContainer = false;
  88. }
  89. [Fact]
  90. public void Init_Null_Driver_Should_Pick_A_Driver ()
  91. {
  92. Application.Init (null);
  93. Assert.NotNull (Application.Driver);
  94. Shutdown ();
  95. }
  96. [Theory]
  97. [InlineData (typeof (FakeDriver))]
  98. [InlineData (typeof (NetDriver))]
  99. //[InlineData (typeof (ANSIDriver))]
  100. [InlineData (typeof (WindowsDriver))]
  101. [InlineData (typeof (CursesDriver))]
  102. public void Init_DriverName_Should_Pick_Correct_Driver (Type driverType)
  103. {
  104. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  105. Application.Init (driverName: driverType.Name);
  106. Assert.NotNull (Application.Driver);
  107. Assert.Equal (driverType, Application.Driver.GetType ());
  108. Shutdown ();
  109. }
  110. [Fact]
  111. public void Init_Begin_End_Cleans_Up ()
  112. {
  113. Init ();
  114. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  115. // if we don't stop
  116. Application.Iteration += (s, a) => {
  117. Application.RequestStop ();
  118. };
  119. RunState runstate = null;
  120. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
  121. Assert.NotNull (e.State);
  122. runstate = e.State;
  123. };
  124. Application.NotifyNewRunState += NewRunStateFn;
  125. var topLevel = new Toplevel ();
  126. var rs = Application.Begin (topLevel);
  127. Assert.NotNull (rs);
  128. Assert.NotNull (runstate);
  129. Assert.Equal (rs, runstate);
  130. Assert.Equal (topLevel, Application.Top);
  131. Assert.Equal (topLevel, Application.Current);
  132. Application.NotifyNewRunState -= NewRunStateFn;
  133. Application.End (runstate);
  134. Assert.Null (Application.Current);
  135. Assert.NotNull (Application.Top);
  136. Assert.NotNull (Application.MainLoop);
  137. Assert.NotNull (Application.Driver);
  138. Shutdown ();
  139. Assert.Null (Application.Top);
  140. Assert.Null (Application.MainLoop);
  141. Assert.Null (Application.Driver);
  142. }
  143. [Fact]
  144. public void InitWithTopLevelFactory_Begin_End_Cleans_Up ()
  145. {
  146. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  147. // if we don't stop
  148. Application.Iteration += (s, a) => {
  149. Application.RequestStop ();
  150. };
  151. // NOTE: Run<T>, when called after Init has been called behaves differently than
  152. // when called if Init has not been called.
  153. Toplevel topLevel = null;
  154. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  155. RunState runstate = null;
  156. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
  157. Assert.NotNull (e.State);
  158. runstate = e.State;
  159. };
  160. Application.NotifyNewRunState += NewRunStateFn;
  161. var rs = Application.Begin (topLevel);
  162. Assert.NotNull (rs);
  163. Assert.NotNull (runstate);
  164. Assert.Equal (rs, runstate);
  165. Assert.Equal (topLevel, Application.Top);
  166. Assert.Equal (topLevel, Application.Current);
  167. Application.NotifyNewRunState -= NewRunStateFn;
  168. Application.End (runstate);
  169. Assert.Null (Application.Current);
  170. Assert.NotNull (Application.Top);
  171. Assert.NotNull (Application.MainLoop);
  172. Assert.NotNull (Application.Driver);
  173. Shutdown ();
  174. Assert.Null (Application.Top);
  175. Assert.Null (Application.MainLoop);
  176. Assert.Null (Application.Driver);
  177. }
  178. [Fact]
  179. public void Begin_Null_Toplevel_Throws ()
  180. {
  181. // Setup Mock driver
  182. Init ();
  183. // Test null Toplevel
  184. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  185. Shutdown ();
  186. Assert.Null (Application.Top);
  187. Assert.Null (Application.MainLoop);
  188. Assert.Null (Application.Driver);
  189. }
  190. #region RunTests
  191. [Fact]
  192. public void Run_T_After_InitWithDriver_with_TopLevel_Throws ()
  193. {
  194. // Setup Mock driver
  195. Init ();
  196. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not dervied from TopLevel)
  197. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> (errorHandler: null));
  198. Shutdown ();
  199. Assert.Null (Application.Top);
  200. Assert.Null (Application.MainLoop);
  201. Assert.Null (Application.Driver);
  202. }
  203. [Fact]
  204. public void Run_T_After_InitWithDriver_with_TopLevel_and_Driver_Throws ()
  205. {
  206. // Setup Mock driver
  207. Init ();
  208. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not dervied from TopLevel)
  209. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> (null, new FakeDriver ()));
  210. Shutdown ();
  211. Assert.Null (Application.Top);
  212. Assert.Null (Application.MainLoop);
  213. Assert.Null (Application.Driver);
  214. }
  215. [Fact]
  216. public void Run_T_After_InitWithDriver_with_TestTopLevel_DoesNotThrow ()
  217. {
  218. // Setup Mock driver
  219. Init ();
  220. Application.Iteration += (s, a) => {
  221. Application.RequestStop ();
  222. };
  223. // Init has been called and we're passing no driver to Run<TestTopLevel>. This is ok.
  224. Application.Run<TestToplevel> ();
  225. Shutdown ();
  226. Assert.Null (Application.Top);
  227. Assert.Null (Application.MainLoop);
  228. Assert.Null (Application.Driver);
  229. }
  230. [Fact]
  231. public void Run_T_After_InitNullDriver_with_TestTopLevel_Throws ()
  232. {
  233. Application.ForceDriver = "FakeDriver";
  234. Application.Init (null);
  235. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  236. Application.Iteration += (s, a) => {
  237. Application.RequestStop ();
  238. };
  239. // Init has been called without selecting a driver and we're passing no driver to Run<TestTopLevel>. Bad
  240. Application.Run<TestToplevel> ();
  241. Shutdown ();
  242. Assert.Null (Application.Top);
  243. Assert.Null (Application.MainLoop);
  244. Assert.Null (Application.Driver);
  245. }
  246. [Fact]
  247. public void Run_T_Init_Driver_Cleared_with_TestTopLevel_Throws ()
  248. {
  249. Init ();
  250. Application.Driver = null;
  251. Application.Iteration += (s, a) => {
  252. Application.RequestStop ();
  253. };
  254. // Init has been called, but Driver has been set to null. Bad.
  255. Assert.Throws<InvalidOperationException> (() => Application.Run<TestToplevel> ());
  256. Shutdown ();
  257. Assert.Null (Application.Top);
  258. Assert.Null (Application.MainLoop);
  259. Assert.Null (Application.Driver);
  260. }
  261. [Fact]
  262. public void Run_T_NoInit_DoesNotThrow ()
  263. {
  264. Application.ForceDriver = "FakeDriver";
  265. Application.Iteration += (s, a) => {
  266. Application.RequestStop ();
  267. };
  268. Application.Run<TestToplevel> ();
  269. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  270. Shutdown ();
  271. Assert.Null (Application.Top);
  272. Assert.Null (Application.MainLoop);
  273. Assert.Null (Application.Driver);
  274. }
  275. [Fact]
  276. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  277. {
  278. Application.Iteration += (s, a) => {
  279. Application.RequestStop ();
  280. };
  281. // Init has NOT been called and we're passing a valid driver to Run<TestTopLevel>. This is ok.
  282. Application.Run<TestToplevel> (null, new FakeDriver ());
  283. Shutdown ();
  284. Assert.Null (Application.Top);
  285. Assert.Null (Application.MainLoop);
  286. Assert.Null (Application.Driver);
  287. }
  288. [Fact]
  289. public void Run_RequestStop_Stops ()
  290. {
  291. // Setup Mock driver
  292. Init ();
  293. var top = new Toplevel ();
  294. var rs = Application.Begin (top);
  295. Assert.NotNull (rs);
  296. Assert.Equal (top, Application.Current);
  297. Application.Iteration += (s, a) => {
  298. Application.RequestStop ();
  299. };
  300. Application.Run (top);
  301. Application.Shutdown ();
  302. Assert.Null (Application.Current);
  303. Assert.Null (Application.Top);
  304. Assert.Null (Application.MainLoop);
  305. Assert.Null (Application.Driver);
  306. }
  307. [Fact]
  308. public void Run_RunningFalse_Stops ()
  309. {
  310. // Setup Mock driver
  311. Init ();
  312. var top = new Toplevel ();
  313. var rs = Application.Begin (top);
  314. Assert.NotNull (rs);
  315. Assert.Equal (top, Application.Current);
  316. Application.Iteration += (s, a) => {
  317. top.Running = false;
  318. };
  319. Application.Run (top);
  320. Application.Shutdown ();
  321. Assert.Null (Application.Current);
  322. Assert.Null (Application.Top);
  323. Assert.Null (Application.MainLoop);
  324. Assert.Null (Application.Driver);
  325. }
  326. [Fact]
  327. public void Run_Loaded_Ready_Unlodaded_Events ()
  328. {
  329. Init ();
  330. var top = Application.Top;
  331. int count = 0;
  332. top.Loaded += (s, e) => count++;
  333. top.Ready += (s, e) => count++;
  334. top.Unloaded += (s, e) => count++;
  335. Application.Iteration += (s, a) => Application.RequestStop ();
  336. Application.Run ();
  337. Application.Shutdown ();
  338. Assert.Equal (3, count);
  339. }
  340. // TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
  341. [Fact]
  342. public void Run_Toplevel_With_Modal_View_Does_Not_Refresh_If_Not_Dirty ()
  343. {
  344. Init ();
  345. int count = 0;
  346. // Don't use Dialog here as it has more layout logic. Use Window instead.
  347. Dialog d = null;
  348. var top = Application.Top;
  349. top.DrawContent += (s, a) => count++;
  350. int iteration = -1;
  351. Application.Iteration += (s, a) => {
  352. iteration++;
  353. if (iteration == 0) {
  354. // TODO: Don't use Dialog here as it has more layout logic. Use Window instead.
  355. d = new Dialog ();
  356. d.DrawContent += (s, a) => count++;
  357. Application.Run (d);
  358. } else if (iteration < 3) {
  359. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.ReportMousePosition }));
  360. Assert.False (top.NeedsDisplay);
  361. Assert.False (top.SubViewNeedsDisplay);
  362. Assert.False (top.LayoutNeeded);
  363. Assert.False (d.NeedsDisplay);
  364. Assert.False (d.SubViewNeedsDisplay);
  365. Assert.False (d.LayoutNeeded);
  366. } else {
  367. Application.RequestStop ();
  368. }
  369. };
  370. Application.Run ();
  371. Application.Shutdown ();
  372. // 1 - First top load, 1 - Dialog load, 1 - Dialog unload, Total - 3.
  373. Assert.Equal (3, count);
  374. }
  375. // TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
  376. [Fact]
  377. public void Run_A_Modal_Toplevel_Refresh_Background_On_Moving ()
  378. {
  379. Init ();
  380. // Don't use Dialog here as it has more layout logic. Use Window instead.
  381. var w = new Window () { Width = 5, Height = 5 };
  382. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  383. var rs = Application.Begin (w);
  384. TestHelpers.AssertDriverContentsWithFrameAre (@"
  385. ┌───┐
  386. │ │
  387. │ │
  388. │ │
  389. └───┘", _output);
  390. var attributes = new Attribute [] {
  391. // 0
  392. new Attribute (ColorName.White, ColorName.Black),
  393. // 1
  394. Colors.Base.Normal
  395. };
  396. TestHelpers.AssertDriverAttributesAre (@"
  397. 1111100000
  398. 1111100000
  399. 1111100000
  400. 1111100000
  401. 1111100000
  402. ", null, attributes);
  403. // TODO: In PR #2920 this breaks because the mouse is not grabbed anymore.
  404. // TODO: Move the mouse grap/drag mode from Toplevel to Border.
  405. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () { X = 0, Y = 0, Flags = MouseFlags.Button1Pressed }));
  406. Assert.Equal (w, Application.MouseGrabView);
  407. // Move down and to the right.
  408. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () { X = 1, Y = 1, Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition }));
  409. Application.Refresh ();
  410. TestHelpers.AssertDriverContentsWithFrameAre (@"
  411. ┌───┐
  412. │ │
  413. │ │
  414. │ │
  415. └───┘", _output);
  416. attributes = new Attribute [] {
  417. // 0
  418. new Attribute (ColorName.White, ColorName.Black),
  419. // 1
  420. Colors.Base.Normal
  421. };
  422. TestHelpers.AssertDriverAttributesAre (@"
  423. 0000000000
  424. 0111110000
  425. 0111110000
  426. 0111110000
  427. 0111110000
  428. 0111110000
  429. ", null, attributes);
  430. Application.End (rs);
  431. Application.Shutdown ();
  432. }
  433. // TODO: Add tests for Run that test errorHandler
  434. #endregion
  435. #region ShutdownTests
  436. [Fact]
  437. public async void Shutdown_Allows_Async ()
  438. {
  439. bool isCompletedSuccessfully = false;
  440. async Task TaskWithAsyncContinuation ()
  441. {
  442. await Task.Yield ();
  443. await Task.Yield ();
  444. isCompletedSuccessfully = true;
  445. }
  446. Init ();
  447. Application.Shutdown ();
  448. Assert.False (isCompletedSuccessfully);
  449. await TaskWithAsyncContinuation ();
  450. Thread.Sleep (100);
  451. Assert.True (isCompletedSuccessfully);
  452. }
  453. [Fact]
  454. public void Shutdown_Resets_SyncContext ()
  455. {
  456. Init ();
  457. Application.Shutdown ();
  458. Assert.Null (SynchronizationContext.Current);
  459. }
  460. #endregion
  461. [Fact, AutoInitShutdown]
  462. public void Begin_Sets_Application_Top_To_Console_Size ()
  463. {
  464. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  465. Application.Begin (Application.Top);
  466. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  467. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  468. Assert.Equal (new Rect (0, 0, 5, 5), Application.Top.Frame);
  469. }
  470. [Fact]
  471. [AutoInitShutdown]
  472. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  473. {
  474. var t1 = new Toplevel ();
  475. var t2 = new Toplevel ();
  476. var t3 = new Toplevel ();
  477. // Don't use Dialog here as it has more layout logic. Use Window instead.
  478. var d = new Dialog ();
  479. var t4 = new Toplevel ();
  480. // t1, t2, t3, d, t4
  481. int iterations = 5;
  482. t1.Ready += (s, e) => {
  483. Assert.Equal (t1, Application.Top);
  484. Application.Run (t2);
  485. };
  486. t2.Ready += (s, e) => {
  487. Assert.Equal (t2, Application.Top);
  488. Application.Run (t3);
  489. };
  490. t3.Ready += (s, e) => {
  491. Assert.Equal (t3, Application.Top);
  492. Application.Run (d);
  493. };
  494. d.Ready += (s, e) => {
  495. Assert.Equal (t3, Application.Top);
  496. Application.Run (t4);
  497. };
  498. t4.Ready += (s, e) => {
  499. Assert.Equal (t4, Application.Top);
  500. t4.RequestStop ();
  501. d.RequestStop ();
  502. t3.RequestStop ();
  503. t2.RequestStop ();
  504. };
  505. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  506. t2.Closed += (s, _) => {
  507. t1.RequestStop ();
  508. };
  509. Application.Iteration += (s, a) => {
  510. if (iterations == 5) {
  511. // The Current still is t4 because Current.Running is false.
  512. Assert.Equal (t4, Application.Current);
  513. Assert.False (Application.Current.Running);
  514. Assert.Equal (t4, Application.Top);
  515. } else if (iterations == 4) {
  516. // The Current is d and Current.Running is false.
  517. Assert.Equal (d, Application.Current);
  518. Assert.False (Application.Current.Running);
  519. Assert.Equal (t4, Application.Top);
  520. } else if (iterations == 3) {
  521. // The Current is t3 and Current.Running is false.
  522. Assert.Equal (t3, Application.Current);
  523. Assert.False (Application.Current.Running);
  524. Assert.Equal (t3, Application.Top);
  525. } else if (iterations == 2) {
  526. // The Current is t2 and Current.Running is false.
  527. Assert.Equal (t2, Application.Current);
  528. Assert.False (Application.Current.Running);
  529. Assert.Equal (t2, Application.Top);
  530. } else {
  531. // The Current is t1.
  532. Assert.Equal (t1, Application.Current);
  533. Assert.False (Application.Current.Running);
  534. Assert.Equal (t1, Application.Top);
  535. }
  536. iterations--;
  537. };
  538. Application.Run (t1);
  539. Assert.Equal (t1, Application.Top);
  540. }
  541. [Fact]
  542. [AutoInitShutdown]
  543. public void Internal_Properties_Correct ()
  544. {
  545. Assert.True (Application._initialized);
  546. Assert.NotNull (Application.Top);
  547. var rs = Application.Begin (Application.Top);
  548. Assert.Equal (Application.Top, rs.Toplevel);
  549. Assert.Null (Application.MouseGrabView); // public
  550. Assert.Null (Application.WantContinuousButtonPressedView); // public
  551. Assert.False (Application.MoveToOverlappedChild (Application.Top));
  552. }
  553. // Invoke Tests
  554. // TODO: Test with threading scenarios
  555. [Fact]
  556. public void Invoke_Adds_Idle ()
  557. {
  558. Application.Init (new FakeDriver ());
  559. var top = new Toplevel ();
  560. var rs = Application.Begin (top);
  561. bool firstIteration = false;
  562. int actionCalled = 0;
  563. Application.Invoke (() => { actionCalled++; });
  564. Application.MainLoop.Running = true;
  565. Application.RunIteration (ref rs, ref firstIteration);
  566. Assert.Equal (1, actionCalled);
  567. Application.Shutdown ();
  568. }
  569. }