Skip to content

Commit

Permalink
Add unit tests for LinkUtilities (#12647)
Browse files Browse the repository at this point in the history
related #10453

Proposed changes
Add unit tests for LinkUtilities to test its GetIELinkBehavior and EnsureLinkFonts methods.
  • Loading branch information
Olina-Zhang authored Jan 7, 2025
1 parent 52ad3f5 commit e1a1553
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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;
using Microsoft.Win32;

namespace System.Windows.Forms.Tests;

public class LinkUtilitiesTests
{
[WinFormsFact]
public void LinkUtilities_GetIELinkBehavior_ReturnsExpected()
{
// 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
{
"no" => LinkBehavior.NeverUnderline,
"hover" => LinkBehavior.HoverUnderline,
"always" => LinkBehavior.AlwaysUnderline,
_ => LinkBehavior.AlwaysUnderline
};

LinkUtilities.GetIELinkBehavior().Should().Be(expectedBehavior);
}

[WinFormsTheory]
[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;

Action act = () => LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont);

act.Should().NotThrow();
linkFont.Should().BeOfType<Font>();
hoverLinkFont.Should().BeOfType<Font>();
}

[WinFormsTheory]
[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;

Action act = () => LinkUtilities.EnsureLinkFonts(baseFont, behavior, ref linkFont, ref hoverLinkFont, isActive: true);

act.Should().NotThrow();
linkFont.Should().BeOfType<Font>();
hoverLinkFont.Should().BeOfType<Font>();
}
}

0 comments on commit e1a1553

Please sign in to comment.