Skip to content

Commit 99866c8

Browse files
committed
Add internal static InitState method to subscribe events.
1 parent ce17fdd commit 99866c8

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

Terminal.Gui/Application/Application.Initialization.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ internal static void InternalInit (
163163
);
164164
}
165165

166-
Driver.SizeChanged += Driver_SizeChanged;
167-
Driver.KeyDown += Driver_KeyDown;
168-
Driver.KeyUp += Driver_KeyUp;
169-
Driver.MouseEvent += Driver_MouseEvent;
166+
InitState ();
170167

171168
SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
172169

@@ -176,6 +173,16 @@ internal static void InternalInit (
176173
InitializedChanged?.Invoke (null, new (init));
177174
}
178175

176+
internal static void InitState ()
177+
{
178+
ArgumentNullException.ThrowIfNull (Driver);
179+
180+
Driver.SizeChanged += Driver_SizeChanged;
181+
Driver.KeyDown += Driver_KeyDown;
182+
Driver.KeyUp += Driver_KeyUp;
183+
Driver.MouseEvent += Driver_MouseEvent;
184+
}
185+
179186
private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
180187
private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
181188
private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }

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.InitState ();
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.InitState ());
648+
}
649+
644650
private void Init ()
645651
{
646652
Application.Init (new FakeDriver ());

UnitTests/TestHelpers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public override void After (MethodInfo methodUnderTest)
198198
View.Diagnostics = ViewDiagnosticFlags.Off;
199199

200200
Application.ResetState (true);
201+
Assert.Null (Application.Driver);
201202
Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
202203
base.After (methodUnderTest);
203204
}
@@ -209,6 +210,9 @@ public override void Before (MethodInfo methodUnderTest)
209210
Application.ResetState (true);
210211
Assert.Null (Application.Driver);
211212
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.InitState ();
212216

213217
base.Before (methodUnderTest);
214218
}

0 commit comments

Comments
 (0)