ApplicationTests.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. namespace Terminal.Gui.ApplicationTests;
  4. public class ApplicationTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public ApplicationTests (ITestOutputHelper output)
  8. {
  9. _output = output;
  10. ConsoleDriver.RunningUnitTests = true;
  11. #if DEBUG_IDISPOSABLE
  12. Responder.Instances.Clear ();
  13. RunState.Instances.Clear ();
  14. #endif
  15. }
  16. [Fact]
  17. public void Begin_Null_Toplevel_Throws ()
  18. {
  19. // Setup Mock driver
  20. Init ();
  21. // Test null Toplevel
  22. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  23. Shutdown ();
  24. Assert.Null (Application.Top);
  25. Assert.Null (Application.MainLoop);
  26. Assert.Null (Application.Driver);
  27. }
  28. [Fact]
  29. [AutoInitShutdown]
  30. public void Begin_Sets_Application_Top_To_Console_Size ()
  31. {
  32. Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame);
  33. Application.Begin (Application.Top);
  34. Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame);
  35. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  36. Assert.Equal (new Rectangle (0, 0, 5, 5), Application.Top.Frame);
  37. }
  38. [Fact]
  39. public void End_Should_Not_Dispose_ApplicationTop_Shutdown_Should ()
  40. {
  41. Init ();
  42. RunState rs = Application.Begin (Application.Top);
  43. Assert.Equal (rs.Toplevel, Application.Top);
  44. Application.End (rs);
  45. #if DEBUG_IDISPOSABLE
  46. Assert.True (rs.WasDisposed);
  47. Assert.True (Application.Top.WasDisposed); // Is true because the rs.Toplevel is the same as Application.Top
  48. #endif
  49. Assert.Null (rs.Toplevel);
  50. var top = Application.Top;
  51. Shutdown ();
  52. #if DEBUG_IDISPOSABLE
  53. Assert.True (top.WasDisposed);
  54. #endif
  55. Assert.Null (Application.Top);
  56. }
  57. [Fact]
  58. public void Init_Begin_End_Cleans_Up ()
  59. {
  60. Init ();
  61. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  62. // if we don't stop
  63. Application.Iteration += (s, a) => { Application.RequestStop (); };
  64. RunState runstate = null;
  65. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) =>
  66. {
  67. Assert.NotNull (e.State);
  68. runstate = e.State;
  69. };
  70. Application.NotifyNewRunState += NewRunStateFn;
  71. var topLevel = new Toplevel ();
  72. RunState rs = Application.Begin (topLevel);
  73. Assert.NotNull (rs);
  74. Assert.NotNull (runstate);
  75. Assert.Equal (rs, runstate);
  76. Assert.Equal (topLevel, Application.Top);
  77. Assert.Equal (topLevel, Application.Current);
  78. Application.NotifyNewRunState -= NewRunStateFn;
  79. Application.End (runstate);
  80. Assert.Null (Application.Current);
  81. Assert.NotNull (Application.Top);
  82. Assert.NotNull (Application.MainLoop);
  83. Assert.NotNull (Application.Driver);
  84. Shutdown ();
  85. Assert.Null (Application.Top);
  86. Assert.Null (Application.MainLoop);
  87. Assert.Null (Application.Driver);
  88. }
  89. [Theory]
  90. [InlineData (typeof (FakeDriver))]
  91. [InlineData (typeof (NetDriver))]
  92. //[InlineData (typeof (ANSIDriver))]
  93. [InlineData (typeof (WindowsDriver))]
  94. [InlineData (typeof (CursesDriver))]
  95. public void Init_DriverName_Should_Pick_Correct_Driver (Type driverType)
  96. {
  97. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  98. Application.Init (driverName: driverType.Name);
  99. Assert.NotNull (Application.Driver);
  100. Assert.Equal (driverType, Application.Driver.GetType ());
  101. Shutdown ();
  102. }
  103. [Fact]
  104. public void Init_Null_Driver_Should_Pick_A_Driver ()
  105. {
  106. Application.Init ();
  107. Assert.NotNull (Application.Driver);
  108. Shutdown ();
  109. }
  110. [Fact]
  111. public void Init_ResetState_Resets_Properties ()
  112. {
  113. ConfigurationManager.ThrowOnJsonErrors = true;
  114. // For all the fields/properties of Application, check that they are reset to their default values
  115. // Set some values
  116. Application.Init ();
  117. Application._initialized = true;
  118. // Reset
  119. Application.ResetState ();
  120. void CheckReset ()
  121. {
  122. // Check that all fields and properties are set to their default values
  123. // Public Properties
  124. Assert.Null (Application.Top);
  125. Assert.Null (Application.Current);
  126. Assert.Null (Application.MouseGrabView);
  127. Assert.Null (Application.WantContinuousButtonPressedView);
  128. // Don't check Application.ForceDriver
  129. // Assert.Empty (Application.ForceDriver);
  130. Assert.False (Application.Force16Colors);
  131. Assert.Null (Application.Driver);
  132. Assert.Null (Application.MainLoop);
  133. Assert.False (Application.EndAfterFirstIteration);
  134. Assert.Equal (Key.Empty, Application.AlternateBackwardKey);
  135. Assert.Equal (Key.Empty, Application.AlternateForwardKey);
  136. Assert.Equal (Key.Empty, Application.QuitKey);
  137. Assert.Null (Application.OverlappedChildren);
  138. Assert.Null (Application.OverlappedTop);
  139. // Internal properties
  140. Assert.False (Application._initialized);
  141. Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures);
  142. Assert.False (Application._forceFakeConsole);
  143. Assert.Equal (-1, Application._mainThreadId);
  144. Assert.Empty (Application._topLevels);
  145. Assert.Null (Application._mouseEnteredView);
  146. // Events - Can't check
  147. //Assert.Null (Application.NotifyNewRunState);
  148. //Assert.Null (Application.NotifyNewRunState);
  149. //Assert.Null (Application.Iteration);
  150. //Assert.Null (Application.SizeChanging);
  151. //Assert.Null (Application.GrabbedMouse);
  152. //Assert.Null (Application.UnGrabbingMouse);
  153. //Assert.Null (Application.GrabbedMouse);
  154. //Assert.Null (Application.UnGrabbedMouse);
  155. //Assert.Null (Application.MouseEvent);
  156. //Assert.Null (Application.KeyDown);
  157. //Assert.Null (Application.KeyUp);
  158. }
  159. CheckReset ();
  160. // Set the values that can be set
  161. Application._initialized = true;
  162. Application._forceFakeConsole = true;
  163. Application._mainThreadId = 1;
  164. //Application._topLevels = new List<Toplevel> ();
  165. Application._mouseEnteredView = new View ();
  166. //Application.SupportedCultures = new List<CultureInfo> ();
  167. Application.Force16Colors = true;
  168. //Application.ForceDriver = "driver";
  169. Application.EndAfterFirstIteration = true;
  170. Application.AlternateBackwardKey = Key.A;
  171. Application.AlternateForwardKey = Key.B;
  172. Application.QuitKey = Key.C;
  173. //Application.OverlappedChildren = new List<View> ();
  174. //Application.OverlappedTop =
  175. Application._mouseEnteredView = new View ();
  176. //Application.WantContinuousButtonPressedView = new View ();
  177. Application.ResetState ();
  178. CheckReset ();
  179. ConfigurationManager.ThrowOnJsonErrors = false;
  180. }
  181. [Fact]
  182. public void Init_Shutdown_Cleans_Up ()
  183. {
  184. // Verify initial state is per spec
  185. //Pre_Init_State ();
  186. Application.Init (new FakeDriver ());
  187. // Verify post-Init state is correct
  188. //Post_Init_State ();
  189. Application.Shutdown ();
  190. // Verify state is back to initial
  191. //Pre_Init_State ();
  192. #if DEBUG_IDISPOSABLE
  193. // Validate there are no outstanding Responder-based instances
  194. // after a scenario was selected to run. This proves the main UI Catalog
  195. // 'app' closed cleanly.
  196. Assert.Empty (Responder.Instances);
  197. #endif
  198. }
  199. [Fact]
  200. public void Init_Unbalanced_Throws ()
  201. {
  202. Application.Init (new FakeDriver ());
  203. Toplevel topLevel = null;
  204. Assert.Throws<InvalidOperationException> (
  205. () =>
  206. Application.InternalInit (
  207. () => topLevel = new TestToplevel (),
  208. new FakeDriver ()
  209. )
  210. );
  211. Shutdown ();
  212. Assert.Null (Application.Top);
  213. Assert.Null (Application.MainLoop);
  214. Assert.Null (Application.Driver);
  215. // Now try the other way
  216. topLevel = null;
  217. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  218. Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
  219. Shutdown ();
  220. Assert.Null (Application.Top);
  221. Assert.Null (Application.MainLoop);
  222. Assert.Null (Application.Driver);
  223. }
  224. [Fact]
  225. public void InitWithTopLevelFactory_Begin_End_Cleans_Up ()
  226. {
  227. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  228. // if we don't stop
  229. Application.Iteration += (s, a) => { Application.RequestStop (); };
  230. // NOTE: Run<T>, when called after Init has been called behaves differently than
  231. // when called if Init has not been called.
  232. Toplevel topLevel = null;
  233. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  234. RunState runstate = null;
  235. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) =>
  236. {
  237. Assert.NotNull (e.State);
  238. runstate = e.State;
  239. };
  240. Application.NotifyNewRunState += NewRunStateFn;
  241. RunState rs = Application.Begin (topLevel);
  242. Assert.NotNull (rs);
  243. Assert.NotNull (runstate);
  244. Assert.Equal (rs, runstate);
  245. Assert.Equal (topLevel, Application.Top);
  246. Assert.Equal (topLevel, Application.Current);
  247. Application.NotifyNewRunState -= NewRunStateFn;
  248. Application.End (runstate);
  249. Assert.Null (Application.Current);
  250. Assert.NotNull (Application.Top);
  251. Assert.NotNull (Application.MainLoop);
  252. Assert.NotNull (Application.Driver);
  253. Shutdown ();
  254. Assert.Null (Application.Top);
  255. Assert.Null (Application.MainLoop);
  256. Assert.Null (Application.Driver);
  257. }
  258. [Fact]
  259. [AutoInitShutdown]
  260. public void Internal_Properties_Correct ()
  261. {
  262. Assert.True (Application._initialized);
  263. Assert.NotNull (Application.Top);
  264. RunState rs = Application.Begin (Application.Top);
  265. Assert.Equal (Application.Top, rs.Toplevel);
  266. Assert.Null (Application.MouseGrabView); // public
  267. Assert.Null (Application.WantContinuousButtonPressedView); // public
  268. Assert.False (Application.MoveToOverlappedChild (Application.Top));
  269. }
  270. // Invoke Tests
  271. // TODO: Test with threading scenarios
  272. [Fact]
  273. public void Invoke_Adds_Idle ()
  274. {
  275. Application.Init (new FakeDriver ());
  276. var top = new Toplevel ();
  277. RunState rs = Application.Begin (top);
  278. var firstIteration = false;
  279. var actionCalled = 0;
  280. Application.Invoke (() => { actionCalled++; });
  281. Application.MainLoop.Running = true;
  282. Application.RunIteration (ref rs, ref firstIteration);
  283. Assert.Equal (1, actionCalled);
  284. Application.Shutdown ();
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  289. {
  290. var top = Application.Top;
  291. var t1 = new Toplevel ();
  292. var t2 = new Toplevel ();
  293. var t3 = new Toplevel ();
  294. // Don't use Dialog here as it has more layout logic. Use Window instead.
  295. var d = new Dialog ();
  296. var t4 = new Toplevel ();
  297. // t1, t2, t3, d, t4
  298. var iterations = 5;
  299. t1.Ready += (s, e) =>
  300. {
  301. Assert.Equal (t1, Application.Top);
  302. Application.Run (t2);
  303. };
  304. t2.Ready += (s, e) =>
  305. {
  306. Assert.Equal (t2, Application.Top);
  307. Application.Run (t3);
  308. };
  309. t3.Ready += (s, e) =>
  310. {
  311. Assert.Equal (t3, Application.Top);
  312. Application.Run (d);
  313. };
  314. d.Ready += (s, e) =>
  315. {
  316. Assert.Equal (t3, Application.Top);
  317. Application.Run (t4);
  318. };
  319. t4.Ready += (s, e) =>
  320. {
  321. Assert.Equal (t4, Application.Top);
  322. t4.RequestStop ();
  323. d.RequestStop ();
  324. t3.RequestStop ();
  325. t2.RequestStop ();
  326. };
  327. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  328. t2.Closed += (s, _) => { t1.RequestStop (); };
  329. Application.Iteration += (s, a) =>
  330. {
  331. if (iterations == 5)
  332. {
  333. // The Current still is t4 because Current.Running is false.
  334. Assert.Equal (t4, Application.Current);
  335. Assert.False (Application.Current.Running);
  336. Assert.Equal (t4, Application.Top);
  337. }
  338. else if (iterations == 4)
  339. {
  340. // The Current is d and Current.Running is false.
  341. Assert.Equal (d, Application.Current);
  342. Assert.False (Application.Current.Running);
  343. Assert.Equal (t4, Application.Top);
  344. }
  345. else if (iterations == 3)
  346. {
  347. // The Current is t3 and Current.Running is false.
  348. Assert.Equal (t3, Application.Current);
  349. Assert.False (Application.Current.Running);
  350. Assert.Equal (t3, Application.Top);
  351. }
  352. else if (iterations == 2)
  353. {
  354. // The Current is t2 and Current.Running is false.
  355. Assert.Equal (t2, Application.Current);
  356. Assert.False (Application.Current.Running);
  357. Assert.Equal (t2, Application.Top);
  358. }
  359. else
  360. {
  361. // The Current is t1.
  362. Assert.Equal (t1, Application.Current);
  363. Assert.False (Application.Current.Running);
  364. Assert.Equal (t1, Application.Top);
  365. }
  366. iterations--;
  367. };
  368. Application.Run (t1);
  369. Assert.NotEqual (t1, Application.Top);
  370. Assert.Equal (top, Application.Top);
  371. #if DEBUG_IDISPOSABLE
  372. Assert.True (Application.Top.WasDisposed);
  373. #endif
  374. }
  375. private void Init ()
  376. {
  377. Application.Init (new FakeDriver ());
  378. Assert.NotNull (Application.Driver);
  379. Assert.NotNull (Application.MainLoop);
  380. Assert.NotNull (SynchronizationContext.Current);
  381. }
  382. private void Post_Init_State ()
  383. {
  384. Assert.NotNull (Application.Driver);
  385. Assert.NotNull (Application.Top);
  386. Assert.NotNull (Application.Current);
  387. Assert.NotNull (Application.MainLoop);
  388. // FakeDriver is always 80x25
  389. Assert.Equal (80, Application.Driver.Cols);
  390. Assert.Equal (25, Application.Driver.Rows);
  391. }
  392. private void Pre_Init_State ()
  393. {
  394. Assert.Null (Application.Driver);
  395. Assert.Null (Application.Top);
  396. Assert.Null (Application.Current);
  397. Assert.Null (Application.MainLoop);
  398. }
  399. private void Shutdown () { Application.Shutdown (); }
  400. private class TestToplevel : Toplevel
  401. {
  402. public TestToplevel () { IsOverlappedContainer = false; }
  403. }
  404. #region RunTests
  405. [Fact]
  406. public void Run_T_After_InitWithDriver_with_TopLevel_Throws ()
  407. {
  408. // Setup Mock driver
  409. Init ();
  410. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not derived from TopLevel)
  411. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> ());
  412. Shutdown ();
  413. Assert.Null (Application.Top);
  414. Assert.Null (Application.MainLoop);
  415. Assert.Null (Application.Driver);
  416. }
  417. [Fact]
  418. public void Run_T_After_InitWithDriver_with_TopLevel_and_Driver_Throws ()
  419. {
  420. // Setup Mock driver
  421. Init ();
  422. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not derivied from TopLevel)
  423. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> (null, new FakeDriver ()));
  424. Shutdown ();
  425. Assert.Null (Application.Top);
  426. Assert.Null (Application.MainLoop);
  427. Assert.Null (Application.Driver);
  428. }
  429. [Fact]
  430. [TestRespondersDisposed]
  431. public void Run_T_After_Init_Disposes_Application_Top ()
  432. {
  433. Init ();
  434. // Init created a Toplevel and assigned it to Application.Top
  435. var initTop = Application.Top;
  436. Application.Iteration += (s, a) =>
  437. {
  438. Assert.NotEqual(initTop, Application.Top);
  439. #if DEBUG_IDISPOSABLE
  440. Assert.True (initTop.WasDisposed);
  441. #endif
  442. Application.RequestStop ();
  443. };
  444. Application.Run<TestToplevel> ();
  445. #if DEBUG_IDISPOSABLE
  446. Assert.True (initTop.WasDisposed);
  447. #endif
  448. Shutdown ();
  449. Assert.Null (Application.Top);
  450. Assert.Null (Application.MainLoop);
  451. Assert.Null (Application.Driver);
  452. }
  453. [Fact]
  454. [TestRespondersDisposed]
  455. public void Run_T_After_InitWithDriver_with_TestTopLevel_DoesNotThrow ()
  456. {
  457. // Setup Mock driver
  458. Init ();
  459. Application.Iteration += (s, a) => { Application.RequestStop (); };
  460. // Init has been called and we're passing no driver to Run<TestTopLevel>. This is ok.
  461. Application.Run<TestToplevel> ();
  462. Shutdown ();
  463. Assert.Null (Application.Top);
  464. Assert.Null (Application.MainLoop);
  465. Assert.Null (Application.Driver);
  466. }
  467. [Fact]
  468. [TestRespondersDisposed]
  469. public void Run_T_After_InitNullDriver_with_TestTopLevel_DoesNotThrow ()
  470. {
  471. Application.ForceDriver = "FakeDriver";
  472. Application.Init ();
  473. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  474. Application.Iteration += (s, a) => { Application.RequestStop (); };
  475. // Init has been called, selecting FakeDriver; we're passing no driver to Run<TestTopLevel>. Should be fine.
  476. Application.Run<TestToplevel> ();
  477. Shutdown ();
  478. Assert.Null (Application.Top);
  479. Assert.Null (Application.MainLoop);
  480. Assert.Null (Application.Driver);
  481. }
  482. [Fact]
  483. [TestRespondersDisposed]
  484. public void Run_T_Init_Driver_Cleared_with_TestTopLevel_Throws ()
  485. {
  486. Init ();
  487. Application.Driver = null;
  488. Application.Iteration += (s, a) => { Application.RequestStop (); };
  489. // Init has been called, but Driver has been set to null. Bad.
  490. Assert.Throws<InvalidOperationException> (() => Application.Run<TestToplevel> ());
  491. Shutdown ();
  492. Assert.Null (Application.Top);
  493. Assert.Null (Application.MainLoop);
  494. Assert.Null (Application.Driver);
  495. }
  496. [Fact]
  497. [TestRespondersDisposed]
  498. public void Run_T_NoInit_DoesNotThrow ()
  499. {
  500. Application.ForceDriver = "FakeDriver";
  501. Application.Iteration += (s, a) => { Application.RequestStop (); };
  502. Application.Run<TestToplevel> ();
  503. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  504. Shutdown ();
  505. Assert.Null (Application.Top);
  506. Assert.Null (Application.MainLoop);
  507. Assert.Null (Application.Driver);
  508. }
  509. [Fact]
  510. [TestRespondersDisposed]
  511. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  512. {
  513. Application.Iteration += (s, a) => { Application.RequestStop (); };
  514. // Init has NOT been called and we're passing a valid driver to Run<TestTopLevel>. This is ok.
  515. Application.Run<TestToplevel> (null, new FakeDriver ());
  516. Shutdown ();
  517. Assert.Null (Application.Top);
  518. Assert.Null (Application.MainLoop);
  519. Assert.Null (Application.Driver);
  520. }
  521. [Fact]
  522. [TestRespondersDisposed]
  523. public void Run_RequestStop_Stops ()
  524. {
  525. // Setup Mock driver
  526. Init ();
  527. var top = new Toplevel ();
  528. RunState rs = Application.Begin (top);
  529. Assert.NotNull (rs);
  530. Assert.Equal (top, Application.Current);
  531. Application.Iteration += (s, a) => { Application.RequestStop (); };
  532. Application.Run (top);
  533. Application.Shutdown ();
  534. Assert.Null (Application.Current);
  535. Assert.Null (Application.Top);
  536. Assert.Null (Application.MainLoop);
  537. Assert.Null (Application.Driver);
  538. }
  539. [Fact]
  540. [TestRespondersDisposed]
  541. public void Run_RunningFalse_Stops ()
  542. {
  543. // Setup Mock driver
  544. Init ();
  545. var top = new Toplevel ();
  546. RunState rs = Application.Begin (top);
  547. Assert.NotNull (rs);
  548. Assert.Equal (top, Application.Current);
  549. Application.Iteration += (s, a) => { top.Running = false; };
  550. Application.Run (top);
  551. Application.Shutdown ();
  552. Assert.Null (Application.Current);
  553. Assert.Null (Application.Top);
  554. Assert.Null (Application.MainLoop);
  555. Assert.Null (Application.Driver);
  556. }
  557. [Fact]
  558. [TestRespondersDisposed]
  559. public void Run_Loaded_Ready_Unlodaded_Events ()
  560. {
  561. Init ();
  562. Toplevel top = Application.Top;
  563. var count = 0;
  564. top.Loaded += (s, e) => count++;
  565. top.Ready += (s, e) => count++;
  566. top.Unloaded += (s, e) => count++;
  567. Application.Iteration += (s, a) => Application.RequestStop ();
  568. Application.Run ();
  569. Application.Shutdown ();
  570. Assert.Equal (3, count);
  571. }
  572. // TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
  573. [Fact]
  574. public void Run_Toplevel_With_Modal_View_Does_Not_Refresh_If_Not_Dirty ()
  575. {
  576. Init ();
  577. var count = 0;
  578. // Don't use Dialog here as it has more layout logic. Use Window instead.
  579. Dialog d = null;
  580. Toplevel top = Application.Top;
  581. top.DrawContent += (s, a) => count++;
  582. int iteration = -1;
  583. Application.Iteration += (s, a) =>
  584. {
  585. iteration++;
  586. if (iteration == 0)
  587. {
  588. // TODO: Don't use Dialog here as it has more layout logic. Use Window instead.
  589. d = new Dialog ();
  590. d.DrawContent += (s, a) => count++;
  591. Application.Run (d);
  592. }
  593. else if (iteration < 3)
  594. {
  595. Application.OnMouseEvent (
  596. new MouseEventEventArgs (
  597. new MouseEvent
  598. { X = 0, Y = 0, Flags = MouseFlags.ReportMousePosition }
  599. )
  600. );
  601. Assert.False (top.NeedsDisplay);
  602. Assert.False (top.SubViewNeedsDisplay);
  603. Assert.False (top.LayoutNeeded);
  604. Assert.False (d.NeedsDisplay);
  605. Assert.False (d.SubViewNeedsDisplay);
  606. Assert.False (d.LayoutNeeded);
  607. }
  608. else
  609. {
  610. Application.RequestStop ();
  611. }
  612. };
  613. Application.Run ();
  614. Application.Shutdown ();
  615. // 1 - First top load, 1 - Dialog load, 1 - Dialog unload, Total - 3.
  616. Assert.Equal (3, count);
  617. }
  618. // TODO: All Toplevel layout tests should be moved to ToplevelTests.cs
  619. [Fact]
  620. public void Run_A_Modal_Toplevel_Refresh_Background_On_Moving ()
  621. {
  622. Init ();
  623. // Don't use Dialog here as it has more layout logic. Use Window instead.
  624. var w = new Window { Width = 5, Height = 5 };
  625. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  626. RunState rs = Application.Begin (w);
  627. TestHelpers.AssertDriverContentsWithFrameAre (
  628. @"
  629. ┌───┐
  630. │ │
  631. │ │
  632. │ │
  633. └───┘",
  634. _output
  635. );
  636. Attribute [] attributes =
  637. {
  638. // 0
  639. new (ColorName.White, ColorName.Black),
  640. // 1
  641. Colors.ColorSchemes ["Base"].Normal
  642. };
  643. TestHelpers.AssertDriverAttributesAre (
  644. @"
  645. 1111100000
  646. 1111100000
  647. 1111100000
  648. 1111100000
  649. 1111100000
  650. ",
  651. null,
  652. attributes
  653. );
  654. // TODO: In PR #2920 this breaks because the mouse is not grabbed anymore.
  655. // TODO: Move the mouse grap/drag mode from Toplevel to Border.
  656. Application.OnMouseEvent (
  657. new MouseEventEventArgs (
  658. new MouseEvent { X = 0, Y = 0, Flags = MouseFlags.Button1Pressed }
  659. )
  660. );
  661. Assert.Equal (w.Border, Application.MouseGrabView);
  662. // Move down and to the right.
  663. Application.OnMouseEvent (
  664. new MouseEventEventArgs (
  665. new MouseEvent
  666. {
  667. X = 1,
  668. Y = 1,
  669. Flags = MouseFlags.Button1Pressed
  670. | MouseFlags.ReportMousePosition
  671. }
  672. )
  673. );
  674. Application.Refresh ();
  675. TestHelpers.AssertDriverContentsWithFrameAre (
  676. @"
  677. ┌───┐
  678. │ │
  679. │ │
  680. │ │
  681. └───┘",
  682. _output
  683. );
  684. attributes = new []
  685. {
  686. // 0
  687. new (ColorName.White, ColorName.Black),
  688. // 1
  689. Colors.ColorSchemes ["Base"].Normal
  690. };
  691. TestHelpers.AssertDriverAttributesAre (
  692. @"
  693. 0000000000
  694. 0111110000
  695. 0111110000
  696. 0111110000
  697. 0111110000
  698. 0111110000
  699. ",
  700. null,
  701. attributes
  702. );
  703. Application.End (rs);
  704. Application.Shutdown ();
  705. }
  706. [Fact]
  707. public void End_Disposing_Correctly ()
  708. {
  709. Init ();
  710. var top = Application.Top;
  711. Window w = new ();
  712. w.Ready += (s, e) => Application.RequestStop (); // Causes `End` to be called
  713. Application.Run(w);
  714. #if DEBUG_IDISPOSABLE
  715. Assert.True (w.WasDisposed);
  716. #endif
  717. Assert.NotNull (w);
  718. Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init
  719. Assert.NotNull (Application.Top);
  720. Assert.NotEqual(w, Application.Top);
  721. Assert.Equal(top, Application.Top);
  722. Assert.Null (Application.Current);
  723. var exception = Record.Exception (() => Application.Run(w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again
  724. Assert.NotNull (exception);
  725. Application.Shutdown ();
  726. Assert.NotNull (w);
  727. Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init
  728. Assert.Null (Application.Top);
  729. Assert.Null (Application.Current);
  730. Assert.NotNull (top);
  731. }
  732. // TODO: Add tests for Run that test errorHandler
  733. #endregion
  734. #region ShutdownTests
  735. [Fact]
  736. public async void Shutdown_Allows_Async ()
  737. {
  738. var isCompletedSuccessfully = false;
  739. async Task TaskWithAsyncContinuation ()
  740. {
  741. await Task.Yield ();
  742. await Task.Yield ();
  743. isCompletedSuccessfully = true;
  744. }
  745. Init ();
  746. Application.Shutdown ();
  747. Assert.False (isCompletedSuccessfully);
  748. await TaskWithAsyncContinuation ();
  749. Thread.Sleep (100);
  750. Assert.True (isCompletedSuccessfully);
  751. }
  752. [Fact]
  753. public void Shutdown_Resets_SyncContext ()
  754. {
  755. Init ();
  756. Application.Shutdown ();
  757. Assert.Null (SynchronizationContext.Current);
  758. }
  759. #endregion
  760. }