mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-29 01:07:58 +01:00
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:
@@ -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); }
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user