diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 148b5e614..826b3c7ee 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -10,6 +10,7 @@ public class ApplicationTests { _output = output; ConsoleDriver.RunningUnitTests = true; + ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.None; #if DEBUG_IDISPOSABLE Responder.Instances.Clear (); @@ -397,6 +398,12 @@ public class ApplicationTests #endif } + [Fact] + public void Shutdown_Alone_Does_Nothing () + { + Application.Shutdown (); + } + [Theory] [InlineData (typeof (FakeDriver))] [InlineData (typeof (NetDriver))] diff --git a/UnitTests/Application/Mouse/ApplicationMouseTests.cs b/UnitTests/Application/Mouse/ApplicationMouseTests.cs index d25f9adc7..2097dbc18 100644 --- a/UnitTests/Application/Mouse/ApplicationMouseTests.cs +++ b/UnitTests/Application/Mouse/ApplicationMouseTests.cs @@ -138,7 +138,7 @@ public class ApplicationMouseTests /// Tests that the mouse coordinates passed to the focused view are correct when the mouse is clicked. With /// Frames; Frame != Viewport /// - [AutoInitShutdown] + //[AutoInitShutdown] [Theory] // click on border @@ -200,12 +200,12 @@ public class ApplicationMouseTests var clicked = false; - var top = new Toplevel (); - top.X = 0; - top.Y = 0; - top.Width = size.Width * 2; - top.Height = size.Height * 2; - top.BorderStyle = LineStyle.None; + Application.Top = new Toplevel (); + Application.Top.X = 0; + Application.Top.Y = 0; + Application.Top.Width = size.Width * 2; + Application.Top.Height = size.Height * 2; + Application.Top.BorderStyle = LineStyle.None; var view = new View { X = pos.X, Y = pos.Y, Width = size.Width, Height = size.Height }; @@ -213,8 +213,8 @@ public class ApplicationMouseTests view.BorderStyle = LineStyle.Single; view.CanFocus = true; - top.Add (view); - Application.Begin (top); + Application.Top.Add (view); + var mouseEvent = new MouseEvent { Position = new (clickX, clickY), Flags = MouseFlags.Button1Clicked }; view.MouseClick += (s, e) => @@ -226,7 +226,8 @@ public class ApplicationMouseTests Application.OnMouseEvent (mouseEvent); Assert.Equal (expectedClicked, clicked); - top.Dispose (); + Application.Top.Dispose (); + Application.ResetState (ignoreDisposed: true); } #endregion mouse coordinate tests diff --git a/UnitTests/Configuration/AppScopeTests.cs b/UnitTests/Configuration/AppScopeTests.cs index 92f66a47f..059f724d2 100644 --- a/UnitTests/Configuration/AppScopeTests.cs +++ b/UnitTests/Configuration/AppScopeTests.cs @@ -15,7 +15,7 @@ public class AppScopeTests }; [Fact] - [AutoInitShutdown] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void Apply_ShouldApplyUpdatedProperties () { Reset (); diff --git a/UnitTests/Configuration/ConfigurationMangerTests.cs b/UnitTests/Configuration/ConfigurationMangerTests.cs index 68e10cd98..c7b5ef8a4 100644 --- a/UnitTests/Configuration/ConfigurationMangerTests.cs +++ b/UnitTests/Configuration/ConfigurationMangerTests.cs @@ -149,6 +149,8 @@ public class ConfigurationManagerTests [Fact] public void Load_FiresUpdated () { + ConfigLocations savedLocations = Locations; + Locations = ConfigLocations.All; Reset (); Settings! ["Application.QuitKey"].PropertyValue = Key.Q; @@ -183,6 +185,7 @@ public class ConfigurationManagerTests Updated -= ConfigurationManager_Updated; Reset (); + Locations = savedLocations; } [Fact] @@ -414,6 +417,9 @@ public class ConfigurationManagerTests [Fact] public void TestConfigPropertyOmitClassName () { + ConfigLocations savedLocations = Locations; + Locations = ConfigLocations.All; + // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes" PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes"); var scp = (SerializableConfigurationProperty)pi!.GetCustomAttribute (typeof (SerializableConfigurationProperty)); @@ -422,6 +428,8 @@ public class ConfigurationManagerTests Reset (); Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo); + + Locations = savedLocations; } [Fact] @@ -652,6 +660,9 @@ public class ConfigurationManagerTests [Fact] public void TestConfigurationManagerUpdateFromJson () { + ConfigLocations savedLocations = Locations; + Locations = ConfigLocations.All; + // Arrange var json = @" { @@ -816,6 +827,8 @@ public class ConfigurationManagerTests Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground); Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background); Reset (); + + Locations = savedLocations; } [Fact] diff --git a/UnitTests/Configuration/SettingsScopeTests.cs b/UnitTests/Configuration/SettingsScopeTests.cs index a5df19f35..d743b977f 100644 --- a/UnitTests/Configuration/SettingsScopeTests.cs +++ b/UnitTests/Configuration/SettingsScopeTests.cs @@ -5,7 +5,7 @@ namespace Terminal.Gui.ConfigurationTests; public class SettingsScopeTests { [Fact] - [AutoInitShutdown] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void Apply_ShouldApplyProperties () { // arrange @@ -55,6 +55,8 @@ public class SettingsScopeTests [Fact] public void GetHardCodedDefaults_ShouldSetProperties () { + ConfigLocations savedLocations = Locations; + Locations = ConfigLocations.DefaultOnly; Reset (); Assert.Equal (5, ((Dictionary)Settings ["Themes"].PropertyValue).Count); @@ -72,5 +74,7 @@ public class SettingsScopeTests Assert.True (Settings ["Themes"].PropertyValue is Dictionary); Assert.Single ((Dictionary)Settings ["Themes"].PropertyValue); + + Locations = savedLocations; } } diff --git a/UnitTests/Configuration/ThemeScopeTests.cs b/UnitTests/Configuration/ThemeScopeTests.cs index 4da1928c7..64d13e0b4 100644 --- a/UnitTests/Configuration/ThemeScopeTests.cs +++ b/UnitTests/Configuration/ThemeScopeTests.cs @@ -15,6 +15,7 @@ public class ThemeScopeTests }; [Fact] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void AllThemesPresent () { Reset (); @@ -24,7 +25,7 @@ public class ThemeScopeTests } [Fact] - [AutoInitShutdown] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void Apply_ShouldApplyUpdatedProperties () { Reset (); @@ -53,6 +54,7 @@ public class ThemeScopeTests } [Fact] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void TestSerialize_RoundTrip () { Reset (); @@ -69,6 +71,7 @@ public class ThemeScopeTests } [Fact] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void ThemeManager_ClassMethodsWork () { Reset (); diff --git a/UnitTests/Configuration/ThemeTests.cs b/UnitTests/Configuration/ThemeTests.cs index fc1694f8a..ffce969d8 100644 --- a/UnitTests/Configuration/ThemeTests.cs +++ b/UnitTests/Configuration/ThemeTests.cs @@ -10,7 +10,7 @@ public class ThemeTests Converters = { new AttributeJsonConverter (), new ColorJsonConverter () } }; - [Fact] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void TestApply () { Reset (); @@ -32,6 +32,7 @@ public class ThemeTests } [Fact] + [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)] public void TestApply_UpdatesColors () { // Arrange diff --git a/UnitTests/TestHelpers.cs b/UnitTests/TestHelpers.cs index bb76409c2..989e77e80 100644 --- a/UnitTests/TestHelpers.cs +++ b/UnitTests/TestHelpers.cs @@ -5,6 +5,7 @@ using System.Text; using System.Text.RegularExpressions; using Xunit.Abstractions; using Xunit.Sdk; +using static Terminal.Gui.ConfigurationManager; namespace Terminal.Gui; @@ -99,11 +100,7 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute Application.ResetState (ignoreDisposed: true); #endif ConfigurationManager.Reset (); - - if (CM.Locations != CM.ConfigLocations.None) - { - SetCurrentConfig (_savedValues); - } + ConfigurationManager.Locations = CM.ConfigLocations.None; } @@ -131,11 +128,6 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute } #endif Application.Init ((ConsoleDriver)Activator.CreateInstance (_driverType)); - - if (CM.Locations != CM.ConfigLocations.None) - { - _savedValues = GetCurrentConfig (); - } } } @@ -143,65 +135,7 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute private List _savedValues; - private List GetCurrentConfig () - { - CM.Reset (); - - List savedValues = - [ - Dialog.DefaultButtonAlignment, - Dialog.DefaultButtonAlignmentModes, - MessageBox.DefaultBorderStyle - ]; - CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = Alignment.End; - CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue = AlignmentModes.AddSpaceBetweenItems; - CM.Themes! ["Default"] ["MessageBox.DefaultBorderStyle"].PropertyValue = LineStyle.Double; - ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply (); - - return savedValues; - } - - private void SetCurrentConfig (List values) - { - CM.Reset (); - bool needApply = false; - - foreach (object value in values) - { - switch (value) - { - case Alignment alignment: - if ((Alignment)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue! != alignment) - { - needApply = true; - CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = alignment; - } - - break; - case AlignmentModes alignmentModes: - if ((AlignmentModes)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue! != alignmentModes) - { - needApply = true; - CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue = alignmentModes; - } - - break; - case LineStyle lineStyle: - if ((LineStyle)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue! != lineStyle) - { - needApply = true; - CM.Themes! ["Default"] ["MessageBox.DefaultBorderStyle"].PropertyValue = lineStyle; - } - - break; - } - } - - if (needApply) - { - ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply (); - } - } + } [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)] @@ -259,6 +193,7 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute Application.ResetState (); Assert.Null (Application.Driver); Application.Driver = new FakeDriver { Rows = 25, Cols = 25 }; + base.Before (methodUnderTest); } } @@ -766,11 +701,11 @@ internal partial class TestHelpers string replaced = toReplace; replaced = Environment.NewLine.Length switch - { - 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), - 1 => replaced.Replace ("\r\n", Environment.NewLine), - var _ => replaced - }; + { + 2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine), + 1 => replaced.Replace ("\r\n", Environment.NewLine), + var _ => replaced + }; return replaced; }