ApplicationImplBeginEndTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #nullable enable
  2. using Xunit.Abstractions;
  3. namespace UnitTests.ApplicationTests;
  4. /// <summary>
  5. /// Comprehensive tests for ApplicationImpl.Begin/End logic that manages Current and SessionStack.
  6. /// These tests ensure the fragile state management logic is robust and catches regressions.
  7. /// Tests work directly with ApplicationImpl instances to avoid global Application state issues.
  8. /// </summary>
  9. public class ApplicationImplBeginEndTests (ITestOutputHelper output)
  10. {
  11. private readonly ITestOutputHelper _output = output;
  12. private IApplication NewApplicationImpl ()
  13. {
  14. IApplication app = Application.Create ();
  15. return app;
  16. }
  17. [Fact]
  18. public void Begin_WithNullToplevel_ThrowsArgumentNullException ()
  19. {
  20. IApplication app = NewApplicationImpl ();
  21. try
  22. {
  23. Assert.Throws<ArgumentNullException> (() => app.Begin ((Toplevel)null!));
  24. }
  25. finally
  26. {
  27. app.Shutdown ();
  28. }
  29. }
  30. [Fact]
  31. public void Begin_SetsCurrent_WhenCurrentIsNull ()
  32. {
  33. IApplication app = NewApplicationImpl ();
  34. Toplevel? toplevel = null;
  35. try
  36. {
  37. toplevel = new ();
  38. Assert.Null (app.TopRunnable);
  39. app.Begin (toplevel);
  40. Assert.NotNull (app.TopRunnable);
  41. Assert.Same (toplevel, app.TopRunnable);
  42. Assert.Single (app.SessionStack);
  43. }
  44. finally
  45. {
  46. toplevel?.Dispose ();
  47. app.Shutdown ();
  48. }
  49. }
  50. [Fact]
  51. public void Begin_PushesToSessionStack ()
  52. {
  53. IApplication app = NewApplicationImpl ();
  54. Toplevel? toplevel1 = null;
  55. Toplevel? toplevel2 = null;
  56. try
  57. {
  58. toplevel1 = new () { Id = "1" };
  59. toplevel2 = new () { Id = "2" };
  60. app.Begin (toplevel1);
  61. Assert.Single (app.SessionStack);
  62. Assert.Same (toplevel1, app.TopRunnable);
  63. app.Begin (toplevel2);
  64. Assert.Equal (2, app.SessionStack.Count);
  65. Assert.Same (toplevel2, app.TopRunnable);
  66. }
  67. finally
  68. {
  69. toplevel1?.Dispose ();
  70. toplevel2?.Dispose ();
  71. app.Shutdown ();
  72. }
  73. }
  74. [Fact]
  75. public void Begin_SetsUniqueToplevelId_WhenIdIsEmpty ()
  76. {
  77. IApplication app = NewApplicationImpl ();
  78. Toplevel? toplevel1 = null;
  79. Toplevel? toplevel2 = null;
  80. Toplevel? toplevel3 = null;
  81. try
  82. {
  83. toplevel1 = new ();
  84. toplevel2 = new ();
  85. toplevel3 = new ();
  86. Assert.Empty (toplevel1.Id);
  87. Assert.Empty (toplevel2.Id);
  88. Assert.Empty (toplevel3.Id);
  89. app.Begin (toplevel1);
  90. app.Begin (toplevel2);
  91. app.Begin (toplevel3);
  92. Assert.NotEmpty (toplevel1.Id);
  93. Assert.NotEmpty (toplevel2.Id);
  94. Assert.NotEmpty (toplevel3.Id);
  95. // IDs should be unique
  96. Assert.NotEqual (toplevel1.Id, toplevel2.Id);
  97. Assert.NotEqual (toplevel2.Id, toplevel3.Id);
  98. Assert.NotEqual (toplevel1.Id, toplevel3.Id);
  99. }
  100. finally
  101. {
  102. toplevel1?.Dispose ();
  103. toplevel2?.Dispose ();
  104. toplevel3?.Dispose ();
  105. app.Shutdown ();
  106. }
  107. }
  108. [Fact]
  109. public void End_WithNullSessionToken_ThrowsArgumentNullException ()
  110. {
  111. IApplication app = NewApplicationImpl ();
  112. try
  113. {
  114. Assert.Throws<ArgumentNullException> (() => app.End ((SessionToken)null!));
  115. }
  116. finally
  117. {
  118. app.Shutdown ();
  119. }
  120. }
  121. [Fact]
  122. public void End_PopsSessionStack ()
  123. {
  124. IApplication app = NewApplicationImpl ();
  125. Toplevel? toplevel1 = null;
  126. Toplevel? toplevel2 = null;
  127. try
  128. {
  129. toplevel1 = new () { Id = "1" };
  130. toplevel2 = new () { Id = "2" };
  131. SessionToken token1 = app.Begin (toplevel1);
  132. SessionToken token2 = app.Begin (toplevel2);
  133. Assert.Equal (2, app.SessionStack.Count);
  134. app.End (token2);
  135. Assert.Single (app.SessionStack);
  136. Assert.Same (toplevel1, app.TopRunnable);
  137. app.End (token1);
  138. Assert.Empty (app.SessionStack);
  139. }
  140. finally
  141. {
  142. toplevel1?.Dispose ();
  143. toplevel2?.Dispose ();
  144. app.Shutdown ();
  145. }
  146. }
  147. [Fact]
  148. public void End_ThrowsArgumentException_WhenNotBalanced ()
  149. {
  150. IApplication app = NewApplicationImpl ();
  151. Toplevel? toplevel1 = null;
  152. Toplevel? toplevel2 = null;
  153. try
  154. {
  155. toplevel1 = new () { Id = "1" };
  156. toplevel2 = new () { Id = "2" };
  157. SessionToken token1 = app.Begin (toplevel1);
  158. SessionToken token2 = app.Begin (toplevel2);
  159. // Trying to end token1 when token2 is on top should throw
  160. // NOTE: This throws but has the side effect of popping token2 from the stack
  161. Assert.Throws<ArgumentException> (() => app.End (token1));
  162. // Don't try to clean up with more End calls - the state is now inconsistent
  163. // Let Shutdown/ResetState handle cleanup
  164. }
  165. finally
  166. {
  167. // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
  168. toplevel1?.Dispose ();
  169. toplevel2?.Dispose ();
  170. // Shutdown will call ResetState which clears any remaining state
  171. app.Shutdown ();
  172. }
  173. }
  174. [Fact]
  175. public void End_RestoresCurrentToPreviousToplevel ()
  176. {
  177. IApplication app = NewApplicationImpl ();
  178. Toplevel? toplevel1 = null;
  179. Toplevel? toplevel2 = null;
  180. Toplevel? toplevel3 = null;
  181. try
  182. {
  183. toplevel1 = new () { Id = "1" };
  184. toplevel2 = new () { Id = "2" };
  185. toplevel3 = new () { Id = "3" };
  186. SessionToken token1 = app.Begin (toplevel1);
  187. SessionToken token2 = app.Begin (toplevel2);
  188. SessionToken token3 = app.Begin (toplevel3);
  189. Assert.Same (toplevel3, app.TopRunnable);
  190. app.End (token3);
  191. Assert.Same (toplevel2, app.TopRunnable);
  192. app.End (token2);
  193. Assert.Same (toplevel1, app.TopRunnable);
  194. app.End (token1);
  195. }
  196. finally
  197. {
  198. toplevel1?.Dispose ();
  199. toplevel2?.Dispose ();
  200. toplevel3?.Dispose ();
  201. app.Shutdown ();
  202. }
  203. }
  204. [Fact]
  205. public void MultipleBeginEnd_MaintainsStackIntegrity ()
  206. {
  207. IApplication app = NewApplicationImpl ();
  208. List<Toplevel> toplevels = new ();
  209. List<SessionToken> tokens = new ();
  210. try
  211. {
  212. // Begin multiple toplevels
  213. for (var i = 0; i < 5; i++)
  214. {
  215. var toplevel = new Toplevel { Id = $"toplevel-{i}" };
  216. toplevels.Add (toplevel);
  217. tokens.Add (app.Begin (toplevel));
  218. }
  219. Assert.Equal (5, app.SessionStack.Count);
  220. Assert.Same (toplevels [4], app.TopRunnable);
  221. // End them in reverse order (LIFO)
  222. for (var i = 4; i >= 0; i--)
  223. {
  224. app.End (tokens [i]);
  225. if (i > 0)
  226. {
  227. Assert.Equal (i, app.SessionStack.Count);
  228. Assert.Same (toplevels [i - 1], app.TopRunnable);
  229. }
  230. else
  231. {
  232. Assert.Empty (app.SessionStack);
  233. }
  234. }
  235. }
  236. finally
  237. {
  238. foreach (Toplevel toplevel in toplevels)
  239. {
  240. toplevel.Dispose ();
  241. }
  242. app.Shutdown ();
  243. }
  244. }
  245. [Fact]
  246. public void End_UpdatesCachedSessionTokenToplevel ()
  247. {
  248. IApplication app = NewApplicationImpl ();
  249. Toplevel? toplevel = null;
  250. try
  251. {
  252. toplevel = new ();
  253. SessionToken token = app.Begin (toplevel);
  254. Assert.Null (app.CachedSessionTokenToplevel);
  255. app.End (token);
  256. Assert.Same (toplevel, app.CachedSessionTokenToplevel);
  257. }
  258. finally
  259. {
  260. toplevel?.Dispose ();
  261. app.Shutdown ();
  262. }
  263. }
  264. [Fact]
  265. public void End_NullsSessionTokenToplevel ()
  266. {
  267. IApplication app = NewApplicationImpl ();
  268. Toplevel? toplevel = null;
  269. try
  270. {
  271. toplevel = new ();
  272. SessionToken token = app.Begin (toplevel);
  273. Assert.Same (toplevel, token.Toplevel);
  274. app.End (token);
  275. Assert.Null (token.Toplevel);
  276. }
  277. finally
  278. {
  279. toplevel?.Dispose ();
  280. app.Shutdown ();
  281. }
  282. }
  283. [Fact]
  284. public void ResetState_ClearsSessionStack ()
  285. {
  286. IApplication app = NewApplicationImpl ();
  287. Toplevel? toplevel1 = null;
  288. Toplevel? toplevel2 = null;
  289. try
  290. {
  291. toplevel1 = new () { Id = "1" };
  292. toplevel2 = new () { Id = "2" };
  293. app.Begin (toplevel1);
  294. app.Begin (toplevel2);
  295. Assert.Equal (2, app.SessionStack.Count);
  296. Assert.NotNull (app.TopRunnable);
  297. }
  298. finally
  299. {
  300. // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
  301. toplevel1?.Dispose ();
  302. toplevel2?.Dispose ();
  303. // Shutdown calls ResetState, which will clear SessionStack and set Current to null
  304. app.Shutdown ();
  305. // Verify cleanup happened
  306. Assert.Empty (app.SessionStack);
  307. Assert.Null (app.TopRunnable);
  308. Assert.Null (app.CachedSessionTokenToplevel);
  309. }
  310. }
  311. [Fact]
  312. public void ResetState_StopsAllRunningToplevels ()
  313. {
  314. IApplication app = NewApplicationImpl ();
  315. Toplevel? toplevel1 = null;
  316. Toplevel? toplevel2 = null;
  317. try
  318. {
  319. toplevel1 = new () { Id = "1", Running = true };
  320. toplevel2 = new () { Id = "2", Running = true };
  321. app.Begin (toplevel1);
  322. app.Begin (toplevel2);
  323. Assert.True (toplevel1.Running);
  324. Assert.True (toplevel2.Running);
  325. }
  326. finally
  327. {
  328. // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
  329. toplevel1?.Dispose ();
  330. toplevel2?.Dispose ();
  331. // Shutdown calls ResetState, which will stop all running toplevels
  332. app.Shutdown ();
  333. // Verify toplevels were stopped
  334. Assert.False (toplevel1!.Running);
  335. Assert.False (toplevel2!.Running);
  336. }
  337. }
  338. [Fact]
  339. public void Begin_ActivatesNewToplevel_WhenCurrentExists ()
  340. {
  341. IApplication app = NewApplicationImpl ();
  342. Toplevel? toplevel1 = null;
  343. Toplevel? toplevel2 = null;
  344. try
  345. {
  346. toplevel1 = new () { Id = "1" };
  347. toplevel2 = new () { Id = "2" };
  348. var toplevel1Deactivated = false;
  349. var toplevel2Activated = false;
  350. toplevel1.Deactivate += (s, e) => toplevel1Deactivated = true;
  351. toplevel2.Activate += (s, e) => toplevel2Activated = true;
  352. app.Begin (toplevel1);
  353. app.Begin (toplevel2);
  354. Assert.True (toplevel1Deactivated);
  355. Assert.True (toplevel2Activated);
  356. Assert.Same (toplevel2, app.TopRunnable);
  357. }
  358. finally
  359. {
  360. toplevel1?.Dispose ();
  361. toplevel2?.Dispose ();
  362. app.Shutdown ();
  363. }
  364. }
  365. [Fact]
  366. public void Begin_DoesNotDuplicateToplevel_WhenIdAlreadyExists ()
  367. {
  368. IApplication app = NewApplicationImpl ();
  369. Toplevel? toplevel = null;
  370. try
  371. {
  372. toplevel = new () { Id = "test-id" };
  373. app.Begin (toplevel);
  374. Assert.Single (app.SessionStack);
  375. // Calling Begin again with same toplevel should not duplicate
  376. app.Begin (toplevel);
  377. Assert.Single (app.SessionStack);
  378. }
  379. finally
  380. {
  381. toplevel?.Dispose ();
  382. app.Shutdown ();
  383. }
  384. }
  385. [Fact]
  386. public void SessionStack_ContainsAllBegunToplevels ()
  387. {
  388. IApplication app = NewApplicationImpl ();
  389. List<Toplevel> toplevels = new ();
  390. try
  391. {
  392. for (var i = 0; i < 10; i++)
  393. {
  394. var toplevel = new Toplevel { Id = $"toplevel-{i}" };
  395. toplevels.Add (toplevel);
  396. app.Begin (toplevel);
  397. }
  398. // All toplevels should be in the stack
  399. Assert.Equal (10, app.SessionStack.Count);
  400. // Verify stack contains all toplevels
  401. List<Toplevel> stackList = app.SessionStack.ToList ();
  402. foreach (Toplevel toplevel in toplevels)
  403. {
  404. Assert.Contains (toplevel, stackList);
  405. }
  406. }
  407. finally
  408. {
  409. foreach (Toplevel toplevel in toplevels)
  410. {
  411. toplevel.Dispose ();
  412. }
  413. app.Shutdown ();
  414. }
  415. }
  416. }