From ad6925a13af06c5f201d4c2c2106923bda4ed344 Mon Sep 17 00:00:00 2001 From: Tig Date: Sat, 22 Nov 2025 21:51:04 -0700 Subject: [PATCH] WIP: Almost there! Refactored tests and code to align with the modern instance-based application model. Key changes include: - Disabled Sixel rendering in `OutputBase.cs` due to dependency on legacy static `Application` object. - Hardcoded `force16Colors` to `false` in `WindowsOutput.cs` with a `BUGBUG` note. - Updated `ApplicationImplTests` to use `ApplicationImpl.SetInstance` and return `ApplicationImpl.Instance`. - Refactored `ApplicationModelFencingTests` to use `Application.Create()` and added `ResetModelUsageTracking()` for model switching. - Removed legacy `DriverTests` and reintroduced updated versions with cross-platform driver tests. - Reverted `ArrangementTests` and `ShortcutTests` to use legacy static `ApplicationImpl.Instance`. - Reintroduced driver tests in `DriverTests.cs` with modern `Application.Create()` and added `TestTop` for driver content verification. - General cleanup, including removal of outdated code and addition of `BUGBUG` notes for temporary workarounds. --- Terminal.Gui/Drivers/OutputBase.cs | 18 +++--- .../Drivers/WindowsDriver/WindowsOutput.cs | 3 +- .../Application/ApplicationImplTests.cs | 4 +- .../ApplicationModelFencingTests.cs | 14 ++++- Tests/UnitTests/Drivers/DriverTests.cs | 63 ------------------- Tests/UnitTests/View/ArrangementTests.cs | 6 +- Tests/UnitTests/Views/ShortcutTests.cs | 2 +- .../Drivers/DriverTests.cs | 56 ++++++++++++++++- 8 files changed, 85 insertions(+), 81 deletions(-) delete mode 100644 Tests/UnitTests/Drivers/DriverTests.cs diff --git a/Terminal.Gui/Drivers/OutputBase.cs b/Terminal.Gui/Drivers/OutputBase.cs index 6c10fad0b..ad1f4120e 100644 --- a/Terminal.Gui/Drivers/OutputBase.cs +++ b/Terminal.Gui/Drivers/OutputBase.cs @@ -90,14 +90,16 @@ public abstract class OutputBase } } - foreach (SixelToRender s in Application.Sixel) - { - if (!string.IsNullOrWhiteSpace (s.SixelData)) - { - SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y); - Console.Out.Write (s.SixelData); - } - } + // BUGBUG: The Sixel impl depends on the legacy static Application object + // BUGBUG: Disabled for now + //foreach (SixelToRender s in Application.Sixel) + //{ + // if (!string.IsNullOrWhiteSpace (s.SixelData)) + // { + // SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y); + // Console.Out.Write (s.SixelData); + // } + //} SetCursorVisibility (savedVisibility ?? CursorVisibility.Default); _cachedCursorVisibility = savedVisibility; diff --git a/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs b/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs index 1859a9b1b..b351696a2 100644 --- a/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs +++ b/Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs @@ -357,7 +357,8 @@ internal partial class WindowsOutput : OutputBase, IOutput { // BUGBUG: This is bad. It does not work if the app was crated without // BUGBUG: Apis. - bool force16Colors = ApplicationImpl.Instance.Force16Colors; + // bool force16Colors = ApplicationImpl.Instance.Force16Colors; + bool force16Colors = false; if (force16Colors) { diff --git a/Tests/UnitTests/Application/ApplicationImplTests.cs b/Tests/UnitTests/Application/ApplicationImplTests.cs index c995c02e8..ece1897d3 100644 --- a/Tests/UnitTests/Application/ApplicationImplTests.cs +++ b/Tests/UnitTests/Application/ApplicationImplTests.cs @@ -27,7 +27,9 @@ public class ApplicationImplTests m.Setup (f => f.CreateOutput ()).Returns (consoleOutput.Object); m.Setup (f => f.CreateSizeMonitor (It.IsAny (), It.IsAny ())).Returns (Mock.Of ()); - return new ApplicationImpl (m.Object); + // TODO: Move these tests to Parallelizable tests + ApplicationImpl.SetInstance(new ApplicationImpl (m.Object)); + return ApplicationImpl.Instance; } [Fact] diff --git a/Tests/UnitTests/Application/ApplicationModelFencingTests.cs b/Tests/UnitTests/Application/ApplicationModelFencingTests.cs index 5db21d397..a9009baa7 100644 --- a/Tests/UnitTests/Application/ApplicationModelFencingTests.cs +++ b/Tests/UnitTests/Application/ApplicationModelFencingTests.cs @@ -17,7 +17,7 @@ public class ApplicationModelFencingTests public void Create_ThenInstanceAccess_ThrowsInvalidOperationException () { // Create a modern instance-based application - IApplication app = ApplicationImpl.Instance; // Force legacy + IApplication app = Application.Create (); app.Init ("fake"); // Attempting to initialize using the legacy static model should throw @@ -31,6 +31,8 @@ public class ApplicationModelFencingTests // Clean up app.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); + } [Fact] @@ -43,7 +45,7 @@ public class ApplicationModelFencingTests // Attempting to create and initialize with modern instance-based model should throw InvalidOperationException ex = Assert.Throws (() => { - IApplication app = ApplicationImpl.Instance; // Force legacy + IApplication app = Application.Create (); app.Init ("fake"); }); @@ -52,6 +54,7 @@ public class ApplicationModelFencingTests // Clean up staticInstance.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } [Fact] @@ -72,13 +75,14 @@ public class ApplicationModelFencingTests // Clean up staticInstance.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } [Fact] public void Create_ThenInit_ThrowsInvalidOperationException () { // Create a modern instance-based application - IApplication app = ApplicationImpl.Instance; // Force legacy + IApplication app = Application.Create (); app.Init ("fake"); // Attempting to initialize using the legacy static model should throw @@ -92,6 +96,7 @@ public class ApplicationModelFencingTests // Clean up app.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } [Fact] @@ -110,6 +115,7 @@ public class ApplicationModelFencingTests app1.Shutdown (); app2.Shutdown (); app3.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } [Fact] @@ -126,6 +132,7 @@ public class ApplicationModelFencingTests // Clean up instance1.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } [Fact] @@ -150,5 +157,6 @@ public class ApplicationModelFencingTests IApplication app2 = Application.Create (); Assert.NotNull (app2); app2.Shutdown (); + ApplicationImpl.ResetModelUsageTracking (); } } diff --git a/Tests/UnitTests/Drivers/DriverTests.cs b/Tests/UnitTests/Drivers/DriverTests.cs deleted file mode 100644 index 202ec7208..000000000 --- a/Tests/UnitTests/Drivers/DriverTests.cs +++ /dev/null @@ -1,63 +0,0 @@ -#nullable enable -using Xunit.Abstractions; - -namespace UnitTests.DriverTests; - -public class DriverTests (ITestOutputHelper output) -{ - private readonly ITestOutputHelper _output = output; - - [Theory] - [InlineData ("fake")] - [InlineData ("windows")] - [InlineData ("dotnet")] - [InlineData ("unix")] - public void All_Drivers_Init_Shutdown_Cross_Platform (string driverName) - { - IApplication? app = Application.Create (); - app.Init (driverName); - app.Shutdown (); - } - - [Theory] - [InlineData ("fake")] - [InlineData ("windows")] - [InlineData ("dotnet")] - [InlineData ("unix")] - public void All_Drivers_Run_Cross_Platform (string driverName) - { - IApplication? app = Application.Create (); - app.Init (driverName); - app.StopAfterFirstIteration = true; - app.Run ().Dispose (); - app.Shutdown (); - } - - [Theory] - [InlineData ("fake")] - [InlineData ("windows")] - [InlineData ("dotnet")] - [InlineData ("unix")] - public void All_Drivers_LayoutAndDraw_Cross_Platform (string driverName) - { - IApplication? app = Application.Create (); - app.Init (driverName); - app.StopAfterFirstIteration = true; - app.Run ().Dispose (); - - DriverAssert.AssertDriverContentsWithFrameAre (driverName!, _output, app.Driver); - - app.Shutdown (); - } -} - -public class TestTop : Toplevel -{ - /// - public override void BeginInit () - { - Text = Driver!.GetName ()!; - BorderStyle = LineStyle.None; - base.BeginInit (); - } -} diff --git a/Tests/UnitTests/View/ArrangementTests.cs b/Tests/UnitTests/View/ArrangementTests.cs index 13e5d880a..c6b88b8ce 100644 --- a/Tests/UnitTests/View/ArrangementTests.cs +++ b/Tests/UnitTests/View/ArrangementTests.cs @@ -17,7 +17,7 @@ public class ArrangementTests (ITestOutputHelper output) Width = 80, Height = 25 }; - superView.App = Application.Create (); + superView.App = ApplicationImpl.Instance; var movableView = new View { @@ -82,7 +82,7 @@ public class ArrangementTests (ITestOutputHelper output) var superView = new View { - App = Application.Create (), + App = ApplicationImpl.Instance, Width = 80, Height = 25 }; @@ -148,7 +148,7 @@ public class ArrangementTests (ITestOutputHelper output) // This test verifies MouseGrabHandler properly releases when switching between views var superView = new View { Width = 80, Height = 25 }; - superView.App = Application.Create (); + superView.App = ApplicationImpl.Instance; var view1 = new View { diff --git a/Tests/UnitTests/Views/ShortcutTests.cs b/Tests/UnitTests/Views/ShortcutTests.cs index df5dac215..412e41c33 100644 --- a/Tests/UnitTests/Views/ShortcutTests.cs +++ b/Tests/UnitTests/Views/ShortcutTests.cs @@ -349,7 +349,7 @@ public class ShortcutTests [InlineData (KeyCode.F1, 0)] public void KeyDown_App_Scope_Invokes_Accept (KeyCode key, int expectedAccept) { - Application.TopRunnable = new () { App = Application.Create () }; + Application.TopRunnable = new () { App = ApplicationImpl.Instance }; var shortcut = new Shortcut { diff --git a/Tests/UnitTestsParallelizable/Drivers/DriverTests.cs b/Tests/UnitTestsParallelizable/Drivers/DriverTests.cs index 511c7e12f..c1b67cc90 100644 --- a/Tests/UnitTestsParallelizable/Drivers/DriverTests.cs +++ b/Tests/UnitTestsParallelizable/Drivers/DriverTests.cs @@ -3,7 +3,7 @@ using Xunit.Abstractions; namespace UnitTests_Parallelizable.DriverTests; -public class DriverTests : FakeDriverBase +public class DriverTests (ITestOutputHelper output) : FakeDriverBase { [Theory] [InlineData (null, true)] @@ -49,4 +49,58 @@ public class DriverTests : FakeDriverBase driver.End (); } + + [Theory] + [InlineData ("fake")] + [InlineData ("windows")] + [InlineData ("dotnet")] + [InlineData ("unix")] + public void All_Drivers_Init_Shutdown_Cross_Platform (string driverName) + { + IApplication? app = Application.Create (); + app.Init (driverName); + app.Shutdown (); + } + + [Theory] + [InlineData ("fake")] + [InlineData ("windows")] + [InlineData ("dotnet")] + [InlineData ("unix")] + public void All_Drivers_Run_Cross_Platform (string driverName) + { + IApplication? app = Application.Create (); + app.Init (driverName); + app.StopAfterFirstIteration = true; + app.Run ().Dispose (); + app.Shutdown (); + } + + [Theory] + [InlineData ("fake")] + [InlineData ("windows")] + [InlineData ("dotnet")] + [InlineData ("unix")] + public void All_Drivers_LayoutAndDraw_Cross_Platform (string driverName) + { + IApplication? app = Application.Create (); + app.Init (driverName); + app.StopAfterFirstIteration = true; + app.Run ().Dispose (); + + DriverAssert.AssertDriverContentsWithFrameAre (driverName!, output, app.Driver); + + app.Shutdown (); + } +} + +public class TestTop : Toplevel +{ + /// + public override void BeginInit () + { + Text = Driver!.GetName ()!; + BorderStyle = LineStyle.None; + base.BeginInit (); + } }