فهرست منبع

Add internal static InitState method to subscribe events.

BDisp 9 ماه پیش
والد
کامیت
99866c80b5

+ 11 - 4
Terminal.Gui/Application/Application.Initialization.cs

@@ -163,10 +163,7 @@ public static partial class Application // Initialization (Init/Shutdown)
                                                 );
         }
 
-        Driver.SizeChanged += Driver_SizeChanged;
-        Driver.KeyDown += Driver_KeyDown;
-        Driver.KeyUp += Driver_KeyUp;
-        Driver.MouseEvent += Driver_MouseEvent;
+        InitState ();
 
         SynchronizationContext.SetSynchronizationContext (new MainLoopSyncContext ());
 
@@ -176,6 +173,16 @@ public static partial class Application // Initialization (Init/Shutdown)
         InitializedChanged?.Invoke (null, new (init));
     }
 
+    internal static void InitState ()
+    {
+        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); }

+ 20 - 0
UnitTests/Application/ApplicationScreenTests.cs

@@ -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.InitState ();
+        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 - 0
UnitTests/Application/ApplicationTests.cs

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

+ 4 - 0
UnitTests/TestHelpers.cs

@@ -198,6 +198,7 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
         View.Diagnostics = ViewDiagnosticFlags.Off;
 
         Application.ResetState (true);
+        Assert.Null (Application.Driver);
         Assert.Equal (new (0, 0, 2048, 2048), Application.Screen);
         base.After (methodUnderTest);
     }
@@ -209,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.InitState ();
 
         base.Before (methodUnderTest);
     }