-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing usages of
LocalAppContextSwitches
with `AppContextSwitchS…
…cope` (#11539) * Replacing usages of LocalAppContextSwitches with AppContextSwitchScopes * Fix usings * Refactoring to use named parameter and make AppContextSwitchNames use constants * move files to better support TargetFrameworkName in AppContextSwitchScope * More refactoring for LocalAppContextSwitchesTest * Changes from review * Remove try finally blocks * Refactor tests based on feedback * Fix test expected values * change from review
- Loading branch information
Showing
20 changed files
with
394 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Scope for enabling / disabling the AnchorLayoutV2 Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Common/tests/TestUtilities/ApplyParentFontToMenusScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Scope for enabling / disabling the ApplyParentFontToMenus Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Common/tests/TestUtilities/DataGridViewUIAStartRowCountAtZeroScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
/// <summary> | ||
/// Scope for enabling / disabling the DataGridViewUIAStartRowCountAtZero Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Common/tests/TestUtilities/NoClientNotificationsScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Scope for enabling / disabling the NoClientNotifications Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Common/tests/TestUtilities/ScaleTopLevelFormMinMaxSizeForDpiScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Scope for enabling / disabling the ScaleTopLevelFormMinMaxSizeForDpi Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Common/tests/TestUtilities/ServicePointManagerCheckCrlScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
/// <summary> | ||
/// Scope for enabling / disabling the ServicePointManagerCheckCrl Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Common/tests/TestUtilities/TreeNodeCollectionAddRangeRespectsSortOrderScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
/// <summary> | ||
/// Scope for enabling / disabling the TreeNodeCollectionAddRangeRespectsSortOrder Switch. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/System.Windows.Forms.Primitives/tests/TestUtilities/TargetFrameworkNameScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
/// <summary> | ||
/// Scope for setting the see <see cref="LocalAppContextSwitches.TargetFrameworkName" /> temporarily. | ||
/// Use in a <see langword="using"/> statement. | ||
/// </summary> | ||
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; | ||
} | ||
} |
Oops, something went wrong.