Skip to content

Commit 5367e7b

Browse files
authored
Merge pull request #3879 from BDisp/v2_3836_setupfakefriver-after-fix
Fixes #3836. SetupFakeDriver sometimes causes failure in the unit test.
2 parents dbfe521 + 58a63c2 commit 5367e7b

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

Terminal.Gui/Application/Application.Initialization.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ internal static void InternalInit (
150150
try
151151
{
152152
MainLoop = Driver!.Init ();
153+
SubscribeDriverEvents ();
153154
}
154155
catch (InvalidOperationException ex)
155156
{
@@ -163,11 +164,6 @@ internal static void InternalInit (
163164
);
164165
}
165166

166-
Driver.SizeChanged += Driver_SizeChanged;
167-
Driver.KeyDown += Driver_KeyDown;
168-
Driver.KeyUp += Driver_KeyUp;
169-
Driver.MouseEvent += Driver_MouseEvent;
170-
171167
SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
172168

173169
SupportedCultures = GetSupportedCultures ();
@@ -176,6 +172,26 @@ internal static void InternalInit (
176172
InitializedChanged?.Invoke (null, new (init));
177173
}
178174

175+
internal static void SubscribeDriverEvents ()
176+
{
177+
ArgumentNullException.ThrowIfNull (Driver);
178+
179+
Driver.SizeChanged += Driver_SizeChanged;
180+
Driver.KeyDown += Driver_KeyDown;
181+
Driver.KeyUp += Driver_KeyUp;
182+
Driver.MouseEvent += Driver_MouseEvent;
183+
}
184+
185+
internal static void UnsubscribeDriverEvents ()
186+
{
187+
ArgumentNullException.ThrowIfNull (Driver);
188+
189+
Driver.SizeChanged -= Driver_SizeChanged;
190+
Driver.KeyDown -= Driver_KeyDown;
191+
Driver.KeyUp -= Driver_KeyUp;
192+
Driver.MouseEvent -= Driver_MouseEvent;
193+
}
194+
179195
private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
180196
private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
181197
private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }

Terminal.Gui/Application/Application.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ internal static void ResetState (bool ignoreDisposed = false)
177177
// Driver stuff
178178
if (Driver is { })
179179
{
180-
Driver.SizeChanged -= Driver_SizeChanged;
181-
Driver.KeyDown -= Driver_KeyDown;
182-
Driver.KeyUp -= Driver_KeyUp;
183-
Driver.MouseEvent -= Driver_MouseEvent;
180+
UnsubscribeDriverEvents ();
184181
Driver?.End ();
185182
Driver = null;
186183
}

UnitTests/Application/ApplicationScreenTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,24 @@ public void ClearContents_Called_When_Top_Frame_Changes ()
6565
Application.Top = null;
6666
Application.Shutdown ();
6767
}
68+
69+
[Fact]
70+
public void Screen_Changes_OnSizeChanged_Without_Call_Application_Init ()
71+
{
72+
// Arrange
73+
Application.ResetState (true);
74+
Assert.Null (Application.Driver);
75+
Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
76+
Application.SubscribeDriverEvents ();
77+
Assert.Equal (new (0, 0, 25, 25), Application.Screen);
78+
79+
// Act
80+
(((FakeDriver)Application.Driver)!).SetBufferSize (120, 30);
81+
82+
// Assert
83+
Assert.Equal (new (0, 0, 120, 30), Application.Screen);
84+
85+
// Cleanup
86+
Application.ResetState (true);
87+
}
6888
}

UnitTests/Application/ApplicationTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ public void Screen_Size_Changes ()
641641
Application.Shutdown ();
642642
}
643643

644+
[Fact]
645+
public void InitState_Throws_If_Driver_Is_Null ()
646+
{
647+
Assert.Throws<ArgumentNullException> (static () => Application.SubscribeDriverEvents ());
648+
}
649+
644650
private void Init ()
645651
{
646652
Application.Init (new FakeDriver ());

UnitTests/TestHelpers.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,9 @@ public override void After (MethodInfo methodUnderTest)
197197
// Turn off diagnostic flags in case some test left them on
198198
View.Diagnostics = ViewDiagnosticFlags.Off;
199199

200-
if (Application.Driver is { })
201-
{
202-
((FakeDriver)Application.Driver).Rows = 25;
203-
((FakeDriver)Application.Driver).Cols = 25;
204-
((FakeDriver)Application.Driver).End ();
205-
}
206-
207-
Application.Driver = null;
200+
Application.ResetState (true);
201+
Assert.Null (Application.Driver);
202+
Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
208203
base.After (methodUnderTest);
209204
}
210205

@@ -215,6 +210,9 @@ public override void Before (MethodInfo methodUnderTest)
215210
Application.ResetState (true);
216211
Assert.Null (Application.Driver);
217212
Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
213+
Assert.Equal (new (0, 0, 25, 25), Application.Screen);
214+
// Ensures subscribing events, at least for the SizeChanged event
215+
Application.SubscribeDriverEvents ();
218216

219217
base.Before (methodUnderTest);
220218
}

0 commit comments

Comments
 (0)