Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Android): Fix NRE when TextBlock.FontFamily is null #7132

Merged
merged 1 commit into from
Sep 20, 2021
Merged
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
@@ -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 };
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved
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)
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved
{
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)
jeromelaban marked this conversation as resolved.
Show resolved Hide resolved
=> _entries.Add(key, typeFace);

internal void Clear()
Expand Down