Skip to content

Commit

Permalink
Merge pull request #7132 from unoplatform/dev/jela/textblock-nre-android
Browse files Browse the repository at this point in the history
fix(Android): Fix NRE when TextBlock.FontFamily is null
  • Loading branch information
mergify[bot] authored Sep 20, 2021
2 parents 1b8b0a2 + 5a428c9 commit 205cd2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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));
}
}
}
14 changes: 8 additions & 6 deletions src/Uno.UI/UI/Xaml/Extensions/FontHelper.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Android.App;
#nullable enable

using Android.App;
using Android.Graphics;
using System;
using System.Collections.Generic;
Expand All @@ -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))
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 10 additions & 8 deletions src/Uno.UI/UI/Xaml/Extensions/FontHelper.Lookup.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Android.App;
#nullable enable

using Android.App;
using Android.Graphics;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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)
{
Expand All @@ -55,19 +57,19 @@ 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;
}

result = null;
return false;
}

internal void Add(Entry key, Typeface typeFace)
internal void Add(Entry key, Typeface? typeFace)
=> _entries.Add(key, typeFace);

internal void Clear()
Expand Down

0 comments on commit 205cd2a

Please sign in to comment.