| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- #nullable enable
- using Xunit.Abstractions;
- namespace ApplicationTests.RunnableTests;
- /// <summary>
- /// Integration tests for IApplication's IRunnable support.
- /// Tests the full lifecycle of IRunnable instances through Application methods.
- /// </summary>
- public class ApplicationRunnableIntegrationTests
- {
- [Fact]
- public void Begin_AddsRunnableToStack ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- int stackCountBefore = app.SessionStack?.Count ?? 0;
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.NotNull (token);
- Assert.NotNull (token.Runnable);
- Assert.Same (runnable, token.Runnable);
- Assert.Equal (stackCountBefore + 1, app.SessionStack?.Count ?? 0);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_CanBeCanceled_ByIsRunningChanging ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- CancelableRunnable runnable = new () { CancelStart = true };
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert - Should not be added to stack if canceled
- Assert.False (runnable.IsRunning);
- // Token not created
- Assert.Null (token);
- }
- [Fact]
- public void Begin_RaisesIsModalChangedEvent ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- var isModalChangedRaised = false;
- bool? receivedValue = null;
- runnable.IsModalChanged += (s, e) =>
- {
- isModalChangedRaised = true;
- receivedValue = e.Value;
- };
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.True (isModalChangedRaised);
- Assert.True (receivedValue);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_RaisesIsRunningChangedEvent ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- var isRunningChangedRaised = false;
- bool? receivedValue = null;
- runnable.IsRunningChanged += (s, e) =>
- {
- isRunningChangedRaised = true;
- receivedValue = e.Value;
- };
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.True (isRunningChangedRaised);
- Assert.True (receivedValue);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_RaisesIsRunningChangingEvent ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- var isRunningChangingRaised = false;
- bool? oldValue = null;
- bool? newValue = null;
- runnable.IsRunningChanging += (s, e) =>
- {
- isRunningChangingRaised = true;
- oldValue = e.CurrentValue;
- newValue = e.NewValue;
- };
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.True (isRunningChangingRaised);
- Assert.False (oldValue);
- Assert.True (newValue);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_SetsIsModalToTrue ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.True (runnable.IsModal);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_SetsIsRunningToTrue ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- // Act
- SessionToken? token = app.Begin (runnable);
- // Assert
- Assert.True (runnable.IsRunning);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void Begin_ThrowsOnNullRunnable ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- // Act & Assert
- Assert.Throws<ArgumentNullException> (() => app.Begin ((IRunnable)null!));
- }
- [Fact]
- public void End_CanBeCanceled_ByIsRunningChanging ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- CancelableRunnable runnable = new () { CancelStop = true };
- SessionToken? token = app.Begin (runnable);
- runnable.CancelStop = true; // Enable cancellation
- // Act
- app.End (token!);
- // Assert - Should still be running if canceled
- Assert.True (runnable.IsRunning);
- // Force end by disabling cancellation
- runnable.CancelStop = false;
- app.End (token!);
- }
- [Fact]
- public void End_ClearsTokenRunnable ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- // Act
- app.End (token!);
- // Assert
- Assert.Null (token!.Runnable);
- }
- [Fact]
- public void End_RaisesIsRunningChangedEvent ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- var isRunningChangedRaised = false;
- bool? receivedValue = null;
- runnable.IsRunningChanged += (s, e) =>
- {
- isRunningChangedRaised = true;
- receivedValue = e.Value;
- };
- // Act
- app.End (token!);
- // Assert
- Assert.True (isRunningChangedRaised);
- Assert.False (receivedValue);
- }
- [Fact]
- public void End_RaisesIsRunningChangingEvent ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- var isRunningChangingRaised = false;
- bool? oldValue = null;
- bool? newValue = null;
- runnable.IsRunningChanging += (s, e) =>
- {
- isRunningChangingRaised = true;
- oldValue = e.CurrentValue;
- newValue = e.NewValue;
- };
- // Act
- app.End (token!);
- // Assert
- Assert.True (isRunningChangingRaised);
- Assert.True (oldValue);
- Assert.False (newValue);
- }
- [Fact]
- public void End_RemovesRunnableFromStack ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- int stackCountBefore = app.SessionStack?.Count ?? 0;
- // Act
- app.End (token!);
- // Assert
- Assert.Equal (stackCountBefore - 1, app.SessionStack?.Count ?? 0);
- }
- [Fact]
- public void End_SetsIsModalToFalse ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- // Act
- app.End (token!);
- // Assert
- Assert.False (runnable.IsModal);
- }
- [Fact]
- public void End_SetsIsRunningToFalse ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- // Act
- app.End (token!);
- // Assert
- Assert.False (runnable.IsRunning);
- }
- [Fact]
- public void End_ThrowsOnNullToken ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- // Act & Assert
- Assert.Throws<ArgumentNullException> (() => app.End ((SessionToken)null!));
- }
- [Fact]
- public void End_ClearsMouseGrabView ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable = new ();
- SessionToken? token = app.Begin (runnable);
- app.Mouse.GrabMouse (runnable);
- app.End (token!);
- Assert.Null (app.Mouse.MouseGrabView);
- runnable.Dispose ();
- app.Dispose ();
- }
- [Fact]
- public void MultipleRunnables_IndependentResults ()
- {
- // Arrange
- Runnable<int> runnable1 = new ();
- Runnable<string> runnable2 = new ();
- // Act
- runnable1.Result = 42;
- runnable2.Result = "test";
- // Assert
- Assert.Equal (42, runnable1.Result);
- Assert.Equal ("test", runnable2.Result);
- }
- [Fact]
- public void NestedBegin_MaintainsStackOrder ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable1 = new () { Id = "1" };
- Runnable<int> runnable2 = new () { Id = "2" };
- // Act
- SessionToken token1 = app.Begin (runnable1)!;
- SessionToken token2 = app.Begin (runnable2)!;
- // Assert - runnable2 should be on top
- Assert.True (runnable2.IsModal);
- Assert.False (runnable1.IsModal);
- Assert.True (runnable1.IsRunning); // Still running, just not modal
- Assert.True (runnable2.IsRunning);
- // Cleanup
- app.End (token2);
- app.End (token1);
- }
- [Fact]
- public void NestedEnd_RestoresPreviousModal ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- Runnable<int> runnable1 = new () { Id = "1" };
- Runnable<int> runnable2 = new () { Id = "2" };
- SessionToken token1 = app.Begin (runnable1)!;
- SessionToken token2 = app.Begin (runnable2)!;
- // Act - End the top runnable
- app.End (token2);
- // Assert - runnable1 should become modal again
- Assert.True (runnable1.IsModal);
- Assert.False (runnable2.IsModal);
- Assert.True (runnable1.IsRunning);
- Assert.False (runnable2.IsRunning);
- // Cleanup
- app.End (token1);
- }
- [Fact]
- public void RequestStop_WithIRunnable_WorksCorrectly ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- StoppableRunnable runnable = new ();
- SessionToken? token = app.Begin (runnable);
- // Act
- app.RequestStop (runnable);
- // Assert - RequestStop should trigger End eventually
- // For now, just verify it doesn't throw
- Assert.NotNull (runnable);
- // Cleanup
- app.End (token!);
- }
- [Fact]
- public void RequestStop_WithNull_UsesTopRunnable ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- StoppableRunnable runnable = new ();
- SessionToken? token = app.Begin (runnable);
- // Act
- app.RequestStop ((IRunnable?)null);
- // Assert - Should not throw
- Assert.NotNull (runnable);
- // Cleanup
- app.End (token!);
- }
- [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
- public void RunGeneric_CreatesAndReturnsRunnable ()
- {
- // Arrange
- IApplication app = CreateAndInitApp ();
- app.StopAfterFirstIteration = true;
- // Act - With fluent API, Run<T>() returns IApplication for chaining
- IApplication result = app.Run<TestRunnable> ();
- // Assert
- Assert.NotNull (result);
- Assert.Same (app, result); // Fluent API returns this
- // Note: Run blocks until stopped, but StopAfterFirstIteration makes it return immediately
- // The runnable is automatically disposed by Dispose()
- }
- [Fact (Skip = "Run methods with main loop are not suitable for parallel tests - use non-parallel UnitTests instead")]
- public void RunGeneric_ThrowsIfNotInitialized ()
- {
- // Arrange
- IApplication app = Application.Create ();
- // Don't call Init
- // Act & Assert
- Assert.Throws<NotInitializedException> (() => app.Run<TestRunnable> ());
- // Cleanup
- app.Dispose ();
- }
- private IApplication CreateAndInitApp ()
- {
- IApplication app = Application.Create ();
- app.Init ("fake");
- return app;
- }
- /// <summary>
- /// Test runnable that can cancel lifecycle changes.
- /// </summary>
- private class CancelableRunnable : Runnable<int>
- {
- public bool CancelStart { get; set; }
- public bool CancelStop { get; set; }
- protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
- {
- if (newIsRunning && CancelStart)
- {
- return true; // Cancel starting
- }
- if (!newIsRunning && CancelStop)
- {
- return true; // Cancel stopping
- }
- return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
- }
- }
- /// <summary>
- /// Test runnable that can be stopped.
- /// </summary>
- private class StoppableRunnable : Runnable<int>
- {
- public override void RequestStop ()
- {
- WasStopRequested = true;
- base.RequestStop ();
- }
- public bool WasStopRequested { get; private set; }
- }
- /// <summary>
- /// Test runnable for generic Run tests.
- /// </summary>
- private class TestRunnable : Runnable<int>
- {
- public TestRunnable () { Id = "TestRunnable"; }
- }
- }
|