diff --git a/src/Controls/src/Core/Editor/Editor.Mapper.cs b/src/Controls/src/Core/Editor/Editor.Mapper.cs index e76abb40f447..cd4b9549d6bb 100644 --- a/src/Controls/src/Core/Editor/Editor.Mapper.cs +++ b/src/Controls/src/Core/Editor/Editor.Mapper.cs @@ -6,19 +6,24 @@ namespace Microsoft.Maui.Controls { public partial class Editor { + internal static Action DefaultTextMapping { get; private set; } +#if ANDROID + internal static Action Material3TextMapping { get; private set; } +#endif + internal static new void RemapForControls() { // Adjust the mappings to preserve Controls.Editor legacy behaviors #if WINDOWS EditorHandler.Mapper.ReplaceMapping(PlatformConfiguration.WindowsSpecific.InputView.DetectReadingOrderFromContentProperty.PropertyName, MapDetectReadingOrderFromContent); #endif - EditorHandler.Mapper.ReplaceMapping(nameof(Text), MapText); + DefaultTextMapping = RegisterTextMapping(EditorHandler.Mapper, MapText); EditorHandler.Mapper.ReplaceMapping(nameof(TextTransform), MapText); #if ANDROID if (RuntimeFeature.IsMaterial3Enabled) { - EditorHandler2.Mapper.ReplaceMapping(nameof(Text), MapText); + Material3TextMapping = RegisterTextMapping(EditorHandler2.Mapper, MapText); EditorHandler2.Mapper.ReplaceMapping(nameof(TextTransform), MapText); EditorHandler2.Mapper.AppendToMapping(nameof(VisualElement.IsFocused), InputView.MapIsFocused); EditorHandler2.CommandMapper.PrependToMapping(nameof(IEditor.Focus), InputView.MapFocus); diff --git a/src/Controls/src/Core/Entry/Entry.Mapper.cs b/src/Controls/src/Core/Entry/Entry.Mapper.cs index 1d3d9285c740..81bd66c477b2 100644 --- a/src/Controls/src/Core/Entry/Entry.Mapper.cs +++ b/src/Controls/src/Core/Entry/Entry.Mapper.cs @@ -6,6 +6,11 @@ namespace Microsoft.Maui.Controls { public partial class Entry { + internal static Action DefaultTextMapping { get; private set; } +#if ANDROID + internal static Action Material3TextMapping { get; private set; } +#endif + internal static new void RemapForControls() { // Adjust the mappings to preserve Controls.Entry legacy behaviors @@ -17,7 +22,7 @@ public partial class Entry EntryHandler.Mapper.ReplaceMapping(PlatformConfiguration.iOSSpecific.Entry.CursorColorProperty.PropertyName, MapCursorColor); EntryHandler.Mapper.ReplaceMapping(PlatformConfiguration.iOSSpecific.Entry.AdjustsFontSizeToFitWidthProperty.PropertyName, MapAdjustsFontSizeToFitWidth); #endif - EntryHandler.Mapper.ReplaceMapping(nameof(Text), MapText); + DefaultTextMapping = RegisterTextMapping(EntryHandler.Mapper, MapText); EntryHandler.Mapper.ReplaceMapping(nameof(TextTransform), MapText); // Material3 Entry Handler mappings @@ -25,7 +30,7 @@ public partial class Entry if (RuntimeFeature.IsMaterial3Enabled) { EntryHandler2.Mapper.ReplaceMapping(PlatformConfiguration.AndroidSpecific.Entry.ImeOptionsProperty.PropertyName, MapImeOptions); - EntryHandler2.Mapper.ReplaceMapping(nameof(Text), MapText); + Material3TextMapping = RegisterTextMapping(EntryHandler2.Mapper, MapText); EntryHandler2.Mapper.ReplaceMapping(nameof(TextTransform), MapText); EntryHandler2.Mapper.AppendToMapping(nameof(VisualElement.IsFocused), InputView.MapIsFocused); EntryHandler2.Mapper.AppendToMapping(nameof(VisualElement.IsVisible), InputView.MapIsVisible); diff --git a/src/Controls/src/Core/InputView/InputView.cs b/src/Controls/src/Core/InputView/InputView.cs index e311bcb44658..a89f33388b1e 100644 --- a/src/Controls/src/Core/InputView/InputView.cs +++ b/src/Controls/src/Core/InputView/InputView.cs @@ -4,6 +4,13 @@ using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Graphics; +using Microsoft.Maui.Handlers; +#if IOS || MACCATALYST || WINDOWS +using Microsoft.Maui.Platform; +#endif +#if IOS || MACCATALYST +using UIKit; +#endif namespace Microsoft.Maui.Controls { @@ -312,7 +319,109 @@ int ITextInput.CursorPosition string ITextInput.Text { get => Text; - set => SetValue(TextProperty, value, SetterSpecificity.FromHandler); + set + { +#if WINDOWS + if (this is SearchBar && Handler is SearchBarHandler) + { + // AutoSuggestBox ignores the mapper's programmatic echo: only the display is transformed. + SetValue(TextProperty, value, SetterSpecificity.FromHandler); + return; + } +#endif + // Normalize before notifying subscribers, without changing the casing of passwords. + var transformedText = value; + if (value is not null && (Handler is null || UsesDefaultTextMapping()) && !IsPasswordInput) + { + var textElement = GetHandlerTextElement(); + var transform = textElement?.TextTransform ?? TextTransform; + if (transform is TextTransform.Lowercase or TextTransform.Uppercase) + transformedText = textElement is not null + ? textElement.UpdateFormsText(value, transform) + : TextTransformUtilities.GetTransformedText(value, transform); + } + + if (value != transformedText && Text == transformedText) + { + // The native edit needs correcting even though the managed value hasn't changed. + Handler?.UpdateValue(nameof(ITextInput.Text)); + return; + } + + SetValue(TextProperty, transformedText, SetterSpecificity.FromHandler); + } + } + + private protected static Action RegisterTextMapping( + IPropertyMapper mapper, Action mapText) + where TView : InputView where THandler : IElementHandler + { + mapper.ReplaceMapping(nameof(Text), mapText); + return mapper.GetProperty(nameof(Text)); + } + + bool UsesDefaultTextMapping() + { + if (Handler is not ElementHandler handler) + return false; + + var mapping = handler._mapper.GetProperty(nameof(Text)); + if (mapping is null) + return false; + + // Compare the active mapping, not the handler type or mutable global mapper. + // Controls can remap Text, but a stock handler can also be used without that setup. + return (this, handler) switch + { +#if ANDROID + (Entry, EntryHandler2) => mapping == Entry.Material3TextMapping || mapping == EntryHandler2.BuiltInTextMapping, + (Editor, EditorHandler2) => mapping == Editor.Material3TextMapping || mapping == EditorHandler2.BuiltInTextMapping, + (SearchBar, SearchBarHandler2) => mapping == SearchBar.Material3TextMapping || mapping == SearchBarHandler2.BuiltInTextMapping, +#endif + (Entry, IEntryHandler) => mapping == Entry.DefaultTextMapping || mapping == EntryHandler.BuiltInTextMapping, + (Editor, IEditorHandler) => mapping == Editor.DefaultTextMapping || mapping == EditorHandler.BuiltInTextMapping, + (SearchBar, ISearchBarHandler) => mapping == SearchBar.DefaultTextMapping || mapping == SearchBarHandler.BuiltInTextMapping, + _ => false + }; + } + + ITextElement GetHandlerTextElement() + { + // Match the native mappers that dispatch through ITextElement. +#if ANDROID + if (this is Entry or Editor || (this is SearchBar && Handler is SearchBarHandler2)) + return this; +#elif IOS || MACCATALYST + if (this is Entry or Editor) + return this; +#endif + return null; + } + + bool IsPasswordInput + { + get + { +#if ANDROID + if (this is SearchBar && Handler is SearchBarHandler2 searchBarHandler) + return Platform.EditTextExtensions.IsPasswordInput(searchBarHandler.QueryEditor, this); + + if (this is Entry or Editor) + { + var editText = Handler is EntryHandler2 entryHandler + ? entryHandler.PlatformView?.EditText + : Handler?.PlatformView as global::Android.Widget.EditText; + return Platform.EditTextExtensions.IsPasswordInput(editText, this); + } +#elif IOS || MACCATALYST + if ((this is Entry or Editor) && Handler?.PlatformView is IUITextInput textInput && textInput.GetSecureTextEntry()) + return true; +#elif WINDOWS + if (this is Entry && Handler?.PlatformView is MauiPasswordTextBox { IsPassword: true }) + return true; +#endif + return this is Entry entry && entry.IsPassword; + } } private protected override void OnBindablePropertySet(BindableProperty property, object original, object value, bool changed, bool willFirePropertyChanged) diff --git a/src/Controls/src/Core/Platform/Android/Extensions/EditTextExtensions.cs b/src/Controls/src/Core/Platform/Android/Extensions/EditTextExtensions.cs index 0a950ac78b5d..9f185b861b5d 100644 --- a/src/Controls/src/Core/Platform/Android/Extensions/EditTextExtensions.cs +++ b/src/Controls/src/Core/Platform/Android/Extensions/EditTextExtensions.cs @@ -23,21 +23,27 @@ internal static (string oldText, string newText) GetTexts(EditText editText, str { var oldText = editText.Text ?? string.Empty; - var inputType = editText.InputType; + var newText = textElement.UpdateFormsText(text, + IsPasswordInput(editText, textElement) ? TextTransform.None : textElement.TextTransform); + return (oldText, newText); + } + + internal static bool IsPasswordInput(EditText editText, ITextElement textElement) + { // A read-only EditText has no InputType flags (TYPE_NULL), so check what sets the password ones too: // IsPassword on an Entry, the keyboard on other inputs - bool isPasswordEnabled = - (textElement is Entry entry ? entry.IsPassword : textElement is InputView inputView && inputView.Keyboard == Keyboard.Password) || - (inputType.HasFlag(InputTypes.ClassText) && + if (textElement is Entry entry ? entry.IsPassword : textElement is InputView inputView && inputView.Keyboard == Keyboard.Password) + return true; + + if (editText is null) + return false; + + var inputType = editText.InputType; + return (inputType.HasFlag(InputTypes.ClassText) && inputType.HasFlag(InputTypes.TextVariationPassword)) || (inputType.HasFlag(InputTypes.ClassNumber) && inputType.HasFlag(InputTypes.NumberVariationPassword)); - - var newText = textElement.UpdateFormsText(text, - isPasswordEnabled ? TextTransform.None : textElement.TextTransform); - - return (oldText, newText); } public static void UpdateText(this EditText editText, InputView inputView) => diff --git a/src/Controls/src/Core/SearchBar/SearchBar.Mapper.cs b/src/Controls/src/Core/SearchBar/SearchBar.Mapper.cs index d05efd265ec8..6ff0ec9718de 100644 --- a/src/Controls/src/Core/SearchBar/SearchBar.Mapper.cs +++ b/src/Controls/src/Core/SearchBar/SearchBar.Mapper.cs @@ -6,6 +6,11 @@ namespace Microsoft.Maui.Controls { public partial class SearchBar { + internal static Action DefaultTextMapping { get; private set; } +#if ANDROID + internal static Action Material3TextMapping { get; private set; } +#endif + static SearchBar() { // Register dependency: SearchCommand depends on SearchCommandParameter for CanExecute evaluation @@ -26,16 +31,16 @@ static SearchBar() if (RuntimeFeature.IsMaterial3Enabled) { // Material3 SearchBar handler mappings - SearchBarHandler2.Mapper.ReplaceMapping(nameof(Text), MapText); + Material3TextMapping = RegisterTextMapping(SearchBarHandler2.Mapper, MapText); SearchBarHandler2.Mapper.ReplaceMapping(nameof(TextTransform), MapText); } else { - SearchBarHandler.Mapper.ReplaceMapping(nameof(Text), MapText); + DefaultTextMapping = RegisterTextMapping(SearchBarHandler.Mapper, MapText); SearchBarHandler.Mapper.ReplaceMapping(nameof(TextTransform), MapText); } #else - SearchBarHandler.Mapper.ReplaceMapping(nameof(Text), MapText); + DefaultTextMapping = RegisterTextMapping(SearchBarHandler.Mapper, MapText); SearchBarHandler.Mapper.ReplaceMapping(nameof(TextTransform), MapText); #endif diff --git a/src/Controls/tests/Core.UnitTests/InputViewTests.cs b/src/Controls/tests/Core.UnitTests/InputViewTests.cs index 1b2c3a45c147..176f663ebd5a 100644 --- a/src/Controls/tests/Core.UnitTests/InputViewTests.cs +++ b/src/Controls/tests/Core.UnitTests/InputViewTests.cs @@ -3,12 +3,117 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Maui.Handlers; using Xunit; namespace Microsoft.Maui.Controls.Core.UnitTests { public class InputViewTests : BaseTestFixture { + [Theory] + [InlineData(TextTransform.Uppercase, "d", "D")] + [InlineData(TextTransform.Lowercase, "D", "d")] + [InlineData(TextTransform.None, "d", "d")] + public void HandlerTextChangesRaiseOneEventWithTransformedText(TextTransform transform, string input, string expected) + { + foreach (var view in new InputView[] { new Entry(), new Editor(), new SearchBar() }) + { + view.TextTransform = transform; + view.Text = "123"; + var changes = new List(); + view.TextChanged += (_, args) => changes.Add(args); + + var textInput = (ITextInput)view; + textInput.Text = input; + textInput.Text = expected; // Simulate the native echo after mapping. + + Assert.Equal(expected, view.Text); + var change = Assert.Single(changes); + Assert.Equal("123", change.OldTextValue); + Assert.Equal(expected, change.NewTextValue); + } + } + + [Theory] + [InlineData(TextTransform.Uppercase)] + [InlineData(TextTransform.Lowercase)] + public void HandlerTextChangesPreservePasswordCasing(TextTransform transform) + { + var entry = new Entry { IsPassword = true, TextTransform = transform }; + var boundEntry = new Entry(); + entry.SetBinding(InputView.TextProperty, new Binding(nameof(InputView.Text), source: boundEntry)); + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args); + + ((ITextInput)entry).Text = "aB"; + + Assert.Equal("aB", entry.Text); + Assert.Equal("aB", boundEntry.Text); + Assert.Equal("aB", Assert.Single(changes).NewTextValue); + } + + [Theory] + [InlineData(TextTransform.Uppercase, "aB")] + [InlineData(TextTransform.Lowercase, "Ab")] + public void ReplacementTextMappingPreservesRawInput(TextTransform transform, string input) + { + var entry = new Entry { TextTransform = transform }; + var source = new Entry { Text = "initial" }; + entry.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source)); + var handler = new ReplacementTextEntryHandler(); + entry.Handler = handler; + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + handler.PlatformText = input; + ((ITextInput)entry).UpdateText(input); + + Assert.Equal(input, handler.PlatformText); + Assert.Equal(input, entry.Text); + Assert.Equal(input, source.Text); + Assert.Equal(input, Assert.Single(changes)); + } + + [Theory] + [InlineData(null)] + [InlineData("x")] + public void DirectTextInputNullAssignmentPreservesNull(string initialText) + { + var source = new Entry { Text = initialText }; + var editor = new Editor(); + editor.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source)); + var changes = new List(); + editor.TextChanged += (_, args) => changes.Add(args); + + ((ITextInput)editor).Text = null; + + Assert.Null(editor.Text); + Assert.Null(source.Text); + if (initialText is null) + Assert.Empty(changes); + else + { + var change = Assert.Single(changes); + Assert.Equal(initialText, change.OldTextValue); + Assert.Null(change.NewTextValue); + } + } + + class ReplacementTextEntryHandler : EntryHandler + { + static readonly PropertyMapper ReplacementMapper = new(EntryHandler.Mapper) + { + [nameof(IEntry.Text)] = (handler, view) => handler.PlatformText = view.Text, + }; + + public ReplacementTextEntryHandler() : base(ReplacementMapper) + { + } + + public string PlatformText { get; set; } + + protected override object CreatePlatformView() => new object(); + } [Theory] [InlineData(typeof(Entry))] diff --git a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Android.cs index 28f5550e0910..0ae628800d62 100644 --- a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Android.cs @@ -1,5 +1,7 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; using System.Threading.Tasks; +using Android.Text; using AndroidX.AppCompat.Widget; using Microsoft.Maui.Controls; using Microsoft.Maui.Handlers; @@ -98,6 +100,69 @@ public async Task ReadOnlyEditorIgnoresTextTransformOnlyWithPasswordKeyboard(boo Assert.Equal(expectedText, editor.Text); } + [Fact] + public async Task NativeTextEditRaisesOneTransformedChange() + { + var editor = new Editor { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(editor, handler => + { + var platformEditor = GetPlatformControl(handler); + var changes = new List(); + editor.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = platformEditor.EditableText; + text.Replace(0, text.Length(), "d"); + + Assert.Equal("D", platformEditor.Text); + Assert.Equal("D", editor.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + + [Fact] + public async Task PasswordKeyboardPlatformTextPreservesCasing() + { + var editor = new Editor { Keyboard = Keyboard.Password, TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(editor, handler => + { + var changes = 0; + editor.TextChanged += (_, _) => changes++; + + SetPlatformText(handler, "aB"); + + Assert.Equal("aB", editor.Text); + Assert.Equal("aB", GetPlatformControl(handler).Text); + Assert.Equal(1, changes); + }); + } + + [Fact] + public async Task NativePasswordInputTypePreservesCasing() + { + const string input = "aB"; + var editor = new Editor { TextTransform = TextTransform.Uppercase }; + var bindingSource = new Entry(); + editor.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: bindingSource)); + + await AttachAndRun(editor, handler => + { + var platformEditor = GetPlatformControl(handler); + platformEditor.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword; + var changes = new List(); + editor.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = platformEditor.EditableText; + text.Replace(0, text.Length(), input); + + Assert.Equal(input, platformEditor.Text); + Assert.Equal(input, editor.Text); + Assert.Equal(input, bindingSource.Text); + Assert.Equal(input, Assert.Single(changes)); + }); + } + [Theory] [InlineData(false)] [InlineData(true)] diff --git a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Windows.cs index 94e2dff8bde7..ecd182d17293 100644 --- a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.Windows.cs @@ -1,10 +1,12 @@ #nullable enable using System; +using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using Microsoft.Maui.Controls; using Microsoft.Maui.Dispatching; using Microsoft.Maui.Handlers; +using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml.Controls; using Xunit; using WFlowDirection = Microsoft.UI.Xaml.FlowDirection; @@ -34,6 +36,35 @@ Task GetPlatformOpacity(EditorHandler editorHandler) static void SetPlatformText(EditorHandler editorHandler, string text) => GetPlatformControl(editorHandler).Text = text; + [Fact] + public async Task NativeTextEditRaisesOneTransformedChange() + { + var editor = new Editor { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(editor, async handler => + { + var textBox = GetPlatformControl(handler); + var changes = new List(); + var changed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + editor.TextChanged += (_, args) => + { + changes.Add(args.NewTextValue); + changed.TrySetResult(); + }; + + SetPlatformText(handler, "d"); + await changed.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + var idle = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Assert.True(textBox.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => idle.TrySetResult())); + await idle.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + Assert.Equal("D", textBox.Text); + Assert.Equal("D", editor.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + static int GetPlatformCursorPosition(EditorHandler editorHandler) => GetPlatformControl(editorHandler).SelectionStart; diff --git a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.iOS.cs b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.iOS.cs index 8f2bdaa586cb..5e8743de521b 100644 --- a/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.iOS.cs +++ b/src/Controls/tests/DeviceTests/Elements/Editor/EditorTests.iOS.cs @@ -1,4 +1,6 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.Controls; @@ -26,6 +28,33 @@ static Task GetPlatformText(EditorHandler handler) static void SetPlatformText(EditorHandler editorHandler, string text) => GetPlatformControl(editorHandler).Text = text; + [Fact] + public async Task NativeTextEditUsesCustomTextTransform() + { + const string expected = "\u0130"; + var editor = new TurkishEditor { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(editor, handler => + { + var changes = new List(); + editor.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + SetPlatformText(handler, "i"); + + Assert.Equal(expected, GetPlatformControl(handler).Text); + Assert.Equal(expected, editor.Text); + Assert.Equal(expected, Assert.Single(changes)); + }); + } + + sealed class TurkishEditor : Editor, ITextElement + { + string ITextElement.UpdateFormsText(string original, TextTransform transform) => + transform == TextTransform.Uppercase + ? (original ?? string.Empty).ToUpper(CultureInfo.GetCultureInfo("tr-TR")) + : UpdateFormsText(original, transform); + } + static int GetPlatformCursorPosition(EditorHandler editorHandler) { var nativeEditor = GetPlatformControl(editorHandler); diff --git a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Android.cs index 9709b0b1ebb7..f627b0c9cda4 100644 --- a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Android.cs @@ -1,5 +1,7 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; using System.Threading.Tasks; +using Android.Text; using AndroidX.AppCompat.Widget; using Google.Android.Material.TextField; using Microsoft.Maui.Controls; @@ -97,6 +99,71 @@ public async Task ReadOnlyEntryIgnoresTextTransformOnlyWhenItIsAPassword(bool is Assert.Equal(expectedText, entry.Text); } + [Theory] + [InlineData(TextTransform.Uppercase, "A", "a")] + [InlineData(TextTransform.Lowercase, "a", "A")] + public async Task CaseOnlyPlatformEditRestoresTransformedTextWithoutNotification(TextTransform transform, string expected, string input) + { + var entry = new Entry { TextTransform = transform, Text = expected }; + + await AttachAndRun(entry, handler => + { + var changes = 0; + entry.TextChanged += (_, _) => changes++; + var platformEntry = GetPlatformControl(handler); + + platformEntry.EditableText.Replace(0, platformEntry.EditableText.Length(), input); + + Assert.Equal(expected, platformEntry.Text); + Assert.Equal(expected, entry.Text); + Assert.Equal(0, changes); + }); + } + + [Fact] + public async Task Material3NonSecureNativeTextEditRaisesOneTransformedChange() + { + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, handler => + { + var platformEntry = handler.PlatformView.EditText; + Assert.NotNull(platformEntry); + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = platformEntry.EditableText; + text.Replace(0, text.Length(), "d"); + + Assert.Equal("D", platformEntry.Text); + Assert.Equal("D", entry.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + + [Fact] + public async Task Material3NativePasswordInputTypePreservesCasing() + { + const string input = "aB"; + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, handler => + { + var platformEntry = handler.PlatformView.EditText; + Assert.NotNull(platformEntry); + platformEntry.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword; + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = platformEntry.EditableText; + text.Replace(0, text.Length(), input); + + Assert.Equal(input, platformEntry.Text); + Assert.Equal(input, entry.Text); + Assert.Equal(input, Assert.Single(changes)); + }); + } + [Fact] public async Task ClearButtonFollowsIsReadOnly() { diff --git a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Windows.cs index 3a5874c2d493..b840dd2ff47f 100644 --- a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.Windows.cs @@ -1,10 +1,13 @@ #nullable enable +using System; +using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using Microsoft.Maui.Controls; using Microsoft.Maui.Dispatching; using Microsoft.Maui.Handlers; using Microsoft.Maui.Platform; +using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml.Controls; using Xunit; using WControl = Microsoft.UI.Xaml.Controls.Control; @@ -37,6 +40,64 @@ Task GetPlatformOpacity(EntryHandler entryHandler) static void SetPlatformText(EntryHandler entryHandler, string text) => GetPlatformControl(entryHandler).Text = text; + [Fact] + public async Task NonSecureNativeTextEditRaisesOneTransformedChange() + { + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, async handler => + { + var textBox = Assert.IsType(GetPlatformControl(handler)); + Assert.False(textBox.IsPassword); + var changes = new List(); + var changed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + entry.TextChanged += (_, args) => + { + changes.Add(args.NewTextValue); + changed.TrySetResult(); + }; + + SetPlatformText(handler, "d"); + await changed.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + // Drain the native echo before checking the number of managed notifications. + var idle = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Assert.True(textBox.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => idle.TrySetResult())); + await idle.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + Assert.Equal("D", textBox.Text); + Assert.Equal("D", entry.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + + [Fact] + public async Task NativePasswordTextBoxPreservesCasing() + { + const string input = "aB"; + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, async handler => + { + var passwordBox = Assert.IsType(GetPlatformControl(handler)); + passwordBox.IsPassword = true; + var changes = new List(); + var changed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + entry.TextChanged += (_, args) => + { + changes.Add(args.NewTextValue); + changed.TrySetResult(); + }; + + passwordBox.Password = input; + await changed.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + Assert.Equal(input, passwordBox.Password); + Assert.Equal(input, entry.Text); + Assert.Equal(input, Assert.Single(changes)); + }); + } + static int GetPlatformCursorPosition(EntryHandler entryHandler) => GetPlatformControl(entryHandler).SelectionStart; diff --git a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.iOS.cs b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.iOS.cs index 9730f2dda466..5ed6b4c6eb92 100644 --- a/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.iOS.cs +++ b/src/Controls/tests/DeviceTests/Elements/Entry/EntryTests.iOS.cs @@ -1,4 +1,6 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using CoreGraphics; @@ -28,6 +30,105 @@ static Task GetPlatformText(EntryHandler handler) static void SetPlatformText(EntryHandler entryHandler, string text) => GetPlatformControl(entryHandler).Text = text; + [Fact] + public async Task NativeTextEditUsesCustomTextTransform() + { + const string expected = "\u0130"; + var entry = new TurkishEntry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, handler => + { + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + SetPlatformText(handler, "i"); + + Assert.Equal(expected, GetPlatformControl(handler).Text); + Assert.Equal(expected, entry.Text); + Assert.Equal(expected, Assert.Single(changes)); + }); + } + + sealed class TurkishEntry : Entry, ITextElement + { + string ITextElement.UpdateFormsText(string original, TextTransform transform) => + transform == TextTransform.Uppercase + ? (original ?? string.Empty).ToUpper(CultureInfo.GetCultureInfo("tr-TR")) + : UpdateFormsText(original, transform); + } + + [Theory] + [InlineData(TextTransform.Uppercase, TextTransform.None, "aB")] + [InlineData(TextTransform.None, TextTransform.Uppercase, "AB")] + public async Task NativeEditUsesInterfaceTextTransform(TextTransform concreteTransform, TextTransform interfaceTransform, string expected) + { + var entry = new InterfaceTransformEntry { TextTransform = concreteTransform }; + ((ITextElement)entry).TextTransform = interfaceTransform; + var source = new Entry(); + entry.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source)); + + await AttachAndRun(entry, handler => + { + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + SetPlatformText(handler, "aB"); + + Assert.Equal(expected, GetPlatformControl(handler).Text); + Assert.Equal(expected, entry.Text); + Assert.Equal(expected, source.Text); + Assert.Equal(expected, Assert.Single(changes)); + }); + } + + sealed class InterfaceTransformEntry : Entry, ITextElement + { + TextTransform ITextElement.TextTransform { get; set; } + } + + [Fact] + public async Task NonSecureNativeTextEditRaisesOneTransformedChange() + { + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(entry, handler => + { + var textField = GetPlatformControl(handler); + Assert.False(textField.SecureTextEntry); + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + SetPlatformText(handler, "d"); + + Assert.Equal("D", textField.Text); + Assert.Equal("D", entry.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + + [Fact] + public async Task NativeSecureTextEntryPreservesCasing() + { + var entry = new Entry { TextTransform = TextTransform.Uppercase }; + var bindingSource = new Entry(); + entry.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: bindingSource)); + + await AttachAndRun(entry, handler => + { + var textField = GetPlatformControl(handler); + textField.SecureTextEntry = true; + var changes = new List(); + entry.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + SetPlatformText(handler, "aB"); + + Assert.Equal("aB", textField.Text); + Assert.Equal("aB", entry.Text); + Assert.Equal("aB", bindingSource.Text); + Assert.Equal("aB", Assert.Single(changes)); + }); + } + static int GetPlatformCursorPosition(EntryHandler entryHandler) { var textField = GetPlatformControl(entryHandler); diff --git a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Android.cs index 81d54b2f6845..eab940f4e88f 100644 --- a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Android.cs @@ -1,4 +1,6 @@ -using System.ComponentModel; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using Android.Widget; @@ -43,6 +45,79 @@ Task GetPlatformOpacity(SearchBarHandler searchBarHandler) }); } + [Theory] + [InlineData(false, true, false, "aB", "AB")] + [InlineData(true, true, false, "aB", "aB")] + [InlineData(true, false, false, "d", "D")] + [InlineData(false, false, true, "i", "I")] + [InlineData(true, false, true, "i", "\u0130")] + public async Task PlatformEditRespectsSearchBarHandlerAndKeyboard(bool material3, bool passwordKeyboard, bool customTransform, string input, string expectedText) + { + SearchBar searchBar = customTransform ? new TurkishSearchBar() : new SearchBar(); + searchBar.Keyboard = passwordKeyboard ? Keyboard.Password : Keyboard.Default; + searchBar.TextTransform = TextTransform.Uppercase; + + if (material3) + await AttachAndRun(searchBar, handler => AssertPlatformEdit(handler.QueryEditor)); + else + await AttachAndRun(searchBar, handler => AssertPlatformEdit(handler.QueryEditor)); + + void AssertPlatformEdit(EditText queryEditor) + { + Assert.NotNull(queryEditor); + var changes = new List(); + searchBar.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = queryEditor.EditableText; + text.Replace(0, text.Length(), input); + + Assert.Equal(expectedText, queryEditor.Text); + Assert.Equal(expectedText, searchBar.Text); + Assert.Equal(expectedText, Assert.Single(changes)); + } + } + + sealed class TurkishSearchBar : SearchBar, ITextElement + { + string ITextElement.UpdateFormsText(string original, TextTransform transform) => + transform == TextTransform.Uppercase + ? (original ?? string.Empty).ToUpper(CultureInfo.GetCultureInfo("tr-TR")) + : UpdateFormsText(original, transform); + } + + [Theory] + [InlineData(false, "AB")] + [InlineData(true, "aB")] + public async Task NativeEditUsesInterfaceTextTransformOnlyForMaterial3(bool material3, string expected) + { + var searchBar = new InterfaceTransformSearchBar { TextTransform = TextTransform.Uppercase }; + ((ITextElement)searchBar).TextTransform = TextTransform.None; + + if (material3) + await AttachAndRun(searchBar, handler => AssertNativeEdit(handler.QueryEditor)); + else + await AttachAndRun(searchBar, handler => AssertNativeEdit(handler.QueryEditor)); + + void AssertNativeEdit(EditText queryEditor) + { + Assert.NotNull(queryEditor); + var changes = new List(); + searchBar.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + var text = queryEditor.EditableText; + text.Replace(0, text.Length(), "aB"); + + Assert.Equal(expected, queryEditor.Text); + Assert.Equal(expected, searchBar.Text); + Assert.Equal(expected, Assert.Single(changes)); + } + } + + sealed class InterfaceTransformSearchBar : SearchBar, ITextElement + { + TextTransform ITextElement.TextTransform { get; set; } + } + [Fact] [Description("The ScaleX property of a SearchBar should match with native ScaleX")] public async Task ScaleXConsistent() diff --git a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Windows.cs index 28f5b5a0c7b5..b9a6590d8fa2 100644 --- a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.Windows.cs @@ -1,9 +1,12 @@ #nullable enable +using System; +using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using Microsoft.Maui.Controls; using Microsoft.Maui.Handlers; using Microsoft.Maui.Platform; +using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml.Controls; using Xunit; @@ -19,6 +22,36 @@ static Task GetPlatformText(SearchBarHandler handler) return InvokeOnMainThreadAsync(() => GetPlatformControl(handler).Text); } + [Theory] + [InlineData(TextTransform.Uppercase, "AB")] + [InlineData(TextTransform.Lowercase, "ab")] + public async Task HandlerEditPreservesQueryCasingWhileMappingDisplay(TextTransform transform, string displayText) + { + const string query = "aB"; + var source = new Entry(); + var searchBar = new SearchBar { TextTransform = transform }; + searchBar.SetBinding(InputView.TextProperty, new Binding(nameof(Entry.Text), source: source)); + + await AttachAndRun(searchBar, async handler => + { + var platformSearchBar = GetPlatformControl(handler); + var changes = new List(); + searchBar.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + // Pass the query through the same virtual-view setter as the WinUI user-input callback. + handler.VirtualView.Text = query; + + var idle = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Assert.True(platformSearchBar.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () => idle.TrySetResult())); + await idle.Task.WaitAsync(TimeSpan.FromSeconds(5)); + + Assert.Equal(query, searchBar.Text); + Assert.Equal(query, source.Text); + Assert.Equal(query, Assert.Single(changes)); + Assert.Equal(displayText, platformSearchBar.Text); + }); + } + static int GetPlatformSelectionLength(SearchBarHandler searchBarHandler) { var platformSearchBar = GetPlatformControl(searchBarHandler); diff --git a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.iOS.cs b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.iOS.cs index d569c60067dd..90de3ec4b282 100644 --- a/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.iOS.cs +++ b/src/Controls/tests/DeviceTests/Elements/SearchBar/SearchBarTests.iOS.cs @@ -1,7 +1,10 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Maui.Controls; using Microsoft.Maui.Handlers; using Microsoft.Maui.Platform; using UIKit; +using Xunit; namespace Microsoft.Maui.DeviceTests { @@ -15,6 +18,27 @@ static Task GetPlatformText(SearchBarHandler handler) return InvokeOnMainThreadAsync(() => GetPlatformControl(handler).Text); } + [Fact] + public async Task NativeTextEditRaisesOneTransformedChange() + { + var searchBar = new SearchBar { TextTransform = TextTransform.Uppercase }; + + await AttachAndRun(searchBar, handler => + { + var queryEditor = handler.QueryEditor; + Assert.NotNull(queryEditor); + var changes = new List(); + searchBar.TextChanged += (_, args) => changes.Add(args.NewTextValue); + + queryEditor.Text = "d"; + queryEditor.SendActionForControlEvents(UIControlEvent.EditingChanged); + + Assert.Equal("D", queryEditor.Text); + Assert.Equal("D", searchBar.Text); + Assert.Equal("D", Assert.Single(changes)); + }); + } + static Task GetPlatformCancelButtonColor(SearchBarHandler handler) => InvokeOnMainThreadAsync(() => { if (handler.PlatformView.TraitCollection.UserInterfaceIdiom == UIUserInterfaceIdiom.Mac) diff --git a/src/Core/src/Handlers/Editor/EditorHandler.cs b/src/Core/src/Handlers/Editor/EditorHandler.cs index 02ec47b32db4..505e755f24a8 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler.cs @@ -36,6 +36,9 @@ public partial class EditorHandler : IEditorHandler #endif }; + internal static readonly System.Action? BuiltInTextMapping = + Mapper.GetProperty(nameof(IEditor.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) { #if ANDROID diff --git a/src/Core/src/Handlers/Editor/EditorHandler2.Android.cs b/src/Core/src/Handlers/Editor/EditorHandler2.Android.cs index b355f21a9738..b5f72e3d4ade 100644 --- a/src/Core/src/Handlers/Editor/EditorHandler2.Android.cs +++ b/src/Core/src/Handlers/Editor/EditorHandler2.Android.cs @@ -32,6 +32,9 @@ public class EditorHandler2 : ViewHandler [nameof(IEditor.SelectionLength)] = MapSelectionLength, }; + internal static readonly Action? BuiltInTextMapping = + Mapper.GetProperty(nameof(IEditor.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) { diff --git a/src/Core/src/Handlers/Entry/EntryHandler.cs b/src/Core/src/Handlers/Entry/EntryHandler.cs index fa0db0cedc62..20cb3c21a692 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.cs @@ -42,6 +42,9 @@ public partial class EntryHandler : IEntryHandler [nameof(IEntry.SelectionLength)] = MapSelectionLength }; + internal static readonly System.Action? BuiltInTextMapping = + Mapper.GetProperty(nameof(IEntry.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) { #if ANDROID diff --git a/src/Core/src/Handlers/Entry/EntryHandler2.Android.cs b/src/Core/src/Handlers/Entry/EntryHandler2.Android.cs index f9bb3a2ec84b..0cd7914c3e5d 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler2.Android.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler2.Android.cs @@ -39,6 +39,9 @@ public class EntryHandler2 : ViewHandler [nameof(IView.FlowDirection)] = MapFlowDirection, }; + internal static readonly Action? BuiltInTextMapping = + Mapper.GetProperty(nameof(IEntry.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) { diff --git a/src/Core/src/Handlers/SearchBar/SearchBarHandler.cs b/src/Core/src/Handlers/SearchBar/SearchBarHandler.cs index 59c1e35cad3e..529865eff99b 100644 --- a/src/Core/src/Handlers/SearchBar/SearchBarHandler.cs +++ b/src/Core/src/Handlers/SearchBar/SearchBarHandler.cs @@ -42,6 +42,9 @@ public partial class SearchBarHandler : ISearchBarHandler #endif }; + internal static readonly System.Action? BuiltInTextMapping = + Mapper.GetProperty(nameof(ISearchBar.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) { #if ANDROID diff --git a/src/Core/src/Handlers/SearchBar/SearchBarHandler2.Android.cs b/src/Core/src/Handlers/SearchBar/SearchBarHandler2.Android.cs index 493ed01fd262..b02282a02d2e 100644 --- a/src/Core/src/Handlers/SearchBar/SearchBarHandler2.Android.cs +++ b/src/Core/src/Handlers/SearchBar/SearchBarHandler2.Android.cs @@ -37,6 +37,9 @@ public class SearchBarHandler2 : ViewHandler? BuiltInTextMapping = + Mapper.GetProperty(nameof(ISearchBar.Text)); + public static CommandMapper CommandMapper = new(ViewCommandMapper) {