MainLoopTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. using System.Diagnostics;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. namespace UnitTests.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. // Use Stopwatch ticks since TimedEvents now uses Stopwatch.GetTimestamp internally
  188. long originTicks = Stopwatch.GetTimestamp () * TimeSpan.TicksPerSecond / Stopwatch.Frequency;
  189. var callbackCount = 0;
  190. Func<bool> callback = () =>
  191. {
  192. callbackCount++;
  193. return true;
  194. };
  195. object sender = null;
  196. TimeoutEventArgs args = null;
  197. ml.TimedEvents.Added += (s, e) =>
  198. {
  199. sender = s;
  200. args = e;
  201. };
  202. object token = ml.TimedEvents.Add (TimeSpan.FromMilliseconds (ms), callback);
  203. Assert.Same (ml.TimedEvents, sender);
  204. Assert.NotNull (args.Timeout);
  205. Assert.True (args.Ticks - originTicks >= 100 * TimeSpan.TicksPerMillisecond);
  206. }
  207. [Fact]
  208. public void AddTimer_In_Parallel_Wont_Throw ()
  209. {
  210. var ml = new MainLoop (new FakeMainLoop ());
  211. const int ms = 100;
  212. object token1 = null, token2 = null;
  213. var callbackCount = 0;
  214. Func<bool> callback = () =>
  215. {
  216. callbackCount++;
  217. if (callbackCount == 2)
  218. {
  219. ml.Stop ();
  220. }
  221. return true;
  222. };
  223. Parallel.Invoke (
  224. () => token1 = ml.TimedEvents.Add (TimeSpan.FromMilliseconds (ms), callback),
  225. () => token2 = ml.TimedEvents.Add (TimeSpan.FromMilliseconds (ms), callback)
  226. );
  227. ml.Run ();
  228. Assert.NotNull (token1);
  229. Assert.NotNull (token2);
  230. Assert.True (ml.TimedEvents.Remove (token1));
  231. Assert.True (ml.TimedEvents.Remove (token2));
  232. Assert.Equal (2, callbackCount);
  233. }
  234. [Fact]
  235. public void AddTimer_Remove_NotCalled ()
  236. {
  237. var ml = new MainLoop (new FakeMainLoop ());
  238. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  239. // Force stop if 10 iterations
  240. var stopCount = 0;
  241. Func<bool> fnStop = () =>
  242. {
  243. stopCount++;
  244. if (stopCount == 10)
  245. {
  246. ml.Stop ();
  247. }
  248. return true;
  249. };
  250. ml.TimedEvents.Add (TimeSpan.Zero, fnStop);
  251. var callbackCount = 0;
  252. Func<bool> callback = () =>
  253. {
  254. callbackCount++;
  255. return true;
  256. };
  257. object token = ml.TimedEvents.Add (ms, callback);
  258. Assert.True (ml.TimedEvents.Remove (token));
  259. ml.Run ();
  260. Assert.Equal (0, callbackCount);
  261. }
  262. [Fact]
  263. public void AddTimer_ReturnFalse_StopsBeingCalled ()
  264. {
  265. var ml = new MainLoop (new FakeMainLoop ());
  266. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  267. // Force stop if 10 iterations
  268. var stopCount = 0;
  269. Func<bool> fnStop = () =>
  270. {
  271. Thread.Sleep (10); // Sleep to enable timer to fire
  272. stopCount++;
  273. if (stopCount == 10)
  274. {
  275. ml.Stop ();
  276. }
  277. return true;
  278. };
  279. ml.TimedEvents.Add (TimeSpan.Zero, fnStop);
  280. var callbackCount = 0;
  281. Func<bool> callback = () =>
  282. {
  283. callbackCount++;
  284. return false;
  285. };
  286. object token = ml.TimedEvents.Add (ms, callback);
  287. ml.Run ();
  288. Assert.Equal (1, callbackCount);
  289. Assert.Equal (10, stopCount);
  290. Assert.False (ml.TimedEvents.Remove (token));
  291. }
  292. [Fact]
  293. public void AddTimer_Run_Called ()
  294. {
  295. var ml = new MainLoop (new FakeMainLoop ());
  296. var ms = 100;
  297. var callbackCount = 0;
  298. Func<bool> callback = () =>
  299. {
  300. callbackCount++;
  301. ml.Stop ();
  302. return true;
  303. };
  304. object token = ml.TimedEvents.Add (TimeSpan.FromMilliseconds (ms), callback);
  305. ml.Run ();
  306. Assert.True (ml.TimedEvents.Remove (token));
  307. Assert.Equal (1, callbackCount);
  308. }
  309. [Fact]
  310. public void AddTimer_Run_CalledAtApproximatelyRightTime ()
  311. {
  312. var ml = new MainLoop (new FakeMainLoop ());
  313. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  314. var watch = new Stopwatch ();
  315. var callbackCount = 0;
  316. Func<bool> callback = () =>
  317. {
  318. watch.Stop ();
  319. callbackCount++;
  320. ml.Stop ();
  321. return true;
  322. };
  323. object token = ml.TimedEvents.Add (ms, callback);
  324. watch.Start ();
  325. ml.Run ();
  326. // +/- 100ms should be good enuf
  327. // https://github.com/xunit/assert.xunit/pull/25
  328. Assert.Equal (ms * callbackCount, watch.Elapsed, new MillisecondTolerance (100));
  329. Assert.True (ml.TimedEvents.Remove (token));
  330. Assert.Equal (1, callbackCount);
  331. }
  332. [Fact]
  333. public void AddTimer_Run_CalledTwiceApproximatelyRightTime ()
  334. {
  335. var ml = new MainLoop (new FakeMainLoop ());
  336. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  337. var watch = new Stopwatch ();
  338. var callbackCount = 0;
  339. Func<bool> callback = () =>
  340. {
  341. callbackCount++;
  342. if (callbackCount == 2)
  343. {
  344. watch.Stop ();
  345. ml.Stop ();
  346. }
  347. return true;
  348. };
  349. object token = ml.TimedEvents.Add (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.TimedEvents.Remove (token));
  356. Assert.Equal (2, callbackCount);
  357. }
  358. [Fact]
  359. public void CheckTimersAndIdleHandlers_NoTimers_Returns_False ()
  360. {
  361. var ml = new MainLoop (new FakeMainLoop ());
  362. bool retVal = ml.TimedEvents.CheckTimers(out int waitTimeOut);
  363. Assert.False (retVal);
  364. Assert.Equal (-1, waitTimeOut);
  365. }
  366. [Fact]
  367. public void CheckTimersAndIdleHandlers_NoTimers_WithIdle_Returns_True ()
  368. {
  369. var ml = new MainLoop (new FakeMainLoop ());
  370. Func<bool> fnTrue = () => true;
  371. ml.TimedEvents.Add (TimeSpan.Zero, fnTrue);
  372. bool retVal = ml.TimedEvents.CheckTimers(out int waitTimeOut);
  373. Assert.True (retVal);
  374. Assert.Equal (0, waitTimeOut);
  375. }
  376. [Fact]
  377. public void CheckTimersAndIdleHandlers_With1Timer_Returns_Timer ()
  378. {
  379. var ml = new MainLoop (new FakeMainLoop ());
  380. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  381. static bool Callback () { return false; }
  382. _ = ml.TimedEvents.Add (ms, Callback);
  383. bool retVal = ml.TimedEvents.CheckTimers (out int waitTimeOut);
  384. Assert.True (retVal);
  385. // It should take < 10ms to execute to here
  386. Assert.True (ms.TotalMilliseconds <= waitTimeOut + 10);
  387. }
  388. [Fact]
  389. public void CheckTimersAndIdleHandlers_With2Timers_Returns_Timer ()
  390. {
  391. var ml = new MainLoop (new FakeMainLoop ());
  392. TimeSpan ms = TimeSpan.FromMilliseconds (50);
  393. static bool Callback () { return false; }
  394. _ = ml.TimedEvents.Add (ms, Callback);
  395. _ = ml.TimedEvents.Add (ms, Callback);
  396. bool retVal = ml.TimedEvents.CheckTimers (out int waitTimeOut);
  397. Assert.True (retVal);
  398. // It should take < 10ms to execute to here
  399. Assert.True (ms.TotalMilliseconds <= waitTimeOut + 10);
  400. }
  401. [Fact]
  402. public void False_Idle_Stops_It_Being_Called_Again ()
  403. {
  404. var ml = new MainLoop (new FakeMainLoop ());
  405. var functionCalled = 0;
  406. Func<bool> fn1 = () =>
  407. {
  408. functionCalled++;
  409. if (functionCalled == 10)
  410. {
  411. return false;
  412. }
  413. return true;
  414. };
  415. // Force stop if 20 iterations
  416. var stopCount = 0;
  417. Func<bool> fnStop = () =>
  418. {
  419. stopCount++;
  420. if (stopCount == 20)
  421. {
  422. ml.Stop ();
  423. }
  424. return true;
  425. };
  426. var a = ml.TimedEvents.Add (TimeSpan.Zero, fnStop);
  427. var b = ml.TimedEvents.Add (TimeSpan.Zero, fn1);
  428. ml.Run ();
  429. Assert.True (ml.TimedEvents.Remove (a));
  430. Assert.False (ml.TimedEvents.Remove (a));
  431. Assert.Equal (10, functionCalled);
  432. Assert.Equal (20, stopCount);
  433. }
  434. [Fact]
  435. public void Internal_Tests ()
  436. {
  437. var testMainloop = new TestMainloop ();
  438. var mainloop = new MainLoop (testMainloop);
  439. Assert.Empty (mainloop.TimedEvents.Timeouts);
  440. Assert.NotNull (
  441. new Terminal.Gui.App.Timeout { Span = new (), Callback = () => true }
  442. );
  443. }
  444. [Theory]
  445. [MemberData (nameof (TestAddTimeout))]
  446. public void Mainloop_Invoke_Or_AddTimeout_Can_Be_Used_For_Events_Or_Actions (
  447. Action action,
  448. string pclickMe,
  449. string pcancel,
  450. string ppewPew,
  451. int pzero,
  452. int pone,
  453. int ptwo,
  454. int pthree,
  455. int pfour
  456. )
  457. {
  458. // TODO: Expand this test to test all drivers
  459. Application.Init (null, "fakedriver");
  460. total = 0;
  461. btn = null;
  462. clickMe = pclickMe;
  463. cancel = pcancel;
  464. pewPew = ppewPew;
  465. zero = pzero;
  466. one = pone;
  467. two = ptwo;
  468. three = pthree;
  469. four = pfour;
  470. taskCompleted = false;
  471. var btnLaunch = new Button { Text = "Open Window" };
  472. btnLaunch.Accepting += (s, e) => action ();
  473. var top = new Toplevel ();
  474. top.Add (btnLaunch);
  475. int iterations = -1;
  476. Application.Iteration += (s, a) =>
  477. {
  478. iterations++;
  479. if (iterations == 0)
  480. {
  481. Assert.Null (btn);
  482. Assert.Equal (zero, total);
  483. Assert.False (btnLaunch.NewKeyDownEvent (Key.Space));
  484. if (btn == null)
  485. {
  486. Assert.Null (btn);
  487. Assert.Equal (zero, total);
  488. }
  489. else
  490. {
  491. Assert.Equal (clickMe, btn.Text);
  492. Assert.Equal (four, total);
  493. }
  494. }
  495. else if (iterations == 1)
  496. {
  497. Assert.Equal (clickMe, btn.Text);
  498. Assert.Equal (zero, total);
  499. Assert.False (btn.NewKeyDownEvent (Key.Space));
  500. Assert.Equal (cancel, btn.Text);
  501. Assert.Equal (one, total);
  502. }
  503. else if (taskCompleted)
  504. {
  505. Application.RequestStop ();
  506. }
  507. };
  508. Application.Run (top);
  509. top.Dispose ();
  510. Assert.True (taskCompleted);
  511. Assert.Equal (clickMe, btn.Text);
  512. Assert.Equal (four, total);
  513. Application.Shutdown ();
  514. }
  515. [Fact]
  516. public void RemoveIdle_Function_NotCalled ()
  517. {
  518. var ml = new MainLoop (new FakeMainLoop ());
  519. var functionCalled = 0;
  520. Func<bool> fn = () =>
  521. {
  522. functionCalled++;
  523. return true;
  524. };
  525. Assert.False (ml.TimedEvents.Remove ("flibble"));
  526. ml.RunIteration ();
  527. Assert.Equal (0, functionCalled);
  528. }
  529. [Fact]
  530. public void Run_Runs_Idle_Stop_Stops_Idle ()
  531. {
  532. var ml = new MainLoop (new FakeMainLoop ());
  533. var functionCalled = 0;
  534. Func<bool> fn = () =>
  535. {
  536. functionCalled++;
  537. if (functionCalled == 10)
  538. {
  539. ml.Stop ();
  540. }
  541. return true;
  542. };
  543. var a = ml.TimedEvents.Add (TimeSpan.Zero, fn);
  544. ml.Run ();
  545. Assert.True (ml.TimedEvents.Remove (a));
  546. Assert.Equal (10, functionCalled);
  547. }
  548. public static IEnumerable<object []> TestAddTimeout
  549. {
  550. get
  551. {
  552. // Goes fine
  553. Action a1 = StartWindow;
  554. yield return new object [] { a1, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  555. // Also goes fine
  556. Action a2 = () => Application.Invoke (StartWindow);
  557. yield return new object [] { a2, "Click Me", "Cancel", "Pew Pew", 0, 1, 2, 3, 4 };
  558. }
  559. }
  560. private static async void RunAsyncTest (object sender, EventArgs e)
  561. {
  562. Assert.Equal (clickMe, btn.Text);
  563. Assert.Equal (zero, total);
  564. btn.Text = "Cancel";
  565. Interlocked.Increment (ref total);
  566. btn.SetNeedsDraw ();
  567. await Task.Run (
  568. () =>
  569. {
  570. try
  571. {
  572. Assert.Equal (cancel, btn.Text);
  573. Assert.Equal (one, total);
  574. RunSql ();
  575. }
  576. finally
  577. {
  578. SetReadyToRun ();
  579. }
  580. }
  581. )
  582. .ContinueWith (
  583. async (s, e) =>
  584. {
  585. await Task.Delay (1000);
  586. Assert.Equal (clickMe, btn.Text);
  587. Assert.Equal (three, total);
  588. Interlocked.Increment (ref total);
  589. Assert.Equal (clickMe, btn.Text);
  590. Assert.Equal (four, total);
  591. taskCompleted = true;
  592. },
  593. TaskScheduler.FromCurrentSynchronizationContext ()
  594. );
  595. }
  596. private static void RunSql ()
  597. {
  598. Thread.Sleep (100);
  599. Assert.Equal (cancel, btn.Text);
  600. Assert.Equal (one, total);
  601. Application.Invoke (
  602. () =>
  603. {
  604. btn.Text = "Pew Pew";
  605. Interlocked.Increment (ref total);
  606. btn.SetNeedsDraw ();
  607. }
  608. );
  609. }
  610. private static void SetReadyToRun ()
  611. {
  612. Thread.Sleep (100);
  613. Assert.Equal (pewPew, btn.Text);
  614. Assert.Equal (two, total);
  615. Application.Invoke (
  616. () =>
  617. {
  618. btn.Text = "Click Me";
  619. Interlocked.Increment (ref total);
  620. btn.SetNeedsDraw ();
  621. }
  622. );
  623. }
  624. private static void StartWindow ()
  625. {
  626. var startWindow = new Window { Modal = true };
  627. btn = new() { Text = "Click Me" };
  628. btn.Accepting += RunAsyncTest;
  629. var totalbtn = new Button { X = Pos.Right (btn), Text = "total" };
  630. totalbtn.Accepting += (s, e) => { MessageBox.Query ("Count", $"Count is {total}", "Ok"); };
  631. startWindow.Add (btn);
  632. startWindow.Add (totalbtn);
  633. Application.Run (startWindow);
  634. Assert.Equal (clickMe, btn.Text);
  635. Assert.Equal (four, total);
  636. Application.RequestStop ();
  637. }
  638. private class MillisecondTolerance : IEqualityComparer<TimeSpan>
  639. {
  640. public MillisecondTolerance (int tolerance) { _tolerance = tolerance; }
  641. private readonly int _tolerance;
  642. public bool Equals (TimeSpan x, TimeSpan y) { return Math.Abs (x.Milliseconds - y.Milliseconds) <= _tolerance; }
  643. public int GetHashCode (TimeSpan obj) { return obj.GetHashCode (); }
  644. }
  645. private class TestMainloop : IMainLoopDriver
  646. {
  647. private MainLoop mainLoop;
  648. public bool EventsPending () { throw new NotImplementedException (); }
  649. public void Iteration () { throw new NotImplementedException (); }
  650. public void TearDown () { throw new NotImplementedException (); }
  651. public void Setup (MainLoop mainLoop) { this.mainLoop = mainLoop; }
  652. public void Wakeup () { throw new NotImplementedException (); }
  653. }
  654. }