MainLoopDriverTests.cs 13 KB

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