Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;

namespace Microsoft.Toolkit.Uwp.UI.Extensions
{
/// <summary>
/// Custom <see cref="MarkupExtension"/> which can provide <see cref="FontIconSource"/> values.
/// </summary>
[Bindable]
[MarkupExtensionReturnType(ReturnType = typeof(FontIconSource))]
public class FontIconSourceExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="string"/> representing the icon to display.
/// </summary>
public string Glyph { get; set; }

/// <summary>
/// Gets or sets the size of the icon to display.
/// </summary>
public double FontSize { get; set; }

/// <summary>
/// Gets or sets the font family to use to display the icon. If <see langword="null"/>, "Segoe MDL2 Assets" will be used.
/// </summary>
public FontFamily FontFamily { get; set; }

/// <summary>
/// Gets or sets the thickness of the icon glyph.
/// </summary>
public FontWeight FontWeight { get; set; } = FontWeights.Normal;

/// <summary>
/// Gets or sets the font style for the icon glyph.
/// </summary>
public FontStyle FontStyle { get; set; } = FontStyle.Normal;

/// <summary>
/// Gets or sets a value indicating whether automatic text enlargement, to reflect the system text size setting, is enabled.
/// </summary>
public bool IsTextScaleFactorEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the icon is mirrored when the flow direction is right to left.
/// </summary>
public bool MirroredWhenRightToLeft { get; set; }

/// <inheritdoc/>
protected override object ProvideValue()
{
var fontIcon = new FontIconSource
{
Glyph = Glyph,
FontFamily = FontFamily ?? new FontFamily("Segoe MDL2 Assets"),
FontWeight = FontWeight,
FontStyle = FontStyle,
IsTextScaleFactorEnabled = IsTextScaleFactorEnabled,
MirroredWhenRightToLeft = MirroredWhenRightToLeft
};

if (FontSize > 0)
{
fontIcon.FontSize = FontSize;
}

return fontIcon;
}
}
}
113 changes: 113 additions & 0 deletions UnitTests/Extensions/Test_FontIconSourceExtensionMarkupExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using Windows.UI.Text;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_FontIconSourceExtensionMarkupExtension
{
[TestCategory("FontIconSourceExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconSourceExtension_MarkupExtension_ProvideSegoeMdl2Asset()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:controls=""using:UnitTests.Extensions"">
<controls:MockSwipeItem x:Name=""Check"" IconSource=""{ex:FontIconSource Glyph=&#xE105;}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as MockSwipeItem;

Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree.");

var icon = button.IconSource as FontIconSource;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE105", "Expected icon glyph to be E105.");
Assert.AreEqual(icon.FontFamily.Source, "Segoe MDL2 Assets", "Expected font family to be Segoe MDL2 Assets");
}

[TestCategory("FontIconSourceExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconSourceExtension_MarkupExtension_ProvideSegoeUI()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:controls=""using:UnitTests.Extensions"">
<controls:MockSwipeItem x:Name=""Check"" IconSource=""{ex:FontIconSource Glyph=&#xE14D;, FontFamily='Segoe UI'}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as MockSwipeItem;

Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree.");

var icon = button.IconSource as FontIconSource;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE14D", "Expected icon glyph to be E14D.");
Assert.AreEqual(icon.FontFamily.Source, "Segoe UI", "Expected font family to be Segoe UI");
}

[TestCategory("FontIconSourceExtensionMarkupExtension")]
[UITestMethod]
public void Test_FontIconSourceExtension_MarkupExtension_ProvideCustomFontIcon()
{
var treeroot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
xmlns:controls=""using:UnitTests.Extensions"">
<controls:MockSwipeItem x:Name=""Check"" Icon=""{ex:FontIconSource Glyph=&#xE14D;, FontSize=7, FontFamily='Segoe UI', FontWeight=Bold, FontStyle=Italic, IsTextScaleFactorEnabled=True, MirroredWhenRightToLeft=True}""/>
</Page>") as FrameworkElement;

var button = treeroot.FindChildByName("Check") as MockSwipeItem;

Assert.IsNotNull(button, $"Could not find the {nameof(MockSwipeItem)} control in tree.");

var icon = button.IconSource as FontIconSource;

Assert.IsNotNull(icon, $"Could not find the {nameof(FontIcon)} element in button.");

Assert.AreEqual(icon.Glyph, "\uE14D", "Expected icon glyph to be E14D.");
Assert.AreEqual(icon.FontSize, 7.0, "Expected font size of 7");
Assert.AreEqual(icon.FontFamily.Source, "Segoe MDL2 Assets", "Expected font family to be Segoe UI");
Assert.AreEqual(icon.FontWeight, FontWeights.Bold, "Expected bold font weight");
Assert.AreEqual(icon.FontStyle, FontStyle.Italic, "Expected italic font style");
Assert.AreEqual(icon.IsTextScaleFactorEnabled, true, "Expected IsTextScaleFactorEnabled set to true");
Assert.AreEqual(icon.MirroredWhenRightToLeft, true, "Expected MirroredWhenRightToLeft set to true");
}
}

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402", Justification = "Mock control for tests")]
public class MockSwipeItem : Control
{
public IconSource IconSource
{
get => (IconSource)GetValue(IconSourceProperty);
set => SetValue(IconSourceProperty, value);
}

public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register(
nameof(IconSource),
typeof(IconSource),
typeof(MockSwipeItem),
new PropertyMetadata(default(IconSource)));
}
}
1 change: 1 addition & 0 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
<Compile Include="Diagnostics\Test_Guard.Array.cs" />
<Compile Include="Diagnostics\Test_Guard.cs" />
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
<Compile Include="Extensions\Test_FontIconSourceExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_TypeExtensions.cs" />
<Compile Include="Extensions\Test_ValueTypeExtensions.cs" />
<Compile Include="Extensions\Test_ArrayExtensions.cs" />
Expand Down