ExpressionObserverTests_DataValidation.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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
  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. }
  28. [Fact]
  29. public void Exception_Validation_Sends_DataValidationError()
  30. {
  31. var data = new ExceptionTest { MustBePositive = 5 };
  32. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  33. var validationMessageFound = false;
  34. observer.OfType<BindingNotification>()
  35. .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  36. .Subscribe(_ => validationMessageFound = true);
  37. observer.SetValue(-5);
  38. Assert.True(validationMessageFound);
  39. }
  40. [Fact]
  41. public void Indei_Validation_Does_Not_Subscribe_When_DataValidatation_Not_Enabled()
  42. {
  43. var data = new IndeiTest { MustBePositive = 5 };
  44. var observer = new ExpressionObserver(data, nameof(data.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 = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  53. var sub = observer.Subscribe(_ => { });
  54. Assert.Equal(1, data.ErrorsChangedSubscriptionCount);
  55. sub.Dispose();
  56. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  57. }
  58. [Fact]
  59. public void Validation_Plugins_Send_Correct_Notifications()
  60. {
  61. var data = new IndeiTest();
  62. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  63. var result = new List<object>();
  64. observer.Subscribe(x => result.Add(x));
  65. observer.SetValue(5);
  66. observer.SetValue(-5);
  67. observer.SetValue("foo");
  68. observer.SetValue(5);
  69. Assert.Equal(new[]
  70. {
  71. new BindingNotification(0),
  72. // Value is notified twice as ErrorsChanged is always called by IndeiTest.
  73. new BindingNotification(5),
  74. new BindingNotification(5),
  75. // Value is first signalled without an error as validation hasn't been updated.
  76. new BindingNotification(-5),
  77. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, -5),
  78. // Exception is thrown by trying to set value to "foo".
  79. new BindingNotification(
  80. new ArgumentException("Object of type 'System.String' cannot be converted to type 'System.Int32'."),
  81. BindingErrorType.DataValidationError),
  82. // Value is set then validation is updated.
  83. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, 5),
  84. new BindingNotification(5),
  85. }, result);
  86. }
  87. [Fact]
  88. public void Doesnt_Subscribe_To_Indei_Of_Intermediate_Object_In_Chain()
  89. {
  90. var data = new Container
  91. {
  92. Inner = new IndeiTest()
  93. };
  94. var observer = new ExpressionObserver(
  95. data,
  96. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  97. true);
  98. observer.Subscribe(_ => { });
  99. // We may want to change this but I've never seen an example of data validation on an
  100. // intermediate object in a chain so for the moment I'm not sure what the result of
  101. // validating such a thing should look like.
  102. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  103. Assert.Equal(1, ((IndeiTest)data.Inner).ErrorsChangedSubscriptionCount);
  104. }
  105. [Fact]
  106. public void Sends_Correct_Notifications_With_Property_Chain()
  107. {
  108. var container = new Container();
  109. var inner = new IndeiTest();
  110. var observer = new ExpressionObserver(
  111. container,
  112. $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}",
  113. true);
  114. var result = new List<object>();
  115. observer.Subscribe(x => result.Add(x));
  116. Assert.Equal(new[]
  117. {
  118. new BindingNotification(
  119. new NullReferenceException(),
  120. BindingErrorType.Error,
  121. AvaloniaProperty.UnsetValue),
  122. }, result);
  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 object _inner;
  175. public object 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. }