RunTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace ApplicationTests;
  4. public class RunTests
  5. {
  6. [Fact]
  7. public void Run_RequestStop_Stops ()
  8. {
  9. IApplication? app = Application.Create ();
  10. app.Init ("fake");
  11. var top = new Runnable ();
  12. SessionToken? sessionToken = app.Begin (top);
  13. Assert.NotNull (sessionToken);
  14. app.Iteration += OnApplicationOnIteration;
  15. app.Run (top);
  16. app.Iteration -= OnApplicationOnIteration;
  17. top.Dispose ();
  18. return;
  19. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a) { app.RequestStop (); }
  20. }
  21. [Fact]
  22. public void Run_T_Init_Driver_Cleared_with_Runnable_Throws ()
  23. {
  24. IApplication? app = Application.Create ();
  25. app.Init ("fake");
  26. app.Driver = null;
  27. app.StopAfterFirstIteration = true;
  28. // Init has been called, but Driver has been set to null. Bad.
  29. Assert.Throws<InvalidOperationException> (() => app.Run<Runnable> ());
  30. }
  31. [Fact]
  32. public void Run_Iteration_Fires ()
  33. {
  34. var iteration = 0;
  35. IApplication app = Application.Create ();
  36. app.Init ("fake");
  37. app.Iteration += Application_Iteration;
  38. app.Run<Runnable> ();
  39. app.Iteration -= Application_Iteration;
  40. Assert.Equal (1, iteration);
  41. app.Dispose ();
  42. return;
  43. void Application_Iteration (object? sender, EventArgs<IApplication?> e)
  44. {
  45. iteration++;
  46. app.RequestStop ();
  47. }
  48. }
  49. [Fact]
  50. public void Run_T_After_InitWithDriver_with_Runnable_and_Driver_Does_Not_Throw ()
  51. {
  52. IApplication app = Application.Create ();
  53. app.StopAfterFirstIteration = true;
  54. // Run<Runnable<bool>> when already initialized or not with a Driver will not throw (because Window is derived from Runnable)
  55. // Using another type not derived from Runnable will throws at compile time
  56. app.Run<Window> (null, "fake");
  57. // Run<Runnable<bool>> when already initialized or not with a Driver will not throw (because Dialog is derived from Runnable)
  58. app.Run<Dialog> (null, "fake");
  59. app.Dispose ();
  60. }
  61. [Fact]
  62. public void Run_T_After_Init_Does_Not_Disposes_Application_Top ()
  63. {
  64. IApplication app = Application.Create ();
  65. app.Init ("fake");
  66. // Init doesn't create a Runnable and assigned it to app.TopRunnable
  67. // but Begin does
  68. var initTop = new Runnable ();
  69. app.Iteration += OnApplicationOnIteration;
  70. app.Run<Runnable> ();
  71. app.Iteration -= OnApplicationOnIteration;
  72. #if DEBUG_IDISPOSABLE
  73. Assert.False (initTop.WasDisposed);
  74. initTop.Dispose ();
  75. Assert.True (initTop.WasDisposed);
  76. #endif
  77. initTop.Dispose ();
  78. app.Dispose ();
  79. return;
  80. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  81. {
  82. Assert.NotEqual (initTop, app.TopRunnableView);
  83. #if DEBUG_IDISPOSABLE
  84. Assert.False (initTop.WasDisposed);
  85. #endif
  86. app.RequestStop ();
  87. }
  88. }
  89. [Fact]
  90. public void Run_T_After_InitWithDriver_with_TestRunnable_DoesNotThrow ()
  91. {
  92. IApplication app = Application.Create ();
  93. app.Init ("fake");
  94. app.StopAfterFirstIteration = true;
  95. // Init has been called and we're passing no driver to Run<TestRunnable>. This is ok.
  96. app.Run<Window> ();
  97. app.Dispose ();
  98. }
  99. [Fact]
  100. public void Run_T_After_InitNullDriver_with_TestRunnable_DoesNotThrow ()
  101. {
  102. IApplication app = Application.Create ();
  103. app.Init ("fake");
  104. app.StopAfterFirstIteration = true;
  105. // Init has been called, selecting FakeDriver; we're passing no driver to Run<TestRunnable>. Should be fine.
  106. app.Run<Window> ();
  107. app.Dispose ();
  108. }
  109. [Fact]
  110. public void Run_T_NoInit_DoesNotThrow ()
  111. {
  112. IApplication app = Application.Create ();
  113. app.StopAfterFirstIteration = true;
  114. app.Run<Window> ();
  115. app.Dispose ();
  116. }
  117. [Fact]
  118. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  119. {
  120. IApplication app = Application.Create ();
  121. app.StopAfterFirstIteration = true;
  122. // Init has NOT been called and we're passing a valid driver to Run<TestRunnable>. This is ok.
  123. app.Run<Runnable> (null, "fake");
  124. app.Dispose ();
  125. }
  126. [Fact]
  127. public void Run_Sets_Running_True ()
  128. {
  129. IApplication app = Application.Create ();
  130. app.Init ("fake");
  131. var top = new Runnable ();
  132. SessionToken? rs = app.Begin (top);
  133. Assert.NotNull (rs);
  134. app.Iteration += OnApplicationOnIteration;
  135. app.Run (top);
  136. app.Iteration -= OnApplicationOnIteration;
  137. top.Dispose ();
  138. app.Dispose ();
  139. return;
  140. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  141. {
  142. Assert.True (top.IsRunning);
  143. top.RequestStop ();
  144. }
  145. }
  146. [Fact]
  147. public void Run_A_Modal_Runnable_Refresh_Background_On_Moving ()
  148. {
  149. IApplication app = Application.Create ();
  150. app.Init ("fake");
  151. // Don't use Dialog here as it has more layout logic. Use Window instead.
  152. var w = new Window
  153. {
  154. Width = 5, Height = 5,
  155. Arrangement = ViewArrangement.Movable
  156. };
  157. app.Driver!.SetScreenSize (10, 10);
  158. SessionToken? rs = app.Begin (w);
  159. // Don't use visuals to test as style of border can change over time.
  160. Assert.Equal (new (0, 0), w.Frame.Location);
  161. app.Mouse.RaiseMouseEvent (new () { Flags = MouseFlags.Button1Pressed });
  162. Assert.Equal (w.Border, app.Mouse.MouseGrabView);
  163. Assert.Equal (new (0, 0), w.Frame.Location);
  164. // Move down and to the right.
  165. app.Mouse.RaiseMouseEvent (new () { ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition });
  166. Assert.Equal (new (1, 1), w.Frame.Location);
  167. app.End (rs!);
  168. w.Dispose ();
  169. app.Dispose ();
  170. }
  171. [Fact]
  172. public void Run_T_Creates_Top_Without_Init ()
  173. {
  174. IApplication app = Application.Create ();
  175. app.StopAfterFirstIteration = true;
  176. app.SessionEnded += OnApplicationOnSessionEnded;
  177. app.Run<Window> (null, "fake");
  178. Assert.Null (app.TopRunnableView);
  179. app.Dispose ();
  180. Assert.Null (app.TopRunnableView);
  181. return;
  182. void OnApplicationOnSessionEnded (object? sender, SessionTokenEventArgs e)
  183. {
  184. app.SessionEnded -= OnApplicationOnSessionEnded;
  185. e.State.Result = (e.State.Runnable as IRunnable<object?>)?.Result;
  186. }
  187. }
  188. }