ExpressionObserverTests_DataValidation.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.Markup.Data;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. namespace Avalonia.Markup.UnitTests.Data
  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. observer.Subscribe(x => result.Add(x));
  67. observer.SetValue(5);
  68. observer.SetValue(-5);
  69. observer.SetValue("foo");
  70. observer.SetValue(5);
  71. Assert.Equal(new[]
  72. {
  73. new BindingNotification(0),
  74. // Value is notified twice as ErrorsChanged is always called by IndeiTest.
  75. new BindingNotification(5),
  76. new BindingNotification(5),
  77. // Value is first signalled without an error as validation hasn't been updated.
  78. new BindingNotification(-5),
  79. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, -5),
  80. // Exception is thrown by trying to set value to "foo".
  81. new BindingNotification(
  82. new ArgumentException("Object of type 'System.String' cannot be converted to type 'System.Int32'."),
  83. BindingErrorType.DataValidationError),
  84. // Value is set then validation is updated.
  85. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, 5),
  86. new BindingNotification(5),
  87. }, result);
  88. GC.KeepAlive(data);
  89. }
  90. [Fact]
  91. public void Doesnt_Subscribe_To_Indei_Of_Intermediate_Object_In_Chain()
  92. {
  93. var data = new Container
  94. {
  95. Inner = new IndeiTest()
  96. };
  97. var observer = new ExpressionObserver(
  98. data,
  99. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  100. 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, ((IndeiTest)data.Inner).ErrorsChangedSubscriptionCount);
  107. }
  108. [Fact]
  109. public void Sends_Correct_Notifications_With_Property_Chain()
  110. {
  111. var container = new Container();
  112. var inner = new IndeiTest();
  113. var observer = new ExpressionObserver(
  114. container,
  115. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  116. true);
  117. var result = new List<object>();
  118. observer.Subscribe(x => result.Add(x));
  119. Assert.Equal(new[]
  120. {
  121. new BindingNotification(
  122. new MarkupBindingChainException("Null value", "Inner.MustBePositive", "Inner"),
  123. BindingErrorType.Error,
  124. AvaloniaProperty.UnsetValue),
  125. }, result);
  126. GC.KeepAlive(container);
  127. GC.KeepAlive(inner);
  128. }
  129. public class ExceptionTest : NotifyingBase
  130. {
  131. private int _mustBePositive;
  132. public int MustBePositive
  133. {
  134. get { return _mustBePositive; }
  135. set
  136. {
  137. if (value <= 0)
  138. {
  139. throw new ArgumentOutOfRangeException(nameof(value));
  140. }
  141. _mustBePositive = value;
  142. RaisePropertyChanged();
  143. }
  144. }
  145. }
  146. private class IndeiTest : IndeiBase
  147. {
  148. private int _mustBePositive;
  149. private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
  150. public int MustBePositive
  151. {
  152. get { return _mustBePositive; }
  153. set
  154. {
  155. _mustBePositive = value;
  156. RaisePropertyChanged();
  157. if (value >= 0)
  158. {
  159. _errors.Remove(nameof(MustBePositive));
  160. RaiseErrorsChanged(nameof(MustBePositive));
  161. }
  162. else
  163. {
  164. _errors[nameof(MustBePositive)] = new[] { "Must be positive" };
  165. RaiseErrorsChanged(nameof(MustBePositive));
  166. }
  167. }
  168. }
  169. public override bool HasErrors => _mustBePositive >= 0;
  170. public override IEnumerable GetErrors(string propertyName)
  171. {
  172. IList<string> result;
  173. _errors.TryGetValue(propertyName, out result);
  174. return result;
  175. }
  176. }
  177. private class Container : IndeiBase
  178. {
  179. private object _inner;
  180. public object Inner
  181. {
  182. get { return _inner; }
  183. set { _inner = value; RaisePropertyChanged(); }
  184. }
  185. public override bool HasErrors => false;
  186. public override IEnumerable GetErrors(string propertyName) => null;
  187. }
  188. }
  189. }