TextBoxTests_ValidationState.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using Avalonia.Markup.Xaml.Data;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class TextBoxTests_ValidationState
  13. {
  14. [Fact]
  15. public void Setter_Exceptions_Should_Set_ValidationState()
  16. {
  17. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  18. {
  19. var target = new TextBox();
  20. var binding = new Binding(nameof(ExceptionTest.LessThan10));
  21. binding.Source = new ExceptionTest();
  22. binding.EnableValidation = true;
  23. target.Bind(TextBox.TextProperty, binding);
  24. Assert.True(target.ValidationStatus.IsValid);
  25. target.Text = "20";
  26. Assert.False(target.ValidationStatus.IsValid);
  27. target.Text = "1";
  28. Assert.True(target.ValidationStatus.IsValid);
  29. }
  30. }
  31. [Fact(Skip = "TODO: Not yet passing")]
  32. public void Unconvertable_Value_Should_Set_ValidationState()
  33. {
  34. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  35. {
  36. var target = new TextBox();
  37. var binding = new Binding(nameof(ExceptionTest.LessThan10));
  38. binding.Source = new ExceptionTest();
  39. binding.EnableValidation = true;
  40. target.Bind(TextBox.TextProperty, binding);
  41. Assert.True(target.ValidationStatus.IsValid);
  42. target.Text = "foo";
  43. Assert.False(target.ValidationStatus.IsValid);
  44. target.Text = "1";
  45. Assert.True(target.ValidationStatus.IsValid);
  46. }
  47. }
  48. [Fact]
  49. public void Indei_Should_Set_ValidationState()
  50. {
  51. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  52. {
  53. var target = new TextBox();
  54. var binding = new Binding(nameof(ExceptionTest.LessThan10));
  55. binding.Source = new IndeiTest();
  56. binding.EnableValidation = true;
  57. target.Bind(TextBox.TextProperty, binding);
  58. Assert.True(target.ValidationStatus.IsValid);
  59. target.Text = "20";
  60. Assert.False(target.ValidationStatus.IsValid);
  61. target.Text = "1";
  62. Assert.True(target.ValidationStatus.IsValid);
  63. }
  64. }
  65. private class ExceptionTest
  66. {
  67. private int _lessThan10;
  68. public int LessThan10
  69. {
  70. get { return _lessThan10; }
  71. set
  72. {
  73. if (value < 10)
  74. {
  75. _lessThan10 = value;
  76. }
  77. else
  78. {
  79. throw new InvalidOperationException("More than 10.");
  80. }
  81. }
  82. }
  83. }
  84. private class IndeiTest : INotifyDataErrorInfo
  85. {
  86. private int _lessThan10;
  87. private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
  88. public int LessThan10
  89. {
  90. get { return _lessThan10; }
  91. set
  92. {
  93. if (value < 10)
  94. {
  95. _lessThan10 = value;
  96. _errors.Remove(nameof(LessThan10));
  97. ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(LessThan10)));
  98. }
  99. else
  100. {
  101. _errors[nameof(LessThan10)] = new[] { "More than 10" };
  102. ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(LessThan10)));
  103. }
  104. }
  105. }
  106. public bool HasErrors => _lessThan10 >= 10;
  107. public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
  108. public IEnumerable GetErrors(string propertyName)
  109. {
  110. IList<string> result;
  111. _errors.TryGetValue(propertyName, out result);
  112. return result;
  113. }
  114. }
  115. }
  116. }