From efa9cf4a02cb22749bce258b68829f5a1098f5e0 Mon Sep 17 00:00:00 2001 From: Andrew Hoefling Date: Thu, 27 Jun 2019 20:23:32 -0400 Subject: [PATCH] Adds FontImage Markup Extension for FontImageSource (#6398) * Added FontImage markup extension which simplifies usage of FontImageSource into any ImageSource * Added unit tests to cover TabBar which is used in Shell * Cleaned up xaml for test code * Removed class references from unit test project which were added by mistake * Removed FontImageExtension from markup parser * Added ContentProperty to markup extension * Added Size to FontImageExtension * Updated unit tests for new properties * added unit tests to certify each property was set correctly by the markup extension * Added content property test to certify that Glyph is being set correctly * Flipped order in unit tests of expected and actual * Updated default value for Size to use the FontImageSource default's value. This will keep it in sync if that value ever changes fixes #6376 --- .../FontImageExtension.xaml | 12 ++++ .../FontImageExtension.xaml.cs | 65 ++++++++++++++++++++++ .../MarkupExtensions/FontImageExtension.cs | 30 ++++++++++ 3 files changed, 107 insertions(+) create mode 100644 Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml.cs create mode 100644 Xamarin.Forms.Xaml/MarkupExtensions/FontImageExtension.cs diff --git a/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml b/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml new file mode 100644 index 0000000..b1ffeef --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml.cs new file mode 100644 index 0000000..1d61d65 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/FontImageExtension.xaml.cs @@ -0,0 +1,65 @@ +using NUnit.Framework; + +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class FontImageExtension : TabBar + { + public FontImageExtension() => InitializeComponent(); + public FontImageExtension(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + public static string FontFamily => "MyFontFamily"; + public static string Glyph => "MyGlyph"; + public static Color Color => Color.Black; + public static double Size = 50d; + + [TestFixture] + class Tests + { + [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); + [TearDown] public void TearDown() => Device.PlatformServices = null; + + [TestCase(true), TestCase(false)] + public void FontImageExtension_Positive(bool useCompiledXaml) + { + var layout = new FontImageExtension(useCompiledXaml); + var tabs = layout.AllChildren; + + foreach (var tab in tabs) + { + Tab myTab = (Tab)tab; + if (myTab == null) + continue; + + Assert.That(myTab.Icon, Is.TypeOf()); + + var fontImage = (FontImageSource)myTab.Icon; + Assert.AreEqual(FontFamily, fontImage.FontFamily); + Assert.AreEqual(Glyph, fontImage.Glyph); + Assert.AreEqual(Size, fontImage.Size); + Assert.AreEqual(Color, fontImage.Color); + } + } + + [TestCase(true), TestCase(false)] + public void FontImageExtension_Negative(bool useCompiledXaml) + { + var layout = new FontImageExtension(useCompiledXaml); + var tabs = layout.AllChildren; + + foreach (var tab in tabs) + { + Tab myTab = (Tab)tab; + if (myTab == null) + continue; + + Assert.That(myTab.Icon, Is.Not.TypeOf()); + } + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml/MarkupExtensions/FontImageExtension.cs b/Xamarin.Forms.Xaml/MarkupExtensions/FontImageExtension.cs new file mode 100644 index 0000000..41e4c13 --- /dev/null +++ b/Xamarin.Forms.Xaml/MarkupExtensions/FontImageExtension.cs @@ -0,0 +1,30 @@ +using System; + +namespace Xamarin.Forms.Xaml +{ + [AcceptEmptyServiceProvider] + [ContentProperty(nameof(Glyph))] + public class FontImageExtension : IMarkupExtension + { + public string FontFamily { get; set; } + public string Glyph { get; set; } + public Color Color { get; set; } + public double Size { get; set; } = (double)FontImageSource.SizeProperty.DefaultValue; + + public ImageSource ProvideValue(IServiceProvider serviceProvider) + { + return new FontImageSource + { + FontFamily = FontFamily, + Glyph = Glyph, + Color = Color, + Size = Size + }; + } + + object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) + { + return (this as IMarkupExtension).ProvideValue(serviceProvider); + } + } +} -- 2.7.4