diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBlock.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBlock.cs index 593a35f544e0..99ebd8c30f6e 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBlock.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_TextBlock.cs @@ -1,9 +1,11 @@ using System.Linq; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Documents; +using static Private.Infrastructure.TestServices; namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls { @@ -54,5 +56,14 @@ public void Check_Text_When_Having_Inline_Text_In_Span() Assert.AreEqual("did", ((Run)((Italic)inlines[1]).Inlines.Single()).Text); Assert.AreEqual(" my text go?", ((Run)inlines[2]).Text); } + + [TestMethod] + [RunsOnUIThread] + public async Task When_Null_FontFamily() + { + var SUT = new TextBlock { Text = "Some text", FontFamily = null }; + WindowHelper.WindowContent = SUT; + SUT.Measure(new Size(1000, 1000)); + } } } diff --git a/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs b/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs index 94c335247373..e17bd308d2bc 100644 --- a/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs +++ b/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs @@ -1,4 +1,6 @@ -using Android.App; +#nullable enable + +using Android.App; using Android.Graphics; using System; using System.Collections.Generic; @@ -20,9 +22,9 @@ internal partial class FontHelper private static bool _assetsListed; private static readonly string DefaultFontFamilyName = "sans-serif"; - internal static Typeface FontFamilyToTypeFace(FontFamily fontFamily, FontWeight fontWeight, TypefaceStyle style = TypefaceStyle.Normal) + internal static Typeface? FontFamilyToTypeFace(FontFamily? fontFamily, FontWeight fontWeight, TypefaceStyle style = TypefaceStyle.Normal) { - var entry = new FontFamilyToTypeFaceDictionary.Entry(fontFamily.Source, fontWeight, style); + var entry = new FontFamilyToTypeFaceDictionary.Entry(fontFamily?.Source, fontWeight, style); if (!_fontFamilyToTypeFaceDictionary.TryGetValue(entry , out var typeFace)) { @@ -33,14 +35,14 @@ internal static Typeface FontFamilyToTypeFace(FontFamily fontFamily, FontWeight return typeFace; } - internal static Typeface InternalFontFamilyToTypeFace(FontFamily fontFamily, FontWeight fontWeight, TypefaceStyle style) + internal static Typeface? InternalFontFamilyToTypeFace(FontFamily? fontFamily, FontWeight fontWeight, TypefaceStyle style) { if (fontFamily?.Source == null || fontFamily.Equals(FontFamily.Default)) { fontFamily = GetDefaultFontFamily(fontWeight); } - Typeface typeface; + Typeface? typeface; try { @@ -72,7 +74,7 @@ internal static Typeface InternalFontFamilyToTypeFace(FontFamily fontFamily, Fon { typeface = Android.Graphics.Typeface.CreateFromAsset(Android.App.Application.Context.Assets, actualAsset); - if (style != typeface.Style) + if (style != typeface?.Style) { typeface = Typeface.Create(typeface, style); } diff --git a/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Lookup.Android.cs b/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Lookup.Android.cs index 2bd27425957a..8436c8ceebaf 100644 --- a/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Lookup.Android.cs +++ b/src/Uno.UI/UI/Xaml/Extensions/FontHelper.Lookup.Android.cs @@ -1,4 +1,6 @@ -using Android.App; +#nullable enable + +using Android.App; using Android.Graphics; using System; using System.Collections.Generic; @@ -26,19 +28,19 @@ public class Entry { private readonly int _hashCode; - public Entry(string fontFamily, FontWeight fontWeight, TypefaceStyle style) + public Entry(string? fontFamily, FontWeight fontWeight, TypefaceStyle style) { FontFamily = fontFamily; FontWeight = fontWeight; Style = style; - _hashCode = FontFamily.GetHashCode() ^ FontWeight.GetHashCode() ^ Style.GetHashCode(); + _hashCode = (FontFamily?.GetHashCode() ?? 0) ^ FontWeight.GetHashCode() ^ Style.GetHashCode(); } - public string FontFamily { get; } + public string? FontFamily { get; } public FontWeight FontWeight { get; } public TypefaceStyle Style { get; } - public override bool Equals(object other) + public override bool Equals(object? other) { if (other is Entry otherEntry) { @@ -55,11 +57,11 @@ public override bool Equals(object other) private readonly HashtableEx _entries = new HashtableEx(); - internal bool TryGetValue(Entry key, out Typeface result) + internal bool TryGetValue(Entry key, out Typeface? result) { if (_entries.TryGetValue(key, out var value)) { - result = (Typeface)value; + result = (Typeface?)value; return true; } @@ -67,7 +69,7 @@ internal bool TryGetValue(Entry key, out Typeface result) return false; } - internal void Add(Entry key, Typeface typeFace) + internal void Add(Entry key, Typeface? typeFace) => _entries.Add(key, typeFace); internal void Clear()