MainLoopTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 CheckTimersAndIdleHandlers_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 CheckTimersAndIdleHandlers_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 CheckTimersAndIdleHandlers_With1Timer_Returns_Timer ()
  437. {
  438. var ml = new MainLoop (new FakeMainLoop ());
  439. var ms = TimeSpan.FromMilliseconds (50);
  440. static bool Callback (MainLoop loop) => false;
  441. _ = ml.AddTimeout (ms, Callback);
  442. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  443. Assert.True (retVal);
  444. // It should take < 10ms to execute to here
  445. Assert.True (ms.TotalMilliseconds <= (waitTimeOut + 10));
  446. }
  447. [Fact]
  448. public void CheckTimersAndIdleHandlers_With2Timers_Returns_Timer ()
  449. {
  450. var ml = new MainLoop (new FakeMainLoop ());
  451. var ms = TimeSpan.FromMilliseconds (50);
  452. static bool Callback (MainLoop loop) => false;
  453. _ = ml.AddTimeout (ms, Callback);
  454. _ = ml.AddTimeout (ms, Callback);
  455. var retVal = ml.CheckTimersAndIdleHandlers (out var waitTimeOut);
  456. Assert.True (retVal);
  457. // It should take < 10ms to execute to here
  458. Assert.True (ms.TotalMilliseconds <= (waitTimeOut + 10));
  459. }
  460. [Fact]
  461. public void Internal_Tests ()
  462. {
  463. var testMainloop = new TestMainloop ();
  464. var mainloop = new MainLoop (testMainloop);
  465. Assert.Empty (mainloop._timeouts);
  466. Assert.Empty (mainloop._idleHandlers);
  467. Assert.NotNull (new Timeout () {
  468. Span = new TimeSpan (),
  469. Callback = (_) => true
  470. });
  471. }
  472. private class TestMainloop : IMainLoopDriver {
  473. private MainLoop mainLoop;
  474. public bool EventsPending ()
  475. {
  476. throw new NotImplementedException ();
  477. }
  478. public void Iteration ()
  479. {
  480. throw new NotImplementedException ();
  481. }
  482. public void TearDown ()
  483. {
  484. throw new NotImplementedException ();
  485. }
  486. public void Setup (MainLoop mainLoop)
  487. {
  488. this.mainLoop = mainLoop;
  489. }
  490. public void Wakeup ()
  491. {
  492. throw new NotImplementedException ();
  493. }
  494. }
  495. // TODO: EventsPending tests
  496. // - wait = true
  497. // - wait = false
  498. // TODO: Add IMainLoop tests
  499. volatile static int tbCounter = 0;
  500. static ManualResetEventSlim _wakeUp = new ManualResetEventSlim (false);
  501. private static void Launch (Random r, TextField tf, int target)
  502. {
  503. Task.Run (() => {
  504. Thread.Sleep (r.Next (2, 4));
  505. Application.MainLoop.Invoke (() => {
  506. tf.Text = $"index{r.Next ()}";
  507. Interlocked.Increment (ref tbCounter);
  508. if (target == tbCounter) // On last increment wake up the check
  509. _wakeUp.Set ();
  510. });
  511. });
  512. }
  513. private static void RunTest (Random r, TextField tf, int numPasses, int numIncrements, int pollMs)
  514. {
  515. for (int j = 0; j < numPasses; j++) {
  516. _wakeUp.Reset ();
  517. for (var i = 0; i < numIncrements; i++) Launch (r, tf, (j + 1) * numIncrements);
  518. while (tbCounter != (j + 1) * numIncrements) // Wait for tbCounter to reach expected value
  519. {
  520. var tbNow = tbCounter;
  521. _wakeUp.Wait (pollMs);
  522. if (tbCounter == tbNow) {
  523. // No change after wait: Idle handlers added via Application.MainLoop.Invoke have gone missing
  524. Application.MainLoop.Invoke (() => Application.RequestStop ());
  525. throw new TimeoutException (
  526. $"Timeout: Increment lost. tbCounter ({tbCounter}) didn't " +
  527. $"change after waiting {pollMs} ms. Failed to reach {(j + 1) * numIncrements} on pass {j + 1}");
  528. }
  529. };
  530. }
  531. Application.MainLoop.Invoke (() => Application.RequestStop ());
  532. }
  533. [Fact]
  534. [AutoInitShutdown]
  535. public async Task InvokeLeakTest ()
  536. {
  537. Random r = new ();
  538. TextField tf = new ();
  539. Application.Top.Add (tf);
  540. const int numPasses = 5;
  541. const int numIncrements = 5000;
  542. const int pollMs = 10000;
  543. var task = Task.Run (() => RunTest (r, tf, numPasses, numIncrements, pollMs));
  544. // blocks here until the RequestStop is processed at the end of the test
  545. Application.Run ();
  546. await task; // Propagate exception if any occurred
  547. Assert.Equal (numIncrements * numPasses, tbCounter);
  548. }
  549. private static int total;
  550. private static Button btn;
  551. private static string clickMe;
  552. private static string cancel;
  553. private static string pewPew;
  554. private static int zero;
  555. private static int one;
  556. private static int two;
  557. private static int three;
  558. private static int four;
  559. private static bool taskCompleted;
  560. [Theory, AutoInitShutdown]
  561. [MemberData (nameof (TestAddIdle))]
  562. 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)
  563. {
  564. total = 0;
  565. btn = null;
  566. clickMe = pclickMe;
  567. cancel = pcancel;
  568. pewPew = ppewPew;
  569. zero = pzero;
  570. one = pone;
  571. two = ptwo;
  572. three = pthree;
  573. four = pfour;
  574. taskCompleted = false;
  575. var btnLaunch = new Button ("Open Window");
  576. btnLaunch.Clicked += (s, e) => action ();
  577. Application.Top.Add (btnLaunch);
  578. var iterations = -1;
  579. Application.Iteration += () => {
  580. iterations++;
  581. if (iterations == 0) {
  582. Assert.Null (btn);
  583. Assert.Equal (zero, total);
  584. Assert.True (btnLaunch.ProcessKey (new KeyEvent (Key.Enter, null)));
  585. if (btn == null) {
  586. Assert.Null (btn);
  587. Assert.Equal (zero, total);
  588. } else {
  589. Assert.Equal (clickMe, btn.Text);
  590. Assert.Equal (four, total);
  591. }
  592. } else if (iterations == 1) {
  593. Assert.Equal (clickMe, btn.Text);
  594. Assert.Equal (zero, total);
  595. Assert.True (btn.ProcessKey (new KeyEvent (Key.Enter, null)));
  596. Assert.Equal (cancel, btn.Text);
  597. Assert.Equal (one, total);
  598. } else if (taskCompleted) {
  599. Application.RequestStop ();
  600. }
  601. };
  602. Application.Run ();
  603. Assert.True (taskCompleted);
  604. Assert.Equal (clickMe, btn.Text);
  605. Assert.Equal (four, total);
  606. }
  607. public static IEnumerable<object []> TestAddIdle {
  608. get {
  609. // Goes fine
  610. Action a1 = StartWindow;
  611. yield return new object [] { a1, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  612. // Also goes fine
  613. Action a2 = () => Application.MainLoop.Invoke (StartWindow);
  614. yield return new object [] { a2, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  615. }
  616. }
  617. private static void StartWindow ()
  618. {
  619. var startWindow = new Window {
  620. Modal = true
  621. };
  622. btn = new Button {
  623. Text = "Click Me"
  624. };
  625. btn.Clicked += RunAsyncTest;
  626. var totalbtn = new Button () {
  627. X = Pos.Right (btn),
  628. Text = "total"
  629. };
  630. totalbtn.Clicked += (s, e) => {
  631. MessageBox.Query ("Count", $"Count is {total}", "Ok");
  632. };
  633. startWindow.Add (btn);
  634. startWindow.Add (totalbtn);
  635. Application.Run (startWindow);
  636. Assert.Equal (clickMe, btn.Text);
  637. Assert.Equal (four, total);
  638. Application.RequestStop ();
  639. }
  640. private static async void RunAsyncTest (object sender, EventArgs e)
  641. {
  642. Assert.Equal (clickMe, btn.Text);
  643. Assert.Equal (zero, total);
  644. btn.Text = "Cancel";
  645. Interlocked.Increment (ref total);
  646. btn.SetNeedsDisplay ();
  647. await Task.Run (() => {
  648. try {
  649. Assert.Equal (cancel, btn.Text);
  650. Assert.Equal (one, total);
  651. RunSql ();
  652. } finally {
  653. SetReadyToRun ();
  654. }
  655. }).ContinueWith (async (s, e) => {
  656. await Task.Delay (1000);
  657. Assert.Equal (clickMe, btn.Text);
  658. Assert.Equal (three, total);
  659. Interlocked.Increment (ref total);
  660. Assert.Equal (clickMe, btn.Text);
  661. Assert.Equal (four, total);
  662. taskCompleted = true;
  663. }, TaskScheduler.FromCurrentSynchronizationContext ());
  664. }
  665. private static void RunSql ()
  666. {
  667. Thread.Sleep (100);
  668. Assert.Equal (cancel, btn.Text);
  669. Assert.Equal (one, total);
  670. Application.MainLoop.Invoke (() => {
  671. btn.Text = "Pew Pew";
  672. Interlocked.Increment (ref total);
  673. btn.SetNeedsDisplay ();
  674. });
  675. }
  676. private static void SetReadyToRun ()
  677. {
  678. Thread.Sleep (100);
  679. Assert.Equal (pewPew, btn.Text);
  680. Assert.Equal (two, total);
  681. Application.MainLoop.Invoke (() => {
  682. btn.Text = "Click Me";
  683. Interlocked.Increment (ref total);
  684. btn.SetNeedsDisplay ();
  685. });
  686. }
  687. }
  688. }