ExpressionObserverTests_DataValidation.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Avalonia.Data;
  5. using Avalonia.Data.Core;
  6. using Avalonia.Threading;
  7. using Avalonia.UnitTests;
  8. using Xunit;
  9. namespace Avalonia.Base.UnitTests.Data.Core
  10. {
  11. [InvariantCulture]
  12. public class ExpressionObserverTests_DataValidation
  13. {
  14. ////[Fact]
  15. ////public void Doesnt_Send_DataValidationError_When_DataValidatation_Not_Enabled()
  16. ////{
  17. //// var data = new ExceptionTest { MustBePositive = 5 };
  18. //// var observer = ExpressionObserver.Create(data, o => o.MustBePositive, false);
  19. //// var validationMessageFound = false;
  20. //// observer.OfType<BindingNotification>()
  21. //// .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  22. //// .Subscribe(_ => validationMessageFound = true);
  23. //// observer.SetValue(-5);
  24. //// Assert.False(validationMessageFound);
  25. //// GC.KeepAlive(data);
  26. ////}
  27. ////[Fact]
  28. ////public void Exception_Validation_Sends_DataValidationError()
  29. ////{
  30. //// var data = new ExceptionTest { MustBePositive = 5 };
  31. //// var observer = ExpressionObserver.Create(data, o => o.MustBePositive, true);
  32. //// var validationMessageFound = false;
  33. //// observer.OfType<BindingNotification>()
  34. //// .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  35. //// .Subscribe(_ => validationMessageFound = true);
  36. //// observer.SetValue(-5);
  37. //// Assert.True(validationMessageFound);
  38. //// GC.KeepAlive(data);
  39. ////}
  40. [Fact]
  41. public void Indei_Validation_Does_Not_Subscribe_When_DataValidation_Not_Enabled()
  42. {
  43. var data = new IndeiTest { MustBePositive = 5 };
  44. var observer = ExpressionObserver.Create(data, o => o.MustBePositive, false);
  45. observer.Subscribe(_ => { });
  46. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  47. }
  48. [Fact]
  49. public void Enabled_Indei_Validation_Subscribes()
  50. {
  51. var data = new IndeiTest { MustBePositive = 5 };
  52. var observer = ExpressionObserver.Create(data, o => o.MustBePositive, true);
  53. var sub = observer.Subscribe(_ => { });
  54. Assert.Equal(1, data.ErrorsChangedSubscriptionCount);
  55. sub.Dispose();
  56. // Forces WeakEvent compact
  57. Dispatcher.UIThread.RunJobs();
  58. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  59. }
  60. [Fact]
  61. public void Validation_Plugins_Send_Correct_Notifications()
  62. {
  63. var data = new IndeiTest();
  64. var observer = ExpressionObserver.Create(data, o => o.MustBePositive, true);
  65. var result = new List<object>();
  66. var errmsg = string.Empty;
  67. try { typeof(IndeiTest).GetProperty(nameof(IndeiTest.MustBePositive)).SetValue(data, "foo"); }
  68. catch(Exception e) { errmsg = e.Message; }
  69. observer.Subscribe(x => result.Add(x));
  70. observer.SetValue(5);
  71. observer.SetValue(-5);
  72. observer.SetValue("foo");
  73. observer.SetValue(5);
  74. Assert.Equal(new[]
  75. {
  76. new BindingNotification(0),
  77. // Value is notified twice as ErrorsChanged is always called by IndeiTest.
  78. new BindingNotification(5),
  79. new BindingNotification(5),
  80. // Value is first signalled without an error as validation hasn't been updated.
  81. new BindingNotification(-5),
  82. new BindingNotification(new DataValidationException("Must be positive"), BindingErrorType.DataValidationError, -5),
  83. // Exception is thrown by trying to set value to "foo".
  84. new BindingNotification(
  85. new ArgumentException(errmsg),
  86. BindingErrorType.DataValidationError),
  87. // Value is set then validation is updated.
  88. new BindingNotification(new DataValidationException("Must be positive"), BindingErrorType.DataValidationError, 5),
  89. new BindingNotification(5),
  90. }, result);
  91. GC.KeepAlive(data);
  92. }
  93. [Fact]
  94. public void Doesnt_Subscribe_To_Indei_Of_Intermediate_Object_In_Chain()
  95. {
  96. var data = new Container
  97. {
  98. Inner = new IndeiTest()
  99. };
  100. var observer = ExpressionObserver.Create(data, o => o.Inner.MustBePositive, true);
  101. observer.Subscribe(_ => { });
  102. // We may want to change this but I've never seen an example of data validation on an
  103. // intermediate object in a chain so for the moment I'm not sure what the result of
  104. // validating such a thing should look like.
  105. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  106. Assert.Equal(1, data.Inner.ErrorsChangedSubscriptionCount);
  107. }
  108. [Fact]
  109. public void Sends_Correct_Notifications_With_Property_Chain()
  110. {
  111. var container = new Container();
  112. var observer = ExpressionObserver.Create(container, o => o.Inner.MustBePositive, true);
  113. var result = new List<object>();
  114. observer.Subscribe(x => result.Add(x));
  115. Assert.Equal(new[]
  116. {
  117. new BindingNotification(
  118. new MarkupBindingChainException("Null value", "o => o.Inner.MustBePositive", "Inner"),
  119. BindingErrorType.Error,
  120. AvaloniaProperty.UnsetValue),
  121. }, result);
  122. GC.KeepAlive(container);
  123. }
  124. public class ExceptionTest : NotifyingBase
  125. {
  126. private int _mustBePositive;
  127. public int MustBePositive
  128. {
  129. get { return _mustBePositive; }
  130. set
  131. {
  132. if (value <= 0)
  133. {
  134. throw new ArgumentOutOfRangeException(nameof(value));
  135. }
  136. _mustBePositive = value;
  137. RaisePropertyChanged();
  138. }
  139. }
  140. }
  141. private class IndeiTest : IndeiBase
  142. {
  143. private int _mustBePositive;
  144. private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
  145. public int MustBePositive
  146. {
  147. get { return _mustBePositive; }
  148. set
  149. {
  150. _mustBePositive = value;
  151. RaisePropertyChanged();
  152. if (value >= 0)
  153. {
  154. _errors.Remove(nameof(MustBePositive));
  155. RaiseErrorsChanged(nameof(MustBePositive));
  156. }
  157. else
  158. {
  159. _errors[nameof(MustBePositive)] = new[] { "Must be positive" };
  160. RaiseErrorsChanged(nameof(MustBePositive));
  161. }
  162. }
  163. }
  164. public override bool HasErrors => _mustBePositive >= 0;
  165. public override IEnumerable GetErrors(string propertyName)
  166. {
  167. IList<string> result;
  168. _errors.TryGetValue(propertyName, out result);
  169. return result;
  170. }
  171. }
  172. private class Container : IndeiBase
  173. {
  174. private IndeiTest _inner;
  175. public IndeiTest Inner
  176. {
  177. get { return _inner; }
  178. set { _inner = value; RaisePropertyChanged(); }
  179. }
  180. public override bool HasErrors => false;
  181. public override IEnumerable GetErrors(string propertyName) => null;
  182. }
  183. }
  184. }