MainLoopDriverTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.DriverTests;
  11. public class MainLoopDriverTests {
  12. public MainLoopDriverTests (ITestOutputHelper output)
  13. {
  14. ConsoleDriver.RunningUnitTests = true;
  15. }
  16. [Theory]
  17. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  18. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  19. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  20. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  21. public void MainLoop_Constructs_Disposes (Type driverType, Type mainLoopDriverType)
  22. {
  23. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  24. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  25. var mainLoop = new MainLoop (mainLoopDriver);
  26. // Check default values
  27. Assert.NotNull (mainLoop);
  28. Assert.Equal (mainLoopDriver, mainLoop.MainLoopDriver);
  29. Assert.Empty (mainLoop.IdleHandlers);
  30. Assert.Empty (mainLoop.Timeouts);
  31. Assert.False (mainLoop.Running);
  32. // Clean up
  33. mainLoop.Dispose ();
  34. // TODO: It'd be nice if we could really verify IMainLoopDriver.TearDown was called
  35. // and that it was actually cleaned up.
  36. Assert.Null (mainLoop.MainLoopDriver);
  37. Assert.Empty (mainLoop.IdleHandlers);
  38. Assert.Empty (mainLoop.Timeouts);
  39. Assert.False (mainLoop.Running);
  40. }
  41. [Theory]
  42. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  43. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  44. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  45. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  46. public void MainLoop_AddTimeout_ValidParameters_ReturnsToken (Type driverType, Type mainLoopDriverType)
  47. {
  48. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  49. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  50. var mainLoop = new MainLoop (mainLoopDriver);
  51. var callbackInvoked = false;
  52. var token = mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => {
  53. callbackInvoked = true;
  54. return false;
  55. });
  56. Assert.NotNull (token);
  57. mainLoop.RunIteration (); // Run an iteration to process the timeout
  58. Assert.False (callbackInvoked); // Callback should not be invoked immediately
  59. Thread.Sleep (200); // Wait for the timeout
  60. mainLoop.RunIteration (); // Run an iteration to process the timeout
  61. Assert.True (callbackInvoked); // Callback should be invoked after the timeout
  62. mainLoop.Dispose ();
  63. }
  64. [Theory]
  65. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  66. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  67. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  68. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  69. public void MainLoop_RemoveTimeout_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  70. {
  71. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  72. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  73. var mainLoop = new MainLoop (mainLoopDriver);
  74. var token = mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => false);
  75. var result = mainLoop.RemoveTimeout (token);
  76. Assert.True (result);
  77. mainLoop.Dispose ();
  78. }
  79. [Theory]
  80. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  81. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  82. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  83. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  84. public void MainLoop_RemoveTimeout_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
  85. {
  86. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  87. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  88. var mainLoop = new MainLoop (mainLoopDriver);
  89. var result = mainLoop.RemoveTimeout (new object ());
  90. Assert.False (result);
  91. }
  92. [Theory]
  93. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  94. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  95. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  96. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  97. public void MainLoop_AddIdle_ValidIdleHandler_ReturnsToken (Type driverType, Type mainLoopDriverType)
  98. {
  99. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  100. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  101. var mainLoop = new MainLoop (mainLoopDriver);
  102. var idleHandlerInvoked = false;
  103. bool IdleHandler ()
  104. {
  105. idleHandlerInvoked = true;
  106. return false;
  107. }
  108. Func<bool> token = mainLoop.AddIdle (IdleHandler);
  109. Assert.NotNull (token);
  110. Assert.False (idleHandlerInvoked); // Idle handler should not be invoked immediately
  111. mainLoop.RunIteration (); // Run an iteration to process the idle handler
  112. Assert.True (idleHandlerInvoked); // Idle handler should be invoked after processing
  113. mainLoop.Dispose ();
  114. }
  115. [Theory]
  116. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  117. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  118. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  119. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  120. public void MainLoop_RemoveIdle_ValidToken_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  121. {
  122. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  123. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  124. var mainLoop = new MainLoop (mainLoopDriver);
  125. bool IdleHandler () => false;
  126. Func<bool> token = mainLoop.AddIdle (IdleHandler);
  127. var result = mainLoop.RemoveIdle (token);
  128. Assert.True (result);
  129. mainLoop.Dispose ();
  130. }
  131. [Theory]
  132. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  133. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  134. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  135. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  136. public void MainLoop_RemoveIdle_InvalidToken_ReturnsFalse (Type driverType, Type mainLoopDriverType)
  137. {
  138. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  139. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  140. var mainLoop = new MainLoop (mainLoopDriver);
  141. var result = mainLoop.RemoveIdle (() => false);
  142. Assert.False (result);
  143. mainLoop.Dispose ();
  144. }
  145. [Theory]
  146. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  147. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  148. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  149. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  150. public void MainLoop_RunIteration_ValidIdleHandler_CallsIdleHandler (Type driverType, Type mainLoopDriverType)
  151. {
  152. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  153. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  154. var mainLoop = new MainLoop (mainLoopDriver);
  155. var idleHandlerInvoked = false;
  156. Func<bool> idleHandler = () => {
  157. idleHandlerInvoked = true;
  158. return false;
  159. };
  160. mainLoop.AddIdle (idleHandler);
  161. mainLoop.RunIteration (); // Run an iteration to process the idle handler
  162. Assert.True (idleHandlerInvoked);
  163. mainLoop.Dispose ();
  164. }
  165. [Theory]
  166. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  167. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  168. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  169. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  170. public void MainLoop_CheckTimersAndIdleHandlers_NoTimersOrIdleHandlers_ReturnsFalse (Type driverType, Type mainLoopDriverType)
  171. {
  172. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  173. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  174. var mainLoop = new MainLoop (mainLoopDriver);
  175. var result = mainLoop.CheckTimersAndIdleHandlers (out var waitTimeout);
  176. Assert.False (result);
  177. Assert.Equal (-1, waitTimeout);
  178. mainLoop.Dispose ();
  179. }
  180. [Theory]
  181. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  182. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  183. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  184. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  185. public void MainLoop_CheckTimersAndIdleHandlers_TimersActive_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  186. {
  187. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  188. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  189. var mainLoop = new MainLoop (mainLoopDriver);
  190. mainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), () => false);
  191. var result = mainLoop.CheckTimersAndIdleHandlers (out var waitTimeout);
  192. Assert.True (result);
  193. Assert.True (waitTimeout >= 0);
  194. mainLoop.Dispose ();
  195. }
  196. [Theory]
  197. [InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  198. [InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  199. [InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  200. [InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  201. public void MainLoop_CheckTimersAndIdleHandlers_IdleHandlersActive_ReturnsTrue (Type driverType, Type mainLoopDriverType)
  202. {
  203. var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  204. var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  205. var mainLoop = new MainLoop (mainLoopDriver);
  206. mainLoop.AddIdle (() => false);
  207. var result = mainLoop.CheckTimersAndIdleHandlers (out var waitTimeout);
  208. Assert.True (result);
  209. Assert.Equal (-1, waitTimeout);
  210. mainLoop.Dispose ();
  211. }
  212. //[Theory]
  213. //[InlineData (typeof (FakeDriver), typeof (FakeMainLoop))]
  214. //[InlineData (typeof (NetDriver), typeof (NetMainLoop))]
  215. //[InlineData (typeof (CursesDriver), typeof (UnixMainLoop))]
  216. //[InlineData (typeof (WindowsDriver), typeof (WindowsMainLoop))]
  217. //public void MainLoop_Invoke_ValidAction_RunsAction (Type driverType, Type mainLoopDriverType)
  218. //{
  219. // var driver = (ConsoleDriver)Activator.CreateInstance (driverType);
  220. // var mainLoopDriver = (IMainLoopDriver)Activator.CreateInstance (mainLoopDriverType, new object [] { driver });
  221. // var mainLoop = new MainLoop (mainLoopDriver);
  222. // var actionInvoked = false;
  223. // mainLoop.Invoke (() => { actionInvoked = true; });
  224. // mainLoop.RunIteration (); // Run an iteration to process the action.
  225. // Assert.True (actionInvoked);
  226. // mainLoop.Dispose ();
  227. //}
  228. }