RunnableIntegrationTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace ApplicationTests.RunnableTests;
  4. /// <summary>
  5. /// Integration tests for IApplication's IRunnable support.
  6. /// Tests the full lifecycle of IRunnable instances through Application methods.
  7. /// </summary>
  8. public class ApplicationRunnableIntegrationTests
  9. {
  10. [Fact]
  11. public void Begin_AddsRunnableToStack ()
  12. {
  13. // Arrange
  14. IApplication app = CreateAndInitApp ();
  15. Runnable<int> runnable = new ();
  16. int stackCountBefore = app.SessionStack?.Count ?? 0;
  17. // Act
  18. SessionToken? token = app.Begin (runnable);
  19. // Assert
  20. Assert.NotNull (token);
  21. Assert.NotNull (token.Runnable);
  22. Assert.Same (runnable, token.Runnable);
  23. Assert.Equal (stackCountBefore + 1, app.SessionStack?.Count ?? 0);
  24. // Cleanup
  25. app.End (token!);
  26. }
  27. [Fact]
  28. public void Begin_CanBeCanceled_ByIsRunningChanging ()
  29. {
  30. // Arrange
  31. IApplication app = CreateAndInitApp ();
  32. CancelableRunnable runnable = new () { CancelStart = true };
  33. // Act
  34. SessionToken? token = app.Begin (runnable);
  35. // Assert - Should not be added to stack if canceled
  36. Assert.False (runnable.IsRunning);
  37. // Token not created
  38. Assert.Null (token);
  39. }
  40. [Fact]
  41. public void Begin_RaisesIsModalChangedEvent ()
  42. {
  43. // Arrange
  44. IApplication app = CreateAndInitApp ();
  45. Runnable<int> runnable = new ();
  46. var isModalChangedRaised = false;
  47. bool? receivedValue = null;
  48. runnable.IsModalChanged += (s, e) =>
  49. {
  50. isModalChangedRaised = true;
  51. receivedValue = e.Value;
  52. };
  53. // Act
  54. SessionToken? token = app.Begin (runnable);
  55. // Assert
  56. Assert.True (isModalChangedRaised);
  57. Assert.True (receivedValue);
  58. // Cleanup
  59. app.End (token!);
  60. }
  61. [Fact]
  62. public void Begin_RaisesIsRunningChangedEvent ()
  63. {
  64. // Arrange
  65. IApplication app = CreateAndInitApp ();
  66. Runnable<int> runnable = new ();
  67. var isRunningChangedRaised = false;
  68. bool? receivedValue = null;
  69. runnable.IsRunningChanged += (s, e) =>
  70. {
  71. isRunningChangedRaised = true;
  72. receivedValue = e.Value;
  73. };
  74. // Act
  75. SessionToken? token = app.Begin (runnable);
  76. // Assert
  77. Assert.True (isRunningChangedRaised);
  78. Assert.True (receivedValue);
  79. // Cleanup
  80. app.End (token!);
  81. }
  82. [Fact]
  83. public void Begin_RaisesIsRunningChangingEvent ()
  84. {
  85. // Arrange
  86. IApplication app = CreateAndInitApp ();
  87. Runnable<int> runnable = new ();
  88. var isRunningChangingRaised = false;
  89. bool? oldValue = null;
  90. bool? newValue = null;
  91. runnable.IsRunningChanging += (s, e) =>
  92. {
  93. isRunningChangingRaised = true;
  94. oldValue = e.CurrentValue;
  95. newValue = e.NewValue;
  96. };
  97. // Act
  98. SessionToken? token = app.Begin (runnable);
  99. // Assert
  100. Assert.True (isRunningChangingRaised);
  101. Assert.False (oldValue);
  102. Assert.True (newValue);
  103. // Cleanup
  104. app.End (token!);
  105. }
  106. [Fact]
  107. public void Begin_SetsIsModalToTrue ()
  108. {
  109. // Arrange
  110. IApplication app = CreateAndInitApp ();
  111. Runnable<int> runnable = new ();
  112. // Act
  113. SessionToken? token = app.Begin (runnable);
  114. // Assert
  115. Assert.True (runnable.IsModal);
  116. // Cleanup
  117. app.End (token!);
  118. }
  119. [Fact]
  120. public void Begin_SetsIsRunningToTrue ()
  121. {
  122. // Arrange
  123. IApplication app = CreateAndInitApp ();
  124. Runnable<int> runnable = new ();
  125. // Act
  126. SessionToken? token = app.Begin (runnable);
  127. // Assert
  128. Assert.True (runnable.IsRunning);
  129. // Cleanup
  130. app.End (token!);
  131. }
  132. [Fact]
  133. public void Begin_ThrowsOnNullRunnable ()
  134. {
  135. // Arrange
  136. IApplication app = CreateAndInitApp ();
  137. // Act & Assert
  138. Assert.Throws<ArgumentNullException> (() => app.Begin ((IRunnable)null!));
  139. }
  140. [Fact]
  141. public void End_CanBeCanceled_ByIsRunningChanging ()
  142. {
  143. // Arrange
  144. IApplication app = CreateAndInitApp ();
  145. CancelableRunnable runnable = new () { CancelStop = true };
  146. SessionToken? token = app.Begin (runnable);
  147. runnable.CancelStop = true; // Enable cancellation
  148. // Act
  149. app.End (token!);
  150. // Assert - Should still be running if canceled
  151. Assert.True (runnable.IsRunning);
  152. // Force end by disabling cancellation
  153. runnable.CancelStop = false;
  154. app.End (token!);
  155. }
  156. [Fact]
  157. public void End_ClearsTokenRunnable ()
  158. {
  159. // Arrange
  160. IApplication app = CreateAndInitApp ();
  161. Runnable<int> runnable = new ();
  162. SessionToken? token = app.Begin (runnable);
  163. // Act
  164. app.End (token!);
  165. // Assert
  166. Assert.Null (token!.Runnable);
  167. }
  168. [Fact]
  169. public void End_RaisesIsRunningChangedEvent ()
  170. {
  171. // Arrange
  172. IApplication app = CreateAndInitApp ();
  173. Runnable<int> runnable = new ();
  174. SessionToken? token = app.Begin (runnable);
  175. var isRunningChangedRaised = false;
  176. bool? receivedValue = null;
  177. runnable.IsRunningChanged += (s, e) =>
  178. {
  179. isRunningChangedRaised = true;
  180. receivedValue = e.Value;
  181. };
  182. // Act
  183. app.End (token!);
  184. // Assert
  185. Assert.True (isRunningChangedRaised);
  186. Assert.False (receivedValue);
  187. }
  188. [Fact]
  189. public void End_RaisesIsRunningChangingEvent ()
  190. {
  191. // Arrange
  192. IApplication app = CreateAndInitApp ();
  193. Runnable<int> runnable = new ();
  194. SessionToken? token = app.Begin (runnable);
  195. var isRunningChangingRaised = false;
  196. bool? oldValue = null;
  197. bool? newValue = null;
  198. runnable.IsRunningChanging += (s, e) =>
  199. {
  200. isRunningChangingRaised = true;
  201. oldValue = e.CurrentValue;
  202. newValue = e.NewValue;
  203. };
  204. // Act
  205. app.End (token!);
  206. // Assert
  207. Assert.True (isRunningChangingRaised);
  208. Assert.True (oldValue);
  209. Assert.False (newValue);
  210. }
  211. [Fact]
  212. public void End_RemovesRunnableFromStack ()
  213. {
  214. // Arrange
  215. IApplication app = CreateAndInitApp ();
  216. Runnable<int> runnable = new ();
  217. SessionToken? token = app.Begin (runnable);
  218. int stackCountBefore = app.SessionStack?.Count ?? 0;
  219. // Act
  220. app.End (token!);
  221. // Assert
  222. Assert.Equal (stackCountBefore - 1, app.SessionStack?.Count ?? 0);
  223. }
  224. [Fact]
  225. public void End_SetsIsModalToFalse ()
  226. {
  227. // Arrange
  228. IApplication app = CreateAndInitApp ();
  229. Runnable<int> runnable = new ();
  230. SessionToken? token = app.Begin (runnable);
  231. // Act
  232. app.End (token!);
  233. // Assert
  234. Assert.False (runnable.IsModal);
  235. }
  236. [Fact]
  237. public void End_SetsIsRunningToFalse ()
  238. {
  239. // Arrange
  240. IApplication app = CreateAndInitApp ();
  241. Runnable<int> runnable = new ();
  242. SessionToken? token = app.Begin (runnable);
  243. // Act
  244. app.End (token!);
  245. // Assert
  246. Assert.False (runnable.IsRunning);
  247. }
  248. [Fact]
  249. public void End_ThrowsOnNullToken ()
  250. {
  251. // Arrange
  252. IApplication app = CreateAndInitApp ();
  253. // Act & Assert
  254. Assert.Throws<ArgumentNullException> (() => app.End ((SessionToken)null!));
  255. }
  256. [Fact]
  257. public void End_ClearsMouseGrabView ()
  258. {
  259. // Arrange
  260. IApplication app = CreateAndInitApp ();
  261. Runnable<int> runnable = new ();
  262. SessionToken? token = app.Begin (runnable);
  263. app.Mouse.GrabMouse (runnable);
  264. app.End (token!);
  265. Assert.Null (app.Mouse.MouseGrabView);
  266. runnable.Dispose ();
  267. app.Dispose ();
  268. }
  269. [Fact]
  270. public void MultipleRunnables_IndependentResults ()
  271. {
  272. // Arrange
  273. Runnable<int> runnable1 = new ();
  274. Runnable<string> runnable2 = new ();
  275. // Act
  276. runnable1.Result = 42;
  277. runnable2.Result = "test";
  278. // Assert
  279. Assert.Equal (42, runnable1.Result);
  280. Assert.Equal ("test", runnable2.Result);
  281. }
  282. [Fact]
  283. public void NestedBegin_MaintainsStackOrder ()
  284. {
  285. // Arrange
  286. IApplication app = CreateAndInitApp ();
  287. Runnable<int> runnable1 = new () { Id = "1" };
  288. Runnable<int> runnable2 = new () { Id = "2" };
  289. // Act
  290. SessionToken token1 = app.Begin (runnable1)!;
  291. SessionToken token2 = app.Begin (runnable2)!;
  292. // Assert - runnable2 should be on top
  293. Assert.True (runnable2.IsModal);
  294. Assert.False (runnable1.IsModal);
  295. Assert.True (runnable1.IsRunning); // Still running, just not modal
  296. Assert.True (runnable2.IsRunning);
  297. // Cleanup
  298. app.End (token2);
  299. app.End (token1);
  300. }
  301. [Fact]
  302. public void NestedEnd_RestoresPreviousModal ()
  303. {
  304. // Arrange
  305. IApplication app = CreateAndInitApp ();
  306. Runnable<int> runnable1 = new () { Id = "1" };
  307. Runnable<int> runnable2 = new () { Id = "2" };
  308. SessionToken token1 = app.Begin (runnable1)!;
  309. SessionToken token2 = app.Begin (runnable2)!;
  310. // Act - End the top runnable
  311. app.End (token2);
  312. // Assert - runnable1 should become modal again
  313. Assert.True (runnable1.IsModal);
  314. Assert.False (runnable2.IsModal);
  315. Assert.True (runnable1.IsRunning);
  316. Assert.False (runnable2.IsRunning);
  317. // Cleanup
  318. app.End (token1);
  319. }
  320. [Fact]
  321. public void RequestStop_WithIRunnable_WorksCorrectly ()
  322. {
  323. // Arrange
  324. IApplication app = CreateAndInitApp ();
  325. StoppableRunnable runnable = new ();
  326. SessionToken? token = app.Begin (runnable);
  327. // Act
  328. app.RequestStop (runnable);
  329. // Assert - RequestStop should trigger End eventually
  330. // For now, just verify it doesn't throw
  331. Assert.NotNull (runnable);
  332. // Cleanup
  333. app.End (token!);
  334. }
  335. [Fact]
  336. public void RequestStop_WithNull_UsesTopRunnable ()
  337. {
  338. // Arrange
  339. IApplication app = CreateAndInitApp ();
  340. StoppableRunnable runnable = new ();
  341. SessionToken? token = app.Begin (runnable);
  342. // Act
  343. app.RequestStop ((IRunnable?)null);
  344. // Assert - Should not throw
  345. Assert.NotNull (runnable);
  346. // Cleanup
  347. app.End (token!);
  348. }
  349. [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
  350. public void RunGeneric_CreatesAndReturnsRunnable ()
  351. {
  352. // Arrange
  353. IApplication app = CreateAndInitApp ();
  354. app.StopAfterFirstIteration = true;
  355. // Act - With fluent API, Run<T>() returns IApplication for chaining
  356. IApplication result = app.Run<TestRunnable> ();
  357. // Assert
  358. Assert.NotNull (result);
  359. Assert.Same (app, result); // Fluent API returns this
  360. // Note: Run blocks until stopped, but StopAfterFirstIteration makes it return immediately
  361. // The runnable is automatically disposed by Dispose()
  362. }
  363. [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
  364. public void RunGeneric_ThrowsIfNotInitialized ()
  365. {
  366. // Arrange
  367. IApplication app = Application.Create ();
  368. // Don't call Init
  369. // Act & Assert
  370. Assert.Throws<NotInitializedException> (() => app.Run<TestRunnable> ());
  371. // Cleanup
  372. app.Dispose ();
  373. }
  374. private IApplication CreateAndInitApp ()
  375. {
  376. IApplication app = Application.Create ();
  377. app.Init ("fake");
  378. return app;
  379. }
  380. /// <summary>
  381. /// Test runnable that can cancel lifecycle changes.
  382. /// </summary>
  383. private class CancelableRunnable : Runnable<int>
  384. {
  385. public bool CancelStart { get; set; }
  386. public bool CancelStop { get; set; }
  387. protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
  388. {
  389. if (newIsRunning && CancelStart)
  390. {
  391. return true; // Cancel starting
  392. }
  393. if (!newIsRunning && CancelStop)
  394. {
  395. return true; // Cancel stopping
  396. }
  397. return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
  398. }
  399. }
  400. /// <summary>
  401. /// Test runnable that can be stopped.
  402. /// </summary>
  403. private class StoppableRunnable : Runnable<int>
  404. {
  405. public override void RequestStop ()
  406. {
  407. WasStopRequested = true;
  408. base.RequestStop ();
  409. }
  410. public bool WasStopRequested { get; private set; }
  411. }
  412. /// <summary>
  413. /// Test runnable for generic Run tests.
  414. /// </summary>
  415. private class TestRunnable : Runnable<int>
  416. {
  417. public TestRunnable () { Id = "TestRunnable"; }
  418. }
  419. }