Skip to content

Commit

Permalink
Merge pull request #3879 from BDisp/v2_3836_setupfakefriver-after-fix
Browse files Browse the repository at this point in the history
Fixes #3836. SetupFakeDriver sometimes causes failure in the unit test.
  • Loading branch information
tig authored Dec 5, 2024
2 parents dbfe521 + 58a63c2 commit 5367e7b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
26 changes: 21 additions & 5 deletions Terminal.Gui/Application/Application.Initialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ internal static void InternalInit (
try
{
MainLoop = Driver!.Init ();
SubscribeDriverEvents ();
}
catch (InvalidOperationException ex)
{
Expand All @@ -163,11 +164,6 @@ internal static void InternalInit (
);
}

Driver.SizeChanged += Driver_SizeChanged;
Driver.KeyDown += Driver_KeyDown;
Driver.KeyUp += Driver_KeyUp;
Driver.MouseEvent += Driver_MouseEvent;

SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());

SupportedCultures = GetSupportedCultures ();
Expand All @@ -176,6 +172,26 @@ internal static void InternalInit (
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); }
Expand Down
5 changes: 1 addition & 4 deletions Terminal.Gui/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ internal static void ResetState (bool ignoreDisposed = false)
// 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;
}
Expand Down
20 changes: 20 additions & 0 deletions UnitTests/Application/ApplicationScreenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,24 @@ public void ClearContents_Called_When_Top_Frame_Changes ()
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);
}
}
6 changes: 6 additions & 0 deletions UnitTests/Application/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,12 @@ public void Screen_Size_Changes ()
Application.Shutdown ();
}

[Fact]
public void InitState_Throws_If_Driver_Is_Null ()
{
Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
}

private void Init ()
{
Application.Init (new FakeDriver ());
Expand Down
14 changes: 6 additions & 8 deletions UnitTests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,9 @@ public override void After (MethodInfo methodUnderTest)
// 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);
}

Expand All @@ -215,6 +210,9 @@ public override void Before (MethodInfo methodUnderTest)
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);
}
Expand Down

0 comments on commit 5367e7b

Please sign in to comment.