From 1fa6bd25eb16e4ace4990bd9638cbd843cb57a5a Mon Sep 17 00:00:00 2001 From: luthfiampas Date: Sun, 7 Mar 2021 14:27:42 +0700 Subject: [PATCH] better TextBox.MaxLength tests --- .../TextBoxTests.cs | 62 +++++++++++++++---- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs index 6ac7799828..9f09c19110 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBoxTests.cs @@ -646,22 +646,49 @@ namespace Avalonia.Controls.UnitTests Assert.Null(target.Text); } } - - [Fact] - public void Text_Box_MaxLength_Work_Properly() + + [Theory] + [InlineData("abc", "d", 3, 0, 0, false, "abc")] + [InlineData("abc", "dd", 4, 3, 3, false, "abcd")] + [InlineData("abc", "ddd", 3, 0, 2, true, "ddc")] + [InlineData("abc", "dddd", 4, 1, 3, true, "addd")] + [InlineData("abc", "ddddd", 5, 3, 3, true, "abcdd")] + public void MaxLength_Works_Properly( + string initalText, + string textInput, + int maxLength, + int selectionStart, + int selectionEnd, + bool fromClipboard, + string expected) { using (UnitTestApplication.Start(Services)) { var target = new TextBox { Template = CreateTemplate(), - Text = "abc", - MaxLength = 3, + Text = initalText, + MaxLength = maxLength, + SelectionStart = selectionStart, + SelectionEnd = selectionEnd }; - - RaiseKeyEvent(target, Key.D, KeyModifiers.None); - - Assert.Equal("abc", target.Text); + + if (fromClipboard) + { + AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); + + var clipboard = AvaloniaLocator.CurrentMutable.GetService(); + clipboard.SetTextAsync(textInput).GetAwaiter().GetResult(); + + RaiseKeyEvent(target, Key.V, KeyModifiers.Control); + clipboard.ClearAsync().GetAwaiter().GetResult(); + } + else + { + RaiseTextEvent(target, textInput); + } + + Assert.Equal(expected, target.Text); } } @@ -758,11 +785,22 @@ namespace Avalonia.Controls.UnitTests private class ClipboardStub : IClipboard // in order to get tests working that use the clipboard { - public Task GetTextAsync() => Task.FromResult(""); + private string _text; + + public Task GetTextAsync() => Task.FromResult(_text); - public Task SetTextAsync(string text) => Task.CompletedTask; + public Task SetTextAsync(string text) + { + _text = text; + return Task.CompletedTask; + } - public Task ClearAsync() => Task.CompletedTask; + public Task ClearAsync() + { + _text = null; + return Task.CompletedTask; + } + public Task SetDataObjectAsync(IDataObject data) => Task.CompletedTask; public Task GetFormatsAsync() => Task.FromResult(Array.Empty());