ApplicationImplBeginEndTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using Xunit;
  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. /// </summary>
  8. [Collection ("Sequential")] // Ensures tests run sequentially, not in parallel
  9. public class ApplicationImplBeginEndTests : IDisposable
  10. {
  11. private readonly ITestOutputHelper _output;
  12. public ApplicationImplBeginEndTests (ITestOutputHelper output)
  13. {
  14. _output = output;
  15. // Ensure clean state before each test
  16. if (Application.Initialized)
  17. {
  18. Application.Shutdown ();
  19. }
  20. }
  21. public void Dispose ()
  22. {
  23. // Ensure Application is shutdown after each test
  24. if (Application.Initialized)
  25. {
  26. Application.Shutdown ();
  27. }
  28. }
  29. [Fact]
  30. public void Begin_WithNullToplevel_ThrowsArgumentNullException ()
  31. {
  32. Application.Init (driverName: "fake");
  33. Assert.Throws<ArgumentNullException> (() => Application.Begin (null!));
  34. }
  35. [Fact]
  36. public void Begin_SetsCurrent_WhenCurrentIsNull ()
  37. {
  38. Application.Init (driverName: "fake");
  39. var toplevel = new Toplevel ();
  40. Assert.Null (Application.Current);
  41. Application.Begin (toplevel);
  42. Assert.NotNull (Application.Current);
  43. Assert.Same (toplevel, Application.Current);
  44. Assert.Single (Application.SessionStack);
  45. toplevel.Dispose ();
  46. }
  47. [Fact]
  48. public void Begin_PushesToSessionStack ()
  49. {
  50. Application.Init (driverName: "fake");
  51. var toplevel1 = new Toplevel { Id = "1" };
  52. var toplevel2 = new Toplevel { Id = "2" };
  53. Application.Begin (toplevel1);
  54. Assert.Single (Application.SessionStack);
  55. Assert.Same (toplevel1, Application.Current);
  56. Application.Begin (toplevel2);
  57. Assert.Equal (2, Application.SessionStack.Count);
  58. Assert.Same (toplevel2, Application.Current);
  59. toplevel1.Dispose ();
  60. toplevel2.Dispose ();
  61. }
  62. [Fact]
  63. public void Begin_SetsUniqueToplevelId_WhenIdIsEmpty ()
  64. {
  65. Application.Init (driverName: "fake");
  66. var toplevel1 = new Toplevel ();
  67. var toplevel2 = new Toplevel ();
  68. var toplevel3 = new Toplevel ();
  69. Assert.Empty (toplevel1.Id);
  70. Assert.Empty (toplevel2.Id);
  71. Assert.Empty (toplevel3.Id);
  72. Application.Begin (toplevel1);
  73. Application.Begin (toplevel2);
  74. Application.Begin (toplevel3);
  75. Assert.NotEmpty (toplevel1.Id);
  76. Assert.NotEmpty (toplevel2.Id);
  77. Assert.NotEmpty (toplevel3.Id);
  78. // IDs should be unique
  79. Assert.NotEqual (toplevel1.Id, toplevel2.Id);
  80. Assert.NotEqual (toplevel2.Id, toplevel3.Id);
  81. Assert.NotEqual (toplevel1.Id, toplevel3.Id);
  82. toplevel1.Dispose ();
  83. toplevel2.Dispose ();
  84. toplevel3.Dispose ();
  85. }
  86. [Fact]
  87. public void End_WithNullSessionToken_ThrowsArgumentNullException ()
  88. {
  89. Application.Init (driverName: "fake");
  90. Assert.Throws<ArgumentNullException> (() => Application.End (null!));
  91. }
  92. [Fact]
  93. public void End_PopsSessionStack ()
  94. {
  95. Application.Init (driverName: "fake");
  96. var toplevel1 = new Toplevel { Id = "1" };
  97. var toplevel2 = new Toplevel { Id = "2" };
  98. SessionToken token1 = Application.Begin (toplevel1);
  99. SessionToken token2 = Application.Begin (toplevel2);
  100. Assert.Equal (2, Application.SessionStack.Count);
  101. Application.End (token2);
  102. Assert.Single (Application.SessionStack);
  103. Assert.Same (toplevel1, Application.Current);
  104. Application.End (token1);
  105. Assert.Empty (Application.SessionStack);
  106. toplevel1.Dispose ();
  107. toplevel2.Dispose ();
  108. }
  109. [Fact]
  110. public void End_ThrowsArgumentException_WhenNotBalanced ()
  111. {
  112. Application.Init (driverName: "fake");
  113. var toplevel1 = new Toplevel { Id = "1" };
  114. var toplevel2 = new Toplevel { Id = "2" };
  115. SessionToken token1 = Application.Begin (toplevel1);
  116. SessionToken token2 = Application.Begin (toplevel2);
  117. // Trying to end token1 when token2 is on top should throw
  118. Assert.Throws<ArgumentException> (() => Application.End (token1));
  119. // Cleanup
  120. Application.End (token2);
  121. Application.End (token1);
  122. toplevel1.Dispose ();
  123. toplevel2.Dispose ();
  124. }
  125. [Fact]
  126. public void End_RestoresCurrentToPreviousToplevel ()
  127. {
  128. Application.Init (driverName: "fake");
  129. var toplevel1 = new Toplevel { Id = "1" };
  130. var toplevel2 = new Toplevel { Id = "2" };
  131. var toplevel3 = new Toplevel { Id = "3" };
  132. SessionToken token1 = Application.Begin (toplevel1);
  133. SessionToken token2 = Application.Begin (toplevel2);
  134. SessionToken token3 = Application.Begin (toplevel3);
  135. Assert.Same (toplevel3, Application.Current);
  136. Application.End (token3);
  137. Assert.Same (toplevel2, Application.Current);
  138. Application.End (token2);
  139. Assert.Same (toplevel1, Application.Current);
  140. Application.End (token1);
  141. toplevel1.Dispose ();
  142. toplevel2.Dispose ();
  143. toplevel3.Dispose ();
  144. }
  145. [Fact]
  146. public void MultipleBeginEnd_MaintainsStackIntegrity ()
  147. {
  148. Application.Init (driverName: "fake");
  149. var toplevels = new List<Toplevel> ();
  150. var tokens = new List<SessionToken> ();
  151. // Begin multiple toplevels
  152. for (int i = 0; i < 5; i++)
  153. {
  154. var toplevel = new Toplevel { Id = $"toplevel-{i}" };
  155. toplevels.Add (toplevel);
  156. tokens.Add (Application.Begin (toplevel));
  157. }
  158. Assert.Equal (5, Application.SessionStack.Count);
  159. Assert.Same (toplevels [4], Application.Current);
  160. // End them in reverse order (LIFO)
  161. for (int i = 4; i >= 0; i--)
  162. {
  163. Application.End (tokens [i]);
  164. if (i > 0)
  165. {
  166. Assert.Equal (i, Application.SessionStack.Count);
  167. Assert.Same (toplevels [i - 1], Application.Current);
  168. }
  169. else
  170. {
  171. Assert.Empty (Application.SessionStack);
  172. }
  173. }
  174. foreach (var toplevel in toplevels)
  175. {
  176. toplevel.Dispose ();
  177. }
  178. }
  179. [Fact]
  180. public void End_UpdatesCachedSessionTokenToplevel ()
  181. {
  182. Application.Init (driverName: "fake");
  183. var toplevel = new Toplevel ();
  184. SessionToken token = Application.Begin (toplevel);
  185. Assert.Null (ApplicationImpl.Instance.CachedSessionTokenToplevel);
  186. Application.End (token);
  187. Assert.Same (toplevel, ApplicationImpl.Instance.CachedSessionTokenToplevel);
  188. toplevel.Dispose ();
  189. }
  190. [Fact]
  191. public void End_NullsSessionTokenToplevel ()
  192. {
  193. Application.Init (driverName: "fake");
  194. var toplevel = new Toplevel ();
  195. SessionToken token = Application.Begin (toplevel);
  196. Assert.Same (toplevel, token.Toplevel);
  197. Application.End (token);
  198. Assert.Null (token.Toplevel);
  199. toplevel.Dispose ();
  200. }
  201. [Fact]
  202. public void ResetState_ClearsSessionStack ()
  203. {
  204. Application.Init (driverName: "fake");
  205. var toplevel1 = new Toplevel { Id = "1" };
  206. var toplevel2 = new Toplevel { Id = "2" };
  207. Application.Begin (toplevel1);
  208. Application.Begin (toplevel2);
  209. Assert.Equal (2, Application.SessionStack.Count);
  210. Assert.NotNull (Application.Current);
  211. ApplicationImpl.Instance.ResetState ();
  212. Assert.Empty (Application.SessionStack);
  213. Assert.Null (Application.Current);
  214. Assert.Null (ApplicationImpl.Instance.CachedSessionTokenToplevel);
  215. toplevel1.Dispose ();
  216. toplevel2.Dispose ();
  217. }
  218. [Fact]
  219. public void ResetState_StopsAllRunningToplevels ()
  220. {
  221. Application.Init (driverName: "fake");
  222. var toplevel1 = new Toplevel { Id = "1", Running = true };
  223. var toplevel2 = new Toplevel { Id = "2", Running = true };
  224. Application.Begin (toplevel1);
  225. Application.Begin (toplevel2);
  226. Assert.True (toplevel1.Running);
  227. Assert.True (toplevel2.Running);
  228. ApplicationImpl.Instance.ResetState ();
  229. Assert.False (toplevel1.Running);
  230. Assert.False (toplevel2.Running);
  231. toplevel1.Dispose ();
  232. toplevel2.Dispose ();
  233. }
  234. [Fact]
  235. public void Begin_ActivatesNewToplevel_WhenCurrentExists ()
  236. {
  237. Application.Init (driverName: "fake");
  238. var toplevel1 = new Toplevel { Id = "1" };
  239. var toplevel2 = new Toplevel { Id = "2" };
  240. bool toplevel1Deactivated = false;
  241. bool toplevel2Activated = false;
  242. toplevel1.Deactivate += (s, e) => toplevel1Deactivated = true;
  243. toplevel2.Activate += (s, e) => toplevel2Activated = true;
  244. Application.Begin (toplevel1);
  245. Application.Begin (toplevel2);
  246. Assert.True (toplevel1Deactivated);
  247. Assert.True (toplevel2Activated);
  248. Assert.Same (toplevel2, Application.Current);
  249. toplevel1.Dispose ();
  250. toplevel2.Dispose ();
  251. }
  252. [Fact]
  253. public void Begin_DoesNotDuplicateToplevel_WhenIdAlreadyExists ()
  254. {
  255. Application.Init (driverName: "fake");
  256. var toplevel = new Toplevel { Id = "test-id" };
  257. Application.Begin (toplevel);
  258. Assert.Single (Application.SessionStack);
  259. // Calling Begin again with same toplevel should not duplicate
  260. Application.Begin (toplevel);
  261. Assert.Single (Application.SessionStack);
  262. toplevel.Dispose ();
  263. }
  264. [Fact]
  265. public void SessionStack_ContainsAllBegunToplevels ()
  266. {
  267. Application.Init (driverName: "fake");
  268. var toplevels = new List<Toplevel> ();
  269. for (int i = 0; i < 10; i++)
  270. {
  271. var toplevel = new Toplevel { Id = $"toplevel-{i}" };
  272. toplevels.Add (toplevel);
  273. Application.Begin (toplevel);
  274. }
  275. // All toplevels should be in the stack
  276. Assert.Equal (10, Application.SessionStack.Count);
  277. // Verify stack contains all toplevels
  278. var stackList = Application.SessionStack.ToList ();
  279. foreach (var toplevel in toplevels)
  280. {
  281. Assert.Contains (toplevel, stackList);
  282. }
  283. foreach (var toplevel in toplevels)
  284. {
  285. toplevel.Dispose ();
  286. }
  287. }
  288. }