diff --git a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.cs b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.cs index 7129823c42fe..c007700f01ea 100644 --- a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.cs @@ -238,6 +238,65 @@ await InvokeOnMainThreadAsync(async () => }); } + [Fact(DisplayName = "Android crash when Entry has more than 5000 characters")] + [Category(TestCategory.Entry)] + public async Task EntryWithLongTextDoesNotCrash() + { + string longText = new string('A', 5001); + var entry = new Entry + { + Text = longText, + }; + + var handler = await CreateHandlerAsync(entry); + var platformEntry = GetPlatformControl(handler); + Assert.NotNull(platformEntry); + Assert.Equal(longText, await GetPlatformText(handler)); + + } + + [Fact(DisplayName = "Entry with text longer text and short text updates correctly")] + [Category(TestCategory.Entry)] + public async Task EntryWithLongTextAndShortText_UpdatesTextCorrectly() + { + string longText = new string('A', 5001); + var entry = new Entry + { + Text = longText + }; + + var handler = await CreateHandlerAsync(entry); +#if !ANDROID + await InvokeOnMainThreadAsync(() => + { + SetPlatformText(handler, "short"); + }); +#else + SetPlatformText(handler, "short"); +#endif + Assert.Equal("short", await GetPlatformText(handler)); + } + + [Fact( +#if WINDOWS + Skip = "Fails on Windows" +#endif + )] + [Description("Entry MaxLength and Text property order is respected")] + [Category(TestCategory.Entry)] + public async Task EntryMaxLengthAndTextOrder_RespectsMaxLength() + { + string longText = new string('C', 50); + var entry = new Entry + { + MaxLength = 10, + Text = longText + }; + + var handler = await CreateHandlerAsync(entry); + Assert.Equal(longText.Substring(0, 10), await GetPlatformText(handler)); + } + [Category(TestCategory.Entry)] [Collection(RunInNewWindowCollection)] public class EntryTextInputTests : TextInputTests diff --git a/src/Core/src/Handlers/Entry/EntryHandler.cs b/src/Core/src/Handlers/Entry/EntryHandler.cs index 3d4d666adffc..aa9347240b6e 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.cs @@ -17,7 +17,12 @@ namespace Microsoft.Maui.Handlers { public partial class EntryHandler : IEntryHandler { - public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) + private static readonly IPropertyMapper EntryPriorityMapper = new PropertyMapper() + { + [nameof(IEntry.MaxLength)] = MapMaxLength, + }; + + public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper, EntryPriorityMapper) { [nameof(IEntry.Background)] = MapBackground, [nameof(IEntry.CharacterSpacing)] = MapCharacterSpacing, @@ -30,7 +35,6 @@ public partial class EntryHandler : IEntryHandler [nameof(IEntry.IsTextPredictionEnabled)] = MapIsTextPredictionEnabled, [nameof(IEntry.IsSpellCheckEnabled)] = MapIsSpellCheckEnabled, [nameof(IEntry.Keyboard)] = MapKeyboard, - [nameof(IEntry.MaxLength)] = MapMaxLength, [nameof(IEntry.Placeholder)] = MapPlaceholder, [nameof(IEntry.PlaceholderColor)] = MapPlaceholderColor, [nameof(IEntry.ReturnType)] = MapReturnType,