MainLoopTests.cs 25 KB

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