ExpressionObserverTests_DataValidation.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.Linq;
  7. using System.Reactive.Linq;
  8. using Avalonia.Data;
  9. using Avalonia.Data.Core;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. namespace Avalonia.Base.UnitTests.Data.Core
  13. {
  14. public class ExpressionObserverTests_DataValidation : IClassFixture<InvariantCultureFixture>
  15. {
  16. [Fact]
  17. public void Doesnt_Send_DataValidationError_When_DataValidatation_Not_Enabled()
  18. {
  19. var data = new ExceptionTest { MustBePositive = 5 };
  20. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false);
  21. var validationMessageFound = false;
  22. observer.OfType<BindingNotification>()
  23. .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  24. .Subscribe(_ => validationMessageFound = true);
  25. observer.SetValue(-5);
  26. Assert.False(validationMessageFound);
  27. GC.KeepAlive(data);
  28. }
  29. [Fact]
  30. public void Exception_Validation_Sends_DataValidationError()
  31. {
  32. var data = new ExceptionTest { MustBePositive = 5 };
  33. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  34. var validationMessageFound = false;
  35. observer.OfType<BindingNotification>()
  36. .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  37. .Subscribe(_ => validationMessageFound = true);
  38. observer.SetValue(-5);
  39. Assert.True(validationMessageFound);
  40. GC.KeepAlive(data);
  41. }
  42. [Fact]
  43. public void Indei_Validation_Does_Not_Subscribe_When_DataValidatation_Not_Enabled()
  44. {
  45. var data = new IndeiTest { MustBePositive = 5 };
  46. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false);
  47. observer.Subscribe(_ => { });
  48. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  49. }
  50. [Fact]
  51. public void Enabled_Indei_Validation_Subscribes()
  52. {
  53. var data = new IndeiTest { MustBePositive = 5 };
  54. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  55. var sub = observer.Subscribe(_ => { });
  56. Assert.Equal(1, data.ErrorsChangedSubscriptionCount);
  57. sub.Dispose();
  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 = new ExpressionObserver(data, nameof(data.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 Exception("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 Exception("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 = new ExpressionObserver(
  101. data,
  102. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  103. true);
  104. observer.Subscribe(_ => { });
  105. // We may want to change this but I've never seen an example of data validation on an
  106. // intermediate object in a chain so for the moment I'm not sure what the result of
  107. // validating such a thing should look like.
  108. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  109. Assert.Equal(1, ((IndeiTest)data.Inner).ErrorsChangedSubscriptionCount);
  110. }
  111. [Fact]
  112. public void Sends_Correct_Notifications_With_Property_Chain()
  113. {
  114. var container = new Container();
  115. var inner = new IndeiTest();
  116. var observer = new ExpressionObserver(
  117. container,
  118. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  119. true);
  120. var result = new List<object>();
  121. observer.Subscribe(x => result.Add(x));
  122. Assert.Equal(new[]
  123. {
  124. new BindingNotification(
  125. new MarkupBindingChainException("Null value", "Inner.MustBePositive", "Inner"),
  126. BindingErrorType.Error,
  127. AvaloniaProperty.UnsetValue),
  128. }, result);
  129. GC.KeepAlive(container);
  130. GC.KeepAlive(inner);
  131. }
  132. public class ExceptionTest : NotifyingBase
  133. {
  134. private int _mustBePositive;
  135. public int MustBePositive
  136. {
  137. get { return _mustBePositive; }
  138. set
  139. {
  140. if (value <= 0)
  141. {
  142. throw new ArgumentOutOfRangeException(nameof(value));
  143. }
  144. _mustBePositive = value;
  145. RaisePropertyChanged();
  146. }
  147. }
  148. }
  149. private class IndeiTest : IndeiBase
  150. {
  151. private int _mustBePositive;
  152. private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
  153. public int MustBePositive
  154. {
  155. get { return _mustBePositive; }
  156. set
  157. {
  158. _mustBePositive = value;
  159. RaisePropertyChanged();
  160. if (value >= 0)
  161. {
  162. _errors.Remove(nameof(MustBePositive));
  163. RaiseErrorsChanged(nameof(MustBePositive));
  164. }
  165. else
  166. {
  167. _errors[nameof(MustBePositive)] = new[] { "Must be positive" };
  168. RaiseErrorsChanged(nameof(MustBePositive));
  169. }
  170. }
  171. }
  172. public override bool HasErrors => _mustBePositive >= 0;
  173. public override IEnumerable GetErrors(string propertyName)
  174. {
  175. IList<string> result;
  176. _errors.TryGetValue(propertyName, out result);
  177. return result;
  178. }
  179. }
  180. private class Container : IndeiBase
  181. {
  182. private object _inner;
  183. public object Inner
  184. {
  185. get { return _inner; }
  186. set { _inner = value; RaisePropertyChanged(); }
  187. }
  188. public override bool HasErrors => false;
  189. public override IEnumerable GetErrors(string propertyName) => null;
  190. }
  191. }
  192. }