Fixe issue 5367 | Crash assigning excessively long text to Editor with MaxLength...
authorFelipe Baltazar <felipe.dasilvabaltazar@gmail.com>
Mon, 4 Nov 2019 20:04:22 +0000 (17:04 -0300)
committerRui Marinho <me@ruimarinho.net>
Mon, 4 Nov 2019 20:04:21 +0000 (20:04 +0000)
* Fixe issue 5367 🐛

* Code review

* code review

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5367.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.Android/Renderers/EditorRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5367.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5367.cs
new file mode 100644 (file)
index 0000000..3b4b3e3
--- /dev/null
@@ -0,0 +1,61 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using NUnit.Framework;
+using Xamarin.UITest;
+using Xamarin.Forms.Core.UITests;
+using Xamarin.UITest.Queries;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 5367, "[Bug] Editor with MaxLength", PlatformAffected.Android)]
+#if UITEST
+       [Category(UITestCategories.Editor)]
+#endif
+       public class Issue5367 : TestContentPage
+       {
+               const string MaxLengthEditor = "MaxLength Editor";
+               const string ForceBigStringButton = "Force Big String Button";
+
+               protected override void Init()
+               {
+                       var maxLength = 14;
+                       var editor = new Editor()
+                       {
+                               AutomationId = MaxLengthEditor,
+                               Text = $"MaxLength = {maxLength}",
+                               MaxLength = maxLength,
+                       };
+
+                       Content = new ScrollView()
+                       {
+                               Content = new StackLayout()
+                               {
+                                       Children =
+                                       {
+                                               editor,
+                                               new Button()
+                                               {
+                                                       AutomationId = ForceBigStringButton,
+                                                       Text = "Force editor text greater than maxlength",
+                                                       Command = new Command(()=> editor.Text += $"This should not appear on editor")
+                                               }
+                                       }
+                               }
+                       };
+               }
+
+#if UITEST
+               [Test]
+               [UiTest(typeof(Editor))]
+               public void Issue5367TestMaxLengthCrashesApp()
+               {
+                       RunningApp.WaitForElement(MaxLengthEditor);
+                       RunningApp.Tap(ForceBigStringButton);
+               }
+#endif
+       }
+}
index f877b7e..386180e 100644 (file)
@@ -36,6 +36,7 @@
       <DependentUpon>Issue7048.xaml</DependentUpon>
       <SubType>Code</SubType>
     </Compile>
+       <Compile Include="$(MSBuildThisFileDirectory)Issue5367.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7253.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7581.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7621.xaml.cs">
index 8a294be..6969616 100644 (file)
@@ -254,6 +254,7 @@ namespace Xamarin.Forms.Platform.Android
                        if (EditText.Text == newText)
                                return;
 
+                       newText = TrimToMaxLength(newText);
                        EditText.Text = newText;
                        EditText.SetSelection(newText.Length);
                }
@@ -291,12 +292,19 @@ namespace Xamarin.Forms.Platform.Android
 
                        currentFilters.Add(new InputFilterLengthFilter(Element.MaxLength));
 
-                       EditText?.SetFilters(currentFilters.ToArray());
+                       if (EditText == null)
+                               return;
+
+                       EditText.SetFilters(currentFilters.ToArray());
+                       EditText.Text = TrimToMaxLength(EditText.Text);
+               }
 
-                       var currentControlText = EditText?.Text;
+               string TrimToMaxLength(string currentText)
+               {
+                       if (currentText == null || currentText.Length <= Element.MaxLength)
+                               return currentText;
 
-                       if (currentControlText.Length > Element.MaxLength)
-                               EditText.Text = currentControlText.Substring(0, Element.MaxLength);
+                       return currentText.Substring(0, Element.MaxLength);
                }
 
                void UpdateIsReadOnly()