ApplicationTests.cs 20 KB

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