MainLoopTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.ComTypes;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Terminal.Gui;
  10. using Xunit;
  11. using Xunit.Sdk;
  12. // Alias Console to MockConsole so we don't accidentally use Console
  13. using Console = Terminal.Gui.FakeConsole;
  14. namespace Terminal.Gui.ApplicationTests {
  15. /// <summary>
  16. /// Tests MainLoop using the FakeMainLoop.
  17. /// </summary>
  18. public class MainLoopTests {
  19. // TODO: Expand to test all the MainLoop implementations.
  20. [Fact]
  21. public void Constructor_Setups_Driver ()
  22. {
  23. var ml = new MainLoop (new FakeMainLoop ());
  24. Assert.NotNull (ml.MainLoopDriver);
  25. }
  26. // Idle Handler tests
  27. [Fact]
  28. public void AddIdle_Adds_And_Removes ()
  29. {
  30. var ml = new MainLoop (new FakeMainLoop ());
  31. Func<bool> fnTrue = () => true;
  32. Func<bool> fnFalse = () => false;
  33. ml.AddIdle (fnTrue);
  34. ml.AddIdle (fnFalse);
  35. Assert.Equal (2, ml.IdleHandlers.Count);
  36. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  37. Assert.NotEqual (fnFalse, ml.IdleHandlers [0]);
  38. Assert.True (ml.RemoveIdle (fnTrue));
  39. Assert.Single (ml.IdleHandlers);
  40. // BUGBUG: This doesn't throw or indicate an error. Ideally RemoveIdle would either
  41. // throw an exception in this case, or return an error.
  42. // No. Only need to return a boolean.
  43. Assert.False (ml.RemoveIdle (fnTrue));
  44. Assert.True (ml.RemoveIdle (fnFalse));
  45. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  46. // throw an exception in this case, or return an error.
  47. // No. Only need to return a boolean.
  48. Assert.False (ml.RemoveIdle (fnFalse));
  49. // Add again, but with dupe
  50. ml.AddIdle (fnTrue);
  51. ml.AddIdle (fnTrue);
  52. Assert.Equal (2, ml.IdleHandlers.Count);
  53. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  54. Assert.True (ml.IdleHandlers [0] ());
  55. Assert.Equal (fnTrue, ml.IdleHandlers [1]);
  56. Assert.True (ml.IdleHandlers [1] ());
  57. Assert.True (ml.RemoveIdle (fnTrue));
  58. Assert.Single (ml.IdleHandlers);
  59. Assert.Equal (fnTrue, ml.IdleHandlers [0]);
  60. Assert.NotEqual (fnFalse, ml.IdleHandlers [0]);
  61. Assert.True (ml.RemoveIdle (fnTrue));
  62. Assert.Empty (ml.IdleHandlers);
  63. // BUGBUG: This doesn't throw an exception or indicate an error. Ideally RemoveIdle would either
  64. // throw an exception in this case, or return an error.
  65. // No. Only need to return a boolean.
  66. Assert.False (ml.RemoveIdle (fnTrue));
  67. }
  68. [Fact]
  69. public void AddIdle_Function_GetsCalled_OnIteration ()
  70. {
  71. var ml = new MainLoop (new FakeMainLoop ());
  72. var functionCalled = 0;
  73. Func<bool> fn = () => {
  74. functionCalled++;
  75. return true;
  76. };
  77. ml.AddIdle (fn);
  78. ml.RunIteration ();
  79. Assert.Equal (1, functionCalled);
  80. }
  81. [Fact]
  82. public void RemoveIdle_Function_NotCalled ()
  83. {
  84. var ml = new MainLoop (new FakeMainLoop ());
  85. var functionCalled = 0;
  86. Func<bool> fn = () => {
  87. functionCalled++;
  88. return true;
  89. };
  90. Assert.False (ml.RemoveIdle (fn));
  91. ml.RunIteration ();
  92. Assert.Equal (0, functionCalled);
  93. }
  94. [Fact]
  95. public void AddThenRemoveIdle_Function_NotCalled ()
  96. {
  97. var ml = new MainLoop (new FakeMainLoop ());
  98. var functionCalled = 0;
  99. Func<bool> fn = () => {
  100. functionCalled++;
  101. return true;
  102. };
  103. ml.AddIdle (fn);
  104. Assert.True (ml.RemoveIdle (fn));
  105. ml.RunIteration ();
  106. Assert.Equal (0, functionCalled);
  107. }
  108. [Fact]
  109. public void AddIdleTwice_Function_CalledTwice ()
  110. {
  111. var ml = new MainLoop (new FakeMainLoop ());
  112. var functionCalled = 0;
  113. Func<bool> fn = () => {
  114. functionCalled++;
  115. return true;
  116. };
  117. ml.AddIdle (fn);
  118. ml.AddIdle (fn);
  119. ml.RunIteration ();
  120. Assert.Equal (2, functionCalled);
  121. Assert.Equal (2, ml.IdleHandlers.Count);
  122. functionCalled = 0;
  123. Assert.True (ml.RemoveIdle (fn));
  124. Assert.Single (ml.IdleHandlers);
  125. ml.RunIteration ();
  126. Assert.Equal (1, functionCalled);
  127. functionCalled = 0;
  128. Assert.True (ml.RemoveIdle (fn));
  129. Assert.Empty (ml.IdleHandlers);
  130. ml.RunIteration ();
  131. Assert.Equal (0, functionCalled);
  132. Assert.False (ml.RemoveIdle (fn));
  133. }
  134. [Fact]
  135. public void False_Idle_Stops_It_Being_Called_Again ()
  136. {
  137. var ml = new MainLoop (new FakeMainLoop ());
  138. var functionCalled = 0;
  139. Func<bool> fn1 = () => {
  140. functionCalled++;
  141. if (functionCalled == 10) return false;
  142. return true;
  143. };
  144. // Force stop if 20 iterations
  145. var stopCount = 0;
  146. Func<bool> fnStop = () => {
  147. stopCount++;
  148. if (stopCount == 20) ml.Stop ();
  149. return true;
  150. };
  151. ml.AddIdle (fnStop);
  152. ml.AddIdle (fn1);
  153. ml.Run ();
  154. Assert.True (ml.RemoveIdle (fnStop));
  155. Assert.False (ml.RemoveIdle (fn1));
  156. Assert.Equal (10, functionCalled);
  157. Assert.Equal (20, stopCount);
  158. }
  159. [Fact]
  160. public void AddIdle_Twice_Returns_False_Called_Twice ()
  161. {
  162. var ml = new MainLoop (new FakeMainLoop ());
  163. var functionCalled = 0;
  164. Func<bool> fn1 = () => {
  165. functionCalled++;
  166. return false;
  167. };
  168. // Force stop if 10 iterations
  169. var stopCount = 0;
  170. Func<bool> fnStop = () => {
  171. stopCount++;
  172. if (stopCount == 10) ml.Stop ();
  173. return true;
  174. };
  175. ml.AddIdle (fnStop);
  176. ml.AddIdle (fn1);
  177. ml.AddIdle (fn1);
  178. ml.Run ();
  179. Assert.True (ml.RemoveIdle (fnStop));
  180. Assert.False (ml.RemoveIdle (fn1));
  181. Assert.False (ml.RemoveIdle (fn1));
  182. Assert.Equal (2, functionCalled);
  183. }
  184. [Fact]
  185. public void Run_Runs_Idle_Stop_Stops_Idle ()
  186. {
  187. var ml = new MainLoop (new FakeMainLoop ());
  188. var functionCalled = 0;
  189. Func<bool> fn = () => {
  190. functionCalled++;
  191. if (functionCalled == 10) {
  192. ml.Stop ();
  193. }
  194. return true;
  195. };
  196. ml.AddIdle (fn);
  197. ml.Run ();
  198. Assert.True (ml.RemoveIdle (fn));
  199. Assert.Equal (10, functionCalled);
  200. }
  201. // Timeout Handler Tests
  202. [Fact]
  203. public void AddTimer_Adds_Removes_NoFaults ()
  204. {
  205. var ml = new MainLoop (new FakeMainLoop ());
  206. var ms = 100;
  207. var callbackCount = 0;
  208. Func<MainLoop, bool> callback = (loop) => {
  209. callbackCount++;
  210. return true;
  211. };
  212. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  213. Assert.True (ml.RemoveTimeout (token));
  214. // BUGBUG: This should probably fault?
  215. // Must return a boolean.
  216. Assert.False (ml.RemoveTimeout (token));
  217. }
  218. // Timeout Handler Tests
  219. [Fact]
  220. public void AddTimer_EventFired ()
  221. {
  222. var ml = new MainLoop (new FakeMainLoop ());
  223. var ms = 100;
  224. var originTicks = DateTime.UtcNow.Ticks;
  225. var callbackCount = 0;
  226. Func<MainLoop, bool> callback = (loop) => {
  227. callbackCount++;
  228. return true;
  229. };
  230. object sender = null;
  231. TimeoutEventArgs args = null;
  232. ml.TimeoutAdded += (s, e) => {
  233. sender = s;
  234. args = e;
  235. };
  236. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  237. Assert.Same (ml, sender);
  238. Assert.NotNull (args.Timeout);
  239. Assert.True (args.Ticks - originTicks >= 100 * TimeSpan.TicksPerMillisecond);
  240. }
  241. [Fact]
  242. public void AddTimer_Run_Called ()
  243. {
  244. var ml = new MainLoop (new FakeMainLoop ());
  245. var ms = 100;
  246. var callbackCount = 0;
  247. Func<MainLoop, bool> callback = (loop) => {
  248. callbackCount++;
  249. ml.Stop ();
  250. return true;
  251. };
  252. var token = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback);
  253. ml.Run ();
  254. Assert.True (ml.RemoveTimeout (token));
  255. Assert.Equal (1, callbackCount);
  256. }
  257. [Fact]
  258. public async Task AddTimer_Duplicate_Keys_Not_Allowed ()
  259. {
  260. var ml = new MainLoop (new FakeMainLoop ());
  261. const int ms = 100;
  262. object token1 = null, token2 = null;
  263. var callbackCount = 0;
  264. Func<MainLoop, bool> callback = (loop) => {
  265. callbackCount++;
  266. if (callbackCount == 2) ml.Stop ();
  267. return true;
  268. };
  269. var task1 = new Task (() => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  270. var task2 = new Task (() => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback));
  271. Assert.Null (token1);
  272. Assert.Null (token2);
  273. task1.Start ();
  274. task2.Start ();
  275. ml.Run ();
  276. Assert.NotNull (token1);
  277. Assert.NotNull (token2);
  278. await Task.WhenAll (task1, task2);
  279. Assert.True (ml.RemoveTimeout (token1));
  280. Assert.True (ml.RemoveTimeout (token2));
  281. Assert.Equal (2, callbackCount);
  282. }
  283. [Fact]
  284. public void AddTimer_In_Parallel_Wont_Throw ()
  285. {
  286. var ml = new MainLoop (new FakeMainLoop ());
  287. const int ms = 100;
  288. object token1 = null, token2 = null;
  289. var callbackCount = 0;
  290. Func<MainLoop, bool> callback = (loop) => {
  291. callbackCount++;
  292. if (callbackCount == 2) ml.Stop ();
  293. return true;
  294. };
  295. Parallel.Invoke (
  296. () => token1 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback),
  297. () => token2 = ml.AddTimeout (TimeSpan.FromMilliseconds (ms), callback)
  298. );
  299. ml.Run ();
  300. Assert.NotNull (token1);
  301. Assert.NotNull (token2);
  302. Assert.True (ml.RemoveTimeout (token1));
  303. Assert.True (ml.RemoveTimeout (token2));
  304. Assert.Equal (2, callbackCount);
  305. }
  306. class MillisecondTolerance : IEqualityComparer<TimeSpan> {
  307. int _tolerance = 0;
  308. public MillisecondTolerance (int tolerance) { _tolerance = tolerance; }
  309. public bool Equals (TimeSpan x, TimeSpan y) => Math.Abs (x.Milliseconds - y.Milliseconds) <= _tolerance;
  310. public int GetHashCode (TimeSpan obj) => obj.GetHashCode ();
  311. }
  312. [Fact]
  313. public void AddTimer_Run_CalledAtApproximatelyRightTime ()
  314. {
  315. var ml = new MainLoop (new FakeMainLoop ());
  316. var ms = TimeSpan.FromMilliseconds (50);
  317. var watch = new System.Diagnostics.Stopwatch ();
  318. var callbackCount = 0;
  319. Func<MainLoop, bool> callback = (loop) => {
  320. watch.Stop ();
  321. callbackCount++;
  322. ml.Stop ();
  323. return true;
  324. };
  325. var token = ml.AddTimeout (ms, callback);
  326. watch.Start ();
  327. ml.Run ();
  328. // +/- 100ms should be good enuf
  329. // https://github.com/xunit/assert.xunit/pull/25
  330. Assert.Equal (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  331. Assert.True (ml.RemoveTimeout (token));
  332. Assert.Equal (1, callbackCount);
  333. }
  334. [Fact]
  335. public void AddTimer_Run_CalledTwiceApproximatelyRightTime ()
  336. {
  337. var ml = new MainLoop (new FakeMainLoop ());
  338. var ms = TimeSpan.FromMilliseconds (50);
  339. var watch = new System.Diagnostics.Stopwatch ();
  340. var callbackCount = 0;
  341. Func<MainLoop, bool> callback = (loop) => {
  342. callbackCount++;
  343. if (callbackCount == 2) {
  344. watch.Stop ();
  345. ml.Stop ();
  346. }
  347. return true;
  348. };
  349. var token = ml.AddTimeout (ms, callback);
  350. watch.Start ();
  351. ml.Run ();
  352. // +/- 100ms should be good enuf
  353. // https://github.com/xunit/assert.xunit/pull/25
  354. Assert.Equal (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  355. Assert.True (ml.RemoveTimeout (token));
  356. Assert.Equal (2, callbackCount);
  357. }
  358. [Fact]
  359. public void AddTimer_Remove_NotCalled ()
  360. {
  361. var ml = new MainLoop (new FakeMainLoop ());
  362. var ms = TimeSpan.FromMilliseconds (50);
  363. // Force stop if 10 iterations
  364. var stopCount = 0;
  365. Func<bool> fnStop = () => {
  366. stopCount++;
  367. if (stopCount == 10) ml.Stop ();
  368. return true;
  369. };
  370. ml.AddIdle (fnStop);
  371. var callbackCount = 0;
  372. Func<MainLoop, bool> callback = (loop) => {
  373. callbackCount++;
  374. return true;
  375. };
  376. var token = ml.AddTimeout (ms, callback);
  377. Assert.True (ml.RemoveTimeout (token));
  378. ml.Run ();
  379. Assert.Equal (0, callbackCount);
  380. }
  381. [Fact]
  382. public void AddTimer_ReturnFalse_StopsBeingCalled ()
  383. {
  384. var ml = new MainLoop (new FakeMainLoop ());
  385. var ms = TimeSpan.FromMilliseconds (50);
  386. // Force stop if 10 iterations
  387. var stopCount = 0;
  388. Func<bool> fnStop = () => {
  389. Thread.Sleep (10); // Sleep to enable timer to fire
  390. stopCount++;
  391. if (stopCount == 10) ml.Stop ();
  392. return true;
  393. };
  394. ml.AddIdle (fnStop);
  395. var callbackCount = 0;
  396. Func<MainLoop, bool> callback = (loop) => {
  397. callbackCount++;
  398. return false;
  399. };
  400. var token = ml.AddTimeout (ms, callback);
  401. ml.Run ();
  402. Assert.Equal (1, callbackCount);
  403. Assert.Equal (10, stopCount);
  404. Assert.False (ml.RemoveTimeout (token));
  405. }
  406. // Invoke Tests
  407. // TODO: Test with threading scenarios
  408. [Fact]
  409. public void Invoke_Adds_Idle ()
  410. {
  411. var ml = new MainLoop (new FakeMainLoop ());
  412. var actionCalled = 0;
  413. ml.Invoke (() => { actionCalled++; });
  414. ml.RunIteration ();
  415. Assert.Equal (1, actionCalled);
  416. }
  417. [Fact]
  418. public void CheckTimer_NoTimers_Returns_False ()
  419. {
  420. var ml = new MainLoop (new FakeMainLoop ());
  421. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  422. Assert.False (retVal);
  423. Assert.Equal (-1, waitTimeOut);
  424. }
  425. [Fact]
  426. public void CheckTimer_NoTimers_WithIdle_Returns_True ()
  427. {
  428. var ml = new MainLoop (new FakeMainLoop ());
  429. Func<bool> fnTrue = () => true;
  430. ml.AddIdle (fnTrue);
  431. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  432. Assert.True (retVal);
  433. Assert.Equal (-1, waitTimeOut);
  434. }
  435. [Fact]
  436. public void CheckTimer_With1Timer_Returns_Timer ()
  437. {
  438. var ml = new MainLoop (new FakeMainLoop ());
  439. var ms = TimeSpan.FromMilliseconds (50);
  440. var callbackCount = 0;
  441. Func<MainLoop, bool> callback = (loop) => {
  442. callbackCount++;
  443. return false;
  444. };
  445. var token = ml.AddTimeout (ms, callback);
  446. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  447. Assert.True (retVal);
  448. // It should take < 10ms to execute to here
  449. Assert.True (ms.TotalMilliseconds <= (waitTimeOut + 10));
  450. }
  451. [Fact]
  452. public void CheckTimer_With2Timers_Returns_Timer ()
  453. {
  454. var ml = new MainLoop (new FakeMainLoop ());
  455. var ms = TimeSpan.FromMilliseconds (50);
  456. var callbackCount = 0;
  457. Func<MainLoop, bool> callback = (loop) => {
  458. callbackCount++;
  459. return false;
  460. };
  461. var token1 = ml.AddTimeout (ms, callback);
  462. var token2 = ml.AddTimeout (ms, callback);
  463. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  464. Assert.True (retVal);
  465. // It should take < 10ms to execute to here
  466. Assert.True (ms.TotalMilliseconds <= (waitTimeOut + 10));
  467. }
  468. [Fact]
  469. public void Internal_Tests ()
  470. {
  471. var testMainloop = new TestMainloop ();
  472. var mainloop = new MainLoop (testMainloop);
  473. Assert.Empty (mainloop._timeouts);
  474. Assert.Empty (mainloop._idleHandlers);
  475. Assert.NotNull (new Timeout () {
  476. Span = new TimeSpan (),
  477. Callback = (_) => true
  478. });
  479. }
  480. private class TestMainloop : IMainLoopDriver {
  481. private MainLoop mainLoop;
  482. public bool EventsPending ()
  483. {
  484. throw new NotImplementedException ();
  485. }
  486. public void Iteration ()
  487. {
  488. throw new NotImplementedException ();
  489. }
  490. public void TearDown ()
  491. {
  492. throw new NotImplementedException ();
  493. }
  494. public void Setup (MainLoop mainLoop)
  495. {
  496. this.mainLoop = mainLoop;
  497. }
  498. public void Wakeup ()
  499. {
  500. throw new NotImplementedException ();
  501. }
  502. }
  503. // TODO: EventsPending tests
  504. // - wait = true
  505. // - wait = false
  506. // TODO: Add IMainLoop tests
  507. volatile static int tbCounter = 0;
  508. static ManualResetEventSlim _wakeUp = new ManualResetEventSlim (false);
  509. private static void Launch (Random r, TextField tf, int target)
  510. {
  511. Task.Run (() => {
  512. Thread.Sleep (r.Next (2, 4));
  513. Application.MainLoop.Invoke (() => {
  514. tf.Text = $"index{r.Next ()}";
  515. Interlocked.Increment (ref tbCounter);
  516. if (target == tbCounter) // On last increment wake up the check
  517. _wakeUp.Set ();
  518. });
  519. });
  520. }
  521. private static void RunTest (Random r, TextField tf, int numPasses, int numIncrements, int pollMs)
  522. {
  523. for (int j = 0; j < numPasses; j++) {
  524. _wakeUp.Reset ();
  525. for (var i = 0; i < numIncrements; i++) Launch (r, tf, (j + 1) * numIncrements);
  526. while (tbCounter != (j + 1) * numIncrements) // Wait for tbCounter to reach expected value
  527. {
  528. var tbNow = tbCounter;
  529. _wakeUp.Wait (pollMs);
  530. if (tbCounter == tbNow) {
  531. // No change after wait: Idle handlers added via Application.MainLoop.Invoke have gone missing
  532. Application.MainLoop.Invoke (() => Application.RequestStop ());
  533. throw new TimeoutException (
  534. $"Timeout: Increment lost. tbCounter ({tbCounter}) didn't " +
  535. $"change after waiting {pollMs} ms. Failed to reach {(j + 1) * numIncrements} on pass {j + 1}");
  536. }
  537. };
  538. }
  539. Application.MainLoop.Invoke (() => Application.RequestStop ());
  540. }
  541. [Fact]
  542. [AutoInitShutdown]
  543. public async Task InvokeLeakTest ()
  544. {
  545. Random r = new ();
  546. TextField tf = new ();
  547. Application.Top.Add (tf);
  548. const int numPasses = 5;
  549. const int numIncrements = 5000;
  550. const int pollMs = 10000;
  551. var task = Task.Run (() => RunTest (r, tf, numPasses, numIncrements, pollMs));
  552. // blocks here until the RequestStop is processed at the end of the test
  553. Application.Run ();
  554. await task; // Propagate exception if any occurred
  555. Assert.Equal (numIncrements * numPasses, tbCounter);
  556. }
  557. private static int total;
  558. private static Button btn;
  559. private static string clickMe;
  560. private static string cancel;
  561. private static string pewPew;
  562. private static int zero;
  563. private static int one;
  564. private static int two;
  565. private static int three;
  566. private static int four;
  567. private static bool taskCompleted;
  568. [Theory, AutoInitShutdown]
  569. [MemberData (nameof (TestAddIdle))]
  570. public void Mainloop_Invoke_Or_AddIdle_Can_Be_Used_For_Events_Or_Actions (Action action, string pclickMe, string pcancel, string ppewPew, int pzero, int pone, int ptwo, int pthree, int pfour)
  571. {
  572. total = 0;
  573. btn = null;
  574. clickMe = pclickMe;
  575. cancel = pcancel;
  576. pewPew = ppewPew;
  577. zero = pzero;
  578. one = pone;
  579. two = ptwo;
  580. three = pthree;
  581. four = pfour;
  582. taskCompleted = false;
  583. var btnLaunch = new Button ("Open Window");
  584. btnLaunch.Clicked += (s, e) => action ();
  585. Application.Top.Add (btnLaunch);
  586. var iterations = -1;
  587. Application.Iteration += () => {
  588. iterations++;
  589. if (iterations == 0) {
  590. Assert.Null (btn);
  591. Assert.Equal (zero, total);
  592. Assert.True (btnLaunch.ProcessKey (new KeyEvent (Key.Enter, null)));
  593. if (btn == null) {
  594. Assert.Null (btn);
  595. Assert.Equal (zero, total);
  596. } else {
  597. Assert.Equal (clickMe, btn.Text);
  598. Assert.Equal (four, total);
  599. }
  600. } else if (iterations == 1) {
  601. Assert.Equal (clickMe, btn.Text);
  602. Assert.Equal (zero, total);
  603. Assert.True (btn.ProcessKey (new KeyEvent (Key.Enter, null)));
  604. Assert.Equal (cancel, btn.Text);
  605. Assert.Equal (one, total);
  606. } else if (taskCompleted) {
  607. Application.RequestStop ();
  608. }
  609. };
  610. Application.Run ();
  611. Assert.True (taskCompleted);
  612. Assert.Equal (clickMe, btn.Text);
  613. Assert.Equal (four, total);
  614. }
  615. public static IEnumerable<object []> TestAddIdle {
  616. get {
  617. // Goes fine
  618. Action a1 = StartWindow;
  619. yield return new object [] { a1, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  620. // Also goes fine
  621. Action a2 = () => Application.MainLoop.Invoke (StartWindow);
  622. yield return new object [] { a2, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  623. }
  624. }
  625. private static void StartWindow ()
  626. {
  627. var startWindow = new Window {
  628. Modal = true
  629. };
  630. btn = new Button {
  631. Text = "Click Me"
  632. };
  633. btn.Clicked += RunAsyncTest;
  634. var totalbtn = new Button () {
  635. X = Pos.Right (btn),
  636. Text = "total"
  637. };
  638. totalbtn.Clicked += (s, e) => {
  639. MessageBox.Query ("Count", $"Count is {total}", "Ok");
  640. };
  641. startWindow.Add (btn);
  642. startWindow.Add (totalbtn);
  643. Application.Run (startWindow);
  644. Assert.Equal (clickMe, btn.Text);
  645. Assert.Equal (four, total);
  646. Application.RequestStop ();
  647. }
  648. private static async void RunAsyncTest (object sender, EventArgs e)
  649. {
  650. Assert.Equal (clickMe, btn.Text);
  651. Assert.Equal (zero, total);
  652. btn.Text = "Cancel";
  653. Interlocked.Increment (ref total);
  654. btn.SetNeedsDisplay ();
  655. await Task.Run (() => {
  656. try {
  657. Assert.Equal (cancel, btn.Text);
  658. Assert.Equal (one, total);
  659. RunSql ();
  660. } finally {
  661. SetReadyToRun ();
  662. }
  663. }).ContinueWith (async (s, e) => {
  664. await Task.Delay (1000);
  665. Assert.Equal (clickMe, btn.Text);
  666. Assert.Equal (three, total);
  667. Interlocked.Increment (ref total);
  668. Assert.Equal (clickMe, btn.Text);
  669. Assert.Equal (four, total);
  670. taskCompleted = true;
  671. }, TaskScheduler.FromCurrentSynchronizationContext ());
  672. }
  673. private static void RunSql ()
  674. {
  675. Thread.Sleep (100);
  676. Assert.Equal (cancel, btn.Text);
  677. Assert.Equal (one, total);
  678. Application.MainLoop.Invoke (() => {
  679. btn.Text = "Pew Pew";
  680. Interlocked.Increment (ref total);
  681. btn.SetNeedsDisplay ();
  682. });
  683. }
  684. private static void SetReadyToRun ()
  685. {
  686. Thread.Sleep (100);
  687. Assert.Equal (pewPew, btn.Text);
  688. Assert.Equal (two, total);
  689. Application.MainLoop.Invoke (() => {
  690. btn.Text = "Click Me";
  691. Interlocked.Increment (ref total);
  692. btn.SetNeedsDisplay ();
  693. });
  694. }
  695. }
  696. }