1
0

ExpressionObserverTests_DataValidation.cs 7.8 KB

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