From a3627aed5e4038b7b105cb98401472f3a5ed4fae Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 25 Jan 2024 05:53:34 +0000 Subject: [PATCH] Fixes #3189. TextValidateField doesn't show the cursor and doesn't have TextChanged event. (#3190) * Prefix private fields with underscore and code cleanup. * Rename to SetInitialProperties and fix a typo. * Cleaning code and some adjustments. * Fix default width for view. * Enable cursor visibility. * Reduces indentation and remove AutoInitShutdown. * Add unit test for default Width is always equal to the provider DisplayText.Length. * Also prevents showing the cursor if position is less than 0. * Add OnTextChanged method and TextChanged event. * Remove unnecessary TextChanged. --- Terminal.Gui/Views/TextValidateField.cs | 243 ++++-- UICatalog/Scenarios/Text.cs | 46 +- UnitTests/Views/TextValidateFieldTests.cs | 955 +++++++++++----------- 3 files changed, 679 insertions(+), 565 deletions(-) diff --git a/Terminal.Gui/Views/TextValidateField.cs b/Terminal.Gui/Views/TextValidateField.cs index 4b967eda02..78eb92786b 100644 --- a/Terminal.Gui/Views/TextValidateField.cs +++ b/Terminal.Gui/Views/TextValidateField.cs @@ -6,12 +6,12 @@ // using System.Text; -using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text.RegularExpressions; using Terminal.Gui.TextValidateProviders; +using System; namespace Terminal.Gui { @@ -89,6 +89,22 @@ public interface ITextValidateProvider { /// Gets the formatted string for display. /// string DisplayText { get; } + + /// + /// Method that invoke the event if it's defined. + /// + /// The previous text before replaced. + /// Returns the + void OnTextChanged (TextChangedEventArgs oldValue); + + /// + /// Changed event, raised when the text has changed. + /// + /// This event is raised when the changes. + /// The passed is a containing the old value. + /// + /// + event EventHandler TextChanged; } ////////////////////////////////////////////////////////////////////////////// @@ -104,7 +120,10 @@ public interface ITextValidateProvider { /// Masking elements /// public class NetMaskedTextProvider : ITextValidateProvider { - MaskedTextProvider provider; + MaskedTextProvider _provider; + + /// + public event EventHandler TextChanged; /// /// Empty Constructor @@ -119,13 +138,13 @@ public NetMaskedTextProvider (string mask) /// public string Mask { get { - return provider?.Mask; + return _provider?.Mask; } set { - var current = provider != null ? provider.ToString (false, false) : string.Empty; - provider = new MaskedTextProvider (value == string.Empty ? "&&&&&&" : value); - if (string.IsNullOrEmpty (current) == false) { - provider.Set (current); + var current = _provider != null ? _provider.ToString (false, false) : string.Empty; + _provider = new MaskedTextProvider (value == string.Empty ? "&&&&&&" : value); + if (!string.IsNullOrEmpty (current)) { + _provider.Set (current); } } } @@ -133,32 +152,32 @@ public string Mask { /// public string Text { get { - return provider.ToString (); + return _provider.ToString (); } set { - provider.Set (value); + _provider.Set (value); } } /// - public bool IsValid => provider.MaskCompleted; + public bool IsValid => _provider.MaskCompleted; /// public bool Fixed => true; /// - public string DisplayText => provider.ToDisplayString (); + public string DisplayText => _provider.ToDisplayString (); /// public int Cursor (int pos) { if (pos < 0) { return CursorStart (); - } else if (pos > provider.Length) { + } else if (pos > _provider.Length) { return CursorEnd (); } else { - var p = provider.FindEditPositionFrom (pos, false); - if (p == -1) p = provider.FindEditPositionFrom (pos, true); + var p = _provider.FindEditPositionFrom (pos, false); + if (p == -1) p = _provider.FindEditPositionFrom (pos, true); return p; } } @@ -167,45 +186,58 @@ public int Cursor (int pos) public int CursorStart () { return - provider.IsEditPosition (0) + _provider.IsEditPosition (0) ? 0 - : provider.FindEditPositionFrom (0, true); + : _provider.FindEditPositionFrom (0, true); } /// public int CursorEnd () { return - provider.IsEditPosition (provider.Length - 1) - ? provider.Length - 1 - : provider.FindEditPositionFrom (provider.Length, false); + _provider.IsEditPosition (_provider.Length - 1) + ? _provider.Length - 1 + : _provider.FindEditPositionFrom (_provider.Length, false); } /// public int CursorLeft (int pos) { - var c = provider.FindEditPositionFrom (pos - 1, false); + var c = _provider.FindEditPositionFrom (pos - 1, false); return c == -1 ? pos : c; } /// public int CursorRight (int pos) { - var c = provider.FindEditPositionFrom (pos + 1, true); + var c = _provider.FindEditPositionFrom (pos + 1, true); return c == -1 ? pos : c; } /// public bool Delete (int pos) { - return provider.Replace (' ', pos);// .RemoveAt (pos); + var oldValue = Text; + var result = _provider.Replace (' ', pos);// .RemoveAt (pos); + if (result) { + OnTextChanged (new TextChangedEventArgs (oldValue)); + } + return result; } /// public bool InsertAt (char ch, int pos) { - return provider.Replace (ch, pos); + var oldValue = Text; + var result = _provider.Replace (ch, pos); + if (result) { + OnTextChanged (new TextChangedEventArgs (oldValue)); + } + return result; } + + /// + public void OnTextChanged (TextChangedEventArgs oldValue) => TextChanged?.Invoke (this, oldValue); } #endregion @@ -215,9 +247,12 @@ public bool InsertAt (char ch, int pos) /// Regex Provider for TextValidateField. /// public class TextRegexProvider : ITextValidateProvider { - Regex regex; - List text; - List pattern; + Regex _regex; + List _text; + List _pattern; + + /// + public event EventHandler TextChanged; /// /// Empty Constructor. @@ -232,10 +267,10 @@ public TextRegexProvider (string pattern) /// public string Pattern { get { - return StringExtensions.ToString (pattern); + return StringExtensions.ToString (_pattern); } set { - pattern = value.ToRuneList (); + _pattern = value.ToRuneList (); CompileMask (); SetupText (); } @@ -244,10 +279,10 @@ public string Pattern { /// public string Text { get { - return StringExtensions.ToString (text); + return StringExtensions.ToString (_text); } set { - text = value != string.Empty ? value.ToRuneList () : null; + _text = value != string.Empty ? value.ToRuneList () : null; SetupText (); } } @@ -258,7 +293,7 @@ public string Text { /// public bool IsValid { get { - return Validate (text); + return Validate (_text); } } @@ -272,7 +307,7 @@ public bool IsValid { bool Validate (List text) { - var match = regex.Match (StringExtensions.ToString (text)); + var match = _regex.Match (StringExtensions.ToString (text)); return match.Success; } @@ -281,7 +316,7 @@ public int Cursor (int pos) { if (pos < 0) { return CursorStart (); - } else if (pos >= text.Count) { + } else if (pos >= _text.Count) { return CursorEnd (); } else { return pos; @@ -297,7 +332,7 @@ public int CursorStart () /// public int CursorEnd () { - return text.Count; + return _text.Count; } /// @@ -312,7 +347,7 @@ public int CursorLeft (int pos) /// public int CursorRight (int pos) { - if (pos < text.Count) { + if (pos < _text.Count) { return pos + 1; } return pos; @@ -321,8 +356,10 @@ public int CursorRight (int pos) /// public bool Delete (int pos) { - if (text.Count > 0 && pos < text.Count) { - text.RemoveAt (pos); + if (_text.Count > 0 && pos < _text.Count) { + var oldValue = Text; + _text.RemoveAt (pos); + OnTextChanged (new TextChangedEventArgs (oldValue)); } return true; } @@ -330,10 +367,12 @@ public bool Delete (int pos) /// public bool InsertAt (char ch, int pos) { - var aux = text.ToList (); + var aux = _text.ToList (); aux.Insert (pos, (Rune)ch); if (Validate (aux) || ValidateOnInput == false) { - text.Insert (pos, (Rune)ch); + var oldValue = Text; + _text.Insert (pos, (Rune)ch); + OnTextChanged (new TextChangedEventArgs (oldValue)); return true; } return false; @@ -341,11 +380,11 @@ public bool InsertAt (char ch, int pos) void SetupText () { - if (text != null && IsValid) { + if (_text != null && IsValid) { return; } - text = new List (); + _text = new List (); } /// @@ -353,8 +392,11 @@ void SetupText () /// private void CompileMask () { - regex = new Regex (StringExtensions.ToString (pattern), RegexOptions.Compiled); + _regex = new Regex (StringExtensions.ToString (_pattern), RegexOptions.Compiled); } + + /// + public void OnTextChanged (TextChangedEventArgs oldValue) => TextChanged?.Invoke (this, oldValue); } #endregion } @@ -364,15 +406,14 @@ private void CompileMask () /// public class TextValidateField : View { - ITextValidateProvider provider; - int cursorPosition = 0; + ITextValidateProvider _provider; + int _cursorPosition; + int _defaultLength = 10; /// /// Initializes a new instance of the class using positioning. /// - public TextValidateField () : this (null) - { - } + public TextValidateField () : this (null) { } /// /// Initializes a new instance of the class using positioning. @@ -383,10 +424,10 @@ public TextValidateField (ITextValidateProvider provider) Provider = provider; } - Initialize (); + SetInitialProperties (); } - void Initialize () + void SetInitialProperties () { Height = 1; CanFocus = true; @@ -415,14 +456,14 @@ void Initialize () /// Provider /// public ITextValidateProvider Provider { - get => provider; + get => _provider; set { - provider = value; - if (provider.Fixed == true) { - this.Width = provider.DisplayText == string.Empty ? 10 : Text.Length; + _provider = value; + if (_provider.Fixed) { + this.Width = _provider.DisplayText == string.Empty ? _defaultLength : _provider.DisplayText.Length; } + // HomeKeyHandler already call SetNeedsDisplay HomeKeyHandler (); - SetNeedsDisplay (); } } @@ -431,11 +472,11 @@ public override bool MouseEvent (MouseEvent mouseEvent) { if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)) { - var c = provider.Cursor (mouseEvent.X - GetMargins (Frame.Width).left); - if (provider.Fixed == false && TextAlignment == TextAlignment.Right && Text.Length > 0) { - c += 1; + var c = _provider.Cursor (mouseEvent.X - GetMargins (Bounds.Width).left); + if (_provider.Fixed == false && TextAlignment == TextAlignment.Right && Text.Length > 0) { + c++; } - cursorPosition = c; + _cursorPosition = c; SetFocus (); SetNeedsDisplay (); return true; @@ -448,17 +489,17 @@ public override bool MouseEvent (MouseEvent mouseEvent) /// public new string Text { get { - if (provider == null) { + if (_provider == null) { return string.Empty; } - return provider.Text; + return _provider.Text; } set { - if (provider == null) { + if (_provider == null) { return; } - provider.Text = value; + _provider.Text = value; SetNeedsDisplay (); } @@ -467,15 +508,23 @@ public override bool MouseEvent (MouseEvent mouseEvent) /// public override void PositionCursor () { - var (left, _) = GetMargins (Frame.Width); + var (left, _) = GetMargins (Bounds.Width); - // Fixed = true, is for inputs thar have fixed width, like masked ones. + // Fixed = true, is for inputs that have fixed width, like masked ones. // Fixed = false, is for normal input. // When it's right-aligned and it's a normal input, the cursor behaves differently. - if (provider?.Fixed == false && TextAlignment == TextAlignment.Right) { - Move (cursorPosition + left - 1, 0); + int curPos; + if (_provider?.Fixed == false && TextAlignment == TextAlignment.Right) { + curPos = _cursorPosition + left - 1; + Move (curPos, 0); + } else { + curPos = _cursorPosition + left; + Move (curPos, 0); + } + if (curPos < 0 || curPos >= Bounds.Width) { + Application.Driver.SetCursorVisibility (CursorVisibility.Invisible); } else { - Move (cursorPosition + left, 0); + Application.Driver.SetCursorVisibility (CursorVisibility.Default); } } @@ -503,7 +552,7 @@ public override void PositionCursor () /// public override void OnDrawContent (Rect contentArea) { - if (provider == null) { + if (_provider == null) { Move (0, 0); Driver.AddStr ("Error: ITextValidateProvider not set!"); return; @@ -525,8 +574,8 @@ public override void OnDrawContent (Rect contentArea) // Content Driver.SetAttribute (textColor); // Content - for (int i = 0; i < provider.DisplayText.Length; i++) { - Driver.AddRune ((Rune)provider.DisplayText [i]); + for (int i = 0; i < _provider.DisplayText.Length; i++) { + Driver.AddRune ((Rune)_provider.DisplayText [i]); } // Right Margin @@ -542,10 +591,10 @@ public override void OnDrawContent (Rect contentArea) /// True if moved. bool CursorLeft () { - var current = cursorPosition; - cursorPosition = provider.CursorLeft (cursorPosition); + var current = _cursorPosition; + _cursorPosition = _provider.CursorLeft (_cursorPosition); SetNeedsDisplay (); - return current != cursorPosition; + return current != _cursorPosition; } /// @@ -554,10 +603,10 @@ bool CursorLeft () /// True if moved. bool CursorRight () { - var current = cursorPosition; - cursorPosition = provider.CursorRight (cursorPosition); + var current = _cursorPosition; + _cursorPosition = _provider.CursorRight (_cursorPosition); SetNeedsDisplay (); - return current != cursorPosition; + return current != _cursorPosition; } /// @@ -566,11 +615,11 @@ bool CursorRight () /// bool BackspaceKeyHandler () { - if (provider.Fixed == false && TextAlignment == TextAlignment.Right && cursorPosition <= 1) { + if (_provider.Fixed == false && TextAlignment == TextAlignment.Right && _cursorPosition <= 1) { return false; } - cursorPosition = provider.CursorLeft (cursorPosition); - provider.Delete (cursorPosition); + _cursorPosition = _provider.CursorLeft (_cursorPosition); + _provider.Delete (_cursorPosition); SetNeedsDisplay (); return true; } @@ -581,10 +630,10 @@ bool BackspaceKeyHandler () /// bool DeleteKeyHandler () { - if (provider.Fixed == false && TextAlignment == TextAlignment.Right) { - cursorPosition = provider.CursorLeft (cursorPosition); + if (_provider.Fixed == false && TextAlignment == TextAlignment.Right) { + _cursorPosition = _provider.CursorLeft (_cursorPosition); } - provider.Delete (cursorPosition); + _provider.Delete (_cursorPosition); SetNeedsDisplay (); return true; } @@ -595,7 +644,7 @@ bool DeleteKeyHandler () /// bool HomeKeyHandler () { - cursorPosition = provider.CursorStart (); + _cursorPosition = _provider.CursorStart (); SetNeedsDisplay (); return true; } @@ -606,7 +655,7 @@ bool HomeKeyHandler () /// bool EndKeyHandler () { - cursorPosition = provider.CursorEnd (); + _cursorPosition = _provider.CursorEnd (); SetNeedsDisplay (); return true; } @@ -614,17 +663,17 @@ bool EndKeyHandler () /// public override bool OnProcessKeyDown (Key a) { - if (provider == null) { + if (_provider == null) { return false; } if (a.AsRune == default) { return false; } - + var key = a.AsRune; - var inserted = provider.InsertAt ((char)key.Value, cursorPosition); + var inserted = _provider.InsertAt ((char)key.Value, _cursorPosition); if (inserted) { CursorRight (); @@ -638,12 +687,28 @@ public override bool OnProcessKeyDown (Key a) /// public virtual bool IsValid { get { - if (provider == null) { + if (_provider == null) { return false; } - return provider.IsValid; + return _provider.IsValid; } } + + /// + public override bool OnEnter (View view) + { + Application.Driver.SetCursorVisibility (CursorVisibility.Default); + + return base.OnEnter (view); + } + + /// + public override bool OnLeave (View view) + { + Application.Driver.SetCursorVisibility (CursorVisibility.Invisible); + + return base.OnLeave (view); + } } } diff --git a/UICatalog/Scenarios/Text.cs b/UICatalog/Scenarios/Text.cs index f0133dbb74..9842f871c4 100644 --- a/UICatalog/Scenarios/Text.cs +++ b/UICatalog/Scenarios/Text.cs @@ -7,7 +7,7 @@ using Terminal.Gui; using Terminal.Gui.TextValidateProviders; -namespace UICatalog.Scenarios; +namespace UICatalog.Scenarios; [ScenarioMetadata ("Text Input Controls", "Tests all text input controls")] [ScenarioCategory ("Controls")] @@ -161,12 +161,12 @@ void TextView_DrawContent (object sender, DrawEventArgs e) }; Win.Add (labelMirroringHexEditor); - var dateField = new DateField (System.DateTime.Now) { - X = 1, - Y = Pos.Bottom (hexEditor) + 1, - Width = 20 - }; - Win.Add (dateField); + var dateField = new DateField (System.DateTime.Now) { + X = 1, + Y = Pos.Bottom (hexEditor) + 1, + Width = 20 + }; + Win.Add (dateField); var labelMirroringDateField = new Label (dateField.Text) { X = Pos.Right (dateField) + 1, @@ -199,21 +199,32 @@ void TextView_DrawContent (object sender, DrawEventArgs e) _timeField.TimeChanged += TimeChanged; // MaskedTextProvider - uses .NET MaskedTextProvider - var netProviderLabel = new Label ("NetMaskedTextProvider [ 999 000 LLL >LLL| AAA aaa ]") { + var netProviderLabel = new Label ("NetMaskedTextProvider [ 999 000 LLL >LLL |AAA aaa ]") { X = Pos.Left (dateField), Y = Pos.Bottom (dateField) + 1 }; Win.Add (netProviderLabel); - var netProvider = new NetMaskedTextProvider ("999 000 LLL > LLL | AAA aaa"); + var netProvider = new NetMaskedTextProvider ("999 000 LLL >LLL |AAA aaa"); var netProviderField = new TextValidateField (netProvider) { X = Pos.Right (netProviderLabel) + 1, Y = Pos.Y (netProviderLabel) }; - Win.Add (netProviderField); + var labelMirroringNetProviderField = new Label (netProviderField.Text) { + X = Pos.Right (netProviderField) + 1, + Y = Pos.Top (netProviderField), + Width = Dim.Width (netProviderField), + Height = Dim.Height (netProviderField) + }; + Win.Add (labelMirroringNetProviderField); + + netProviderField.Provider.TextChanged += (s, prev) => { + labelMirroringNetProviderField.Text = netProviderField.Text; + }; + // TextRegexProvider - Regex provider implemented by Terminal.Gui var regexProvider = new Label ("TextRegexProvider [ ^([0-9]?[0-9]?[0-9]|1000)$ ]") { X = Pos.Left (netProviderLabel), @@ -228,16 +239,27 @@ void TextView_DrawContent (object sender, DrawEventArgs e) Width = 30, TextAlignment = TextAlignment.Centered }; - Win.Add (regexProviderField); + var labelMirroringRegexProviderField = new Label (regexProviderField.Text) { + X = Pos.Right (regexProviderField) + 1, + Y = Pos.Top (regexProviderField), + Width = Dim.Width (regexProviderField), + Height = Dim.Height (regexProviderField) + }; + Win.Add (labelMirroringRegexProviderField); + + regexProviderField.Provider.TextChanged += (s, prev) => { + labelMirroringRegexProviderField.Text = regexProviderField.Text; + }; + var labelAppendAutocomplete = new Label ("Append Autocomplete:") { Y = Pos.Y (regexProviderField) + 2, X = 1 }; var appendAutocompleteTextField = new TextField { X = Pos.Right (labelAppendAutocomplete), - Y = Pos.Bottom (labelAppendAutocomplete), + Y = Pos.Top (labelAppendAutocomplete), Width = Dim.Fill () }; appendAutocompleteTextField.Autocomplete = new AppendAutocomplete (appendAutocompleteTextField); diff --git a/UnitTests/Views/TextValidateFieldTests.cs b/UnitTests/Views/TextValidateFieldTests.cs index 7412233efb..742ceb9563 100644 --- a/UnitTests/Views/TextValidateFieldTests.cs +++ b/UnitTests/Views/TextValidateFieldTests.cs @@ -1,568 +1,595 @@ -using System; -using System.Reflection; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; using Terminal.Gui.TextValidateProviders; using Xunit; -namespace Terminal.Gui.ViewsTests { +namespace Terminal.Gui.ViewsTests; - // TODO: These tests shouild not rely on AutoInitShutdown / Application +public class TextValidateField_NET_Provider_Tests { - public class TextValidateField_NET_Provider_Tests { + [Fact] + public void Initialized_With_Cursor_On_First_Editable_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - [Fact] - [AutoInitShutdown] - public void Initialized_With_Cursor_On_First_Editable_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + field.NewKeyDownEvent (new (KeyCode.D1)); - field.NewKeyDownEvent (new (KeyCode.D1)); - - Assert.Equal ("--(1___)--", field.Provider.DisplayText); - Assert.Equal ("--(1 )--", field.Text); - } + Assert.Equal ("--(1___)--", field.Provider.DisplayText); + Assert.Equal ("--(1 )--", field.Text); + } - [Fact] - [AutoInitShutdown] - public void Input_Ilegal_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + [Fact] + public void Input_Ilegal_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + field.NewKeyDownEvent (new (KeyCode.A)); + + Assert.Equal ("--( )--", field.Text); + Assert.Equal ("--(____)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + } - field.NewKeyDownEvent (new (KeyCode.A)); + [Fact] + public void Home_Key_First_Editable_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + field.NewKeyDownEvent (new (KeyCode.CursorRight)); + field.NewKeyDownEvent (new (KeyCode.CursorRight)); + field.NewKeyDownEvent (new (KeyCode.Home)); + + field.NewKeyDownEvent (new (KeyCode.D1)); + + Assert.Equal ("--(1___)--", field.Provider.DisplayText); + Assert.Equal ("--(1 )--", field.Text); + Assert.False (field.IsValid); + } - Assert.Equal ("--( )--", field.Text); - Assert.Equal ("--(____)--", field.Provider.DisplayText); - Assert.False (field.IsValid); - } + [Fact] + public void End_Key_Last_Editable_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - [Fact] - [AutoInitShutdown] - public void Home_Key_First_Editable_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + field.NewKeyDownEvent (new (KeyCode.End)); - field.NewKeyDownEvent (new (KeyCode.CursorRight)); - field.NewKeyDownEvent (new (KeyCode.CursorRight)); - field.NewKeyDownEvent (new (KeyCode.Home)); + field.NewKeyDownEvent (new (KeyCode.D1)); - field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("--(___1)--", field.Provider.DisplayText); + Assert.Equal ("--( 1)--", field.Text); + Assert.False (field.IsValid); + } - Assert.Equal ("--(1___)--", field.Provider.DisplayText); - Assert.Equal ("--(1 )--", field.Text); - Assert.False (field.IsValid); + [Fact] + public void Right_Key_Stops_In_Last_Editable_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + for (int i = 0; i < 10; i++) { + field.NewKeyDownEvent (new (KeyCode.CursorRight)); } + field.NewKeyDownEvent (new (KeyCode.D1)); - [Fact] - [AutoInitShutdown] - public void End_Key_Last_Editable_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; - - field.NewKeyDownEvent (new (KeyCode.End)); - - field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("--(___1)--", field.Provider.DisplayText); + Assert.Equal ("--( 1)--", field.Text); + Assert.False (field.IsValid); + } - Assert.Equal ("--(___1)--", field.Provider.DisplayText); - Assert.Equal ("--( 1)--", field.Text); - Assert.False (field.IsValid); + [Fact] + public void Left_Key_Stops_In_First_Editable_Character () + { + // * + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + for (int i = 0; i < 10; i++) { + field.NewKeyDownEvent (new (KeyCode.CursorLeft)); } + field.NewKeyDownEvent (new (KeyCode.D1)); - [Fact] - [AutoInitShutdown] - public void Right_Key_Stops_In_Last_Editable_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; - - for (int i = 0; i < 10; i++) { - field.NewKeyDownEvent (new (KeyCode.CursorRight)); - } - field.NewKeyDownEvent (new (KeyCode.D1)); - - Assert.Equal ("--(___1)--", field.Provider.DisplayText); - Assert.Equal ("--( 1)--", field.Text); - Assert.False (field.IsValid); - } + Assert.Equal ("--(1___)--", field.Provider.DisplayText); + Assert.Equal ("--(1 )--", field.Text); + Assert.False (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void Left_Key_Stops_In_First_Editable_Character () - { - // * - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + [Fact] + public void When_Valid_Is_Valid_True () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("--(1 )--", field.Text); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D2)); + Assert.Equal ("--(12 )--", field.Text); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D3)); + Assert.Equal ("--(123 )--", field.Text); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D4)); + Assert.Equal ("--(1234)--", field.Text); + Assert.True (field.IsValid); + } - for (int i = 0; i < 10; i++) { - field.NewKeyDownEvent (new (KeyCode.CursorLeft)); - } - field.NewKeyDownEvent (new (KeyCode.D1)); + [Fact] + public void Insert_Skips_Non_Editable_Characters () + { + // ** ** + // 01234567890 + var field = new TextValidateField (new NetMaskedTextProvider ("--(00-00)--")) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("--(1_-__)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D2)); + Assert.Equal ("--(12-__)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D3)); + Assert.Equal ("--(12-3_)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.D4)); + Assert.Equal ("--(12-34)--", field.Provider.DisplayText); + Assert.True (field.IsValid); + } - Assert.Equal ("--(1___)--", field.Provider.DisplayText); - Assert.Equal ("--(1 )--", field.Text); - Assert.False (field.IsValid); - } + [Fact] + public void Initial_Value_Exact_Valid () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + Assert.Equal ("--(1234)--", field.Text); + Assert.True (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void When_Valid_Is_Valid_True () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + [Fact] + public void Initial_Value_Bigger_Than_Mask_Discarded () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "12345" }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + Assert.Equal ("--(____)--", field.Provider.DisplayText); + Assert.Equal ("--( )--", field.Text); + Assert.False (field.IsValid); + } - field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("--(1 )--", field.Text); - Assert.False (field.IsValid); + [Fact] + public void Initial_Value_Smaller_Than_Mask_Accepted () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "123" }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + Assert.Equal ("--(123_)--", field.Provider.DisplayText); + Assert.Equal ("--(123 )--", field.Text); + Assert.False (field.IsValid); + } - field.NewKeyDownEvent (new (KeyCode.D2)); - Assert.Equal ("--(12 )--", field.Text); - Assert.False (field.IsValid); + [Fact] + public void Delete_Key_Doesnt_Move_Cursor () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - field.NewKeyDownEvent (new (KeyCode.D3)); - Assert.Equal ("--(123 )--", field.Text); - Assert.False (field.IsValid); + Assert.Equal ("--(1234)--", field.Provider.DisplayText); + Assert.True (field.IsValid); - field.NewKeyDownEvent (new (KeyCode.D4)); - Assert.Equal ("--(1234)--", field.Text); - Assert.True (field.IsValid); - } + field.NewKeyDownEvent (new (KeyCode.Delete)); + field.NewKeyDownEvent (new (KeyCode.Delete)); + field.NewKeyDownEvent (new (KeyCode.Delete)); - [Fact] - [AutoInitShutdown] - public void Insert_Skips_Non_Editable_Characters () - { - // ** ** - // 01234567890 - var field = new TextValidateField (new NetMaskedTextProvider ("--(00-00)--")) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + Assert.Equal ("--(_234)--", field.Provider.DisplayText); + Assert.False (field.IsValid); - field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("--(1_-__)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + field.NewKeyDownEvent (new (KeyCode.CursorRight)); + field.NewKeyDownEvent (new (KeyCode.CursorRight)); - field.NewKeyDownEvent (new (KeyCode.D2)); - Assert.Equal ("--(12-__)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + field.NewKeyDownEvent (new (KeyCode.Delete)); + field.NewKeyDownEvent (new (KeyCode.Delete)); + field.NewKeyDownEvent (new (KeyCode.Delete)); - field.NewKeyDownEvent (new (KeyCode.D3)); - Assert.Equal ("--(12-3_)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + Assert.Equal ("--(_2_4)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + } - field.NewKeyDownEvent (new (KeyCode.D4)); - Assert.Equal ("--(12-34)--", field.Provider.DisplayText); - Assert.True (field.IsValid); - } + [Fact] + public void Backspace_Key_Deletes_Previous_Character () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; + + // Go to the end. + field.NewKeyDownEvent (new (KeyCode.End)); + + field.NewKeyDownEvent (new (KeyCode.Backspace)); + Assert.Equal ("--(12_4)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.Backspace)); + Assert.Equal ("--(1__4)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + field.NewKeyDownEvent (new (KeyCode.Backspace)); + Assert.Equal ("--(___4)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + + // One more + field.NewKeyDownEvent (new (KeyCode.Backspace)); + Assert.Equal ("--(___4)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void Initial_Value_Exact_Valid () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + [Fact] + public void Set_Text_After_Initialization () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Left, + Width = 30 + }; - Assert.Equal ("--(1234)--", field.Text); - Assert.True (field.IsValid); - } + field.Text = "1234"; - [Fact] - [AutoInitShutdown] - public void Initial_Value_Bigger_Than_Mask_Discarded () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "12345" }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + Assert.Equal ("--(1234)--", field.Text); + Assert.True (field.IsValid); + } - Assert.Equal ("--(____)--", field.Provider.DisplayText); - Assert.Equal ("--( )--", field.Text); - Assert.False (field.IsValid); - } + [Fact] + public void Changing_The_Mask_Tries_To_Keep_The_Previous_Text () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Left, + Width = 30 + }; + + field.Text = "1234"; + Assert.Equal ("--(1234)--", field.Text); + Assert.True (field.IsValid); + + var provider = field.Provider as NetMaskedTextProvider; + provider.Mask = "--------(00000000)--------"; + Assert.Equal ("--------(1234____)--------", field.Provider.DisplayText); + Assert.False (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void Initial_Value_Smaller_Than_Mask_Accepted () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "123" }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + [Fact] + public void MouseClick_Right_X_Greater_Than_Text_Width_Goes_To_Last_Editable_Position () + { + // **** + // 0123456789 + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Left, + Width = 30 + }; - Assert.Equal ("--(123_)--", field.Provider.DisplayText); - Assert.Equal ("--(123 )--", field.Text); - Assert.False (field.IsValid); - } + field.NewKeyDownEvent (new (KeyCode.D1)); - [Fact] - [AutoInitShutdown] - public void Delete_Key_Doesnt_Move_Cursor () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + Assert.Equal ("--(1___)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + Assert.Equal ("--(1 )--", field.Provider.Text); - Assert.Equal ("--(1234)--", field.Provider.DisplayText); - Assert.True (field.IsValid); + field.MouseEvent (new MouseEvent () { X = 25, Flags = MouseFlags.Button1Pressed }); - field.NewKeyDownEvent (new (KeyCode.Delete)); - field.NewKeyDownEvent (new (KeyCode.Delete)); - field.NewKeyDownEvent (new (KeyCode.Delete)); + field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("--(_234)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + Assert.Equal ("--(1__1)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + Assert.Equal ("--(1 1)--", field.Provider.Text); + } - field.NewKeyDownEvent (new (KeyCode.CursorRight)); - field.NewKeyDownEvent (new (KeyCode.CursorRight)); + [Fact] + public void Default_Width_Is_Always_Equal_To_The_Provider_DisplayText_Length () + { + // 9-Digit or space, optional. 0-Digit, required. L-Letter, required. + // > Shift up. Converts all characters that follow to uppercase. + // | Disable a previous shift up or shift down. + // A-Alphanumeric, required. a-Alphanumeric, optional. + var field = new TextValidateField (new NetMaskedTextProvider ("999 000 LLL >LLL |AAA aaa")); + + Assert.Equal (field.Bounds.Width, field.Provider.DisplayText.Length); + Assert.NotEqual (field.Provider.DisplayText.Length, field.Provider.Text.Length); + Assert.Equal (new string (' ', field.Text.Length), field.Provider.Text); + } - field.NewKeyDownEvent (new (KeyCode.Delete)); - field.NewKeyDownEvent (new (KeyCode.Delete)); - field.NewKeyDownEvent (new (KeyCode.Delete)); + [Fact] + public void OnTextChanged_TextChanged_Event () + { + var wasTextChanged = false; - Assert.Equal ("--(_2_4)--", field.Provider.DisplayText); - Assert.False (field.IsValid); - } + var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { + TextAlignment = TextAlignment.Left, + Width = 30 + }; - [Fact] - [AutoInitShutdown] - public void Backspace_Key_Deletes_Previous_Character () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--") { Text = "1234" }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + field.Provider.TextChanged += (sender, e) => wasTextChanged = true; - // Go to the end. - field.NewKeyDownEvent (new (KeyCode.End)); + field.NewKeyDownEvent (new (KeyCode.D1)); - field.NewKeyDownEvent (new (KeyCode.Backspace)); - Assert.Equal ("--(12_4)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + Assert.Equal ("--(1___)--", field.Provider.DisplayText); + Assert.False (field.IsValid); + Assert.Equal ("--(1 )--", field.Provider.Text); + Assert.True (wasTextChanged); + } +} - field.NewKeyDownEvent (new (KeyCode.Backspace)); - Assert.Equal ("--(1__4)--", field.Provider.DisplayText); - Assert.False (field.IsValid); +public class TextValidateField_Regex_Provider_Tests { - field.NewKeyDownEvent (new (KeyCode.Backspace)); - Assert.Equal ("--(___4)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + [Fact] + public void Input_Without_Validate_On_Input () + { + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { + Width = 20 + }; - // One more - field.NewKeyDownEvent (new (KeyCode.Backspace)); - Assert.Equal ("--(___4)--", field.Provider.DisplayText); - Assert.False (field.IsValid); - } + field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("1", field.Text); + Assert.False (field.IsValid); - [Fact] - [AutoInitShutdown] - public void Set_Text_After_Initialization () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Left, - Width = 30 - }; + field.NewKeyDownEvent (new (KeyCode.D2)); + Assert.Equal ("12", field.Text); + Assert.False (field.IsValid); - field.Text = "1234"; + field.NewKeyDownEvent (new (KeyCode.D3)); + Assert.Equal ("123", field.Text); + Assert.True (field.IsValid); - Assert.Equal ("--(1234)--", field.Text); - Assert.True (field.IsValid); - } + field.NewKeyDownEvent (new (KeyCode.D4)); + Assert.Equal ("1234", field.Text); + Assert.False (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void Changing_The_Mask_Tries_To_Keep_The_Previous_Text () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Left, - Width = 30 - }; + [Fact] + public void Input_With_Validate_On_Input_Set_Text () + { + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$")) { + Width = 20 + }; + + // Input dosen't validates the pattern. + field.NewKeyDownEvent (new (KeyCode.D1)); + Assert.Equal ("", field.Text); + Assert.False (field.IsValid); + + // Dosen't match + field.Text = "12356"; + Assert.Equal ("", field.Text); + Assert.False (field.IsValid); + + // Yes. + field.Text = "123"; + Assert.Equal ("123", field.Text); + Assert.True (field.IsValid); + } - field.Text = "1234"; - Assert.Equal ("--(1234)--", field.Text); - Assert.True (field.IsValid); + [Fact] + public void Text_With_All_Charset () + { + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$")) { + Width = 20 + }; - var provider = field.Provider as NetMaskedTextProvider; - provider.Mask = "--------(00000000)--------"; - Assert.Equal ("--------(1234____)--------", field.Provider.DisplayText); - Assert.False (field.IsValid); + var text = ""; + for (int i = 0; i < 255; i++) { + text += (char)i; } - [Fact] - [AutoInitShutdown] - public void MouseClick_Right_X_Greater_Than_Text_Width_Goes_To_Last_Editable_Position () - { - // **** - // 0123456789 - var field = new TextValidateField (new NetMaskedTextProvider ("--(0000)--")) { - TextAlignment = TextAlignment.Left, - Width = 30 - }; - - field.NewKeyDownEvent (new (KeyCode.D1)); + field.Text = text; - Assert.Equal ("--(1___)--", field.Provider.DisplayText); - Assert.False (field.IsValid); - - field.MouseEvent (new MouseEvent () { X = 25, Flags = MouseFlags.Button1Pressed }); + Assert.False (field.IsValid); + } - field.NewKeyDownEvent (new (KeyCode.D1)); + [Fact] + public void Mask_With_Invalid_Pattern_Exception () + { + // Regex Exception + // Maybe it's not the right behaviour. - Assert.Equal ("--(1__1)--", field.Provider.DisplayText); - Assert.False (field.IsValid); + var mask = ""; + for (int i = 0; i < 255; i++) { + mask += (char)i; } - } - - public class TextValidateField_Regex_Provider_Tests { - [Fact] - [AutoInitShutdown] - public void Input_Without_Validate_On_Input () - { - var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { + try { + var field = new TextValidateField (new TextRegexProvider (mask)) { Width = 20 }; - - field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("1", field.Text); - Assert.False (field.IsValid); - - field.NewKeyDownEvent (new (KeyCode.D2)); - Assert.Equal ("12", field.Text); - Assert.False (field.IsValid); - - field.NewKeyDownEvent (new (KeyCode.D3)); - Assert.Equal ("123", field.Text); - Assert.True (field.IsValid); - - field.NewKeyDownEvent (new (KeyCode.D4)); - Assert.Equal ("1234", field.Text); - Assert.False (field.IsValid); + } catch (RegexParseException ex) { + Assert.True (true, ex.Message); + return; } + Assert.True (false); + } - [Fact] - [AutoInitShutdown] - public void Input_With_Validate_On_Input_Set_Text () - { - var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$")) { - Width = 20 - }; + [Fact] + public void Home_Key_First_Editable_Character () + { + // Range 0 to 1000 + // Accepts 001 too. + var field = new TextValidateField (new TextRegexProvider ("^[0-9]?[0-9]?[0-9]|1000$")) { + Width = 20 + }; - // Input dosen't validates the pattern. - field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("", field.Text); - Assert.False (field.IsValid); + field.NewKeyDownEvent (new (KeyCode.D1)); + field.NewKeyDownEvent (new (KeyCode.D0)); + field.NewKeyDownEvent (new (KeyCode.D0)); + field.NewKeyDownEvent (new (KeyCode.D0)); - // Dosen't match - field.Text = "12356"; - Assert.Equal ("", field.Text); - Assert.False (field.IsValid); + Assert.Equal ("1000", field.Text); + Assert.True (field.IsValid); - // Yes. - field.Text = "123"; - Assert.Equal ("123", field.Text); - Assert.True (field.IsValid); - } + // HOME KEY + field.NewKeyDownEvent (new (KeyCode.Home)); - [Fact] - [AutoInitShutdown] - public void Text_With_All_Charset () - { - var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$")) { - Width = 20 - }; + // DELETE + field.NewKeyDownEvent (new (KeyCode.Delete)); - var text = ""; - for (int i = 0; i < 255; i++) { - text += (char)i; - } + Assert.Equal ("000", field.Text); + Assert.True (field.IsValid); + } - field.Text = text; + [Fact] + public void End_Key_End_Of_Input () + { + // Exactly 5 numbers + var field = new TextValidateField (new TextRegexProvider ("^[0-9]{5}$") { ValidateOnInput = false }) { + Width = 20 + }; - Assert.False (field.IsValid); + for (int i = 0; i < 4; i++) { + field.NewKeyDownEvent (new (KeyCode.D0)); } - [Fact] - [AutoInitShutdown] - public void Mask_With_Invalid_Pattern_Exception () - { - // Regex Exception - // Maybe it's not the right behaviour. - - var mask = ""; - for (int i = 0; i < 255; i++) { - mask += (char)i; - } - - try { - var field = new TextValidateField (new TextRegexProvider (mask)) { - Width = 20 - }; - } catch (RegexParseException ex) { - Assert.True (true, ex.Message); - return; - } - Assert.True (false); - } + Assert.Equal ("0000", field.Text); + Assert.False (field.IsValid); - [Fact] - [AutoInitShutdown] - public void Home_Key_First_Editable_Character () - { - // Range 0 to 1000 - // Accepts 001 too. - var field = new TextValidateField (new TextRegexProvider ("^[0-9]?[0-9]?[0-9]|1000$")) { - Width = 20 - }; + // HOME KEY + field.NewKeyDownEvent (new (KeyCode.Home)); - field.NewKeyDownEvent (new (KeyCode.D1)); - field.NewKeyDownEvent (new (KeyCode.D0)); - field.NewKeyDownEvent (new (KeyCode.D0)); - field.NewKeyDownEvent (new (KeyCode.D0)); + // END KEY + field.NewKeyDownEvent (new (KeyCode.End)); - Assert.Equal ("1000", field.Text); - Assert.True (field.IsValid); + // Insert 9 + field.NewKeyDownEvent (new (KeyCode.D9)); - // HOME KEY - field.NewKeyDownEvent (new (KeyCode.Home)); + Assert.Equal ("00009", field.Text); + Assert.True (field.IsValid); - // DELETE - field.NewKeyDownEvent (new (KeyCode.Delete)); + // Insert 9 + field.NewKeyDownEvent (new (KeyCode.D9)); - Assert.Equal ("000", field.Text); - Assert.True (field.IsValid); - } + Assert.Equal ("000099", field.Text); + Assert.False (field.IsValid); + } - [Fact] - [AutoInitShutdown] - public void End_Key_End_Of_Input () - { - // Exactly 5 numbers - var field = new TextValidateField (new TextRegexProvider ("^[0-9]{5}$") { ValidateOnInput = false }) { - Width = 20 - }; + [Fact] + public void Right_Key_Stops_At_End_And_Insert () + { + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - for (int i = 0; i < 4; i++) { - field.NewKeyDownEvent (new (KeyCode.D0)); - } + field.Text = "123"; - Assert.Equal ("0000", field.Text); - Assert.False (field.IsValid); + for (int i = 0; i < 10; i++) { + field.NewKeyDownEvent (new (KeyCode.CursorRight)); + } - // HOME KEY - field.NewKeyDownEvent (new (KeyCode.Home)); + Assert.Equal ("123", field.Text); + Assert.True (field.IsValid); - // END KEY - field.NewKeyDownEvent (new (KeyCode.End)); + // Insert 4 + field.NewKeyDownEvent (new (KeyCode.D4)); - // Insert 9 - field.NewKeyDownEvent (new (KeyCode.D9)); + Assert.Equal ("1234", field.Text); + Assert.False (field.IsValid); + } - Assert.Equal ("00009", field.Text); - Assert.True (field.IsValid); + [Fact] + public void Left_Key_Stops_At_Start_And_Insert () + { + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - // Insert 9 - field.NewKeyDownEvent (new (KeyCode.D9)); + field.Text = "123"; - Assert.Equal ("000099", field.Text); - Assert.False (field.IsValid); + for (int i = 0; i < 10; i++) { + field.NewKeyDownEvent (new (KeyCode.CursorLeft)); } - [Fact] - [AutoInitShutdown] - public void Right_Key_Stops_At_End_And_Insert () - { - var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; - - field.Text = "123"; - - for (int i = 0; i < 10; i++) { - field.NewKeyDownEvent (new (KeyCode.CursorRight)); - } + Assert.Equal ("123", field.Text); + Assert.True (field.IsValid); - Assert.Equal ("123", field.Text); - Assert.True (field.IsValid); + // Insert 4 + field.NewKeyDownEvent (new (KeyCode.D4)); - // Insert 4 - field.NewKeyDownEvent (new (KeyCode.D4)); - - Assert.Equal ("1234", field.Text); - Assert.False (field.IsValid); - } - - [Fact] - [AutoInitShutdown] - public void Left_Key_Stops_At_Start_And_Insert () - { - var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { - TextAlignment = TextAlignment.Centered, - Width = 20 - }; + Assert.Equal ("4123", field.Text); + Assert.False (field.IsValid); + } - field.Text = "123"; + [Fact] + public void OnTextChanged_TextChanged_Event () + { + var wasTextChanged = false; - for (int i = 0; i < 10; i++) { - field.NewKeyDownEvent (new (KeyCode.CursorLeft)); - } + var field = new TextValidateField (new TextRegexProvider ("^[0-9][0-9][0-9]$") { ValidateOnInput = false }) { + TextAlignment = TextAlignment.Centered, + Width = 20 + }; - Assert.Equal ("123", field.Text); - Assert.True (field.IsValid); + field.Provider.TextChanged += (sender, e) => wasTextChanged = true; - // Insert 4 - field.NewKeyDownEvent (new (KeyCode.D4)); + field.NewKeyDownEvent (new (KeyCode.D1)); - Assert.Equal ("4123", field.Text); - Assert.False (field.IsValid); - } + Assert.Equal ("1", field.Provider.DisplayText); + Assert.False (field.IsValid); + Assert.Equal ("1", field.Provider.Text); + Assert.True (wasTextChanged); } } \ No newline at end of file