From 3809f3d92827333e91b32d0686826806a38f47e8 Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Mon, 16 Dec 2024 10:11:44 +0000 Subject: [PATCH 1/4] Add unit tests for LinkUtilities --- .../Windows/Forms/LinkUtilitiesTests.cs | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs new file mode 100644 index 00000000000..c133965be93 --- /dev/null +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs @@ -0,0 +1,69 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +using System.Drawing; + +namespace System.Windows.Forms.Tests; + +public class LinkUtilitiesTests +{ + [WinFormsTheory] + [InlineData("no", LinkBehavior.NeverUnderline)] + [InlineData("hover", LinkBehavior.HoverUnderline)] + [InlineData("always", LinkBehavior.AlwaysUnderline)] + [InlineData(null, LinkBehavior.AlwaysUnderline)] // Default behavior when key is not set + public void LinkUtilities_GetIELinkBehavior_ReturnsExpected(string? registryValue, LinkBehavior expectedBehavior) + { + // Set registry value if provided + using var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(LinkUtilities.IEMainRegPath); + if (registryValue is not null) + { + key.SetValue("Anchor Underline", registryValue); + } + else + { + key.DeleteValue("Anchor Underline", false); // Ensure no value is set + } + + LinkUtilities.GetIELinkBehavior().Should().Be(expectedBehavior); + } + + [WinFormsTheory] + [InlineData(LinkBehavior.AlwaysUnderline, FontStyle.Underline, FontStyle.Underline)] + [InlineData(LinkBehavior.HoverUnderline, FontStyle.Regular, FontStyle.Underline)] + [InlineData(LinkBehavior.NeverUnderline, FontStyle.Regular, FontStyle.Regular)] + [InlineData(LinkBehavior.SystemDefault, FontStyle.Regular, FontStyle.Regular)] // Adjust if different default styles are expected + public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts(LinkBehavior behavior, FontStyle linkFontStyle, FontStyle hoverLinkFontStyle) + { + using Font baseFont = new("Arial", 12); + Font? linkFont = null; + Font? hoverLinkFont = null; + + LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont); + + linkFont.Should().NotBeNull(); + hoverLinkFont.Should().NotBeNull(); + linkFont!.Style.Should().Be(linkFontStyle); + hoverLinkFont!.Style.Should().Be(hoverLinkFontStyle); + } + + [WinFormsTheory] + [InlineData(LinkBehavior.AlwaysUnderline, FontStyle.Underline | FontStyle.Bold, FontStyle.Underline | FontStyle.Bold)] + [InlineData(LinkBehavior.HoverUnderline, FontStyle.Regular, FontStyle.Underline)] + [InlineData(LinkBehavior.NeverUnderline, FontStyle.Bold, FontStyle.Bold)] + public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts_WithActive(LinkBehavior behavior, FontStyle linkFontStyle, FontStyle hoverLinkFontStyle) + { + using Font baseFont = new("Arial", 12); + Font? linkFont = null; + Font? hoverLinkFont = null; + + LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont, isActive: true); + + linkFont.Should().NotBeNull(); + hoverLinkFont.Should().NotBeNull(); + linkFont!.Style.Should().Be(linkFontStyle); + hoverLinkFont!.Style.Should().Be(hoverLinkFontStyle); + } +} From 90286443d5a593c4ca19e422915a8f98d598f2d3 Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Wed, 18 Dec 2024 01:30:42 +0000 Subject: [PATCH 2/4] Update --- .../tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs index c133965be93..64415212a1e 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs @@ -4,6 +4,7 @@ #nullable enable using System.Drawing; +using Microsoft.Win32; namespace System.Windows.Forms.Tests; @@ -17,7 +18,7 @@ public class LinkUtilitiesTests public void LinkUtilities_GetIELinkBehavior_ReturnsExpected(string? registryValue, LinkBehavior expectedBehavior) { // Set registry value if provided - using var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(LinkUtilities.IEMainRegPath); + RegistryKey? key = Registry.CurrentUser.CreateSubKey(LinkUtilities.IEMainRegPath); if (registryValue is not null) { key.SetValue("Anchor Underline", registryValue); From 0396ff9c7beff3a014cb270d79dcc2f1c5aaf5d0 Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Thu, 19 Dec 2024 07:38:36 +0000 Subject: [PATCH 3/4] handle feedback --- .../Windows/Forms/LinkUtilitiesTests.cs | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs index 64415212a1e..d73947eb5ce 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs @@ -32,39 +32,37 @@ public void LinkUtilities_GetIELinkBehavior_ReturnsExpected(string? registryValu } [WinFormsTheory] - [InlineData(LinkBehavior.AlwaysUnderline, FontStyle.Underline, FontStyle.Underline)] - [InlineData(LinkBehavior.HoverUnderline, FontStyle.Regular, FontStyle.Underline)] - [InlineData(LinkBehavior.NeverUnderline, FontStyle.Regular, FontStyle.Regular)] - [InlineData(LinkBehavior.SystemDefault, FontStyle.Regular, FontStyle.Regular)] // Adjust if different default styles are expected - public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts(LinkBehavior behavior, FontStyle linkFontStyle, FontStyle hoverLinkFontStyle) + [InlineData(LinkBehavior.AlwaysUnderline)] + [InlineData(LinkBehavior.HoverUnderline)] + [InlineData(LinkBehavior.NeverUnderline)] + [InlineData(LinkBehavior.SystemDefault)] + public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts(LinkBehavior behavior) { using Font baseFont = new("Arial", 12); Font? linkFont = null; Font? hoverLinkFont = null; - LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont); + Action act = () => LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont); - linkFont.Should().NotBeNull(); - hoverLinkFont.Should().NotBeNull(); - linkFont!.Style.Should().Be(linkFontStyle); - hoverLinkFont!.Style.Should().Be(hoverLinkFontStyle); + act.Should().NotThrow(); + linkFont.Should().BeOfType(); + hoverLinkFont.Should().BeOfType(); } [WinFormsTheory] - [InlineData(LinkBehavior.AlwaysUnderline, FontStyle.Underline | FontStyle.Bold, FontStyle.Underline | FontStyle.Bold)] - [InlineData(LinkBehavior.HoverUnderline, FontStyle.Regular, FontStyle.Underline)] - [InlineData(LinkBehavior.NeverUnderline, FontStyle.Bold, FontStyle.Bold)] - public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts_WithActive(LinkBehavior behavior, FontStyle linkFontStyle, FontStyle hoverLinkFontStyle) + [InlineData(LinkBehavior.AlwaysUnderline)] + [InlineData(LinkBehavior.HoverUnderline)] + [InlineData(LinkBehavior.NeverUnderline)] + public void LinkUtilities_EnsureLinkFonts_CreatesExpectedFonts_WithActive(LinkBehavior behavior) { using Font baseFont = new("Arial", 12); Font? linkFont = null; Font? hoverLinkFont = null; - LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont, isActive: true); + Action act = () => LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont, isActive: true); - linkFont.Should().NotBeNull(); - hoverLinkFont.Should().NotBeNull(); - linkFont!.Style.Should().Be(linkFontStyle); - hoverLinkFont!.Style.Should().Be(hoverLinkFontStyle); + act.Should().NotThrow(); + linkFont.Should().BeOfType(); + hoverLinkFont.Should().BeOfType(); } } From c8ffa30ca7c69e22bb97607de533d10061133e39 Mon Sep 17 00:00:00 2001 From: "Olina Zhang (Beyondsoft Corporation)" Date: Mon, 30 Dec 2024 02:11:06 +0000 Subject: [PATCH 4/4] Update scenario with read registry values --- .../Windows/Forms/LinkUtilitiesTests.cs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs index d73947eb5ce..228b168df0b 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/LinkUtilitiesTests.cs @@ -10,23 +10,21 @@ namespace System.Windows.Forms.Tests; public class LinkUtilitiesTests { - [WinFormsTheory] - [InlineData("no", LinkBehavior.NeverUnderline)] - [InlineData("hover", LinkBehavior.HoverUnderline)] - [InlineData("always", LinkBehavior.AlwaysUnderline)] - [InlineData(null, LinkBehavior.AlwaysUnderline)] // Default behavior when key is not set - public void LinkUtilities_GetIELinkBehavior_ReturnsExpected(string? registryValue, LinkBehavior expectedBehavior) + [WinFormsFact] + public void LinkUtilities_GetIELinkBehavior_ReturnsExpected() { - // Set registry value if provided - RegistryKey? key = Registry.CurrentUser.CreateSubKey(LinkUtilities.IEMainRegPath); - if (registryValue is not null) - { - key.SetValue("Anchor Underline", registryValue); - } - else + // Read the registry value + RegistryKey? key = Registry.CurrentUser.OpenSubKey(LinkUtilities.IEMainRegPath); + string? registryValue = key?.GetValue("Anchor Underline") as string; + + // Determine the expected behavior based on the registry value + LinkBehavior expectedBehavior = registryValue switch { - key.DeleteValue("Anchor Underline", false); // Ensure no value is set - } + "no" => LinkBehavior.NeverUnderline, + "hover" => LinkBehavior.HoverUnderline, + "always" => LinkBehavior.AlwaysUnderline, + _ => LinkBehavior.AlwaysUnderline + }; LinkUtilities.GetIELinkBehavior().Should().Be(expectedBehavior); }