| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- #nullable enable
- using Xunit;
- using Xunit.Abstractions;
- namespace UnitTests.ApplicationTests;
- /// <summary>
- /// Comprehensive tests for ApplicationImpl.Begin/End logic that manages Current and SessionStack.
- /// These tests ensure the fragile state management logic is robust and catches regressions.
- /// Tests work directly with ApplicationImpl instances to avoid global Application state issues.
- /// </summary>
- public class ApplicationImplBeginEndTests
- {
- private readonly ITestOutputHelper _output;
- public ApplicationImplBeginEndTests (ITestOutputHelper output)
- {
- _output = output;
- }
- private ApplicationImpl NewApplicationImpl ()
- {
- var app = new ApplicationImpl ();
- return app;
- }
- [Fact]
- public void Begin_WithNullToplevel_ThrowsArgumentNullException ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- try
- {
- Assert.Throws<ArgumentNullException> (() => app.Begin (null!));
- }
- finally
- {
- app.Shutdown ();
- }
- }
- [Fact]
- public void Begin_SetsCurrent_WhenCurrentIsNull ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel = null;
-
- try
- {
- toplevel = new Toplevel ();
- Assert.Null (app.Current);
-
- app.Begin (toplevel);
-
- Assert.NotNull (app.Current);
- Assert.Same (toplevel, app.Current);
- Assert.Single (app.SessionStack);
- }
- finally
- {
- toplevel?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void Begin_PushesToSessionStack ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
-
- app.Begin (toplevel1);
- Assert.Single (app.SessionStack);
- Assert.Same (toplevel1, app.Current);
-
- app.Begin (toplevel2);
- Assert.Equal (2, app.SessionStack.Count);
- Assert.Same (toplevel2, app.Current);
- }
- finally
- {
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void Begin_SetsUniqueToplevelId_WhenIdIsEmpty ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
- Toplevel? toplevel3 = null;
-
- try
- {
- toplevel1 = new Toplevel ();
- toplevel2 = new Toplevel ();
- toplevel3 = new Toplevel ();
-
- Assert.Empty (toplevel1.Id);
- Assert.Empty (toplevel2.Id);
- Assert.Empty (toplevel3.Id);
-
- app.Begin (toplevel1);
- app.Begin (toplevel2);
- app.Begin (toplevel3);
-
- Assert.NotEmpty (toplevel1.Id);
- Assert.NotEmpty (toplevel2.Id);
- Assert.NotEmpty (toplevel3.Id);
-
- // IDs should be unique
- Assert.NotEqual (toplevel1.Id, toplevel2.Id);
- Assert.NotEqual (toplevel2.Id, toplevel3.Id);
- Assert.NotEqual (toplevel1.Id, toplevel3.Id);
- }
- finally
- {
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
- toplevel3?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_WithNullSessionToken_ThrowsArgumentNullException ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- try
- {
- Assert.Throws<ArgumentNullException> (() => app.End (null!));
- }
- finally
- {
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_PopsSessionStack ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
-
- SessionToken token1 = app.Begin (toplevel1);
- SessionToken token2 = app.Begin (toplevel2);
-
- Assert.Equal (2, app.SessionStack.Count);
-
- app.End (token2);
-
- Assert.Single (app.SessionStack);
- Assert.Same (toplevel1, app.Current);
-
- app.End (token1);
-
- Assert.Empty (app.SessionStack);
- }
- finally
- {
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_ThrowsArgumentException_WhenNotBalanced ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
-
- SessionToken token1 = app.Begin (toplevel1);
- SessionToken token2 = app.Begin (toplevel2);
-
- // Trying to end token1 when token2 is on top should throw
- // NOTE: This throws but has the side effect of popping token2 from the stack
- Assert.Throws<ArgumentException> (() => app.End (token1));
-
- // Don't try to clean up with more End calls - the state is now inconsistent
- // Let Shutdown/ResetState handle cleanup
- }
- finally
- {
- // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
-
- // Shutdown will call ResetState which clears any remaining state
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_RestoresCurrentToPreviousToplevel ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
- Toplevel? toplevel3 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
- toplevel3 = new Toplevel { Id = "3" };
-
- SessionToken token1 = app.Begin (toplevel1);
- SessionToken token2 = app.Begin (toplevel2);
- SessionToken token3 = app.Begin (toplevel3);
-
- Assert.Same (toplevel3, app.Current);
-
- app.End (token3);
- Assert.Same (toplevel2, app.Current);
-
- app.End (token2);
- Assert.Same (toplevel1, app.Current);
-
- app.End (token1);
- }
- finally
- {
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
- toplevel3?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void MultipleBeginEnd_MaintainsStackIntegrity ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- var toplevels = new List<Toplevel> ();
- var tokens = new List<SessionToken> ();
-
- try
- {
-
- // Begin multiple toplevels
- for (int i = 0; i < 5; i++)
- {
- var toplevel = new Toplevel { Id = $"toplevel-{i}" };
- toplevels.Add (toplevel);
- tokens.Add (app.Begin (toplevel));
- }
-
- Assert.Equal (5, app.SessionStack.Count);
- Assert.Same (toplevels [4], app.Current);
-
- // End them in reverse order (LIFO)
- for (int i = 4; i >= 0; i--)
- {
- app.End (tokens [i]);
-
- if (i > 0)
- {
- Assert.Equal (i, app.SessionStack.Count);
- Assert.Same (toplevels [i - 1], app.Current);
- }
- else
- {
- Assert.Empty (app.SessionStack);
- }
- }
- }
- finally
- {
- foreach (var toplevel in toplevels)
- {
- toplevel.Dispose ();
- }
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_UpdatesCachedSessionTokenToplevel ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel = null;
-
- try
- {
- toplevel = new Toplevel ();
-
- SessionToken token = app.Begin (toplevel);
- Assert.Null (app.CachedSessionTokenToplevel);
-
- app.End (token);
-
- Assert.Same (toplevel, app.CachedSessionTokenToplevel);
- }
- finally
- {
- toplevel?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void End_NullsSessionTokenToplevel ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel = null;
-
- try
- {
- toplevel = new Toplevel ();
-
- SessionToken token = app.Begin (toplevel);
- Assert.Same (toplevel, token.Toplevel);
-
- app.End (token);
-
- Assert.Null (token.Toplevel);
- }
- finally
- {
- toplevel?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void ResetState_ClearsSessionStack ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
-
- app.Begin (toplevel1);
- app.Begin (toplevel2);
-
- Assert.Equal (2, app.SessionStack.Count);
- Assert.NotNull (app.Current);
- }
- finally
- {
- // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
-
- // Shutdown calls ResetState, which will clear SessionStack and set Current to null
- app.Shutdown ();
-
- // Verify cleanup happened
- Assert.Empty (app.SessionStack);
- Assert.Null (app.Current);
- Assert.Null (app.CachedSessionTokenToplevel);
- }
- }
- [Fact]
- public void ResetState_StopsAllRunningToplevels ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1", Running = true };
- toplevel2 = new Toplevel { Id = "2", Running = true };
-
- app.Begin (toplevel1);
- app.Begin (toplevel2);
-
- Assert.True (toplevel1.Running);
- Assert.True (toplevel2.Running);
- }
- finally
- {
- // Dispose toplevels BEFORE Shutdown to satisfy DEBUG_IDISPOSABLE assertions
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
-
- // Shutdown calls ResetState, which will stop all running toplevels
- app.Shutdown ();
-
- // Verify toplevels were stopped
- Assert.False (toplevel1!.Running);
- Assert.False (toplevel2!.Running);
- }
- }
- [Fact]
- public void Begin_ActivatesNewToplevel_WhenCurrentExists ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel1 = null;
- Toplevel? toplevel2 = null;
-
- try
- {
- toplevel1 = new Toplevel { Id = "1" };
- toplevel2 = new Toplevel { Id = "2" };
-
- bool toplevel1Deactivated = false;
- bool toplevel2Activated = false;
-
- toplevel1.Deactivate += (s, e) => toplevel1Deactivated = true;
- toplevel2.Activate += (s, e) => toplevel2Activated = true;
-
- app.Begin (toplevel1);
- app.Begin (toplevel2);
-
- Assert.True (toplevel1Deactivated);
- Assert.True (toplevel2Activated);
- Assert.Same (toplevel2, app.Current);
- }
- finally
- {
- toplevel1?.Dispose ();
- toplevel2?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void Begin_DoesNotDuplicateToplevel_WhenIdAlreadyExists ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- Toplevel? toplevel = null;
-
- try
- {
- toplevel = new Toplevel { Id = "test-id" };
-
- app.Begin (toplevel);
- Assert.Single (app.SessionStack);
-
- // Calling Begin again with same toplevel should not duplicate
- app.Begin (toplevel);
- Assert.Single (app.SessionStack);
- }
- finally
- {
- toplevel?.Dispose ();
- app.Shutdown ();
- }
- }
- [Fact]
- public void SessionStack_ContainsAllBegunToplevels ()
- {
- ApplicationImpl app = NewApplicationImpl ();
- var toplevels = new List<Toplevel> ();
-
- try
- {
-
- for (int i = 0; i < 10; i++)
- {
- var toplevel = new Toplevel { Id = $"toplevel-{i}" };
- toplevels.Add (toplevel);
- app.Begin (toplevel);
- }
-
- // All toplevels should be in the stack
- Assert.Equal (10, app.SessionStack.Count);
-
- // Verify stack contains all toplevels
- var stackList = app.SessionStack.ToList ();
- foreach (var toplevel in toplevels)
- {
- Assert.Contains (toplevel, stackList);
- }
- }
- finally
- {
- foreach (var toplevel in toplevels)
- {
- toplevel.Dispose ();
- }
- app.Shutdown ();
- }
- }
- }
|