ApplicationTests.cs 30 KB

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