diff --git a/src/Common/tests/TestUtilities/AnchorLayoutV2Scope.cs b/src/Common/tests/TestUtilities/AnchorLayoutV2Scope.cs
new file mode 100644
index 00000000000..5f07b05cac9
--- /dev/null
+++ b/src/Common/tests/TestUtilities/AnchorLayoutV2Scope.cs
@@ -0,0 +1,33 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+
+///
+/// Scope for enabling / disabling the AnchorLayoutV2 Switch.
+/// Use in a statement.
+///
+public readonly ref struct AnchorLayoutV2Scope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public AnchorLayoutV2Scope(bool enable)
+ {
+ // Prevent multiple AnchorLayoutV2Scope instances from running simultaneously.
+ // Using Monitor to allow recursion on the same thread.
+ Monitor.Enter(typeof(AnchorLayoutV2Scope));
+ _switchScope = new(AppContextSwitchNames.AnchorLayoutV2, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(AnchorLayoutV2Scope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/AppContextSwitchNames.cs b/src/Common/tests/TestUtilities/AppContextSwitchNames.cs
index cfff428ac46..05e1c3f88ed 100644
--- a/src/Common/tests/TestUtilities/AppContextSwitchNames.cs
+++ b/src/Common/tests/TestUtilities/AppContextSwitchNames.cs
@@ -7,15 +7,60 @@ namespace System;
public static class AppContextSwitchNames
{
+ ///
+ /// The switch that controls whether AnchorLayoutV2 feature is enabled.
+ ///
+ public const string AnchorLayoutV2
+ = "System.Windows.Forms.AnchorLayoutV2";
+
+ ///
+ /// The switch that controls whether the parent font (as set by
+ /// or by the parent control or form's font) is applied to menus.
+ ///
+ public const string ApplyParentFontToMenus
+ = "System.Windows.Forms.ApplyParentFontToMenus";
+
+ ///
+ /// The switch that controls whether or not the DataGridView starts its UI row count at zero.
+ ///
+ public const string DataGridViewUIAStartRowCountAtZero
+ = "System.Windows.Forms.DataGridViewUIAStartRowCountAtZero";
+
///
/// The switch that controls whether or not the is enabled.
///
- public static string EnableUnsafeBinaryFormatterSerialization { get; }
+ public const string EnableUnsafeBinaryFormatterSerialization
= "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization";
///
/// Switch that controls switch caching.
///
- public static string LocalAppContext_DisableCaching { get; }
+ public const string LocalAppContext_DisableCaching
= "TestSwitch.LocalAppContext.DisableCaching";
+
+ ///
+ /// The switch that controls whether UIA notifications are raised.
+ ///
+ public const string NoClientNotifications
+ = "Switch.System.Windows.Forms.AccessibleObject.NoClientNotifications";
+
+ ///
+ /// The switch that controls whether to scale the top level form min/max size for dpi.
+ ///
+ public const string ScaleTopLevelFormMinMaxSizeForDpi
+ = "System.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi";
+
+ ///
+ /// The switch that controls whether certificates are checked against the certificate authority revocation list.
+ /// If true, revoked certificates will not be accepted by WebRequests and WebClients as valid.
+ /// Otherwise, revoked certificates will be accepted as valid.
+ ///
+ public const string ServicePointManagerCheckCrl
+ = "System.Windows.Forms.ServicePointManagerCheckCrl";
+
+ ///
+ /// The switch that controls whether the TreeNodeCollection will insert nodes in the sorted order.
+ ///
+ public const string TreeNodeCollectionAddRangeRespectsSortOrder
+ = "System.Windows.Forms.ApplyParentFontToMenus";
}
diff --git a/src/Common/tests/TestUtilities/ApplyParentFontToMenusScope.cs b/src/Common/tests/TestUtilities/ApplyParentFontToMenusScope.cs
new file mode 100644
index 00000000000..192cad2c4eb
--- /dev/null
+++ b/src/Common/tests/TestUtilities/ApplyParentFontToMenusScope.cs
@@ -0,0 +1,33 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+
+///
+/// Scope for enabling / disabling the ApplyParentFontToMenus Switch.
+/// Use in a statement.
+///
+public readonly ref struct ApplyParentFontToMenusScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public ApplyParentFontToMenusScope(bool enable)
+ {
+ // Prevent multiple ApplyParentFontToMenusScopes from running simultaneously. Using Monitor to allow recursion on
+ // the same thread.
+ Monitor.Enter(typeof(ApplyParentFontToMenusScope));
+ _switchScope = new(AppContextSwitchNames.ApplyParentFontToMenus, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(ApplyParentFontToMenusScope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/BinaryFormatterScope.cs b/src/Common/tests/TestUtilities/BinaryFormatterScope.cs
index 9bfd996f2c2..09fc210a509 100644
--- a/src/Common/tests/TestUtilities/BinaryFormatterScope.cs
+++ b/src/Common/tests/TestUtilities/BinaryFormatterScope.cs
@@ -17,7 +17,7 @@ public BinaryFormatterScope(bool enable)
// Prevent multiple BinaryFormatterScopes from running simultaneously. Using Monitor to allow recursion on
// the same thread.
Monitor.Enter(typeof(BinaryFormatterScope));
- _switchScope = new AppContextSwitchScope(AppContextSwitchNames.EnableUnsafeBinaryFormatterSerialization, enable);
+ _switchScope = new(AppContextSwitchNames.EnableUnsafeBinaryFormatterSerialization, enable);
}
public void Dispose()
diff --git a/src/Common/tests/TestUtilities/DataGridViewUIAStartRowCountAtZeroScope.cs b/src/Common/tests/TestUtilities/DataGridViewUIAStartRowCountAtZeroScope.cs
new file mode 100644
index 00000000000..73992929041
--- /dev/null
+++ b/src/Common/tests/TestUtilities/DataGridViewUIAStartRowCountAtZeroScope.cs
@@ -0,0 +1,32 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+///
+/// Scope for enabling / disabling the DataGridViewUIAStartRowCountAtZero Switch.
+/// Use in a statement.
+///
+public readonly ref struct DataGridViewUIAStartRowCountAtZeroScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public DataGridViewUIAStartRowCountAtZeroScope(bool enable)
+ {
+ // Prevent multiple BinaryFormatterScopes from running simultaneously. Using Monitor to allow recursion on
+ // the same thread.
+ Monitor.Enter(typeof(DataGridViewUIAStartRowCountAtZeroScope));
+ _switchScope = new(AppContextSwitchNames.DataGridViewUIAStartRowCountAtZero, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(DataGridViewUIAStartRowCountAtZeroScope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/NoClientNotificationsScope.cs b/src/Common/tests/TestUtilities/NoClientNotificationsScope.cs
new file mode 100644
index 00000000000..def9b9c6009
--- /dev/null
+++ b/src/Common/tests/TestUtilities/NoClientNotificationsScope.cs
@@ -0,0 +1,33 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+
+///
+/// Scope for enabling / disabling the NoClientNotifications Switch.
+/// Use in a statement.
+///
+public readonly ref struct NoClientNotificationsScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public NoClientNotificationsScope(bool enable)
+ {
+ // Prevent multiple NoClientNotificationsScopes from running simultaneously. Using Monitor to allow recursion on
+ // the same thread.
+ Monitor.Enter(typeof(NoClientNotificationsScope));
+ _switchScope = new(AppContextSwitchNames.NoClientNotifications, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(NoClientNotificationsScope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/ScaleTopLevelFormMinMaxSizeForDpiScope.cs b/src/Common/tests/TestUtilities/ScaleTopLevelFormMinMaxSizeForDpiScope.cs
new file mode 100644
index 00000000000..1bc6029ebfc
--- /dev/null
+++ b/src/Common/tests/TestUtilities/ScaleTopLevelFormMinMaxSizeForDpiScope.cs
@@ -0,0 +1,33 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+
+///
+/// Scope for enabling / disabling the ScaleTopLevelFormMinMaxSizeForDpi Switch.
+/// Use in a statement.
+///
+public readonly ref struct ScaleTopLevelFormMinMaxSizeForDpiScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public ScaleTopLevelFormMinMaxSizeForDpiScope(bool enable)
+ {
+ // Prevent multiple ScaleTopLevelFormMinMaxSizeForDpi from running simultaneously.
+ // Using Monitor to allow recursion on the same thread.
+ Monitor.Enter(typeof(ScaleTopLevelFormMinMaxSizeForDpiScope));
+ _switchScope = new(AppContextSwitchNames.ScaleTopLevelFormMinMaxSizeForDpi, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(ScaleTopLevelFormMinMaxSizeForDpiScope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/ServicePointManagerCheckCrlScope.cs b/src/Common/tests/TestUtilities/ServicePointManagerCheckCrlScope.cs
new file mode 100644
index 00000000000..7f070c99dfb
--- /dev/null
+++ b/src/Common/tests/TestUtilities/ServicePointManagerCheckCrlScope.cs
@@ -0,0 +1,33 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+
+///
+/// Scope for enabling / disabling the ServicePointManagerCheckCrl Switch.
+/// Use in a statement.
+///
+public readonly ref struct ServicePointManagerCheckCrlScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public ServicePointManagerCheckCrlScope(bool enable)
+ {
+ // Prevent multiple ServicePointManagerCheckCrlScope instances from running simultaneously.
+ // Using Monitor to allow recursion on the same thread.
+ Monitor.Enter(typeof(ServicePointManagerCheckCrlScope));
+ _switchScope = new(AppContextSwitchNames.ServicePointManagerCheckCrl, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(ServicePointManagerCheckCrlScope));
+ }
+ }
+}
diff --git a/src/Common/tests/TestUtilities/TreeNodeCollectionAddRangeRespectsSortOrderScope.cs b/src/Common/tests/TestUtilities/TreeNodeCollectionAddRangeRespectsSortOrderScope.cs
new file mode 100644
index 00000000000..57e19df83a1
--- /dev/null
+++ b/src/Common/tests/TestUtilities/TreeNodeCollectionAddRangeRespectsSortOrderScope.cs
@@ -0,0 +1,32 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace System;
+///
+/// Scope for enabling / disabling the TreeNodeCollectionAddRangeRespectsSortOrder Switch.
+/// Use in a statement.
+///
+public readonly ref struct TreeNodeCollectionAddRangeRespectsSortOrderScope
+{
+ private readonly AppContextSwitchScope _switchScope;
+
+ public TreeNodeCollectionAddRangeRespectsSortOrderScope(bool enable)
+ {
+ // Prevent multiple TreeNodeCollectionAddRangeRespectsSortOrderScopes from running simultaneously. Using Monitor to allow recursion on
+ // the same thread.
+ Monitor.Enter(typeof(TreeNodeCollectionAddRangeRespectsSortOrderScope));
+ _switchScope = new(AppContextSwitchNames.TreeNodeCollectionAddRangeRespectsSortOrder, enable);
+ }
+
+ public void Dispose()
+ {
+ try
+ {
+ _switchScope.Dispose();
+ }
+ finally
+ {
+ Monitor.Exit(typeof(TreeNodeCollectionAddRangeRespectsSortOrderScope));
+ }
+ }
+}
diff --git a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
index ae006152b9a..4a9d8926ab4 100644
--- a/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
+++ b/src/System.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
@@ -218,33 +218,4 @@ public static bool TreeNodeCollectionAddRangeRespectsSortOrder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue(TreeNodeCollectionAddRangeRespectsSortOrderSwitchName, ref s_treeNodeCollectionAddRangeRespectsSortOrder);
}
-
- internal static void SetLocalAppContextSwitchValue(string switchName, bool value)
- {
- if (switchName == NoClientNotificationsSwitchName)
- {
- s_noClientNotifications = value ? 1 : 0;
- }
-
- if (switchName == DataGridViewUIAStartRowCountAtZeroSwitchName)
- {
- s_dataGridViewUIAStartRowCountAtZero = value ? 1 : 0;
- }
-
- if (switchName == ApplyParentFontToMenusSwitchName)
- {
- s_applyParentFontToMenus = value ? 1 : 0;
- }
-
- if (switchName == TreeNodeCollectionAddRangeRespectsSortOrderSwitchName)
- {
- s_treeNodeCollectionAddRangeRespectsSortOrder = value ? 1 : 0;
- }
- }
-
- internal static bool GetCachedSwitchValue(string switchName)
- {
- int cachedSwitchValue = 0;
- return GetCachedSwitchValue(switchName, ref cachedSwitchValue);
- }
}
diff --git a/src/System.Windows.Forms.Primitives/tests/TestUtilities/TargetFrameworkNameScope.cs b/src/System.Windows.Forms.Primitives/tests/TestUtilities/TargetFrameworkNameScope.cs
new file mode 100644
index 00000000000..f70bcb49ae0
--- /dev/null
+++ b/src/System.Windows.Forms.Primitives/tests/TestUtilities/TargetFrameworkNameScope.cs
@@ -0,0 +1,41 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Runtime.Versioning;
+using System.Windows.Forms.Primitives;
+
+namespace System;
+
+#nullable enable
+
+///
+/// Scope for setting the see temporarily.
+/// Use in a statement.
+///
+public readonly ref struct TargetFrameworkNameScope
+{
+ private readonly FrameworkName? _previousTargetFrameworkName;
+ private readonly dynamic _testAccessor;
+
+ public TargetFrameworkNameScope(string targetFrameworkName)
+ {
+ _testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
+ ResetLocalSwitches();
+ _previousTargetFrameworkName = LocalAppContextSwitches.TargetFrameworkName;
+ _testAccessor.s_targetFrameworkName = new FrameworkName(targetFrameworkName);
+ }
+
+ public void Dispose()
+ {
+ _testAccessor.s_targetFrameworkName = _previousTargetFrameworkName;
+ ResetLocalSwitches();
+ }
+
+ private void ResetLocalSwitches()
+ {
+ _testAccessor.s_anchorLayoutV2 = 0;
+ _testAccessor.s_scaleTopLevelFormMinMaxSizeForDpi = 0;
+ _testAccessor.s_trackBarModernRendering = 0;
+ _testAccessor.s_servicePointManagerCheckCrl = 0;
+ }
+}
diff --git a/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs
index 1fbae5a7a54..a4cedca1bd8 100644
--- a/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs
+++ b/src/System.Windows.Forms.Primitives/tests/UnitTests/System/Windows/Forms/LocalAppContextSwitchesTest.cs
@@ -1,44 +1,21 @@
// Licensed to the.NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Runtime.Versioning;
using System.Windows.Forms.Primitives;
namespace System.Windows.Forms.Tests;
public class LocalAppContextSwitchesTest
{
- private void ResetLocalSwitches(dynamic testAccessor)
- {
- testAccessor.s_anchorLayoutV2 = 0;
- testAccessor.s_scaleTopLevelFormMinMaxSizeForDpi = 0;
- testAccessor.s_trackBarModernRendering = 0;
- testAccessor.s_servicePointManagerCheckCrl = 0;
- }
-
[WinFormsTheory]
[InlineData(".NETCoreApp,Version=v8.0", true)]
[InlineData(".NETCoreApp,Version=v7.0", false)]
[InlineData(".NET Framework,Version=v4.8", false)]
public void Validate_Default_Switch_Values(string targetFrameworkName, bool expected)
{
- FrameworkName? previousTestTargetFramework = LocalAppContextSwitches.TargetFrameworkName;
- dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
- ResetLocalSwitches(testAccessor);
-
- try
- {
- testAccessor.s_targetFrameworkName = new FrameworkName(targetFrameworkName);
-
- Assert.Equal(expected, LocalAppContextSwitches.TrackBarModernRendering);
- Assert.Equal(expected, LocalAppContextSwitches.ScaleTopLevelFormMinMaxSizeForDpi);
- Assert.Equal(expected, LocalAppContextSwitches.ServicePointManagerCheckCrl);
- }
- finally
- {
- // Reset target framework name.
- testAccessor.s_targetFrameworkName = previousTestTargetFramework;
- ResetLocalSwitches(testAccessor);
- }
+ using TargetFrameworkNameScope scope = new(targetFrameworkName);
+ Assert.Equal(expected, LocalAppContextSwitches.TrackBarModernRendering);
+ Assert.Equal(expected, LocalAppContextSwitches.ScaleTopLevelFormMinMaxSizeForDpi);
+ Assert.Equal(expected, LocalAppContextSwitches.ServicePointManagerCheckCrl);
}
}
diff --git a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/AnchorLayoutTests.cs b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/AnchorLayoutTests.cs
index 511e89647e5..38b5b305aae 100644
--- a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/AnchorLayoutTests.cs
+++ b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/AnchorLayoutTests.cs
@@ -3,7 +3,6 @@
using System.Drawing;
using System.Windows.Forms.Layout;
-using System.Windows.Forms.Primitives;
using Xunit.Abstractions;
namespace System.Windows.Forms.UITests;
@@ -44,23 +43,14 @@ public void Control_ResizeAnchoredControls_ParentHandleCreated_NewAnchorsApplied
[InlineData(AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left, 20, 30, 220, 330)]
public void Control_AnchorLayoutV2_ResizeAnchoredControls_ParentHandleCreated_NewAnchorsApplied(AnchorStyles anchors, int expectedX, int expectedY, int expectedWidth, int expectedHeight)
{
- int previousLayoutSwitch = SetAnchorLayoutV2();
-
- try
- {
- LaunchFormAndVerify(anchors, expectedX, expectedY, expectedWidth, expectedHeight);
- }
- finally
- {
- // Reset switch.
- SetAnchorLayoutV2Switch(previousLayoutSwitch);
- }
+ using AnchorLayoutV2Scope scope = new(enable: true);
+ LaunchFormAndVerify(anchors, expectedX, expectedY, expectedWidth, expectedHeight);
}
[WinFormsFact]
public void Control_NotParented_AnchorsNotComputed()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
@@ -77,8 +67,6 @@ public void Control_NotParented_AnchorsNotComputed()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -86,7 +74,7 @@ public void Control_NotParented_AnchorsNotComputed()
[WinFormsFact]
public void Control_SuspendedLayout_AnchorsNotComputed()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
@@ -100,8 +88,6 @@ public void Control_SuspendedLayout_AnchorsNotComputed()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -109,7 +95,7 @@ public void Control_SuspendedLayout_AnchorsNotComputed()
[WinFormsFact]
public void Control_ResumedLayout_AnchorsComputed()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
@@ -127,8 +113,6 @@ public void Control_ResumedLayout_AnchorsComputed()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -136,7 +120,7 @@ public void Control_ResumedLayout_AnchorsComputed()
[WinFormsFact]
public void ConfigSwitch_Disabled_SuspendedLayout_AnchorsComputed()
{
- int previousSwitchValue = SetAnchorLayoutV1();
+ using AnchorLayoutV2Scope scope = new(enable: false);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
@@ -148,8 +132,6 @@ public void ConfigSwitch_Disabled_SuspendedLayout_AnchorsComputed()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -157,7 +139,7 @@ public void ConfigSwitch_Disabled_SuspendedLayout_AnchorsComputed()
[WinFormsFact]
public void NestedContainer_AnchorsComputed()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
{
@@ -179,8 +161,6 @@ public void NestedContainer_AnchorsComputed()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -188,7 +168,7 @@ public void NestedContainer_AnchorsComputed()
[WinFormsFact]
public void ParentChanged_AnchorsUpdated()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
{
@@ -218,8 +198,6 @@ public void ParentChanged_AnchorsUpdated()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -227,7 +205,7 @@ public void ParentChanged_AnchorsUpdated()
[WinFormsFact]
public void SetBoundsOnAnchoredControl_BoundsChanged()
{
- int previousSwitchValue = SetAnchorLayoutV2();
+ using AnchorLayoutV2Scope scope = new(enable: true);
(Form form, Button button) = GetFormWithAnchoredButton(AnchorAllDirection);
try
{
@@ -254,8 +232,6 @@ public void SetBoundsOnAnchoredControl_BoundsChanged()
}
finally
{
- // Reset switch.
- SetAnchorLayoutV2Switch(previousSwitchValue);
Dispose(form, button);
}
}
@@ -313,23 +289,6 @@ private static (Form, Button) GetFormWithAnchoredButton(AnchorStyles buttonAncho
return (form, button);
}
- private static int SetAnchorLayoutV2Switch(int value)
- {
- // TargetFramework on the test host.exe is NetCoreApp2.1. AppContext.TargetFrameworkName return this value
- // while running unit tests. To avoid using this invalid target framework for unit tests, we are
- // explicitly setting and unsetting the switch.
- // Switch value has 3 states: 0 - unknown, 1 - true, -1 - false
- dynamic localAppContextSwitches = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
- int previousSwitchValue = localAppContextSwitches.s_anchorLayoutV2;
- localAppContextSwitches.s_anchorLayoutV2 = value;
-
- return previousSwitchValue;
- }
-
- private static int SetAnchorLayoutV2() => SetAnchorLayoutV2Switch(value: 1);
-
- private static int SetAnchorLayoutV1() => SetAnchorLayoutV2Switch(value: -1);
-
private static void Dispose(Form form, Button button)
{
button.Dispose();
diff --git a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/Dpi/FormDpiTests.cs b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/Dpi/FormDpiTests.cs
index 0b2963a33f9..bf5fb27dc52 100644
--- a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/Dpi/FormDpiTests.cs
+++ b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/Dpi/FormDpiTests.cs
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Windows.Forms.Primitives;
using Windows.Win32.UI.HiDpi;
using Xunit.Abstractions;
@@ -108,16 +107,10 @@ public void Form_DpiChanged_MinMaxSizeChanged_WithRuntimeSetting(int newDpi)
form.Show();
// Explicitly opt-in to resize min and max sizes with Dpi changed event.
- dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
- testAccessor.s_scaleTopLevelFormMinMaxSizeForDpi = 1;
-
+ using ScaleTopLevelFormMinMaxSizeForDpiScope scope = new(enable: true);
DpiMessageHelper.TriggerDpiMessage(PInvoke.WM_DPICHANGED, form, newDpi);
-
Assert.NotEqual(form.MinimumSize, minSize);
Assert.NotEqual(form.MaximumSize, maxSize);
-
- // Reset switch.
- testAccessor.s_scaleTopLevelFormMinMaxSizeForDpi = -1;
form.Close();
}
finally
diff --git a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/MessageBoxTests.cs b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/MessageBoxTests.cs
index 8b3b1ecd34c..b969faa0ff1 100644
--- a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/MessageBoxTests.cs
+++ b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/MessageBoxTests.cs
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Windows.Forms.Primitives;
-
namespace System.Windows.Forms.UITests;
public class MessageBoxTests
@@ -10,9 +8,7 @@ public class MessageBoxTests
[WinFormsFact]
public void MessageBox_MessageBoxDialogResult_Valid()
{
- bool defaultSwitchValue = LocalAppContextSwitches.GetCachedSwitchValue(LocalAppContextSwitches.NoClientNotificationsSwitchName);
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.NoClientNotificationsSwitchName, true);
+ using NoClientNotificationsScope scope = new(enable: true);
Assert.Equal(DialogResult.None, MessageBox.Show("Testing DialogResult"));
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.NoClientNotificationsSwitchName, defaultSwitchValue);
}
}
diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewCellAccessibleObjectTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewCellAccessibleObjectTests.cs
index acebbd20412..fe440503433 100644
--- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewCellAccessibleObjectTests.cs
+++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewCellAccessibleObjectTests.cs
@@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Drawing;
-using System.Windows.Forms.Primitives;
using Moq;
using Moq.Protected;
using Windows.Win32.System.Variant;
@@ -769,7 +768,7 @@ public void DataGridViewCellAccessibleObject_Row_ReturnExpected()
}
[WinFormsFact]
- public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfFirstRowHidden()
+ public void DataGridViewCellAccessibleObject_Row_ReturnExpected_IfFirstRowHidden()
{
using DataGridView dataGridView = new();
dataGridView.Columns.Add("Column 1", "Column 1");
@@ -789,7 +788,7 @@ public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfFirstRowHidde
}
[WinFormsFact]
- public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfSecondRowHidden()
+ public void DataGridViewCellAccessibleObject_Row_ReturnExpected_IfSecondRowHidden()
{
using DataGridView dataGridView = new();
dataGridView.Columns.Add("Column 1", "Column 1");
@@ -809,7 +808,7 @@ public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfSecondRowHidd
}
[WinFormsFact]
- public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfLastRowHidden()
+ public void DataGridViewCellAccessibleObject_Row_ReturnExpected_IfLastRowHidden()
{
using DataGridView dataGridView = new();
dataGridView.Columns.Add("Column 1", "Column 1");
@@ -829,7 +828,7 @@ public void DataGridViewCellAccessicbleObject_Row_ReturnExpected_IfLastRowHidden
}
[WinFormsFact]
- public void DataGridViewCellAccessibleObject_Сolumn_ReturnExpected_IfOwningColumnNotExist()
+ public void DataGridViewCellAccessibleObject_Column_ReturnExpected_IfOwningColumnNotExist()
{
AccessibleObject accessibleObject = new DataGridViewCellAccessibleObject(new SubDataGridViewCell());
@@ -1457,20 +1456,16 @@ private DataGridView CreateDataGridView(int columnCount, bool createControl = tr
}
// Unit test for https://github.com/dotnet/winforms/issues/7154
- [WinFormsFact]
- public void DataGridView_SwitchConfigured_AdjustsCellRowStartIndices()
+ [WinFormsTheory]
+ [InlineData([false, 1])]
+ [InlineData([true, 0])]
+ public void DataGridView_SwitchConfigured_AdjustsCellRowStartIndices(bool switchValue, int expectedIndex)
{
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZeroSwitchName, true);
-
- using DataGridView dataGridView = new();
+ using DataGridViewUIAStartRowCountAtZeroScope scope = new(switchValue);
+ using DataGridView dataGridView = new DataGridView();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());
-
- Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 0)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);
-
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZeroSwitchName, false);
-
- Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, 1)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);
+ Assert.Equal($"{string.Format(SR.DataGridView_AccRowName, expectedIndex)}, Not sorted.", dataGridView.Rows[0].Cells[0].AccessibilityObject.Name);
}
private class SubDataGridViewCell : DataGridViewCell
diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewRowAccessibleObjectTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewRowAccessibleObjectTests.cs
index 96d55e3559e..4756fdf8619 100644
--- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewRowAccessibleObjectTests.cs
+++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/AccessibleObjects/DataGridViewRowAccessibleObjectTests.cs
@@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Drawing;
-using System.Windows.Forms.Primitives;
using Windows.Win32.UI.Accessibility;
namespace System.Windows.Forms.Tests.AccessibleObjects;
@@ -2387,20 +2386,17 @@ public void DataGridViewRowAccessibleObject_GetPropertyValue_ValueValuePropertyI
}
// Unit test for https://github.com/dotnet/winforms/issues/7154
- [WinFormsFact]
- public void DataGridView_SwitchConfigured_AdjustsRowStartIndices()
+ [WinFormsTheory]
+ [InlineData([false, 1])]
+ [InlineData([true, 0])]
+ public void DataGridView_SwitchConfigured_AdjustsRowStartIndices(bool switchValue, int expectedIndex)
{
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZeroSwitchName, true);
-
- using DataGridView dataGridView = new();
+ using DataGridViewUIAStartRowCountAtZeroScope scope = new(switchValue);
+ using DataGridView dataGridView = new DataGridView();
dataGridView.Columns.Add(new DataGridViewTextBoxColumn());
dataGridView.Rows.Add(new DataGridViewRow());
- Assert.Equal(string.Format(SR.DataGridView_AccRowName, 0), dataGridView.Rows[0].AccessibilityObject.Name);
-
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.DataGridViewUIAStartRowCountAtZeroSwitchName, false);
-
- Assert.Equal(string.Format(SR.DataGridView_AccRowName, 1), dataGridView.Rows[0].AccessibilityObject.Name);
+ Assert.Equal(string.Format(SR.DataGridView_AccRowName, expectedIndex), dataGridView.Rows[0].AccessibilityObject.Name);
}
private class SubDataGridViewCell : DataGridViewCell
diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs
index 553d2ceb0c5..29707aed9d9 100644
--- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs
+++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/PictureBoxTests.cs
@@ -3,7 +3,6 @@
using System.ComponentModel;
using System.Drawing;
-using System.Windows.Forms.Primitives;
using System.Windows.Forms.TestUtilities;
using Moq;
using Point = System.Drawing.Point;
@@ -736,13 +735,10 @@ public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueLocal_GetReturnsE
[BoolData]
public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected(bool switchValue)
{
- dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
+ using ServicePointManagerCheckCrlScope scope = new(switchValue);
try
{
- AppContext.SetSwitch(LocalAppContextSwitches.ServicePointManagerCheckCrlSwitchName, switchValue);
- Assert.Equal(switchValue, LocalAppContextSwitches.ServicePointManagerCheckCrl);
-
using PictureBox pictureBox = new()
{
WaitOnLoad = true
@@ -763,10 +759,6 @@ public void PictureBox_ImageLocation_SetValidWithWaitOnLoadTrueUri_ConfigSwitch_
{
// Swallow network errors.
}
- finally
- {
- testAccessor.s_servicePointManagerCheckCrl = 0;
- }
}
[WinFormsTheory]
@@ -2006,13 +1998,10 @@ public void PictureBox_Load_UrlValidWithWaitOnLoadTrueLocal_GetReturnsExpected()
[BoolData]
public void PictureBox_Load_UrlValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_GetReturnsExpected(bool switchValue)
{
- dynamic testAccessor = typeof(LocalAppContextSwitches).TestAccessor().Dynamic;
+ using ServicePointManagerCheckCrlScope scope = new(switchValue);
try
{
- AppContext.SetSwitch(LocalAppContextSwitches.ServicePointManagerCheckCrlSwitchName, switchValue);
- Assert.Equal(switchValue, LocalAppContextSwitches.ServicePointManagerCheckCrl);
-
using PictureBox pictureBox = new()
{
WaitOnLoad = true
@@ -2033,10 +2022,6 @@ public void PictureBox_Load_UrlValidWithWaitOnLoadTrueUri_ConfigSwitch_CheckCRL_
{
// Swallow network errors.
}
- finally
- {
- testAccessor.s_servicePointManagerCheckCrl = 0;
- }
}
[WinFormsTheory]
diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripTests.cs
index 18e382b374d..add5b8f8ee8 100644
--- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripTests.cs
+++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ToolStripTests.cs
@@ -6,10 +6,9 @@
using System.Drawing.Imaging;
using System.Windows.Forms.TestUtilities;
using Moq;
+using Windows.Win32.System.Ole;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;
-using Windows.Win32.System.Ole;
-using System.Windows.Forms.Primitives;
namespace System.Windows.Forms.Tests;
@@ -1622,34 +1621,41 @@ public void ToolStrip_Font_SetWithItemsWithFontWithHandler_CallsFontChanged()
[WinFormsFact]
public void ToolStrip_Font_ApplyParentFontToMenus_GetReturnFont_SameAsForm()
{
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.ApplyParentFontToMenusSwitchName, true);
-
+ using ApplyParentFontToMenusScope scope = new(enable: true);
using Font font = new("Microsoft Sans Serif", 8.25f);
using Form form = new();
using ToolStrip toolStrip1 = new();
using SubToolStripItem item1 = new();
using SubToolStripItem item2 = new();
- try
- {
- toolStrip1.Items.Add(item1);
- toolStrip1.Items.Add(item2);
- form.Controls.Add(toolStrip1);
- form.Font = font;
+ toolStrip1.Items.Add(item1);
+ toolStrip1.Items.Add(item2);
+ form.Controls.Add(toolStrip1);
+ form.Font = font;
- Assert.True(LocalAppContextSwitches.ApplyParentFontToMenus);
- Assert.Same(form.Font, toolStrip1.Font);
- Assert.Same(form.Font, item1.Font);
- Assert.Same(form.Font, item2.Font);
- }
- finally
- {
- LocalAppContextSwitches.SetLocalAppContextSwitchValue(LocalAppContextSwitches.ApplyParentFontToMenusSwitchName, false);
- }
+ Assert.Same(form.Font, toolStrip1.Font);
+ Assert.Same(form.Font, item1.Font);
+ Assert.Same(form.Font, item2.Font);
+ }
+
+ [WinFormsFact]
+ public void ToolStrip_Font_ApplyParentFontToMenus_GetReturnFont_SameAsToolStripManagerDefaultFont()
+ {
+ using ApplyParentFontToMenusScope scope = new(enable: false);
+ using Font font = new("Microsoft Sans Serif", 8.25f);
+ using Form form = new();
+ using ToolStrip toolStrip1 = new();
+ using SubToolStripItem item1 = new();
+ using SubToolStripItem item2 = new();
+
+ toolStrip1.Items.Add(item1);
+ toolStrip1.Items.Add(item2);
+ form.Controls.Add(toolStrip1);
+ form.Font = font;
Assert.Equal(ToolStripManager.DefaultFont, toolStrip1.Font);
- Assert.Equal(ToolStripManager.DefaultFont, item2.Font);
Assert.Equal(ToolStripManager.DefaultFont, item1.Font);
+ Assert.Equal(ToolStripManager.DefaultFont, item2.Font);
}
public static IEnumerable