Merge pull request #3879 from BDisp/v2_3836_setupfakefriver-after-fix

Fixes #3836. SetupFakeDriver sometimes causes failure in the unit test.
This commit is contained in:
Tig
2024-12-05 13:16:23 -07:00
committed by GitHub
5 changed files with 54 additions and 17 deletions

View File

@@ -150,6 +150,7 @@ public static partial class Application // Initialization (Init/Shutdown)
try
{
MainLoop = Driver!.Init ();
SubscribeDriverEvents ();
}
catch (InvalidOperationException ex)
{
@@ -163,11 +164,6 @@ public static partial class Application // Initialization (Init/Shutdown)
);
}
Driver.SizeChanged += Driver_SizeChanged;
Driver.KeyDown += Driver_KeyDown;
Driver.KeyUp += Driver_KeyUp;
Driver.MouseEvent += Driver_MouseEvent;
SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
SupportedCultures = GetSupportedCultures ();
@@ -176,6 +172,26 @@ public static partial class Application // Initialization (Init/Shutdown)
InitializedChanged?.Invoke (null, new (init));
}
internal static void SubscribeDriverEvents ()
{
ArgumentNullException.ThrowIfNull (Driver);
Driver.SizeChanged += Driver_SizeChanged;
Driver.KeyDown += Driver_KeyDown;
Driver.KeyUp += Driver_KeyUp;
Driver.MouseEvent += Driver_MouseEvent;
}
internal static void UnsubscribeDriverEvents ()
{
ArgumentNullException.ThrowIfNull (Driver);
Driver.SizeChanged -= Driver_SizeChanged;
Driver.KeyDown -= Driver_KeyDown;
Driver.KeyUp -= Driver_KeyUp;
Driver.MouseEvent -= Driver_MouseEvent;
}
private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }

View File

@@ -177,10 +177,7 @@ public static partial class Application
// Driver stuff
if (Driver is { })
{
Driver.SizeChanged -= Driver_SizeChanged;
Driver.KeyDown -= Driver_KeyDown;
Driver.KeyUp -= Driver_KeyUp;
Driver.MouseEvent -= Driver_MouseEvent;
UnsubscribeDriverEvents ();
Driver?.End ();
Driver = null;
}

View File

@@ -65,4 +65,24 @@ public class ApplicationScreenTests (ITestOutputHelper output)
Application.Top = null;
Application.Shutdown ();
}
[Fact]
public void Screen_Changes_OnSizeChanged_Without_Call_Application_Init ()
{
// Arrange
Application.ResetState (true);
Assert.Null (Application.Driver);
Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
Application.SubscribeDriverEvents ();
Assert.Equal (new (0, 0, 25, 25), Application.Screen);
// Act
(((FakeDriver)Application.Driver)!).SetBufferSize (120, 30);
// Assert
Assert.Equal (new (0, 0, 120, 30), Application.Screen);
// Cleanup
Application.ResetState (true);
}
}

View File

@@ -641,6 +641,12 @@ public class ApplicationTests
Application.Shutdown ();
}
[Fact]
public void InitState_Throws_If_Driver_Is_Null ()
{
Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
}
private void Init ()
{
Application.Init (new FakeDriver ());

View File

@@ -197,14 +197,9 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
// Turn off diagnostic flags in case some test left them on
View.Diagnostics = ViewDiagnosticFlags.Off;
if (Application.Driver is { })
{
((FakeDriver)Application.Driver).Rows = 25;
((FakeDriver)Application.Driver).Cols = 25;
((FakeDriver)Application.Driver).End ();
}
Application.Driver = null;
Application.ResetState (true);
Assert.Null (Application.Driver);
Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
base.After (methodUnderTest);
}
@@ -215,6 +210,9 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
Application.ResetState (true);
Assert.Null (Application.Driver);
Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
Assert.Equal (new (0, 0, 25, 25), Application.Screen);
// Ensures subscribing events, at least for the SizeChanged event
Application.SubscribeDriverEvents ();
base.Before (methodUnderTest);
}