diff --git a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs b/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs deleted file mode 100644 index c39a632..0000000 --- a/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System.Windows.Input; -using Moq; -using NFluent; -using Smartway.UiComponent.Inputs.Barcode; -using Xamarin.Forms; -using Xunit; - -namespace Smartway.UiComponent.UnitTests.Inputs.Barcode -{ - public class BarcodeInputBehaviorTest - { - public BarcodeInputBehaviorTest() - { - Command = new Mock(); - Command.Setup(_ => _.CanExecute(It.IsAny())).Returns(true); - Behavior = new BarcodeInputBehavior(); - Behavior.Command = Command.Object; - Entry = new Entry(); - Entry.Behaviors.Add(Behavior); - } - - public Mock Command { get; set; } - - public Entry Entry { get; set; } - - public BarcodeInputBehavior Behavior { get; set; } - } - - public class OnTextChanged : BarcodeInputBehaviorTest - { - [Fact] - public void ValidGencodeFilled() - { - Entry.Text = "2970812075764"; - - Check.That(Entry.Text).IsEqualTo("2970812075764"); - Command.Verify(_ => _.Execute("2970812075764"), Times.Once); - } - - [Fact] - public void UnvalidGencodeFilled() - { - Entry.Text = "297081207576"; - Entry.Text = "2970812075765"; - - Check.That(Entry.Text).IsEqualTo("297081207576"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void GencodeNotTotallyFilled() - { - Entry.Text = "297081207"; - - Check.That(Entry.Text).IsEqualTo("297081207"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void GencodeFilledWithNotDigitChar() - { - Entry.Text = "x"; - - Check.That(Entry.Text).IsEqualTo(null); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void GencodeFilledBigLength() - { - Entry.Text = "29708120757644"; - - Check.That(Entry.Text).IsEqualTo(null); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - - [Fact] - public void CanExecuteIsFalse() - { - Command.Setup(_ => _.CanExecute(It.IsAny())).Returns(false); - - Entry.Text = "2970812075764"; - - Check.That(Entry.Text).IsEqualTo("2970812075764"); - Command.Verify(_ => _.Execute(It.IsAny()), Times.Never); - } - } -} diff --git a/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs b/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs index f644d48..6ce6544 100644 --- a/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs +++ b/Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Windows.Input; using Smartway.Barcode.Ean13; using Smartway.UiComponent.Behaviors; @@ -21,7 +22,7 @@ protected override void OnAttachedTo(BindableObject bindable) base.OnAttachedTo(bindable); AssociatedObject.Focused += OnAssociatedObjectOnFocused; - AssociatedObject.TextChanged += OnTextChanged; + AssociatedObject.Completed += Completed; } protected override void OnDetachingFrom(BindableObject bindable) @@ -29,7 +30,7 @@ protected override void OnDetachingFrom(BindableObject bindable) base.OnDetachingFrom(bindable); AssociatedObject.Focused -= OnAssociatedObjectOnFocused; - AssociatedObject.TextChanged -= OnTextChanged; + AssociatedObject.Completed -= Completed; } private void OnAssociatedObjectOnFocused(object sender, FocusEventArgs e) @@ -37,35 +38,10 @@ private void OnAssociatedObjectOnFocused(object sender, FocusEventArgs e) AssociatedObject.Keyboard = Keyboard.Numeric; } - private void OnTextChanged(object sender, TextChangedEventArgs e) + private void Completed(object sender, EventArgs e) { - if (string.IsNullOrEmpty(e.NewTextValue)) - return; - - if (InputIsInvalid(e.NewTextValue)) - { - AssociatedObject.Text = e.OldTextValue; - return; - } - - if (e.NewTextValue.Length != Ean13.CheckedLength) - return; - - if (Command == null || !Command.CanExecute(null)) - return; - - Command.Execute(e.NewTextValue); - } - - private bool InputIsInvalid(string input) - { - if (input.Length > Ean13.CheckedLength) - return true; - - if (input.Length == Ean13.CheckedLength && !Ean13.Check(input)) - return true; - - return input.ToCharArray().Any(_ => !char.IsDigit(_)); + var entry = (Entry)sender; + Command.Execute(entry.Text); } } }