Fix formatted text text type crash (#13532)

* Add test file for label causing app crash

* Add checks to prevent app crash

* Add relevant unit test

* - add label to stack layout

* - just call layout directly

* Remove UITest

redundant since we already have unit test

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
This commit is contained in:
Rachel Kang 2021-01-30 14:36:18 -05:00 коммит произвёл GitHub
Родитель 80e05926fc
Коммит 475a5e7703
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 78 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,46 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif
namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ManualReview)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.None, 0, "Label with FormattedText, HTML, and Padding shouldn't cause crash", PlatformAffected.iOS)]
public class LabelFormattedTextHtmlPadding : TestContentPage
{
protected override void Init()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "This test passes if app doesn't crash. Label is not expected to actually work" });
Content = new StackLayout()
{
Margin = 20,
Children =
{
new Label()
{
AutomationId = "LabelFormattedTextHtmlPaddingTest",
Text = "If you can see this text, this test has passed"
},
new Label()
{
AutomationId = "LabelFormattedTextHtmlPadding",
FormattedText = formattedString,
TextType = TextType.Html,
Padding = 5
}
}
};
}
}
}

Просмотреть файл

@ -1710,6 +1710,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBackground.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentOffest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentWithZeroMargin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LabelFormattedTextHtmlPadding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13436.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13390.cs" />
</ItemGroup>

Просмотреть файл

@ -82,5 +82,30 @@ namespace Xamarin.Forms.Platform.iOS.UnitTests
Assert.That(actualFont.PointSize, Is.EqualTo(expectedFontSize));
}
[Test, Category("Label"), Category("FormattedText")]
[Description("If Label has FormattedText, HTML, and Padding, app should not crash")]
public async Task LabelWithFormattedTextHTMLAndPaddingDoesNotCrashApp()
{
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "Label with FormattedText, HTML, and Padding" });
var label = new Label
{
FormattedText = formattedString,
TextType = TextType.Html,
Padding = 5
};
var expected = TextType.Html;
var actual = await GetRendererProperty(label, renderer =>
{
var uiLabel = (UILabel)(renderer as LabelRenderer).Control;
uiLabel.Frame = new CoreGraphics.CGRect(0, 0, 200, 200);
uiLabel.RecalculateSpanPositions(label);
return label.TextType;
}, true);
Assert.That(actual, Is.EqualTo(expected));
}
}
}

Просмотреть файл

@ -21,6 +21,12 @@ namespace Xamarin.Forms.Platform.MacOS
{
public static void RecalculateSpanPositions(this NativeLabel control, Label element)
{
if (element == null)
return;
if (element.TextType == TextType.Html)
return;
if (element?.FormattedText?.Spans == null
|| element.FormattedText.Spans.Count == 0)
return;